def actor_info(id_): loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) telegraph = Telegraph() async def main(): await telegraph.create_account('DoubanMovieBot') with utils.my_opener().get(f'https://movie.douban.com/celebrity/{id_}/') as html_res: html_data = html_res.text soup = BeautifulSoup(html_data, 'lxml') # info part headline = soup.find('div', id='headline', class_="item") actor_name = headline.find('div', class_="pic").find('a', class_="nbg").get('title') image_url = headline.find('div', class_="pic").find('a', class_="nbg").get('href') image_html = f'<img src="{image_url}">' ul = headline.find('ul', class_="") ul_html = str(ul).replace('span', 'strong').replace('\n', '').replace(' ', '') info_html = f'{image_html}<h3>影人信息</h3>{ul_html}' # summary part intro = soup.find('div', id="intro") try: summary = intro.find('span', class_="all hidden").text.strip().replace(' ', '') except AttributeError: summary = intro.find('div', class_="bd").text.strip().replace(' ', '') summary_html = f'<h3>影人简介:</h3><p>{summary}</p>' # awards part if soup.find('div', class_='mod').find('div', class_='hd') is not None: with utils.my_opener().get(f'https://movie.douban.com/celebrity/{id_}/awards/') as html_res: html_data = html_res.text soup = BeautifulSoup(html_data, 'lxml') content = soup.find('div', id='content') content_html = f'<h3>{content.h1.text}</h3>' soup_awards = content.find_all('div', class_="awards") for awards in soup_awards: awards_html = str(awards).replace('<div class="awards">', '').replace( '<div class="hd">', '').replace('</div>', '').replace( 'h2', 'h4').replace('\n', '') content_html += awards_html page = await telegraph.create_page(actor_name, f'{info_html}{summary_html}{content_html}') else: page = await telegraph.create_page(actor_name, f'{info_html}{summary_html}') print('生成演员Instant View, URL:', page.url) return page.url try: url = loop.run_until_complete(main()) finally: loop.run_until_complete(telegraph.close()) return url
def test_context_token(telegraph: Telegraph): original_token = telegraph.token with telegraph.with_token('foo'): assert telegraph.token == 'foo' with telegraph.with_token('bar'): assert telegraph.token == 'bar' telegraph.token = 'baz' assert telegraph.token == 'bar' assert telegraph.token == 'foo' assert telegraph.token != original_token assert telegraph.token == 'baz'
async def main(): logging.basicConfig(level=logging.INFO) bot = Bot(token=config.TELEGRAM_API_TOKEN, parse_mode=types.ParseMode.HTML) dp = Dispatcher(bot, storage=RedisStorage2(host=config.REDIS_HOST)) # Работа с БД pool = await asyncpg.create_pool( user=config.PG_USER, password=config.PG_PASSWORD, host=config.PG_HOST, database=config.PG_DATABASE, command_timeout=60, ) await create_tables(pool) telegraph = Telegraph() TelegraphClass.t = telegraph await set_commands(bot) dp.middleware.setup(DbMiddleware(pool)) dp.middleware.setup(ThrottlingMiddleware()) register_inline_mode(dp) register_admin(dp) register_user(dp) register_cart(dp) try: await dp.start_polling() finally: await bot.close()
def test_socks5_proxy(): telegraph = Telegraph(proxy='socks5://example.com:1050', proxy_auth=BasicAuth('username', 'password')) connector = telegraph.session._connector assert isinstance(connector, SocksConnector) assert connector._socks_ver == SocksVer.SOCKS5 assert connector._socks_host == 'example.com' assert connector._socks_port == 1050 assert connector._socks_username == 'username' assert connector._socks_password == 'password'
def test_prepare_content(): telegraph = Telegraph() with pytest.raises(exceptions.ContentRequired): telegraph._prepare_content(None) with pytest.raises(TypeError): telegraph._prepare_content(42) content = telegraph._prepare_content('content') assert isinstance(content, str) content = telegraph._prepare_content(['content']) assert isinstance(content, str)
def test_token_property(telegraph: Telegraph): telegraph.token = 'abcdef01234567890' assert telegraph.token == 'abcdef01234567890' with pytest.raises(TypeError): telegraph.token = 42 account = types.Account(short_name='test', author_name='Test', access_token='abcdef01234567890') telegraph.token = account assert telegraph.token == account.access_token with pytest.raises(TypeError): account = types.Account(short_name='test', author_name='Test') telegraph.token = account del telegraph.token assert telegraph.token is None
from aiogram import Bot, Dispatcher, types from aiogram.contrib.fsm_storage.memory import MemoryStorage from aiograph import Telegraph from data import config from utils.db_api.sqlite import Database bot = Bot(token=config.BOT_TOKEN, parse_mode=types.ParseMode.HTML) storage = MemoryStorage() dp = Dispatcher(bot, storage=storage) telegraph = Telegraph() usersdb = Database(path_to_db="data/users.db")
async def telegraph(): aiograph = Telegraph(token=access_token) yield aiograph await aiograph.close()
def movie_info(id_): loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) telegraph = Telegraph() async def get_info(id_): await telegraph.create_account('DoubanMovieBot') with utils.my_opener().get(f'https://movie.douban.com/subject/{id_}/') as html_res: html_data = html_res.text soup = BeautifulSoup(html_data, 'lxml') # json part json_data = ujson.loads(soup.find('script', type='application/ld+json').text) title = json_data['name'] image_html = f'''<img src="{json_data['image']}">''' score = json_data['aggregateRating']['ratingValue'] if score == '': score = '暂无评分' score_html = f'<strong>评分:</strong>{score}<br />' # rating part try: votes_num = soup.find('span', property="v:votes").text votes_num_html = f'<strong>评分人数:</strong>{votes_num}<br/>' except AttributeError: votes_num_html = f'<strong>评分人数:</strong>暂无评分人数<br/>' # info part info = soup.find('div', id='info') directors = str(info.find_all('a', rel="v:directedBy")).strip('[|]') directors_html = f'''<strong>导演:</strong>{directors if directors != '' else '暂无导演信息'}<br />''' try: author_html = f'''<strong>编剧:</strong>{str( info.find('span', text='编剧').next_sibling.next_sibling).replace('<span class="attrs">', '').replace('</span>', '')}<br />''' except AttributeError: author_html = f'<strong>编剧:</strong>暂无编剧信息<br />' actors = str(info.find_all('a', rel="v:starring")).strip('[|]') actors_html = f'''<strong>主演:</strong>{actors if actors != '' else '暂无主演信息'}<br />''' genre_set = set() for genre in info.find_all('span', property="v:genre"): genre_set.add(genre.text) genre_html = f'''<strong>类型:</strong>{' / '.join(genre_set)}<br/>''' country_html = f'''<strong>制片国家/地区:</strong>{str( info.find('span', text='制片国家/地区:').next_sibling).strip()}<br />''' try: language_html = f'''<strong>语言:</strong>{ str(info.find('span', text='语言:').next_sibling).strip()}<br />''' except AttributeError: language_html = f'''<strong>语言:</strong>暂无信息<br />''' publish_date_set = set() for publish_date in info.find_all('span', property="v:initialReleaseDate"): publish_date_set.add(publish_date.text) publish_date_html = f'''<strong>上映日期:</strong>{' / '.join(publish_date_set)}<br/>''' try: runtime = info.find('span', property="v:runtime").text runtime_html = f'<strong>片长:</strong>{runtime}<br/>' except AttributeError: runtime_html = f'<strong>片长:</strong>暂无信息<br/>' try: aka_html = f'''<strong>又名:</strong>{ str(info.find('span', text='又名:').next_sibling).strip()}<br />''' except AttributeError: aka_html = f'''<strong>又名:</strong>暂无信息<br />''' imdb = str(info.find(href=imdb_pattern)) imdb_html = f'''<strong>IMDb链接:</strong>{imdb if imdb is not None else '暂无imdb信息'}<br />''' info_html = f'{image_html}<h3>电影信息</h3>{score_html}{votes_num_html}{directors_html}' \ f'{author_html}{actors_html}{genre_html}{country_html}{language_html}{publish_date_html}' \ f'{runtime_html}{aka_html}{imdb_html}' info_html = info_html.replace('/celebrity', 'https://movie.douban.com/celebrity') # summary part related_info = soup.find('div', class_='related-info') try: summary = related_info.find('span', class_="all hidden").text.strip().replace(' ', '') except AttributeError: summary = related_info.find('span', property="v:summary").text.strip().replace(' ', '') summary_html = f'<h3>{related_info.h2.i.text}:</h3><p>{summary}</p>' # awards part if soup.find('div', class_='mod') is not None: with utils.my_opener().get(f'https://movie.douban.com/subject/{id_}/awards/') as html_res: html_data = html_res.text soup = BeautifulSoup(html_data, 'lxml') content = soup.find('div', id='content') awards_html = f'<h3>{content.h1.text}:</h3>' for awards in content.find_all('div', class_="awards"): awards_div_h2 = awards.div.h2 awards_href = awards_div_h2.a.get('href') awards_name = awards_div_h2.get_text().replace('\n', '') awards_name_html = f'<br/><em><a href="{awards_href}">{awards_name}</a></em><br/>' awards_category_html = str(awards.find_all('ul')).strip('[|]').replace( ', ', '').replace('<li></li>', '').replace('\n', '') awards_html = f'{awards_html}{awards_name_html}{awards_category_html}' page = await telegraph.create_page(title, f'{info_html}{summary_html}{awards_html}') else: page = await telegraph.create_page(title, f'{info_html}{summary_html}') print('生成电影Instant View, URL:', page.url) return page.url, score try: url, score = loop.run_until_complete(get_info(id_)) finally: loop.run_until_complete(telegraph.close()) return url, score
import asyncio from aiograph import Telegraph telegraph = Telegraph('token') print(f"1. Root: '{telegraph.token}'") with telegraph.with_token('foo'): # Will change token in current context print(f"2. Inside context manager: '{telegraph.token}'") with telegraph.with_token('bar'): # Will change token in current context print(f"3. Inside child context manager: '{telegraph.token}'") telegraph.token = 'baz' # Doesn't affect token inside current context print( f"4. After changing: '{telegraph.token}' (is not changed inside context manager)" ) print(f"5. Inside context manager: '{telegraph.token}'") print(f"6. Root: '{telegraph.token}'") # Shows changed token asyncio.run(telegraph.close())
import asyncio from aiograph import Telegraph loop = asyncio.get_event_loop() telegraph = Telegraph() async def main(): telegraph_url = await telegraph.upload_from_url( 'https://www.python.org/static/img/python-logo.png') print('Uploaded:', telegraph_url) if __name__ == '__main__': try: loop.run_until_complete(main()) except (KeyboardInterrupt, SystemExit): pass finally: loop.run_until_complete( telegraph.close()) # Close the aiohttp.ClientSession
import asyncio from pathlib import Path from aiograph import Telegraph loop = asyncio.get_event_loop() telegraph = Telegraph() content = '<p>Page created by <a href="https://github.com/aiogram/aiograph" target="_blank">AIOGraph</a></p>' \ '<img src="{image}"/>' \ '<h4 id="Lorem-ipsum">Lorem ipsum</h4>' \ '<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ' \ 'ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco ' \ 'laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in vol ' \ 'uptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non ' \ 'proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p> ' \ '<p>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, ' \ 'totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae ' \ 'dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, ' \ 'sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam ' \ 'est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius ' \ 'modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, ' \ 'quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi ' \ 'consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil ' \ 'molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?</p> ' \ '<p><h4 id="FooBar">FooBar</h4><ul><li>Foo</li><li>Bar</li><li>Baz</li></ul></p>' IMAGE_PATH = Path(__file__).parent.parent / 'tests' / 'telegraph.jpg'