]> khamidou — GitHub mirrors - Projet_IC05.git/commitdiff
Modif tweetsToJson.py: ajout d'infos (avatar, booléen site, url complète)
authorRonan Kervella <ronan.kervella@gmail.com>
Sat, 15 Dec 2012 17:09:21 +0000 (18:09 +0100)
committerRonan Kervella <ronan.kervella@gmail.com>
Sat, 15 Dec 2012 17:09:21 +0000 (18:09 +0100)
tweetsToJson.py

index fe085f71467bca36ea9b01d9df3cd2fc92b9ab96..529b9b9beed50a2bc71d08d1fa7a07916383d4cc 100755 (executable)
@@ -1,6 +1,6 @@
-#! /usr/bin/python
+#! /usr/bin/env python2
 #
-# usage: convert input_file output_file
+# usage: tweetsToJson input_file output_file
 # 
 import json
 import sys
@@ -101,26 +101,36 @@ def writeJson(tweetList, outputFile):
 
     for tweet in tweetList:
         if not any (tweet.userName == node["nodeName"] for node in nodeList):
-            nodeList.append({"nodeName":tweet.userName, "nodeScore":compute_user_score(tweet.userName, tweetList)})
+            nodeList.append({"nodeName":tweet.userName, "avatar": tweet.profileImgUrlHttp, "nodeScore":compute_user_score(tweet.userName, tweetList), "webSite":False})
 
         if len(tweet.urls) > 0:
             for url in tweet.urls:
                 url_hostname = urlparse(url).hostname
                 if not any (url_hostname == node["nodeName"] for node in nodeList):
-                    nodeList.append({"nodeName":url_hostname, "nodeScore":compute_site_score(url,tweetList)})
-                    edgesList.append({"source":tweet.userName,"destination":url_hostname, "date": tweet.date})
+                    nodeList.append({"nodeName":url_hostname, "nodeScore":compute_site_score(url,tweetList),"website":True})
+                    edgesList.append({"source":tweet.userName,"destination":url_hostname, "url":url ,"date": tweet.date})
         for mention in tweet.userMentions:
             if not any(mention["name"] == node["nodeName"] for node in nodeList):
-                    nodeList.append({"nodeName":mention["name"],"nodeScore":compute_user_score(mention["name"],tweetList)})
-                    edgesList.append({"source":tweet.userName,"destination":mention["name"], "date":tweet.date, "content":tweet.text})
+                nodeList.append({"nodeName":mention["name"],"nodeScore":compute_user_score(mention["name"],tweetList), "webSite":False})
+                edgesList.append({"source":tweet.userName,"destination":mention["name"], "date":tweet.date, "content":tweet.text})
 
+    edgesList.sort(key=lambda r: r["date"])
     dic = {"nodes":nodeList,"edges": edgesList}
     json.dump(dic, open(outputFile,"w"))
 
+if (len(sys.argv) < 3):
+    print( """
+    usage:  tweetsToJson input output_file
+    - input: input file or directory
+    - output: output file
+    """)
+    sys.exit()
+
 inFile = sys.argv[1]
 outFile = sys.argv[2]
 
 tlist = list()
+cpt = 0
 # if the input file is a directory, parse each file from it
 if (os.path.isdir(inFile)):
     jsonFiles = os.listdir(inFile)
@@ -129,7 +139,12 @@ if (os.path.isdir(inFile)):
         tmpList = (extractTweets(inFile + "/" + file))
         if (len(tlist) > 0):
             tmpList = clearDuplicates(tlist, tmpList)
-            tlist.extend(tmpList)
+        tlist.extend(tmpList)
+        if (cpt % 10 == 0 and cpt > 0):
+            writeJson(tlist,outFile)
+            x = cpt / len(tlist) * 100
+            print("%.2f%% done" % x)
+        cpt += 1
 else:
     tlist = extractTweets(inFile)