Пример #1
0
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
Пример #2
0
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
Пример #3
0
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
Пример #4
0
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())
Пример #5
0
          '<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'


async def main():
    await telegraph.create_account('aiograph-demo')

    photo = (await telegraph.upload(IMAGE_PATH, full=False))[0]
    page_content = content.format(image=photo)
    page = await telegraph.create_page('Demo', page_content)
    print('Created page:', page.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