Skip to content

Instantly share code, notes, and snippets.

@spinofdoom
Last active June 19, 2018 00:55
Show Gist options
  • Select an option

  • Save spinofdoom/1fea0105fbb958e9575736c4d098e5ce to your computer and use it in GitHub Desktop.

Select an option

Save spinofdoom/1fea0105fbb958e9575736c4d098e5ce to your computer and use it in GitHub Desktop.
Headlines [Python 3.6+] - grabs top headlines from NYT, Guardian (U.S.), AJ: English, and WaPo, and displays them as links in a plain HTML page
from datetime import date
from webbrowser import open_new
from os.path import join as pjoin, expanduser
import requests
home = expanduser('~')
today = str(date.today())
newsApiKey = '' # Save your keys here.
guardianApiKey = '' # Free API keys can be generated from
# News API (https://newsapi.org/register) and the
# Guardian (https://open-platform.theguardian.com/access/).
if newsApiKey == '':
newsApiKey = input('Input NewsAPI key: ')
if guardianApiKey == '': # If you don't have your
guardianApiKey = input('Input Guardian API key: ') # API keys saved above,
# they are prompted for.
def news_api_grab(news_source):
url = (f'''https://newsapi.org/v2/top-headlines?sources='''
f'''{news_source}&apiKey={news_api_key}''')
response = requests.get(url)
data = response.json() # Grabs NewsApi Headlines
site, name, body = '', '', '' # from specified source
num = 0 # (https://newsapi.org/sources)
items = data.items()
for k1, v1 in items:
if k1 == 'articles':
for article in v1:
z = article.items()
for k2, v2 in z:
if k2 == 'title':
name = v2
if k2 == 'url':
site = v2
num += 1
body += f'{num}) <a href="{site}">{name}</a><br><br>\n'
return body
def guardian_grab():
url = (f'''https://content.guardianapis.com/us-news?api-key='''
f'''{guardian_api_key}''')
response = requests.get(url)
data = response.json()
s, n, body = '', '', ''
num = 0
items = data.items()
for k1, v1 in items: # Grabs Guardian U.S. headlines
y = v1.items()
for k2, v2 in y:
if k2 == 'results':
for article in v2:
z = article.items()
for k3, v3 in z:
if k3 == 'webTitle':
n = v3
if k3 == 'webUrl':
s = v3
num += 1
body += f'{num}) <a href="{s}">{n}</a><br><br>\n'
return body
body1, body2 = news_api_grab('the-new-york-times'), guardian_grab()
body3 = news_api_grab('al-jazeera-english')
body4 = news_api_grab('the-washington-post')
message = (f"""<!DOCTYPE html>\n<html>\n"""
f"""<meta http-equiv='Content-Type' content='text/html; charset=UTF-8'/>"""
f"""\n<center>\n\n<p><b>'New York Times' Headlines for {today}:</b></p>"""
f"""{body1}\n<p><b>'The Guardian' Headlines for {today}:</b></p>"""
f"""{body2}\n<p><b>'Al Jazeera: English' Headlines for {today}:</b></p>"""
f"""{body3}\n<p><b>'The Washington Post' Headlines for {today}:</b></p>"""
f"""{body4}\n</center>\n</html>""")
end_path = pjoin(home, 'Desktop/headlines.html')
# Writes to Desktop by default
f = open(end_path, 'w')
f.write(message)
f.close()
open_new(f'file://{end_path}')
@spinofdoom
Copy link
Author

Requires:

Only tested on OS X so far.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment