helper.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <<<<<<< HEAD
  2. import datetime
  3. import hashlib
  4. import json
  5. import platform
  6. # import time
  7. import requests
  8. from requests_futures.sessions import FuturesSession
  9. # from scrapy import Selector
  10. # from selenium import webdriver
  11. from hgg070_spider.conf.uid import UID as u_id, UID
  12. # from selenium.webdriver import FirefoxOptions
  13. fs_session = FuturesSession()
  14. =======
  15. import hashlib
  16. import json
  17. from requests_futures.sessions import FuturesSession
  18. from .langconv import *
  19. from .LocalToken import token
  20. >>>>>>> 10c979a4fcc4f2d36f17fa2ecd6de7bad38358f3
  21. class Helper(object):
  22. @staticmethod
  23. <<<<<<< HEAD
  24. def changetime(params):
  25. if params.endswith('p'):
  26. p_time = params[-6:-1]
  27. if p_time.startswith('12'):
  28. us_time = params[:-1] + ":00"
  29. # print(us_time)
  30. # start_time = datetime.datetime.strptime(us_time, "%Y-%m-%d %H:%M:%S") + datetime.timedelta(
  31. # hours=12)
  32. # match_date = start_time.strftime("%Y-%m-%d %H:%M:%S").split(' ')[0]
  33. # match_time = start_time.strftime("%Y-%m-%d %H:%M:%S").split(' ')[1]
  34. else:
  35. params = params[:-1] + ':00'
  36. us_time = datetime.datetime.strptime(params, "%Y-%m-%d %H:%M:%S") + datetime.timedelta(
  37. hours=12)
  38. # start_time = us_time + datetime.timedelta(hours=12)
  39. # match_date = start_time.strftime("%Y-%m-%d %H:%M:%S").split(' ')[0]
  40. # match_time = start_time.strftime("%Y-%m-%d %H:%M:%S").split(' ')[1]
  41. us_time = us_time.strftime("%Y-%m-%d %H:%M:%S")
  42. # pass
  43. else:
  44. us_time = params[:-1] + ':00'
  45. # start_time = datetime.datetime.strptime(params, "%Y-%m-%d %H:%M:%S") + datetime.timedelta(
  46. # hours=12)
  47. # match_date = start_time.strftime("%Y-%m-%d %H:%M:%S").split(' ')[0]
  48. # match_time = start_time.strftime("%Y-%m-%d %H:%M:%S").split(' ')[1]
  49. return us_time
  50. @staticmethod
  51. def get(url, params):
  52. return requests.get(url, data={"data": json.dumps(params), "token": u_id['token']}, timeout=30)
  53. @staticmethod
  54. def post(url, params):
  55. return requests.post(url, data={"data": json.dumps(params), "token": u_id['token']}, timeout=30)
  56. @staticmethod
  57. def async_post(url, params):
  58. try:
  59. # print(u_id['token'])
  60. data = fs_session.post(url, data={"data": json.dumps(params), "token": u_id['token']}).result()
  61. # data = fs_session.post(url, data={"data": json.dumps(params), "token": "agxrI115617094865d15cbaecca9f"}, timeout=30).result()
  62. if data:
  63. response_data = json.loads(data.content.decode('utf-8'))
  64. # print(response_data)
  65. # token异常重新获取
  66. if response_data.get('status') == 10032:
  67. token = Helper.get_token()
  68. sys = platform.system()
  69. if sys == 'Windows':
  70. file_path = os.path.abspath(os.path.join(os.getcwd(), "..")) + "\\conf\\uid.py"
  71. else:
  72. file_path = os.path.abspath(os.path.join(os.getcwd(), "..")) + "/hgg070_spider/conf/uid.py"
  73. if os.path.exists(file_path):
  74. fs = open(file_path, 'w+')
  75. UID['token'] = token
  76. fs.write('UID={}'.format(UID))
  77. fs.close()
  78. data = fs_session.post(url, data={"data": json.dumps(params), "token": u_id['token']}).result()
  79. # data = fs_session.post(url, data={"data": json.dumps(params), "token": u_id['token']})
  80. return data.content.decode('utf-8')
  81. # return data
  82. except requests.exceptions.RequestException as e:
  83. print(e)
  84. @staticmethod
  85. def get_zip_data(list1, list2):
  86. if list1 and list2:
  87. if isinstance(list1, list) and isinstance(list2, list):
  88. return dict(zip(list1, list2))
  89. @staticmethod
  90. def get_token():
  91. cpath = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  92. with open(cpath + '/conf/settings.json', 'r', encoding='utf8') as f:
  93. data = json.load(f)
  94. token_url = data['token_url']
  95. username1 = data['username1']
  96. password1 = data['password1']
  97. r = requests.post(token_url, data={'account': username1, 'password': password1})
  98. token = r.json()['data']['token']
  99. return token
  100. @staticmethod
  101. def genearte_MD5(params):
  102. # 创建md5对象
  103. hl = hashlib.md5()
  104. hl.update(params.encode(encoding='utf-8'))
  105. # print('MD5加密前为 :' + params)
  106. # print('MD5加密后为 :' + hl.hexdigest())
  107. return hl.hexdigest()
  108. if __name__ == '__main__':
  109. import os
  110. print(os.path.abspath(os.path.join(os.getcwd(), "..")) + "\\conf\\uid.py")
  111. print(os.path.abspath(__file__))
  112. =======
  113. def async_post(url, params):
  114. fs_session = FuturesSession()
  115. t_url, t_user, t_password, t_token = token['token_url'], token['username'], token['password'], token['token']
  116. data = fs_session.post(url, data={"data": json.dumps(params), "token": t_token}, timeout=180).result()
  117. try:
  118. new_data = data.json()
  119. if new_data.get('status') == 6:
  120. t_data = fs_session.post(url=t_url, data={'account': t_user, 'password': t_password}).result()
  121. if t_data.json().get('status') == 1:
  122. g_token = t_data.json()['data']['token']
  123. token['token'] = g_token
  124. with open('./utils/LocalToken.py', 'w+', encoding='utf8') as f:
  125. f.write('token = {}'.format(token))
  126. except Exception as e:
  127. print(e)
  128. new_data = {"status": 0, "msg": "接口返回异常", "data": []}
  129. return new_data
  130. @staticmethod
  131. def genearte_MD5(params, pt):
  132. # 创建md5对象
  133. hl = hashlib.md5()
  134. pn = int(pt)
  135. if pn == 3:
  136. param = params + str(pt)
  137. else:
  138. param = params
  139. hl.update(param.encode(encoding='utf-8'))
  140. return hl.hexdigest()
  141. @staticmethod
  142. def genearte_uuid(params):
  143. # 简体
  144. line = Converter("zh-hans").convert(params).replace(' ', '')
  145. # 繁体
  146. # line = Converter("zh-hant").convert(params).replace(' ', '')
  147. hl = hashlib.md5()
  148. hl.update(line.encode(encoding='utf-8'))
  149. return hl.hexdigest()
  150. >>>>>>> 10c979a4fcc4f2d36f17fa2ecd6de7bad38358f3