Skip to content

Instantly share code, notes, and snippets.

@mizchi
Created January 26, 2010 13:31
Show Gist options
  • Save mizchi/286835 to your computer and use it in GitHub Desktop.
Save mizchi/286835 to your computer and use it in GitHub Desktop.
#/usr/bin/python
#-*- encoding:utf8 -*-
import urllib, urllib2, cookielib,simplejson
LIVEDOOR_ID = "['livedoor_id']"
PASSWORD = "['livedoor_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 None
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 None
def parseJson(doc): #テキストをutf-8のjsonオブジェクトに
try:
return simplejson.loads(unicode(doc,"utf-8","replace"))
except:
print "ParseError"
return None
def main():
while not login():
print "Login Failure"
print "Login OK"
#Fetch URL List
ids = getUnreadSubscIDs()
if ids:
for id in ids:
print id["title"].encode('utf_8') #+ i["body"].encode('utf_8')
try:
feed = getUnreadFeedBy(id["subscribe_id"]) #getUnreadFeedBy(i)
for i in feed["items"]:
print " "+ i["title"].encode('utf_8') #+ i["body"].encode('utf_8')
except:
print "error",
print id["title"].encode('utf-8')
else:
print "Not Remained Unread"
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment