Created
April 18, 2013 10:51
-
-
Save Akkiesoft/5411859 to your computer and use it in GitHub Desktop.
TwitterのストリームAPI見はって、自分のポストがあったらハイクに投げるやつ。
ToDo:RTとかリプライの処理も書きたい。
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
# Twitter To HatenaHaiku. | |
import os | |
import sys | |
import json | |
import tweepy | |
import requests | |
from requests.auth import HTTPBasicAuth | |
consumer_key = "" | |
consumer_secret = "" | |
access_key = "" | |
access_secret = "" | |
TwitterUserName = "" | |
HaikuUserName = "" | |
HaikuBasicPass = "" | |
HaikuSourceName = "TwitterToHaiku" | |
auth = tweepy.OAuthHandler(consumer_key, consumer_secret) | |
auth.set_access_token(access_key, access_secret) | |
api = tweepy.API(auth) | |
#me = api.me() | |
class CustomStreamListener(tweepy.StreamListener): | |
def on_status(self, status): | |
if hasattr(status, 'text'): | |
if (status.user.screen_name == TwitterUserName): | |
if (status.source != 'Haiku2Twit'): | |
haiku = {'keyword':'id:'+HaikuUserName, 'status':status.text, 'source':HaikuSourceName} | |
r = requests.post('http://h.hatena.ne.jp/api/statuses/update.json', data=haiku,auth=(HaikuUserName, HaikuBasicPass)) | |
def on_error(self, status_code): | |
print >> sys.stderr, 'Encountered error with status code:', status_code | |
return True # Don't kill the stream | |
def on_timeout(self): | |
print >> sys.stderr, 'Timeout...' | |
return True # Don't kill the stream | |
sapi = tweepy.streaming.Stream(auth, CustomStreamListener()) | |
sapi.userstream() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment