|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +""" |
| 4 | +An Asynchronous version of the script to see how much a given word is |
| 5 | +mentioned in the news today |
| 6 | +
|
| 7 | +Takes advantage of: |
| 8 | +
|
| 9 | +Uses data from the NewsAPI: |
| 10 | +
|
| 11 | +https://newsapi.org |
| 12 | +""" |
| 13 | + |
| 14 | +import time |
| 15 | +import asyncio |
| 16 | +import aiohttp |
| 17 | +import requests |
| 18 | + |
| 19 | +WORD = "trump" |
| 20 | + |
| 21 | +NEWS_API_KEY = "84d0483394c44f288965d7b366e54a74" |
| 22 | + |
| 23 | +base_url = 'https://newsapi.org/v1/' |
| 24 | + |
| 25 | +# use one session for the whole script |
| 26 | +# recommended by the docs |
| 27 | +# session = aiohttp.ClientSession() |
| 28 | + |
| 29 | + |
| 30 | +# this has to run first, so doesn't really need async |
| 31 | +# but why use two reuests libraries ? |
| 32 | +async def get_sources(sources): |
| 33 | + """ |
| 34 | + get all the english language sources of news |
| 35 | +
|
| 36 | + 'https://newsapi.org/v1/sources?language=en' |
| 37 | + """ |
| 38 | + url = base_url + "sources" |
| 39 | + params = {"language": "en"} |
| 40 | + session = aiohttp.ClientSession() |
| 41 | + async with aiohttp.ClientSession() as session: |
| 42 | + async with session.get(url, ssl=False, params=params) as resp: |
| 43 | + data = await resp.json() |
| 44 | + print("Got the sources") |
| 45 | + sources.extend([src['id'].strip() for src in data['sources']]) |
| 46 | + |
| 47 | + |
| 48 | +async def get_articles(source): |
| 49 | + """ |
| 50 | + https://newsapi.org/v1/articles?source=associated-press&sortBy=top&apiKey=1fabc23bb9bc485ca59b3966cbd6ea26 |
| 51 | + """ |
| 52 | + url = base_url + "articles" |
| 53 | + params = {"source": source, |
| 54 | + "apiKey": NEWS_API_KEY, |
| 55 | + # "sortBy": "latest", # some sources don't support latest |
| 56 | + "sortBy": "top", |
| 57 | + # "sortBy": "popular", |
| 58 | + } |
| 59 | + print("requesting:", source) |
| 60 | + async with aiohttp.ClientSession() as session: |
| 61 | + async with session.get(url, ssl=False, params=params) as resp: |
| 62 | + if resp.status != 200: # aiohttpp has "status" |
| 63 | + print("something went wrong with: {}".format(source)) |
| 64 | + await asyncio.sleep(0) |
| 65 | + return |
| 66 | + data = await resp.json() |
| 67 | + print("got the articles from {}".format(source)) |
| 68 | + # the url to the article itself is in data['articles'][i]['url'] |
| 69 | + titles.extend([str(art['title']) + str(art['description']) |
| 70 | + for art in data['articles']]) |
| 71 | + |
| 72 | + |
| 73 | +def count_word(word, titles): |
| 74 | + word = word.lower() |
| 75 | + count = 0 |
| 76 | + for title in titles: |
| 77 | + if word in title.lower(): |
| 78 | + count += 1 |
| 79 | + return count |
| 80 | + |
| 81 | + |
| 82 | +start = time.time() |
| 83 | + |
| 84 | +# start up a loop: |
| 85 | +loop = asyncio.get_event_loop() |
| 86 | + |
| 87 | +# create the objects to hold the data |
| 88 | +sources = [] |
| 89 | +titles = [] |
| 90 | + |
| 91 | +# get the sources -- this is essentially synchronous |
| 92 | +loop.run_until_complete(get_sources(sources)) |
| 93 | + |
| 94 | +# running the loop for the articles |
| 95 | +jobs = asyncio.gather(*(get_articles(source) for source in sources)) |
| 96 | +loop.run_until_complete(jobs) |
| 97 | +loop.close() |
| 98 | +# session.close() |
| 99 | + |
| 100 | +art_count = len(titles) |
| 101 | +word_count = count_word(WORD, titles) |
| 102 | + |
| 103 | +print(WORD, "found {} times in {} articles".format(word_count, art_count)) |
| 104 | +print("Process took {:.0f} seconds".format(time.time() - start)) |
0 commit comments