Created
February 28, 2010 16:45
-
-
Save mizchi/317668 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/python | |
#-*- encoding:utf8 -*- | |
import urllib, urllib2, cookielib,types,operator | |
import simplejson | |
import twitter | |
from time import sleep | |
from Queue import Queue | |
tw=twitter.Api( | |
"your_id", | |
"your_password", | |
) | |
LIVEDOOR_ID="your_livedoor_id" | |
LIVEDOOR_PASSWORD="your_livedoor_password" | |
interval=180 | |
# 認証用 | |
API = 'http://reader.livedoor.com/api' | |
cj = cookielib.CookieJar() | |
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) | |
def login(): | |
try: | |
url ="http://member.livedoor.com/login/index" | |
query = urllib.urlencode([("livedoor_id",LIVEDOOR_ID),("password",LIVEDOOR_PASSWORD)]) | |
res = opener.open(url, query).read() | |
return True | |
except: | |
print False | |
def getUnreadSubscIDs(): #未読記事一覧のidを取得 | |
url =API+"/subs" | |
query = urllib.urlencode([("unread","1")]) | |
try: | |
res = opener.open(url, query).read() | |
return parseJson(res) | |
except: | |
print "IDs Fetch Error " | |
return getUnreadSubscIDs() | |
def getUnreadFeedBy(id): # 未読idから未読記事を返す | |
url =API+"/unread" | |
query = urllib.urlencode([("subscribe_id",id)]) | |
try: | |
res = opener.open(url, query).read() | |
return parseJson(res) | |
except: | |
print "Feed Fetch Error " | |
return getUnreadFeedBy(id) | |
def setDone(id): #指定IDのフィードを未読にする | |
url =API+"/touch_all" | |
query = urllib.urlencode([("subscribe_id",id)]) | |
res = opener.open(url, query).read() | |
return parseJson(res) | |
def setAllDone(ids): | |
for id in ids: | |
setDone(id) | |
def parseJson(doc): #テキストデータをutf-8のjsonオブジェクトに | |
return simplejson.loads(unicode(doc,"utf-8","replace")) | |
def getAllUnreadEntry(): | |
while not login(): | |
print "Login Failure" | |
links=[] | |
text="" | |
#Fetch URL List | |
ids = getUnreadSubscIDs() | |
if ids: | |
for id in ids: | |
title=id["title"].encode('utf_8') #+ i["body"].encode('utf_8') | |
feed = getUnreadFeedBy(id["subscribe_id"]) #getUnreadFeedBy(i) | |
for i in feed["items"]: | |
links.append( | |
[ | |
# ( | |
# i["title"].encode("utf-8")+" "+ | |
# title+":"+ | |
# i["link"].encode("utf-8") | |
# ).replace("\n","\s"), | |
( | |
i["title"]+":"+i["link"].encode("utf-8") | |
).replace("\n","\s"), | |
i["modified_on"] ]) | |
else: | |
print "No Feed " | |
return sorted(links, key=lambda x:(x[1], x[0]),reverse=True) | |
def postToTwitter(text): | |
try: | |
tw.PostUpdate(text) | |
print "PostDone" | |
except: | |
pass | |
def main(): | |
q=Queue() # | |
current_list=getAllUnreadEntry() | |
#for i in current_list | |
# print str(i[1])+" "+i[0] | |
last_update=current_list[0][1] | |
print last_update, | |
print current_list[0][0].encode("utf-8") | |
#main loop | |
while 1: | |
current_list=getAllUnreadEntry() | |
for i in range(len(current_list)): | |
if last_update < current_list[i][1]: | |
q.put(current_list[i]) | |
print last_update | |
else: | |
break | |
last_update=current_list[0][1] | |
while q.qsize>0: | |
print q.qsize(), | |
text=q.get()[0] | |
print text | |
postToTwitter(text) | |
print "|" | |
#sleep(interval) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment