/
facebook__filter_rgraph_output_by_group.py
91 lines (64 loc) · 2.42 KB
/
facebook__filter_rgraph_output_by_group.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# -*- coding: utf-8 -*-
import os
import sys
import json
import facebook
import webbrowser
import shutil
from facebook__fql_query import FQL
from facebook__login import login
HTML_TEMPLATE = '../web_code/jit/rgraph/rgraph.html'
OUT = os.path.basename(HTML_TEMPLATE)
# Reuses out/facebook.friends.json written out by
# facebook__get_friends_rgraph.py
DATA = sys.argv[1]
rgraph = json.loads(open(DATA).read())
try:
ACCESS_TOKEN = open('out/facebook.access_token').read()
except IOError, e:
try:
# If you pass in the access token from the Facebook app as a command line
# parameter, be sure to wrap it in single quotes so that the shell
# doesn't interpret any characters in it. You may also need to escape
# the # character
ACCESS_TOKEN = sys.argv[2]
except IndexError, e:
print >> sys.stderr, \
"Could not either find access token in 'facebook.access_token' or parse args."
ACCESS_TOKEN = login()
gapi = facebook.GraphAPI(ACCESS_TOKEN)
groups = gapi.get_connections('me', 'groups')
# Display groups and prompt the user
for i in range(len(groups['data'])):
print '%s) %s' % (i, groups['data'][i]['name'])
choice = int(raw_input('Pick a group, any group: '))
gid = groups['data'][choice]['id']
# Find the friends in the group
fql = FQL(ACCESS_TOKEN)
q = \
"""select uid from group_member where gid = %s and uid in
(select target_id from connection where source_id = me() and target_type = 'user')
""" \
% (gid, )
uids = [u['uid'] for u in fql.query(q)]
# Filter the previously generated output for these ids
filtered_rgraph = [n for n in rgraph if n['id'] in uids]
# Trim down adjancency lists for anyone not appearing in the graph.
# Note that the full connection data displayed as HTML markup
# in "connections" is still preserved for the global graph.
for n in filtered_rgraph:
n['adjacencies'] = [a for a in n['adjacencies'] if a in uids]
if not os.path.isdir('out'):
os.mkdir('out')
# HTML_TEMPLATE references some dependencies that we need to
# copy into out/
shutil.rmtree('out/jit', ignore_errors=True)
shutil.copytree('../web_code/jit',
'out/jit')
html = open(HTML_TEMPLATE).read() % (json.dumps(filtered_rgraph),)
f = open(os.path.join(os.getcwd(), 'out', 'jit', 'rgraph', OUT), 'w')
f.write(html)
f.close()
print 'Data file written to: %s' % f.name
# Open up the web page in your browser
webbrowser.open('file://' + f.name)