ラベル twitter の投稿を表示しています。 すべての投稿を表示
ラベル twitter の投稿を表示しています。 すべての投稿を表示

2008年7月2日水曜日

pythonでtwitterクライアント。コマンドプロンプト版。

作ったやつを張っとこう。
ざっくり判った。twitterのAPIは簡単で判りやすい。json便利。


Windowsならではの小細工が。
でもこれcmdで立ち上げとくと便利。メモ代わりにどんどん書き込める。

# -*- coding: utf-8 -*-

'''
twitter
friends timeline post
'''
import urllib, urllib2
import json
import calendar
import time
import sys

toplevel_url = 'twitter.com'
target_url = 'twitter.com/statuses/update.json'
protocol = 'http://'
proxy = {'http':'http://hogehoge.com:80/'}
username = 'hogehoge@gmail.com'
password = 'xxxxx'

proxy_handler = urllib2.ProxyHandler(proxy)
passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None, toplevel_url, username, password)
authhandler = urllib2.HTTPBasicAuthHandler(passman)
opener = urllib2.build_opener(proxy_handler, authhandler)
urllib2.install_opener(opener)

def twitter_post(text):
params = urllib.urlencode({'status':text.encode('utf8')})
#print params
urllib2.urlopen(protocol + target_url, params)

def twittertime2time(created_at):
unix_time = calendar.timegm(time.strptime(created_at, '%a %b %d %H:%M:%S +0000 %Y'))
return time.strftime("%a, %d %b %Y %H:%M:%S", time.localtime(unix_time))

while ( True ):
utext = raw_input('What are you doing?: ')
text = unicode(utext, 'mbcs')
twitter_post(text)



毎分チェックに行く方

# -*- coding: utf-8 -*-

'''
twitter
friends timeline loop_get
'''

import urllib2
import json
import calendar
import time
import shelve, sha

SLEEP_TIME = 1 * 60
HISTORY_FILE = 'tw.log'
toplevel_url = 'twitter.com'
target_url = 'twitter.com/statuses/friends_timeline/screen_name.json'
protocol = 'http://'
proxy = {'http':'http://hogehoge.com:80/'}
username = 'hogehoge@gmail.com'
password = 'xxxxxxx'

proxy_handler = urllib2.ProxyHandler(proxy)
passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None, toplevel_url, username, password)
authhandler = urllib2.HTTPBasicAuthHandler(passman)
opener = urllib2.build_opener(proxy_handler, authhandler)
urllib2.install_opener(opener)

def twittertime2time(created_at):
unix_time = calendar.timegm(time.strptime(created_at, '%a %b %d %H:%M:%S +0000 %Y'))
return time.strftime("%b %d %H:%M:%S", time.localtime(unix_time))
#return time.strftime("%a, %d %b %Y %H:%M:%S", time.localtime(unix_time))

def memory_log(time, text):
FLG = True
log = shelve.open( HISTORY_FILE )
s = sha.new( text.encode('utf-8') )
if ( not log.has_key( s.digest() ) ):
log[ s.digest() ] = time
FLG = False
log.close()
return FLG

def print_twitt(contents):
for content in contents:
if ( not memory_log(content['created_at'], content['text']) ):
print twittertime2time(content['created_at']), content['user']['screen_name'], content['text']

while ( True ):
pagehandle = urllib2.urlopen(protocol + target_url)
json_contents = pagehandle.read()
contents = json.read(json_contents)
print_twitt(contents)
time.sleep( SLEEP_TIME )
print '.',

2008年7月1日火曜日

発作的にtwitter クライアントをpythonで実装してみる

なんとなくで出来た。pythonってすごいなあ。
urlib2が便利すぎる。

参考:
perlでの実装例
jsonのパージの仕方
twitterAPI DOC
urlib2でプロキシ通すとき参考にした
twitter時間を日本時間に直すとき使った もいっこ時計型
python辞書型
時間の処理だけ参考にしたpython実装

'''
twitter
friends timeline get
'''

import urllib2
import json
import calendar
import time

toplevel_url = 'twitter.com'
target_url = 'twitter.com/statuses/friends_timeline/screen_name.json'
protocol = 'http://'
proxy = {'http':'http://hogehoge.com:8080/'}
username = 'hogehoge@gmail.com'
password = 'xxxxxxx'
proxy_handler = urllib2.ProxyHandler(proxy)

passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None, toplevel_url, username, password)
authhandler = urllib2.HTTPBasicAuthHandler(passman)
opener = urllib2.build_opener(proxy_handler, authhandler)
urllib2.install_opener(opener)
pagehandle = urllib2.urlopen(protocol + target_url)
json_contents = pagehandle.read()
contents = json.read(json_contents)

def twittertime2time(created_at):
unix_time = calendar.timegm(time.strptime(created_at, '%a %b %d %H:%M:%S +0000 %Y'))
return time.strftime("%a, %d %b %Y %H:%M:%S", time.localtime(unix_time))

for content in contents:
print twittertime2time(content['created_at']), content['user']['screen_name'], content['text']
# for k, v in content.iteritems():
# print k, v
# if k == 'user':
# for uk, uv in v.iteritems():
# print uk, uv

# print contents

驚くくらいjsonの解説が少なくて吃驚。ググり方が悪いのか。
また、twitter APIの戻り値(jsonで渡される中身の数々)に関しても、情報が無い。
取り敢えずcreated_atが作られた時間、userの中に入れ子で色々入ってて、textには本文が入ってる。
以下取れる情報を列挙しとこう。後日何が何かを調べよう。

favorited
truncated
text
created_at
source
in_reply_to_status_id
in_reply_to_user_id
id
user
name
url
profile_image_url
screen_name
followers_count
protected
location
id
description

とりあえず取れるのはこんなところ。