Last active
August 29, 2015 14:00
Revisions
-
ryoco renamed this gist
Apr 25, 2014 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
ryoco created this gist
Apr 20, 2014 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,60 @@ #!/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": [] } } """