Created
March 18, 2018 20:50
-
-
Save IliaMManolov/9c93d7fd45aba108613b381aa1da69e6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import facebook | |
import requests | |
import time | |
start_time = time.time() | |
graph = facebook.GraphAPI(access_token="your token here", version="2.7") | |
# Gets the number of reacts on a post with id post_id | |
def getReactCount(post_id): | |
return graph.get_object(id=post_id, fields='reactions.limit(0).summary(true)') \ | |
[u'reactions'][u'summary'][u'total_count'] | |
# Get the oxfess page | |
page = graph.get_object(id="oxfess") | |
target = raw_input("Enter a file to store the info of the most reacted to posts: ") | |
# Then get the first batch of 100 posts | |
posts = graph.get_connections(id=page[u'id'], connection_name='posts', limit=100) | |
# Use this to store final post ids and reaction counts | |
allpostdata = [] | |
# The number of processed posts | |
postcount = 0 | |
trip = True | |
# Until we can get no more posts | |
while(True): | |
try: | |
print 'Fetching post batch ' + str(postcount) + ' to ' + str(postcount + 100) | |
i = 0 | |
for post in posts['data']: | |
print 'Fetching post number ' + str(i) + ' of the batch' | |
i+=1 | |
allpostdata.append((post['id'], getReactCount(post['id']))) | |
postcount+=100 | |
# Attempt to make a request to the next page of data, if it exists. | |
posts=requests.get(posts['paging']['next']).json() | |
except KeyError: | |
# When there are no more pages (['paging']['next']), break from the | |
# loop and end the script. | |
break | |
print 'Posts have been collected. Sorting...' | |
allpostdata.sort(key = lambda x: x[1]) | |
print 'Processed ' + str(postcount) + 'posts and found these ids for the 10 most reacted posts: ' | |
bestposts = allpostdata[-10:] | |
print bestposts | |
with open(target, 'w') as f: | |
for i, post in enumerate(bestposts): | |
message = graph.get_object(id=post[0])["message"] | |
f.write("Post number " + str(10-i) + ":\n") | |
f.write(message.encode('utf-8')) | |
f.write("\n") | |
f.write("id: " + str(post[0]) + '\n') | |
f.write("Reaction count: " + str(post[1]) + '\n') | |
f.write("\n\n\n") | |
print 'Running time: ' + str(time.time() - start_time) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment