Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import tweepy
- import csv
- import pandas as pd
- import networkx as nx
- from igraph import *
- import time
- def igraph2networkx(g):
- g.write("/Users/rodolphegonzales/Desktop/JH_Network/proxy_file.graphml",format='graphml')
- g = nx.read_graphml("/Users/rodolphegonzales/Desktop/JH_Network/proxy_file.graphml")
- return g
- def networkx2igraph(g):
- nx.write_graphml(g, "/Users/rodolphegonzales/Desktop/JH_Network/proxy_file.graphml")
- g = read("/Users/rodolphegonzales/Desktop/JH_Network/proxy_file.graphml",format='graphml')
- return g
- ####input your credentials here
- consumer_key = ''
- consumer_secret = ''
- access_token = ''
- access_token_secret = ''
- CONSUMER_KEY = 'Cj2NTElg5l0y81hOErJK6GIK4'
- CONSUMER_SECRET = 'wrDmJQfRzrKSzEDcC7LqpZSLfppBOb0LknNM2iydakOwR1BFnC'
- ACCESS_TOKEN_KEY = '14260466-BqxL4mGKJbb5mGkS2FoJbSUOCFtSYwJS9btQgWxca'
- ACCESS_TOKEN_SECRET = 'ajDdNg7kwyehH2qH6TugmjpyUBgsvQ3ueoei1UlA4JSJ0'
- auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
- auth.set_access_token(ACCESS_TOKEN_KEY, ACCESS_TOKEN_SECRET)
- api = tweepy.API(auth,wait_on_rate_limit=True)
- #####United Airlines
- # Open/Create a file to append data
- #csvFile = open('giletsjaunes.csv', 'a')
- try:
- csvfile = open('fait.csv', encoding='utf8')
- fait = csv.reader(csvfile, delimiter=',', quotechar='"')
- fait = list(fait)
- except:
- fait = [['id','Date', 'Auteur', 'Auteur original', 'Description du retweeter', 'Retweets', 'Coeurs', 'Text', 'Coords']]
- #Use csv Writer
- #csvWriter = csv.writer(csvFile)
- #csvWriter.writerow(['id','Date', 'Auteur', 'Auteur original', 'Retweets', 'Coeurs', 'Text', 'Coords'])
- try:
- G = nx.read_graphml('giletsjaunes.graphml')
- # g = Read_GraphML('giletsjaunes.graphml')
- # print(g)
- except:
- G = nx.Graph()
- g = Graph()
- print('new')
- def csv2graphml(csvf):
- G = nx.MultiDiGraph()
- exists = []
- for f in fait:
- if f[2] != 'Auteur':
- print(len(exists),len(fait))
- if f[0] not in exists:
- if f[3] not in G:
- G.add_node(f[3])
- if f[2] not in G:
- G.add_node(f[2])
- G.node[f[2]]['description'] = f[4]
- if G.has_edge(f[3], f[2]):
- G.edge[f[3]][f[2]]['weight'] += 1.0
- else:
- G.add_edge(f[3], f[2])
- G.edge[f[3]][f[2]]['timestamp'] = str(f[1])
- G.edge[f[3]][f[2]]['weight'] = 1.0
- exists.append(f[0])
- else:
- print('exists')
- nx.write_graphml(G, "giletsjaunes_.graphml")
- def csv2gexf(csvf):
- G = nx.MultiDiGraph() # works with G = nx.DiGraph()
- print(G)
- for f in csvf:
- print(f)
- if f[3] not in G:
- G.add_node(f[3])
- if f[2] not in G:
- G.add_node(f[2])
- G.node[f[2]]['description'] = f[4]
- # if G.has_edge(f[3], f[2]):
- # G.adj[f[3]][f[2]]['weight'] += 1.0
- # else:
- G.add_edge(f[3], f[2])
- G.adj[f[3]][f[2]][0]['timestamp'] = str(f[1])
- G.adj[f[3]][f[2]][0]['weight'] = 1.0
- nx.write_gexf(G, "giletsjaunes_.gexf")
- #csv2gexf(fait)
- c = 0
- liens = 0
- for tweet in tweepy.Cursor(api.search,q="#giletjaune",count=10,lang="fr").items():
- print(tweet.coordinates)
- # print(c)
- # if tweet.author.screen_name not in G:
- # G.add_node(tweet.author.screen_name)
- c += 1
- try:
- original = tweet.retweeted_status.user.screen_name
- except:
- original = 0
- # print(tweet.id in [row[0] for row in fait])
- if original != 0 and original != tweet.author.screen_name and str(tweet.id) not in [row[0] for row in fait]:
- try:
- descrip = tweet.author.description
- except:
- descrip = ''
- # G.node[tweet.author.screen_name]['description'] = descrip
- # g.add_vertices()
- # if original not in G:
- # G.add_node(tweet.author.screen_name)
- #print(tweet.created_at,original + " -> " + tweet.author.screen_name)
- # G.add_edge(original, tweet.author.screen_name)
- # G.edge[original][tweet.author.screen_name]['timestamp'] = str(tweet.created_at)r
- liens += 1
- # print(liens,G.number_of_edges())
- fait.append([tweet.id, tweet.created_at, tweet.author.screen_name, original, descrip, tweet.retweet_count, tweet.favorite_count, tweet.text.encode('utf-8'), tweet.coordinates])
- # csvWriter.writerow([tweet.id, tweet.created_at, tweet.author.screen_name, original, tweet.retweet_count, tweet.favorite_count, tweet.text.encode('utf-8'), tweet.coordinates])
- if liens % 500 == 0:
- print('Sauvegarde...')
- with open('fait.csv','w',encoding='utf8') as f:
- writer = csv.writer(f)
- writer.writerows(fait)
- # csv2graphml(fait)
- # G2 = nx.DiGraph()
- # for u,v,data in G.edges(data=True):
- # w = data['weight'] if 'weight' in data else 1.0
- # if G2.has_edge(u,v):
- # G2[u][v]['weight'] += w
- # else:
- # G2.add_edge(u, v, weight=w)
- # G2 = nx.DiGraph(G)
- # nx.write_gexf(G, "giletsjaunes.gexf")
- # except:
- # print('error')
- # pass
- # try:
- # nx.write_graphml(G, "giletsjaunes.graphml")
- # except:
- # pass
- # g.Write_GraphML('giletsjaunes.graphml')
- # break
Advertisement
Add Comment
Please, Sign In to add comment