Skip to content

Instantly share code, notes, and snippets.

@bobonthenet
Last active January 31, 2017 14:01
Show Gist options
  • Save bobonthenet/ef722f8daf0dd13fbb1d2126f4e741a0 to your computer and use it in GitHub Desktop.
Save bobonthenet/ef722f8daf0dd13fbb1d2126f4e741a0 to your computer and use it in GitHub Desktop.
Script for looking at the last 10 posts made by DJT to see if their are duplicate comments made by different users accounts (hint: there are!)
"""
virtualenv facebook
source facebook/bin/activate
pip install facebook-sdk
pip install requests
pip install termcolor
replace your access token in the code
python lookfordupes.py
"""
import facebook
import requests
from collections import Counter
import pickle
import sys
from termcolor import colored, cprint
allComments = []
commentCount = 0
def find_comments(post, commentCount):
while True:
try:
try:
# allPostComments = post['comments']
allPostComments = graph.get_connections(post['id'], 'comments', limit = 250)
commentList = []
# c = []
for a in allPostComments['data']:
c = {'postID': post['id'], 'commentID': a['id'], 'name': a['from']['name'], 'message': a['message']}
commentList.append(c)
commentCount += 1
allPostComments = requests.get(allPostComments['paging']['next']).json()
except(UnicodeEncodeError):
pass
return commentList, commentCount
except KeyError:
# When there are no more pages (['paging']['next']), break from the
# loop and end the script.
break
def find_dupes(allComments):
cnt = Counter()
users = {}
for comment in allComments:
cnt[comment['message']] += 1
try:
users[comment['message']] = users[comment['message']] + ", " + comment['name']
except(KeyError):
users[comment['message']] = comment['name']
for msg in cnt.items():
if msg[1] > 2:
try:
cprint(msg[0], 'green')
print('posted', msg[1], 'times with the following names:', users[msg[0]], '\n\n')
except(UnicodeEncodeError):
pass
# You'll need an access token here to do anything. You can get a temporary one
# here: https://developers.facebook.com/tools/explorer/
access_token = 'EAACEdEose0cBABxv4DrIVlxqS8MwHD2Y1MDbS9buAnbxMQcbZCbg8dBNNDKZBnoCIbDN48ufQsfv174zqpESLYmSPfZB7VXI3mdXyT0ug7LBISqFaM3rvtzPeB6f1eaDDV7870QvwNtgLocpL6gwRaZAby3R93tDa0l7EBKDhDE4sfZAYxAsY'
# Look at Donald Trump's profile by using his Facebook id.
user = 'DonaldTrump'
graph = facebook.GraphAPI(access_token)
profile = graph.get_object(user)
posts = graph.get_connections(profile['id'], 'posts', limit = 10)
# Pickle the posts so that I don't have to keep retrieving them while experimenting with this script.
with open('FB.pickle', 'wb') as f:
pickle.dump(posts, f)
pickle_in = open('FB.pickle', 'rb')
posts = pickle.load(pickle_in)
postCount = 0
for post in posts['data']:
postCount += 1
a,c = find_comments(post, commentCount)
allComments += a
commentCount += c
find_dupes(allComments)
print('Number of posts with comments:', postCount)
print('Overall number of comments:', commentCount)
@bobonthenet
Copy link
Author

Cleaned this up a little bit, I finished it when I was pretty tired and saw some things that didn't need to be there. Now more comments are also being retrieved which provides a better dataset. Lastly, the comments now print in color so it is easier to see where one long comment ends and the next begins. Still a lot more I would like to do but I need to set this aside for a bit and complete some other stuff.

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