Created
June 3, 2013 16:32
-
-
Save angelworm/5699412 to your computer and use it in GitHub Desktop.
tumblrnotesと協力してリブログツリーを出力するそれ: https://gist.github.com/u-e-d/5083450
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 tumblrnotes as tn | |
import igraph | |
import re | |
import sys | |
import os | |
def notes(url): | |
def prittyfy(li): | |
for r in li.findall("blockquote"): | |
li.remove(r) | |
return li.text_content().strip() | |
posts = tn.notes(*tn.fromURL(url)) | |
ret = [] | |
for li in posts: | |
ret.append(prittyfy(li)) | |
return ret | |
def reblogs(txt): | |
note = re.findall("(\S+) reblogged this from (\S+)",txt) | |
if len(note) != 1: | |
return None | |
else: | |
return note[0] | |
def posted(txt): | |
note = re.findall("(\S+) posted this", txt) | |
if len(note) != 1: | |
return None | |
else: | |
return note[0] | |
def seeds(elist): | |
ret = set() | |
for (l,r) in elist: | |
ret.add(l) | |
ret.add(r) | |
return ret | |
def normalize(elist, seed=None): | |
nodes = seeds(elist) | |
tables = dict() | |
indexes= list() | |
index = 0 | |
ret = list() | |
if seed is not None: | |
tables[seed] = index | |
indexes.append(seed) | |
index += 1 | |
for x, y in elist: | |
if x not in tables: | |
tables[x] = index | |
indexes.append(x) | |
index += 1 | |
if y not in tables: | |
tables[y] = index | |
indexes.append(y) | |
index += 1 | |
ret.append((tables[x], tables[y])) | |
return (indexes, ret) | |
def mkgraph(elist): | |
names, nodes = normalize(elist) | |
g = igraph.Graph(0) | |
g.add_vertices(len(nodes)+1) | |
g.vs["name"] = names | |
g.vs["Label"] = names | |
g.add_edges(nodes) | |
return g | |
def main(url): | |
note = notes(url) | |
seed = posted(note[-1]) | |
rblg = filter(lambda x: x is not None, map(reblogs, note)) | |
g = mkgraph(rblg) | |
g.write_graphml(open("%s.graphml"%seed, "w",1)) | |
if __name__ == '__main__': | |
if len(sys.argv) != 2: | |
print "useage: tublrnotes.py url" | |
else: | |
main(sys.argv[1]) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment