-
Notifications
You must be signed in to change notification settings - Fork 143
/
recipe__visualize_rt_graph_graphviz.py
69 lines (47 loc) · 1.67 KB
/
recipe__visualize_rt_graph_graphviz.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
# -*- coding: utf-8 -*-
import os
import sys
import twitter
import networkx as nx
from recipe__create_rt_graph import create_rt_graph
from recipe__oauth_login import oauth_login
from recipe__search import search
# Writes out a DOT language file that can be converted into an
# image by Graphviz
def write_dot_output(g, out_file):
try:
nx.drawing.write_dot(g, out_file)
print >> sys.stderr, 'Data file written to', out_file
except ImportError, e:
# Help for Windows users:
# Not a general purpose method, but representative of
# the same output write_dot would provide for this graph
# if installed and easy to implement
dot = ['"%s" -> "%s" [tweet_id=%s]' % (n1, n2, g[n1][n2]['tweet_id'])
for (n1, n2) in g.edges()]
f = open(out_file, 'w')
f.write('''strict digraph {
%s
}''' % (';\n'.join(dot), ))
f.close()
print >> sys.stderr, 'Data file written to: %s' % f.name
if __name__ == '__main__':
# Your output
OUT = 'twitter_retweet_graph.dot'
# 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 Graphviz output
if not os.path.isdir('out'):
os.mkdir('out')
f = os.path.join(os.getcwd(), 'out', OUT)
write_dot_output(g, f)
print >> sys.stderr, \
'Try this on the DOT output: $ dot -Tpng -O%s %s' % (f, f,)