In [57]:
class tw4:
tak = "imnk4dJfL3RUnLXH4N8ZhxGm2"
taks = "J7OERxGCFETfTh6kZeBLcCySyXp5itk04VxaSpygLBveBn8bjG"
tat = "9060092-rHDMdX4siaAn6BrVJQezTpX8b1awLosl0SWCbtSFF8"
tats = "DBRbXXIq4R8F3vq23Mk6iwVHjobcMkwvhP6H0kZuyHmZG"
In [58]:
from twython import TwythonStreamer
import datetime, pymongo
In [59]:
mongouri = "mongodb://fuxu:julho17@ds027739.mongolab.com:27739/fuxudb"
mongouri2="mongodb://magico:fuxu@ds053429.mongolab.com:53429/fabricio"
In [69]:
class MyStreamer(TwythonStreamer):
def on_success(self, data):
if 'text' in data:
now=datetime.datetime.now()
print now
nick=data['user']["screen_name"].encode('utf-8')
shout=data['text'].encode('utf-8')
shout_transaction={"time":now,"nick":nick,"shout":shout}
try:
client.fuxudb.shouts.insert(shout_transaction)
except:
client=pymongo.MongoClient(mongouri)
client.fuxudb.shouts.insert(shout_transaction)
print shout_transaction
#stream=MyStreamer(tw4.tak,tw4.taks,tw4.tat,tw4.tats)
#stream.statuses.filter(track="#aao0")
In [60]:
from twython import Twython
t = Twython(app_key=tw4.tak,
app_secret=tw4.taks,
oauth_token=tw4.tat,
oauth_token_secret=tw4.tats)
In [67]:
search = t.search(q="#marcocivil",count=100,result_type="recent")
In [75]:
client=pymongo.MongoClient(mongouri)
for status in search["statuses"]:
client.fuxudb.shouts.insert(status)
Out[75]:
In [26]:
usuarios=set([i["user"]["screen_name"] for i in search["statuses"]])
In [27]:
len(usuarios)
Out[27]:
In [73]:
statuses=search["statuses"]
#[i["text"] for i in search["statuses"]]
#statuses[0].keys()
In [70]:
#[(i["retweeted"],i["retweet_count"],i["text"]) for i in statuses[:10]]
In [35]:
msgs_=[(i["text"],i["user"]["screen_name"]) for i in statuses]
In [36]:
import networkx as x
g=x.Graph()
RTs=[i for i in msgs_ if i[0].startswith("RT @")]
sn2s=[]
for RT in RTs:
sn1=RT[1]
foo=RT[0]
try:
bar=foo.index(":")
except:
bar=foo.index("@")+5
sn2=foo[foo.index("@")+1:bar]
if "@" in sn2:
sn2=sn2[:sn2.index("@")]
sn2s+=[sn2]
edge=sn1,sn2
if sn1 in g.nodes(): # quem retweetou
g.node[sn1]["weight"]+=1 # pois é atividade deste
else:
g.add_node(sn1,weight=1.)
if sn2 in g.nodes(): # a fonte do tweet original
pass
else:
g.add_node(sn2,weight=0) # pois não é atividade dele
if g.has_edge(sn1,sn2):
g[sn1][sn2]["weight"]+=1
else:
g.add_edge(sn1, sn2, weight=1.)
In [37]:
g.edges()
Out[37]:
In [38]:
g.nodes()
Out[38]:
In [41]:
x.write_graphml(g,"redeRTMarcoCivil.graphml")
In [42]:
ls
In [74]:
#cat redeRTMarcoCivil.graphml
In [ ]: