Skip to content

Instantly share code, notes, and snippets.

@mizchi
Created January 26, 2010 17:55
Show Gist options
  • Save mizchi/287045 to your computer and use it in GitHub Desktop.
Save mizchi/287045 to your computer and use it in GitHub Desktop.
#/usr/bin/python
#-*- encoding:utf8 -*-
#-*- compile-command: "python ldr_sc.py" -*-
import urllib, urllib2, cookielib
import simplejson,twitter
from time import sleep
from Queue import Queue
"""
新着エントリをTwitterに投げます
Todo:
bitlyに対応
"""
# 設定ファイル読み込み ※ 環境に応じて
conf=simplejson.load(open("config.json"))
LIVEDOOR_ID = conf['livedoor_id']
PASSWORD = conf['livedoor_password']
tw=twitter.Api(
conf['twitter_bot_id'],
conf['twitter_password'],
)
# 認証用
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",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["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]
#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])
last_update=current_list[i][1]
print last_update
else:
break
if q.qsize()>0:
print q.qsize(),
text=q.get()[0]
print text
postToTwitter(text)
else:
print "|"
sleep(180)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment