Created
January 26, 2010 22:03
-
-
Save mizchi/287293 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 -*- | |
#-*- compile-command: "python ldr_sc.py" -*- | |
import urllib, urllib2, cookielib,re | |
from time import sleep | |
from Queue import Queue | |
import simplejson | |
import twitter | |
import bitly | |
import memcache | |
""" | |
Python 2.6 以上 | |
要 :simplejson bitly.py twitter memcache | |
python-bitly - Project Hosting on Google Code http://code.google.com/p/python-bitly/ | |
LDRの新着エントリをTwitterに投げます | |
TODO: | |
""" | |
# 設定ファイル読み込み ※ 環境に応じて | |
conf=simplejson.load(open("config.json")) | |
LIVEDOOR_ID = conf['livedoor_id'] | |
PASSWORD = conf['livedoor_password'] | |
BITLY_KEY=conf['bitly_key'] | |
bitly = bitly.Api(login=conf['id'], apikey=BITLY_KEY) | |
mc = memcache.Client(['localhost:11211']) | |
tw=twixy.Api( | |
conf['twitter_bot_id'], | |
conf['twitter_password'], | |
proxy="http://twixy53.appspot.com/api" | |
) | |
# 認証 | |
API = 'http://reader.livedoor.com/api' | |
cj = cookielib.CookieJar() | |
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) | |
interval=150 | |
def login(): | |
try: | |
url ="http://member.livedoor.com/login/index" | |
query = urllib.urlencode([("livedoor_id",LIVEDOOR_ID),("password",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( | |
[ | |
( | |
"【"+title+ "】"+i["title"].encode("utf-8")+":"+ | |
shortenURL(i["link"].encode("utf-8")) | |
).replace("\n",""), | |
i["modified_on"] ]) | |
else: | |
print "No Feed " | |
return sorted(links, key=lambda x:(x[1], x[0]),reverse=True) | |
def postToTwitter(text): | |
try: | |
if len(text) > 140: | |
text=shortenText(text) | |
print "remove title", | |
tw.PostUpdate(text) | |
print "PostDone" | |
except: | |
pass | |
def shortenURL(url) : | |
return bitly.shorten(url) | |
def shortenText(text) : | |
p=re.compile(u"^【.*?】") | |
return p.sub("",text) | |
def main(): | |
q=Queue() | |
current_list=getAllUnreadEntry() | |
if not mc.get("ldr_latest_update"): | |
print "Set new brake point." | |
mc.set("ldr_latest_update",current_list[0][1]) | |
last_update = mc.get("ldr_latest_update") | |
for i in range(len(current_list)): | |
if last_update < current_list[i][1]: | |
q.put(current_list[i]) | |
print current_list[i][1] | |
else: | |
break | |
mc.set("ldr_latest_update",current_list[0][1]) | |
print current_list[0][1] | |
if q.qsize()>0: | |
print q.qsize(), | |
for i in range(q.qsize()): | |
text=q.get()[0] | |
print text | |
postToTwitter(text) | |
else: | |
print "No Feed" | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment