先日、Twitterで知り合ったdoyaさんと実際にお会いして、互いの職歴や、地元の話、最近のWebサービスのことなど、色んな話をしたのですが、最近注目している技術やWebサービスは何かという話題で、情報を可視化出来るUbigraphの話になり、付属されているデモンストレーションを見せてもらいながら、Pythonを含めた何種類かの言語で操作が可能なこと、特にPythonで書かれたサンプルスクリプトが多く付属されていることを教えてもらった。
早速、Pythonの勉強にサンプルコードを読もうと思ったのですが、どうせならTwitterを可視化した事例がないかと調べたところ、ブラジルのChristian S. Perone氏のblogでTwitterのFollowingを可視化したTwitter3D.pyというコードを発見。
Twitter in 3D ! – Pyevolve
早速ダウンロードして、動かしてみた。
その動画がこちらです(HQ視聴がおすすめです)
ところが、どうもFollowしているのに表示されない人がいるので、Follow数をカウントしたところ、ちょうど100人。どうやらPython-twitterの仕様で100人までしか取得出来ないっぽい。
【ご参考】 テックノート@ama-ch – python-twitterで遊んでみた 基本編
Twitterのapiでは、page=ページ番号 (オプション)が用意され、ページ番号を指定することで、指定ユーザの friend の一覧を100件単位で取得することが出来るようだが、python-twiterにpageパラメータが用意されていない模様。さらに調べてみるとこのようなページが
python-twitter Issue 20:Only return 100 users
trunk版では一ヶ月前に解決されているようです。
そこでeasy_installでインストールしたpython-twitterをアンインストールし、trunk版をインストール。その後、Twitter3D.pyでFollowingを全て取得するコードを追加して無事100人以上表示させることが出来た。
修正を加えたコードはこちら
#!/usr/bin/env python
# coding: utf-8
#
# This code is forking from twitter3D.py (H.Aoshima).
# Christian S. Perone makes the original.
# Please Note it.
# Python-twitter of the trunk version (r137 or higher) is necessary.
#
import time
import xmlrpclib
import socket
from sets import Set
import random
from optparse import OptionParser
try:
import twitter
except:
print "error:\tcan't import python-twitter module, you must install the module:\n"
print "\tto install: 'easy_install python-twitter'\n"
exit()
server_url = 'http://127.0.0.1:20738/RPC2'
screenname = ""
api = ""
G = ""
GREEN_COLOR = "#00ff00"
RED_COLOR = "#ff0000"
YELLOW_COLOR = "#ffff00"
expandedVertices = Set()
vertexes = {}
vertexes_invert = {}
def get_allfriends(root_name): #get all friends
friends, cnt = [], 1
while not len(friends) % 100:
friends += api.GetFriends(root_name, page=cnt)
cnt += 1
return friends
def expand_vertex(v):
if v in expandedVertices:
return 0
expandedVertices.add(v)
G.set_vertex_attribute(v, "color", YELLOW_COLOR)
root_name = vertexes[v]
print "Getting friends of %s..." % root_name
friends = get_allfriends(root_name)
# print len(friends) # debug
if len(friends) <= 0:
color_node = GREEN_COLOR if vertexes[v] != screenname else RED_COLOR
G.set_vertex_attribute(v, "color", color_node)
for friend in friends:
friend_name = friend.GetScreenName()
if vertexes_invert.get(friend_name) is not None:
edge = G.new_edge(v, vertexes_invert[friend_name])
G.set_edge_attribute(edge, "arrow", "true")
else:
new_vertex = G.new_vertex()
G.set_vertex_attribute(new_vertex, "label", friend_name)
G.set_vertex_attribute(new_vertex, "shape", "sphere")
vertexes.update({new_vertex: friend_name})
vertexes_invert.update({friend_name: new_vertex})
edge = G.new_edge(v, new_vertex)
G.set_edge_attribute(edge, "arrow", "true")
color_node = GREEN_COLOR if vertexes[v] != screenname else RED_COLOR
G.set_vertex_attribute(v, "color", color_node)
return 0
if __name__ == "__main__":
print "Twitter3D - Twitter 3D nodes viewer"
print "By Christian S. Perone (@tarantulae)"
print "http://pyevolve.sourceforge.net/wordpress\n"
parser = OptionParser()
parser.add_option("-u", "--username", dest="username",
help="The twitter username", metavar="USERNAME")
parser.add_option("-p", "--password", dest="password",
help="The twitter password (optional)", metavar="PASSWORD")
(options, args) = parser.parse_args()
if (not options.username):
parser.print_help()
exit()
pass_msg = "authenticated" if options.password else "unauthenticated"
print "Starting the Twiter3D for user %s (%s)..." % (options.username, pass_msg)
screenname = options.username
api = twitter.Api(username=options.username, password=options.password)
server = xmlrpclib.Server(server_url)
G = server.ubigraph
try:
G.clear()
except socket.error, msg:
print "error:\tcan't connected to the Ubigraph server, the server is running ?\n\terror message:%s\n" % msg
exit()
root = G.new_vertex()
vertexes.update({root : screenname})
vertexes_invert.update({screenname : root})
G.set_vertex_attribute(root, "color", "#ff0000")
G.set_vertex_attribute(root, "shape", "sphere")
G.set_vertex_attribute(root, "label", screenname)
myPort = random.randint(20700,30000)
G.set_vertex_style_attribute(0, "callback_left_doubleclick",
"http://127.0.0.1:" + str(myPort) + "/expand_vertex")
from SimpleXMLRPCServer import SimpleXMLRPCServer
server = SimpleXMLRPCServer(("localhost", myPort))
server.register_introspection_functions()
server.register_function(expand_vertex)
print "Listening for callbacks from ubigraph on the port %d..." % (myPort,)
server.serve_forever()
Christian S. Perone氏には、Followingを100人以上取得出来るようにしたので、折角なので公開していい?という旨のメールを送り、全然問題ないし、書いたらlink教えてという返事を頂いた(無茶苦茶な英文でやり取りしてもうた><)。
Ubigraphをやったことない人はデモがかっこいいので是非ご覧になってください。
- Newer: 魚貝食事処 おがわ
- Older: 河津桜 – 本州一の早咲き桜
Comments:0
Trackbacks:0
- Trackback URL for this entry
- http://aoshiman.net/weblog/2009/04/17/python-twitter-in-3d-on-ubigraph/trackback/
- Listed below are links to weblogs that reference
- [Python] Twitter in 3D on Ubigraph from “南船北馬”
