Skip to content

Commit

Permalink
修改标题
Browse files Browse the repository at this point in the history
  • Loading branch information
xinlingqudongX committed May 22, 2019
1 parent 3a4febe commit 5f0f33a
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 87 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# TSDK
淘宝爬虫SDK
淘宝爬虫SDK,用于淘宝开放平台或淘宝登录爬取


## 淘宝系列爬虫系列SDK
Expand Down
35 changes: 35 additions & 0 deletions taobao/SDK基类.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,41 @@ def resovle(key_name):
sign_str = '&'.join(dt)

return HMAC(f'{secret}'.encode('utf-8'),sign_str.encode('utf-8'))

def map2str(self,paramData:dict):
'''转换传递的字典为连接字符串'''
def resovle(key_name):
if paramData.get(key_name) == 'null':
return ''
elif not isinstance(paramData.get(key_name,''),str):
dumpStr = json.dumps(paramData.get(key_name),separators=(',',':'))
if key_name == 'data':
return md5(dumpStr.encode()).hexdigest()
else:
return dumpStr
else:
return paramData.get(key_name,'')

keyword_ls = ['utdid','uid','reqbiz-ext','appKey','data','t','api','v','sid','ttid','deviceId','lat','lng','x-features']
if paramData.get('extdata'):
keyword_ls.insert(-1,'extdata')
data = list(map(resovle,keyword_ls))
sign_str = '&'.join(data)

return sign_str

def addHeader(self,headers:dict={}):
default_headers = {
'x-mini-wua':'HHnB_3P3rpjjITYyMfTtldI+UFRpOScjy799La90R\/mybecdU56yAvaqEmEy46f5uQUCDcnIiqfJxLzP4vM0BxDClP8zD\/6RHI1z8StNM6Uqo73b2GC7KN7bQuM\/fXcQeS463',
'x-ttid':'700169@travel_android_9.2.5',
'x-features-':'27',
'x-appkey':'12663307',
'x-pv':''
}
self.headers.update(default_headers)
self.headers.update(headers)
return


# 不能使用静态方法来作为装饰器
# @staticmethod
Expand Down
Binary file modified taobao/__pycache__/SDK基类.cpython-37.pyc
Binary file not shown.
92 changes: 10 additions & 82 deletions taobao/淘宝APP.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,95 +11,23 @@

class TB_APP(Base):

def __init__(self,name:str,config:dict,req_config:dict):
def __init__(self,config:dict={
"domain":"https://acs.m.taobao.com"
}):
'''淘宝app类暂时不做更改,因为暂时也是无用'''
self.config = config
self.req_config = req_config
self.mtop = Session()
super(TB_APP,self).__init__()

def __tb_appInfo(self):
state_info = {
'KEY_ACCESS_TOKEN':'accessToken',
'KEY_API':'api',
'KEY_APPKEY':'appKey',
'KEY_APP_BACKGROUND':'AppBackground',
'KEY_CURRENT_PAGE_NAME':'PageUrl',
'KEY_CURRENT_PAGE_NAME':'PageName',
'KEY_DATA':'data',
'KEY_DEVICEID':'deviceId',
'KEY_EXTDATA':'extdata',
'KEY_LAT':'lat',
'KEY_LNG':'lng',
'KEY_MTEE_UA':'ua',
'KEY_NETTYPE':'netType',
'KEY_NQ':'nq',
'KEY_PV':'pv',
'KEY_REQBIZ_EXT':'reqbiz-ext',
'KEY_SG_ERROR_CODE':'SG_ERROR_CODE',
'KEY_SID':'sid',
'KEY_SIGN':'sign',
'KEY_TIME':'t',
'KEY_TIME_OFFSET':'t_offset',
'KEY_TTID':'ttid',
'KEY_TYPE':'type',
'KEY_UA':'ua',
'KEY_UID':'uid',
'KEY_UMID_TOKEN':'umt',
'KEY_UTDID':'utdid',
'KEY_VERSION':'v',
'KEY_WUA':'wua',
'VALUE_TIME_OFFSET':'0',
'VALUE_PRODUCT_PV':'1.0',
'VALUE_OPEN_PV':'1.0',
'VALUE_INNER_PV':'5.1',
'KEY_X_FEATURES':'x-features',

self.appConfig = {
'trip':''
}
super(TB_APP,self).__init__()

def params_check(self,params:dict):
'''参数检查
如果检查参数缺少但是配置参数是真的话,报错提示缺少必要的参数
如果检查参数缺少但是配置参数假的话,暂时未定义
如果检查参数缺少但是配置参数有默认的话,添加到参数上
'''
options = self.req_config.get('data',[])
for item in options:
if item.get('required',False) and not params.get(item.get('name'),False):
raise Exception(f'缺少必要的参数:{item.get("name")}')
elif not item.get('required',False) and params.get(item.get('name'),False):
pass
elif not item.get('required',False) and not params.get(item.get('name'),False):
params[item.get("name")] = item.get("value")
else:
pass
return params
def getAppkey(self,Appname:str):
return self.appConfig.get(Appname,'21646297')

def getres(self,params:dict):
def execute(self,datas:dict):
pass
res = self.request(**options) if hasattr(self,'request') else self.mtop.request(**options)

def appsign(self):
dt = OrderedDict()
dt.update({
'utdid':'W4d7R8qEOy0DAFAPLFNrh2h1',
'uid':'',
'reqbiz-ext':'',
'data':'',
't':'',
'api':'',
'v':'',
'sid':'',
'ttid':'10035437%40etao_android_8.8.6',
'deviceld':'AnQezS1fsBjqX0zUCeXSMm5HlPCH5TJRP7Xl6-fG1p5K',
'lat':'',
'lng':'',
'x-features':''
})

def __call__(self,**kw):
pass


if __name__ == "__main__":
ts = 淘宝APP()
ts = TB_APP()
12 changes: 8 additions & 4 deletions taobao/淘宝开放平台.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

try:
from .SDK基类 import Base
from ..util.config import TSDKError
except ImportError:
from SDK基类 import Base


from util.config import TSDKError
from collections import OrderedDict
import time
import json
Expand Down Expand Up @@ -37,18 +39,20 @@ def __init__(self,config:dict={
self.config = config

def __init(self):
self.public_params = OrderedDict();
self.public_params = OrderedDict()
if not self.config['appkey'] or not self.config['appsecret']:
raise TSDKError('缺少appkey或appsecret')
self.public_params.update({'app_key':self.config['appkey']})
self.public_params.update({'sign_method':'md5' or 'hmac'})
self.public_params.update({'timestamp':time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())})
self.public_params.update({'v':'2.0'})
self.public_params.update({'format':'json'})

def execute(self,apiname:str,datas:dict):
options = OrderedDict();
options = OrderedDict()
#初始化公共参数
self.__init()
datas.update(self.public_params);
datas.update(self.public_params)
datas.update({'method':apiname})
if datas.get('sign_method','md5') == 'hmac':
sign = self.open_Hmacsign(self.config.get('appsecret',None),datas)
Expand Down
14 changes: 14 additions & 0 deletions util/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,20 @@ def __init__(self):

def getConfig(self,name):
return self.Config_manager.get(name,'')



class TSDKError(Exception):

def __init__(self,errInfo):
super().__init__(self)
self.errinfo = errInfo

def __setError(self):
pass

def __str__(self):
return self.errinfo



Expand Down

0 comments on commit 5f0f33a

Please sign in to comment.