-
Notifications
You must be signed in to change notification settings - Fork 144
/
recipe__create_rt_graph.py
55 lines (37 loc) · 1.35 KB
/
recipe__create_rt_graph.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
# -*- coding: utf-8 -*-
import sys
import json
import twitter
import networkx as nx
from recipe__get_rt_origins import get_rt_origins
from recipe__search import search
from recipe__oauth_login import oauth_login
def create_rt_graph(tweets):
g = nx.DiGraph()
for tweet in tweets:
rt_origins = get_rt_origins(tweet)
if not rt_origins:
continue
for rt_origin in rt_origins:
g.add_edge(rt_origin.encode('ascii', 'ignore'),
tweet['user']['screen_name'].encode('ascii', 'ignore'),
{'tweet_id': tweet['id']}
)
return g
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)
# Print out some stats
print >> sys.stderr, "Number nodes:", g.number_of_nodes()
print >> sys.stderr, "Num edges:", g.number_of_edges()
print >> sys.stderr, "Num connected components:", \
len(nx.connected_components(g.to_undirected()))
print >> sys.stderr, "Node degrees:", sorted(nx.degree(g))