Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
aws lambdaではてなのホッテントリのRSSを取得するpython
#coding: UTF-8
import json
import sys
import urllib.request
import xmltodict
def hotentry(category):
url = "http://b.hatena.ne.jp/hotentry"
if category=="all" or category=="":
url += "?mode=rss"
elif category=="social":
url += "/social.rss"
elif category=="it":
url += "/it.rss"
elif category=="life":
url += "/life.rss"
elif category=="knowledge":
url += "/knowledge.rss"
elif category=="fun":
url += "/fun.rss"
elif category=="entertainment":
url += "/entertainment.rss"
elif category=="game":
url += "/game.rss"
elif category=="economics":
url += "/economics.rss"
return url
def getCategory(event):
category = ""
if (isinstance(event, dict)):
if ("queryStringParameters" in event):
if ("category" in event["queryStringParameters"]):
category = event["queryStringParameters"]["category"]
return category
def lambda_handler(event, context):
category = getCategory(event)
url = hotentry(category)
data = ""
with urllib.request.urlopen(url) as res:
xml = res.read().decode("utf-8")
dict = xmltodict.parse(xml)
data = json.dumps(dict, indent=4)
return {
"statusCode": "200",
'headers': {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin' : '*'
},
"body": data
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment