Skip to content

Instantly share code, notes, and snippets.

@rishi-raj-jain
Created September 9, 2020 19:13
Show Gist options
  • Save rishi-raj-jain/40b28083c25da566a9c70092e0eab463 to your computer and use it in GitHub Desktop.
Save rishi-raj-jain/40b28083c25da566a9c70092e0eab463 to your computer and use it in GitHub Desktop.
import sys, csv, pprint, json
import networkx as nx
import matplotlib.pyplot as plt
import community as community_louvain
import matplotlib.cm as cm
from community import generate_dendrogram, best_partition, partition_at_level
from pymongo import MongoClient
from networkx.readwrite import json_graph
# Connecting to the db
collection= db['tweets']
# Creating empty graph
G= nx.Graph()
# Options while creating the graphs
options = {
'node_color': 'blue',
'node_size': 20,
'width': 1
}
# Keep track of tweets counted
mapOriginal= {}
''' TODO: Add edges to the graph '''
def addToGraph(src, dest):
global G
if G.has_edge(src, dest):
G[src][dest]['weight']+=1
else:
G.add_edge(src, dest, weight=1)
# Iterating in each record
for item in collection.find():
ifRetweeted= True if item.get('retweeted_status') is not None else False
ifQuoted= True if item.get('quoted_status') is not None else False
# If the tweet is original tweet itself (i.e. no retweet or not a quoted one)
if (not ifRetweeted) and (not ifQuoted):
# do nothing...
pass
# In case it's just a retweet
elif (ifRetweeted) and (not ifQuoted):
if mapOriginal.get(item['retweeted_status']['id']) is None:
mapOriginal[item['retweeted_status']['id']]= 1
addToGraph(item['user']['screen_name'], item['retweeted_status']['user']['screen_name'])
# In case it's just a quoted one
elif ifQuoted:
if mapOriginal.get(item['quoted_status']['id']) is None:
# Get the original tweeter & mentions in that post
mapOriginal[item['quoted_status']['id']]= 1
addToGraph(item['user']['screen_name'], item['quoted_status']['user']['screen_name'])
with open('retweeters.csv', 'w', newline='') as fileSave:
writer= csv.writer(fileSave)
for u,v in G.edges():
writer.writerow([u, v, G[u][v]['weight']])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment