rodolpheg

Untitled

Feb 27th, 2020
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.79 KB | None | 0 0
  1. import tweepy
  2. import csv
  3. import pandas as pd
  4. import networkx as nx
  5. from igraph import *
  6. import time
  7.  
  8. def igraph2networkx(g):
  9. g.write("/Users/rodolphegonzales/Desktop/JH_Network/proxy_file.graphml",format='graphml')
  10. g = nx.read_graphml("/Users/rodolphegonzales/Desktop/JH_Network/proxy_file.graphml")
  11. return g
  12.  
  13. def networkx2igraph(g):
  14. nx.write_graphml(g, "/Users/rodolphegonzales/Desktop/JH_Network/proxy_file.graphml")
  15. g = read("/Users/rodolphegonzales/Desktop/JH_Network/proxy_file.graphml",format='graphml')
  16. return g
  17.  
  18.  
  19. ####input your credentials here
  20. consumer_key = ''
  21. consumer_secret = ''
  22. access_token = ''
  23. access_token_secret = ''
  24.  
  25. CONSUMER_KEY = 'Cj2NTElg5l0y81hOErJK6GIK4'
  26. CONSUMER_SECRET = 'wrDmJQfRzrKSzEDcC7LqpZSLfppBOb0LknNM2iydakOwR1BFnC'
  27. ACCESS_TOKEN_KEY = '14260466-BqxL4mGKJbb5mGkS2FoJbSUOCFtSYwJS9btQgWxca'
  28. ACCESS_TOKEN_SECRET = 'ajDdNg7kwyehH2qH6TugmjpyUBgsvQ3ueoei1UlA4JSJ0'
  29.  
  30. auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
  31. auth.set_access_token(ACCESS_TOKEN_KEY, ACCESS_TOKEN_SECRET)
  32. api = tweepy.API(auth,wait_on_rate_limit=True)
  33. #####United Airlines
  34. # Open/Create a file to append data
  35. #csvFile = open('giletsjaunes.csv', 'a')
  36. try:
  37. csvfile = open('fait.csv', encoding='utf8')
  38. fait = csv.reader(csvfile, delimiter=',', quotechar='"')
  39. fait = list(fait)
  40. except:
  41. fait = [['id','Date', 'Auteur', 'Auteur original', 'Description du retweeter', 'Retweets', 'Coeurs', 'Text', 'Coords']]
  42.  
  43.  
  44. #Use csv Writer
  45. #csvWriter = csv.writer(csvFile)
  46. #csvWriter.writerow(['id','Date', 'Auteur', 'Auteur original', 'Retweets', 'Coeurs', 'Text', 'Coords'])
  47.  
  48. try:
  49. G = nx.read_graphml('giletsjaunes.graphml')
  50. # g = Read_GraphML('giletsjaunes.graphml')
  51. # print(g)
  52. except:
  53. G = nx.Graph()
  54. g = Graph()
  55. print('new')
  56.  
  57. def csv2graphml(csvf):
  58. G = nx.MultiDiGraph()
  59. exists = []
  60. for f in fait:
  61. if f[2] != 'Auteur':
  62. print(len(exists),len(fait))
  63. if f[0] not in exists:
  64. if f[3] not in G:
  65. G.add_node(f[3])
  66. if f[2] not in G:
  67. G.add_node(f[2])
  68. G.node[f[2]]['description'] = f[4]
  69. if G.has_edge(f[3], f[2]):
  70. G.edge[f[3]][f[2]]['weight'] += 1.0
  71. else:
  72. G.add_edge(f[3], f[2])
  73. G.edge[f[3]][f[2]]['timestamp'] = str(f[1])
  74. G.edge[f[3]][f[2]]['weight'] = 1.0
  75. exists.append(f[0])
  76. else:
  77. print('exists')
  78. nx.write_graphml(G, "giletsjaunes_.graphml")
  79.  
  80. def csv2gexf(csvf):
  81. G = nx.MultiDiGraph() # works with G = nx.DiGraph()
  82. print(G)
  83. for f in csvf:
  84. print(f)
  85. if f[3] not in G:
  86. G.add_node(f[3])
  87. if f[2] not in G:
  88. G.add_node(f[2])
  89. G.node[f[2]]['description'] = f[4]
  90. # if G.has_edge(f[3], f[2]):
  91. # G.adj[f[3]][f[2]]['weight'] += 1.0
  92. # else:
  93. G.add_edge(f[3], f[2])
  94. G.adj[f[3]][f[2]][0]['timestamp'] = str(f[1])
  95. G.adj[f[3]][f[2]][0]['weight'] = 1.0
  96. nx.write_gexf(G, "giletsjaunes_.gexf")
  97.  
  98. #csv2gexf(fait)
  99.  
  100. c = 0
  101. liens = 0
  102. for tweet in tweepy.Cursor(api.search,q="#giletjaune",count=10,lang="fr").items():
  103. print(tweet.coordinates)
  104.  
  105. # print(c)
  106. # if tweet.author.screen_name not in G:
  107. # G.add_node(tweet.author.screen_name)
  108.  
  109. c += 1
  110.  
  111. try:
  112. original = tweet.retweeted_status.user.screen_name
  113. except:
  114. original = 0
  115. # print(tweet.id in [row[0] for row in fait])
  116. if original != 0 and original != tweet.author.screen_name and str(tweet.id) not in [row[0] for row in fait]:
  117.  
  118. try:
  119. descrip = tweet.author.description
  120. except:
  121. descrip = ''
  122. # G.node[tweet.author.screen_name]['description'] = descrip
  123. # g.add_vertices()
  124.  
  125. # if original not in G:
  126. # G.add_node(tweet.author.screen_name)
  127.  
  128. #print(tweet.created_at,original + " -> " + tweet.author.screen_name)
  129. # G.add_edge(original, tweet.author.screen_name)
  130. # G.edge[original][tweet.author.screen_name]['timestamp'] = str(tweet.created_at)r
  131. liens += 1
  132. # print(liens,G.number_of_edges())
  133.  
  134. 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])
  135.  
  136. # 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])
  137. if liens % 500 == 0:
  138. print('Sauvegarde...')
  139.  
  140. with open('fait.csv','w',encoding='utf8') as f:
  141. writer = csv.writer(f)
  142. writer.writerows(fait)
  143.  
  144. # csv2graphml(fait)
  145.  
  146.  
  147. # G2 = nx.DiGraph()
  148. # for u,v,data in G.edges(data=True):
  149. # w = data['weight'] if 'weight' in data else 1.0
  150. # if G2.has_edge(u,v):
  151. # G2[u][v]['weight'] += w
  152. # else:
  153. # G2.add_edge(u, v, weight=w)
  154.  
  155. # G2 = nx.DiGraph(G)
  156. # nx.write_gexf(G, "giletsjaunes.gexf")
  157. # except:
  158. # print('error')
  159. # pass
  160. # try:
  161. # nx.write_graphml(G, "giletsjaunes.graphml")
  162. # except:
  163. # pass
  164. # g.Write_GraphML('giletsjaunes.graphml')
  165. # break
Advertisement
Add Comment
Please, Sign In to add comment