Skip to content

Instantly share code, notes, and snippets.

@dpenfoldbrown
Last active December 21, 2015 20:08
Show Gist options
  • Save dpenfoldbrown/6358780 to your computer and use it in GitHub Desktop.
Save dpenfoldbrown/6358780 to your computer and use it in GitHub Desktop.
Code to annotate all URLs with liberal, conservative, center, or unknown based on known news sources (eg fox, abc, npr with known affiliations/leanings). Also counts the number of domains found for each in list
import pymongo
# Set up DB client, db, collection
date_tweet_count = {}
date_url_count = {}
for user in collection.find():
for tweet in user['tweets']:
datestr = tweet['created_at']
datefields = datestr.split()
shortdatestr = datefields[-1] + " " + datefields[1] + " " + datefields[2]
if shortdatestr in date_tweet_count:
date_tweet_count += 1
date_url_count += len(tweet['urls'])
else:
date_tweet_count = 1
date_url_count = len(tweet['urls'])
datelist = date_tweet_count.keys() # x-axis
tweetcount = []
for date in datelist:
tweetcount.append(date_tweet_count[date])
# and the same for urlcountlist
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment