Created
March 24, 2011 13:39
-
-
Save mizchi/885067 to your computer and use it in GitHub Desktop.
favoriteみる
This file contains hidden or 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
| #/usr/bin/python | |
| #-*- encoding:utf-8 -*- | |
| myname = "mizchi" | |
| ckey = "" | |
| csecret = "" | |
| atoken ="" | |
| atoken_secret = "" | |
| import time, random | |
| from time import sleep , time ,strftime,localtime | |
| import urllib, urllib2 | |
| import hmac, hashlib | |
| import cgi | |
| import simplejson | |
| import time | |
| import atexit | |
| import simplejson | |
| import Growl | |
| g = Growl.GrowlNotifier( | |
| applicationName='faview', | |
| notifications=['fav']) | |
| g.register() | |
| def goodbye(): | |
| g.notify( | |
| noteType = 'fav', | |
| title = "terminate!", | |
| description = "" , | |
| sticky = False | |
| ) | |
| atexit.register( goodbye ) | |
| state = { | |
| "favorite":{}, | |
| "retweet":{} | |
| } | |
| # Request Token URL | |
| reqt_url = 'http://twitter.com/oauth/request_token' | |
| # Authorize URL | |
| auth_url = 'http://twitter.com/oauth/authorize' | |
| # Access Token URL | |
| acct_url = 'http://twitter.com/oauth/access_token' | |
| # status update | |
| post_url = 'http://twitter.com/statuses/update.xml' | |
| # show friends timeline | |
| chirp_url ='https://userstream.twitter.com/2/user.json' | |
| def make_signature( | |
| params, | |
| url, | |
| method, | |
| csecret, | |
| secret = ""): | |
| # Generate Signature Base String | |
| plist = [] | |
| for i in sorted(params): | |
| plist.append("%s=%s" % (i, params[i])) | |
| pstr = "&".join(plist) | |
| msg = "%s&%s&%s" % (method, urllib.quote(url, ""), | |
| urllib.quote(pstr, "")) | |
| # Calculate Signature | |
| h = hmac.new("%s&%s" % (csecret, secret), msg, hashlib.sha1) | |
| sig = h.digest().encode("base64").strip() | |
| return sig | |
| def init_params(): | |
| p = { | |
| "oauth_consumer_key": ckey, | |
| "oauth_signature_method": "HMAC-SHA1", | |
| "oauth_timestamp": str(int(time.time())), | |
| "oauth_nonce": str(random.getrandbits(64)), | |
| "oauth_version": "1.0" | |
| } | |
| return p | |
| def oauth_header(params): | |
| plist = [] | |
| for p in params: | |
| plist.append('%s="%s"' % (p, urllib.quote(params[p]))) | |
| return "OAuth %s" % (", ".join(plist)) | |
| if not atoken and not atoken_secret: | |
| # Request Parameters | |
| params = init_params() | |
| print "Get request token:", | |
| # Generate Signature | |
| sig = make_signature(params, reqt_url, "GET", csecret) | |
| params["oauth_signature"] = sig | |
| # Get Token | |
| req = urllib2.Request("%s?%s" % (reqt_url, urllib.urlencode(params))) | |
| resp = urllib2.urlopen(req) | |
| print "¥t[OK]" | |
| # Parse Token Parameters | |
| ret = cgi.parse_qs(resp.read()) | |
| token = ret["oauth_token"][0] | |
| token_secret = ret["oauth_token_secret"][0] | |
| # Get PIN | |
| print "* Please access to this URL, and allow." | |
| print "> %s?%s=%s" % (auth_url, "oauth_token", token) | |
| print "¥n* After that, will display 7 digit PIN, input here." | |
| print "PIN ->", | |
| pin = raw_input() | |
| pin = int(pin) | |
| print "Get access token:", | |
| # Generate Access Token Request | |
| params = init_params() | |
| params["oauth_verifier"] = pin | |
| params["oauth_token"] = token | |
| sig = make_signature(params, acct_url, "GET", csecret, token_secret) | |
| params["oauth_signature"] = sig | |
| # Get Access Token | |
| req = urllib2.Request("%s?%s" % (acct_url, urllib.urlencode(params))) | |
| resp = urllib2.urlopen(req) | |
| print "¥t[OK]" | |
| # Parse Access Token | |
| fin = cgi.parse_qs(resp.read()) | |
| atoken = fin["oauth_token"][0] | |
| atoken_secret = fin["oauth_token_secret"][0] | |
| print "Access Token: %s" % atoken | |
| print "Access Token Secret: %s" % atoken_secret | |
| print "Your screen_name is '%s'." % fin["screen_name"][0] | |
| # Get Stream | |
| def getStream(): | |
| url = chirp_url | |
| params = init_params() | |
| params["oauth_token"] = atoken | |
| sig = make_signature(params, url, "GET", csecret, atoken_secret) | |
| params["oauth_signature"] = sig | |
| req = urllib2.Request(url) | |
| req.add_header("Authorization", oauth_header(params)) | |
| return urllib2.urlopen(req) | |
| def add(st,id): | |
| if not id in st: | |
| st[id] = 1 | |
| else : | |
| st[id] += 1 | |
| return st[id] | |
| def main(): | |
| stream = getStream() | |
| stream.readline() | |
| while 1: | |
| json = simplejson.loads(stream.readline()) | |
| if "event" in json: | |
| ev_type = json['event'] | |
| who = json["source"]["screen_name"] | |
| target = json["target"]["screen_name"] | |
| retweet_count = json["target_object"]["retweet_count"] | |
| if ev_type in ["favorite","retweet"] : | |
| text = json["target_object"]["text"] | |
| created_at = json['target_object']['created_at'] | |
| id = json['target_object']['id'] | |
| point = add(state[ev_type],id) | |
| print who , ev_type , target,"fav:",point,"rt:",retweet_count,text ,created_at | |
| if target == myname: | |
| g.notify( | |
| noteType = 'fav', | |
| title = who+":"+ev_type+str(point), | |
| description = text , | |
| sticky = False | |
| ) | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment