Skip to content

Instantly share code, notes, and snippets.

@katsully
Created February 22, 2015 23:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save katsully/a81436ca6ef907d475ed to your computer and use it in GitHub Desktop.
Save katsully/a81436ca6ef907d475ed to your computer and use it in GitHub Desktop.
############################
## By: Kat Sullivan
############################
import facebook
import json
# take in the ceratin comment as a list of words, and the index where the queried word occurs in a list
# return the phrase
def add_to_list(words, index):
if index != 0 and index+1 != len(words):
return (words[index-1] + " " + words[index] + " " + words[index+1] + " ")
elif index == 0:
return (words[index] + " " + words[index+1] + " " + words[index+2] + " ")
else:
return (words[index-2] + " " + words[index-1] + " " + words[index] + " ")
# read in facebook API token
file_key = open("facebook_key.txt")
token = file_key.readline()
file_key.close()
# create Facebook grpah object
graph = facebook.GraphAPI(token)
# scrape the Fox News newsfeed
news_feed = graph.get_connections('FoxNews', 'feed', limit=15)
data = news_feed['data']
comment_trolls = []
# concatenate all comments from posts
for i in data:
comments = i['comments']
comments_data = comments["data"]
comment_trolls.append([comment['message'].encode('utf-8') for comment in comments_data])
comment_trolls = [item for sublist in comment_trolls for item in sublist]
republican_list = ""
obama_list = ""
liberals_list = ""
america_list = ""
stupid_list = ""
# query certain words in each comment
for comment_troll in comment_trolls:
new_list = comment_troll.lower()
words = new_list.split()
if 'republican' in words:
index = words.index('republican')
republican_list += add_to_list(words, index)
if 'obama' in words:
index = words.index('obama')
obama_list += add_to_list(words, index)
if 'liberals' in words:
index = words.index('liberals')
liberals_list += add_to_list(words, index)
if 'america' in words:
index = words.index('america')
america_list += add_to_list(words, index)
if 'stupid' in words:
index = words.index('stupid')
stupid_list += add_to_list(words, index)
# return all created phrases
print republican_list
print obama_list
print liberals_list
print america_list
print stupid_list
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment