Created
November 12, 2012 11:11
-
-
Save raptium/4058738 to your computer and use it in GitHub Desktop.
IEMS5720 Blogosphere
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
from apiclient.discovery import build | |
import httplib2 | |
import json | |
API_KEY = '...' | |
vertices = {} | |
edges = [] | |
def update_user(user): | |
name = user['displayName'] | |
uid = user['id'] | |
vertices[uid] = name | |
urls = [] | |
with open('urls.txt', 'r') as f: | |
for line in f: | |
line = line.strip() | |
if len(line) == 0: | |
continue | |
urls.append(line.replace('blogspot.hk', 'blogspot.com')) | |
service = build('blogger', 'v3', developerKey=API_KEY) | |
for url in urls: | |
blog = service.blogs().getByUrl(url=url).execute() | |
posts = service.posts().list(blogId=blog['id']).execute() | |
for post in posts['items']: | |
author = post['author'] | |
update_user(author) | |
comments = service.comments().list(blogId=blog['id'], postId=post['id']).execute() | |
if not comments.has_key('items'): | |
continue | |
for comment in comments['items']: | |
commenter = comment['author'] | |
update_user(commenter) | |
edges.append((commenter['id'], author['id'])) | |
id_map = {} | |
def get_int_id(uid): | |
if not uid in id_map: | |
id_map[uid] = len(id_map) | |
return id_map[uid] | |
nodes = [] | |
weights = {} | |
links = [] | |
for edge in edges: | |
source = get_int_id(edge[0]) | |
target = get_int_id(edge[1]) | |
if source == target: | |
continue | |
if not (source, target) in weights: | |
weights[(source, target)] = 1 | |
else: | |
weights[(source, target)] += 1 | |
for vertex in sorted(vertices.iteritems(), key=lambda vertex:get_int_id(vertex[0])): | |
nodes.append({ | |
"index": len(nodes), | |
"name": vertex[1], | |
"weight": 0, | |
"in": 0, | |
"out": 0, | |
}) | |
for key in weights: | |
value = weights[key] | |
source, target = key | |
nodes[source]["weight"] += value | |
nodes[source]["out"] += value | |
nodes[target]["weight"] += value | |
nodes[target]["in"] += value | |
links.append({ | |
'source': source, | |
'target': target, | |
'value': value | |
}) | |
print json.dumps({ | |
'nodes': nodes, | |
'links': links | |
}, indent=2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment