Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
NYT Most Shared Articles
import requests
import json
import csv
#here, we're going to retrieve the most shared articles from the NYT within the past 30 days and determine which author has contributed the most articles
#first, we hit up the most-shared endpoint and store the response as an object
pop_request = 'http://api.nytimes.com/svc/mostpopular/v2/mostshared/all-sections/30.json?api-key=[your-key]'
pop = requests.get(pop_request)
#store just the content
pop_content = pop._content
#as the object is currently a string, need to convert to dictionary (for easy field extraction)
pop_content = json.loads(pop_content)
#now, let's take a look at the fields of the popular articles dictionary
for k, v in pop_content.items():
if (k == "results"):
for a in v:
for i,r in a.items():
print i, r
#The fields we want are 'total_shares', 'byline', and, in the interest of fairness, 'published_date'
#for each article, we'll extract the desired fields and add them to lists
dater = []
shares = []
authors = []
for k, v in pop_content.items():
if (k == "results"):
for a in v:
pubdate = a["published_date"]
dater.append(pubdate)
author = a["byline"]
authors.append(author)
sharecount = a["total_shares"]
shares.append(sharecount)
#now we have three lists, one with the publication date, one with the author's name, and one with the total shares.
#think of these lists as columns; now we want to stitch the columns into a table and output that table as a csv
#step one is to create the blank csv output file
csv_out = open('nyt_mostpop_week.csv', 'wb')
mywriter = csv.writer(csv_out)
#give the csv column headers
mywriter.writerow(["Author", "Publish Date", "Total Shares"])
#stitch the three lists together and write each line of the new stitched table as a row in the csv
for row in zip(authors,dater,shares):
mywriter.writerow(row)
csv_out.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.