Skip to content

Instantly share code, notes, and snippets.

@ryoco
Last active August 29, 2015 14:00

Revisions

  1. ryoco renamed this gist Apr 25, 2014. 1 changed file with 0 additions and 0 deletions.
  2. ryoco created this gist Apr 20, 2014.
    60 changes: 60 additions & 0 deletions rakuten_recipe_api_sample_category
    Original 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": []
    }
    }
    """