Skip to content

Instantly share code, notes, and snippets.

@darthbhyrava
Last active November 28, 2018 09:36
Show Gist options
  • Save darthbhyrava/0bede001659f6a0339d1f77005bb89cf to your computer and use it in GitHub Desktop.
Save darthbhyrava/0bede001659f6a0339d1f77005bb89cf to your computer and use it in GitHub Desktop.
Code for REST APIs at IEEE's MOOC Presentation
# Import the two libraries needed for our task: json and requests
import json
import requests
# If requests isn't installed , open up terminal/command line and run `$ pip install requests`
# pip comes pre-installed from Python 2.7 onwards
# We're storing files in the current directory, change as necessary
file = './source-'
# We have to pass some parameters along with our GET request
queries = {}
# Get your unique authentication apiKey at https://newsapi.org/register
# The following apiKey will not work.
queries['apiKey'] = 'e4 ... a6'
# The query topic - you can replace it with whatever topic you wish to query
queries['q'] = 'Donald Trump'
# You can look at more parameters to fine-tune your query at https://newsapi.org/docs/endpoints/everything
# List of sources I got from https://newsapi.org/sources
sources = ['breitbart-news','cnn','fox-news','msnbc','the-new-york-times','the-washington-post']
# Running a loop to cover all sources, and create unique output files for all of them
for source in sources:
queries['sources'] = source
# Read more at https://stackoverflow.com/questions/16877422
response = requests.get('https://newsapi.org/v2/everything', params=queries)
# We're reading the data in a json format
# Read more about JSON at https://www.json.org/
data = json.loads(response.text)
# We're writing the data into a json file
with open(file+source+'.json','a+') as f:
json.dump(data,f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment