Skip to content

Instantly share code, notes, and snippets.

@ryoco
Last active August 29, 2015 14:00
Show Gist options
  • Save ryoco/11105316 to your computer and use it in GitHub Desktop.
Save ryoco/11105316 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from io import StringIO
import sys
import urllib3
import json
def get(appid, category):
http = urllib3.PoolManager()
uri = "https://app.rakuten.co.jp/services/api/Recipe/CategoryList/20121121?applicationId={id}&categoryType={cat}".format(id=appid, cat=category)
return http.request('GET', uri)
def parse_category_name(jsonstr, category):
io = StringIO(jsonstr)
p = json.load(io)
d = []
for i in p["result"][category]:
if (category == "large"):
d.append("{i},{n}".format(i=eu8(i["categoryId"]), n=eu8(i["categoryName"])))
else:
d.append("{i},{n},{pn}".format(i=i["categoryId"], n=eu8(i["categoryName"]), pn=eu8(i["parentCategoryId"])))
return d
def eu8(s):
return s.encode("utf-8")
if __name__ == '__main__':
appid = sys.argv[1]
category = sys.argv[2]
r = get(appid, category)
j = unicode(r.data,"utf-8")
f = open('{cat}_id.txt'.format(cat=category), 'w')
for row in parse_category_name(j,category):
f.write(row + "\n")
f.close()
test = u"""
{
"result": {
"large": [],
"medium": [
{
"categoryId": 275,
"categoryName": "牛肉",
"categoryUrl": "http://recipe.rakuten.co.jp/category/10-275/",
"parentCategoryId": "10"
},
{
"categoryId": 276,
"categoryName": "豚肉",
"categoryUrl": "http://recipe.rakuten.co.jp/category/10-276/",
"parentCategoryId": "10"
}
],
"small": []
}
}
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment