Last active
December 29, 2019 17:47
-
-
Save hareeen/5c7bfaa8ea434755051cf787c330dadc to your computer and use it in GitHub Desktop.
twitterSNA
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import json | |
import time | |
import random | |
authorizationHeader = { | |
'authorization': 'Bearer AAAAAAAAAAAAAAAAAAAAADgkAAEAAAAAXGrt0MHcLgyGKYDatrSdzuxjtnk%3DqMteCixTC3Grr2hU9YaSpWad2MuFs3shkk93Ey3pJRME997Yd9'} | |
id_set = set() | |
result = list() | |
f = open("result.txt", "w") | |
def apiReq(id: str, type: int) -> dict: | |
while True: | |
resp = requests.get( | |
f"https://api.twitter.com/1.1/{'friends' if type else 'followers'}/list.json?id={id}", headers=authorizationHeader) | |
if resp.status_code == 429: | |
print("Reached Limit") | |
time.sleep(60) | |
elif resp.status_code == 200: | |
return resp.json() | |
else: | |
print(resp.json()) | |
inp = input() | |
if inp == 'N': | |
return {} | |
else: | |
return resp.json() | |
def dfs(here, depth: int = 0): | |
global id_set, result | |
if here["protected"]: | |
result.append((here, None)) | |
return | |
else: | |
flwr = apiReq(here["id_str"], 0)["users"] | |
flwg = apiReq(here["id_str"], 1)["users"] | |
flwrSet = set([el["id_str"] for el in flwr]) | |
flwgSet = set([el["id_str"] for el in flwg]) | |
connected = list(flwrSet.intersection(flwgSet)) | |
random.shuffle(connected) | |
if len(connected) > 30: | |
connected = connected[:30] | |
for el in connected: | |
if el in id_set: | |
continue | |
elif depth >= 3: | |
id_set.add(el) | |
else: | |
for i in flwr: | |
if i["id_str"] == el: | |
dfs(i, depth+1) | |
print(here["screen_name"] + " Complete.") | |
result.append((here, connected)) | |
screen_name = input() | |
dfs(requests.get( | |
f"https://api.twitter.com/1.1/users/show.json?screen_name={screen_name}", headers=authorizationHeader).json(), 0) | |
print(len(result)) | |
f.write(str(result)) | |
f.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import networkx as nx | |
import matplotlib.pyplot as plt | |
import numpy as np | |
f = open('result.txt', 'r') | |
data = list() | |
exec("data = " + f.read()) | |
dic = dict() | |
G = nx.Graph() | |
cnt = 0 | |
for el in data: | |
if not el[0]["id_str"] in dic: | |
G.add_node(el[0]["id_str"]) | |
dic[el[0]["id_str"]] = cnt | |
cnt += 1 | |
for here in G.nodes: | |
for there in G.nodes: | |
if here <= there: | |
continue | |
def toSet(s) -> set: | |
return set(s) if s else set() | |
h_idx = dic[here] | |
t_idx = dic[there] | |
isConnected = False | |
if data[h_idx][1] and there in data[h_idx][1]: | |
isConnected = True | |
if data[t_idx][1] and here in data[t_idx][1]: | |
isConnected = True | |
G.add_edge(here, there, weight=float(isConnected)*4 + | |
len(toSet(data[h_idx][1]).intersection(toSet(data[t_idx][1])))) | |
Weights = [0]*len(G) | |
Degrees = [0]*len(G) | |
for here in G.adj: | |
for there, weight in G.adj[here].items(): | |
Weights[dic[here]] += weight['weight'] | |
Degrees[dic[here]] += (1 if weight['weight'] else 0) | |
print(f"Weights: m = {np.mean(Weights)}, sigma = {np.std(Weights)}") | |
print(f"Degrees: m = {np.mean(Degrees)}, sigma = {np.std(Degrees)}") | |
Limit = int(input()) | |
if Limit < 0: | |
exit(0) | |
blackListE = list() | |
for here in G.adj: | |
for there, weight in G.adj[here].items(): | |
if weight['weight'] < Limit: | |
blackListE.append((here, there)) | |
G.remove_edges_from(blackListE) | |
blackListN = list() | |
for here in G.adj: | |
if not len(G.adj[here]): | |
blackListN.append(here) | |
G.remove_nodes_from(blackListN) | |
nx.draw_networkx(G, **{'node_color': 'black', | |
'node_size': 20, 'width': 0.5, 'with_labels': False}) | |
plt.savefig(f'figures/{Limit}.png') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment