-
Notifications
You must be signed in to change notification settings - Fork 144
/
recipe__visualize_rt_graph_protovis.py
76 lines (51 loc) · 1.76 KB
/
recipe__visualize_rt_graph_protovis.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
# -*- coding: utf-8 -*-
import os
import sys
import json
import webbrowser
import twitter
from recipe__create_rt_graph import create_rt_graph
from recipe__oauth_login import oauth_login
from recipe__search import search
# An HTML page that we'll inject Protovis consumable data into
HTML_TEMPLATE = 'etc/twitter_retweet_graph.html'
OUT = os.path.basename(HTML_TEMPLATE)
# Writes out an HTML page that can be opened in the browser
# that displays a graph
def write_protovis_output(g, out_file, html_template):
nodes = g.nodes()
indexed_nodes = {}
idx = 0
for n in nodes:
indexed_nodes.update([(n, idx,)])
idx += 1
links = []
for n1, n2 in g.edges():
links.append({'source' : indexed_nodes[n2],
'target' : indexed_nodes[n1]})
json_data = json.dumps({"nodes" : [{"nodeName" : n} for n in nodes], \
"links" : links}, indent=4)
html = open(html_template).read() % (json_data,)
if not os.path.isdir('out'):
os.mkdir('out')
f = open(out_file, 'w')
f.write(html)
f.close()
print >> sys.stderr, 'Data file written to: %s' % f.name
if __name__ == '__main__':
# Your query
Q = ' '.join(sys.argv[1])
# How many batches of data to grab for the search results
MAX_BATCHES = 2
# How many search results per page
COUNT = 100
# Get some search results for a query
t = oauth_login()
search_results = search(t, q=Q, max_batches=MAX_BATCHES, count=COUNT)
g = create_rt_graph(search_results)
# Write Protovis output and open in browser
if not os.path.isdir('out'):
os.mkdir('out')
f = os.path.join(os.getcwd(), 'out', OUT)
write_protovis_output(g, f, HTML_TEMPLATE)
webbrowser.open('file://' + f)