Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Facebook mining script
import facebook
import json
import settings
g = facebook.GraphAPI('TOKEN')
# get all your friend's id and name
friends = g.get_connections('me', 'friends')['data']
# save every id in friends.json
friend_id = {friend['id']: friend['name'] for friend in friends}
with open('friends.json', 'w') as f:
f.write(json.dumps(friend_id))
import json
import facebook
import pprint
from threading import Thread
from Queue import Queue
import settings
THREAD_NUM = 20
g = facebook.GraphAPI('TOKEN')
in_queue = Queue()
out_queue = Queue()
class LikeThread(Thread):
def __init__(self, in_queue, out_queue):
Thread.__init__(self)
self.in_queue = in_queue
self.out_queue = out_queue
def run(self):
while True:
id, name = self.in_queue.get()
likes = g.get_connections(id, 'likes')['data']
out_queue.put((id, {'name': name, 'likes': likes}))
self.in_queue.task_done()
class SaveThread(Thread):
def __init__(self, out_queue):
Thread.__init__(self)
self.out_queue = out_queue
self.likes = {}
def run(self):
while True:
id, data = self.out_queue.get()
self.likes[id] = data
self.out_queue.task_done()
with open('friends.json') as f:
friends = json.loads(f.read())
for i in range(THREAD_NUM):
thread = LikeThread(in_queue, out_queue)
thread.setDaemon(True)
thread.start()
thread = SaveThread(out_queue)
thread.setDaemon(True)
thread.start()
for key, value in friends.items():
in_queue.put((key, value))
in_queue.join()
out_queue.join()
with open('likes.json', 'w') as f:
f.write(json.dumps(thread.likes, indent=4, ensure_ascii=True))
import json
from collections import Counter
from prettytable import PrettyTable
with open('likes.json') as f:
datas = json.loads(f.read())
counter = Counter(like['category'] for item in datas for like in item)
pt = PrettyTable(field_names=['Category', 'Freq'])
[pt.add_row(item) for item in counter.most_common(20)]
print pt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment