Click here for DMM API specifications https://affiliate.dmm.com/api/ It just returns XML, so you can parse it or whatever you like ...
#coding:utf-8
import urllib2
import time
"""
Get product information using DMM API
"""
class Dmm:
def __init__(self, api_id, affiliate_id):
"""constructor"""
self.baseurl = "http://affiliate-api.dmm.com/"
self.proxy_host = None
self.proxy_port = None
self.api_id = api_id
self.affiliate_id = affiliate_id
self.version = "1.0.0"
self.site = 'DMM.co.jp'
self.service = 'digital'
self.operation = 'ItemList'
self.url = None
def setProxy(self, host, port=8080):
"""Set proxy"""
self.proxy_host = host
self.proxy_port = port
def setVersion(self, version):
"""Set version"""
self.version = version
def setSite(self, site):
"""Set Site"""
self.site = site
def setService(self, service):
"""Set Service"""
self.service = service
def keywordSearch(self, keyword, **options):
"""Search for items"""
params = options
params["keyword"] = keyword
return unicode(self.sendRequest(params),'euc-jp')
#return self.buildURL(params)
def buildURL(self, params):
"""Construct URL address for REST request"""
params["api_id"] = self.api_id
params["affiliate_id"] = self.affiliate_id
params["version"] = self.version
params["site"] = self.site
params["service"] = self.service
params["operation"] = self.operation
params["timestamp"] = time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime())
sorted_params = sorted(params.items())
#Expand hash of params
request = []
for p in sorted_params:
pair = "%s=%s" % (p[0], urllib2.quote(p[1].encode("euc-jp")))
request.append(pair)
url = self.baseurl + "?" + "&".join(request)
return url
def sendRequest(self, params):
"""Send a request to the DMM and return the retrieved XML"""
self.url = self.buildURL(params)
if self.proxy_host:
proxy_handler = urllib2.ProxyHandler({"http":"http://%s:%s/" % (self.proxy_host, self.proxy_port)})
opener = urllib2.build_opener(proxy_handler)
else:
opener = urllib2.build_opener()
return opener.open(self.url).read()
def _test():
dmm = Dmm('API key', 'Affiliate')
print dmm.keywordSearch('sex',hits='100')
if __name__ == '__main__':
_test()
Recommended Posts