bin zhang avatar bin zhang committed 36a5168

初始化pytop测试淘客成功

Comments (0)

Files changed (2)

+# -*- coding: utf-8 -*-
+Format = 'json'
+SignMethod = 'md5'
+APIVersion = '2.0'
+
+class Sandbox:
+    """淘客无法在沙箱中测试"""
+    AppKey     = 'test'
+    AppSecret  = 'test'
+    Gateway    = 'http://gw.api.tbsandbox.com/router/rest'
+
+class Official:
+    AppKey     = 'your key'
+    AppSecret  = 'your secret'
+    Gateway    = 'http://gw.api.taobao.com/router/rest'
+
+Default = 'Official'
+# -*- coding: utf-8 -*-
+"""
+原始出处:http://blog.csdn.net/jakieyoung/archive/2011/01/18/6149769.aspx
+支持top v2.0版,简化,并优化代码
+update by: iamsk<iamsk.info@gmail.com>
+"""
+from datetime import datetime
+import urllib
+import urllib2
+import md5
+
+try:
+    import json
+except ImportError:
+    import simplejson as json
+
+from config import Default, Sandbox, Official
+import config
+
+def tomd5(tempstr):
+    m = md5.new(tempstr)
+    return m.hexdigest().upper()
+
+class Client:
+    def __init__(self):
+        default = eval(Default + '()')
+        self.app_key = default.AppKey
+        self.app_secret = default.AppSecret
+        self.gateway = default.Gateway
+        self.sys_params = {
+            'app_key': self.app_key,
+            'v': config.APIVersion,
+            'format': config.Format,
+            'sign_method': config.SignMethod,
+            'timestamp': datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
+        }
+
+    def _sign(self, params):
+        """签名"""
+        temp = ''.join(["%s%s" % (k, v) for k, v in sorted(params.items())])
+        tempstr = ''.join([self.app_secret, temp, self.app_secret])
+        return tomd5(tempstr)
+
+    def execute(self, request, session=None):
+        d = self.sys_params.copy()
+        d.update(request.get_api_params())
+        d['method'] = request.get_method_name()
+        if session:
+            d['session'] = session
+        d['sign'] = self._sign(d)
+        form_data = urllib.urlencode(d)
+        urlopen = urllib2.urlopen(self.gateway, form_data)
+        rsp = urlopen.read()
+        return json.loads(rsp)
+
+class TopRequest:
+    """Top平台,请求的封装"""
+    def __init__(self, method_name):
+        self.method_name = method_name
+        self.api_params = {}
+
+    def get_api_params(self):
+        return self.api_params
+
+    def get_method_name(self):
+        return self.method_name
+
+    def __setitem__(self, param_name, param_value):
+        self.api_params[param_name] = param_value
+
+if __name__ == '__main__':
+    client = Client()
+    req = TopRequest('taobao.taobaoke.items.convert')
+    req['nick'] = 's_k_ing'
+    req['fields'] = 'num_iid,title,pic_url,price,click_url '
+    req['num_iids'] = '2140908613'
+    j = client.execute(req)
+    print j['taobaoke_items_convert_response']['taobaoke_items']['taobaoke_item'][0]#['title'].encode('utf-8')
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.