In [ ]:
import os
from unidecode import unidecode
DATA_FOLDER = 'twitter'
ROOT_NODE = 'itsdougthepug'
ROOT_NAME = 'itsdougthepug'
ROOT_DEPTH = 1
INCLUDE_ROOT = True
DIRECTED = False
GRAPH_NAME = 'twitter'
def load_successors(node, depth=1, names=None):
successors = set()
path = os.path.join(DATA_FOLDER, node + '.txt')
try:
with open(path, encoding='utf-8') as file:
for line in file:
words = line.split()
if words:
successor = words[0]
successors.add(successor)
if names is not None and len(words) > 1:
names[successor] = unidecode(' '.join(words[1:]))
except FileNotFoundError:
pass
if depth > 1:
successors_copy = successors.copy()
for successor in successors_copy:
successors |= load_successors(node, depth - 1, names)
return successors
def main():
nodes = []
names = {}
successors = load_successors(ROOT_NODE, ROOT_DEPTH, names)
successors.discard(ROOT_NODE)
if INCLUDE_ROOT:
nodes.append(ROOT_NODE)
names[ROOT_NODE] = ROOT_NAME
nodes.extend(successors)
edges = []
for i, node in enumerate(nodes):
successors = load_successors(node)
for successor in successors:
try:
j = nodes.index(successor)
except ValueError:
continue
edges.append((i, j))
if not DIRECTED:
edges = [(i, j) for i, j in edges if i < j and (j, i) in edges]
with open(GRAPH_NAME + '.gml', 'w') as file:
file.write('graph [\n')
file.write(' directed {}\n'.format(int(DIRECTED)))
for i, node in enumerate(nodes):
file.write(' node [\n')
file.write(' id {}\n'.format(i))
if node in names:
label = names[node]
else:
label = node
file.write(' label "{}"\n'.format(label))
file.write(' ]\n')
for i, j in edges:
file.write(' edge [\n')
file.write(' source {}\n'.format(i))
file.write(' target {}\n'.format(j))
file.write(' ]\n')
file.write(']\n')
if __name__ == '__main__':
main()
In [ ]:
import os
import twitter
from twitter.error import TwitterError
ROOT_NODE = 'itsdougthepug'
DATA_FOLDER = 'twitter'
def build_path(node):
return os.path.join(DATA_FOLDER, node + '.txt')
def main():
api = twitter.Api(
consumer_key= 'hYP48JEel4iXw1bGWLYa9uFG0',
consumer_secret='6I6EKYpTtXP2LpVmWS8TL7R33zVNqYV2YlOOI59O8eUdolcEGH',
access_token_key='897052168225730560-8ERaqaR3R4y0O7ejWj9BEDRA7moNXqI',
access_token_secret='Lf3zP2IGefWjjil9ARl23ZU0Ug3AjWofYLg6yEbhO2RXA',
sleep_on_rate_limit=True,
)
root_path = build_path(ROOT_NODE)
with open(root_path, 'w', encoding='utf-8') as root_file:
root_data = api.GetFriends(screen_name=ROOT_NODE)
for root_subdata in root_data:
node = root_subdata.screen_name
print(node)
try:
data = api.GetFriends(screen_name=node)
except TwitterError:
continue
root_file.write(node + '\n')
path = build_path(node)
with open(path, 'w', encoding='utf-8') as file:
for subdata in data:
file.write(subdata.screen_name + '\n')
if __name__ == '__main__':
main()
In [ ]: