helper.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import hashlib
  2. import json
  3. import time
  4. from requests_futures.sessions import FuturesSession
  5. from .langconv import *
  6. from .LocalToken import token
  7. class Helper(object):
  8. @staticmethod
  9. def async_post(url, params):
  10. fs_session = FuturesSession()
  11. t_url, t_user, t_password, t_token = token['token_url'], token['username'], token['password'], token['token']
  12. data = fs_session.post(url, data={"data": json.dumps(params), "token": t_token}, timeout=180).result()
  13. try:
  14. new_data = data.json()
  15. if new_data.get('status') == 6:
  16. t_data = fs_session.post(url=t_url, data={'account': t_user, 'password': t_password}).result()
  17. if t_data.json().get('status') == 1:
  18. g_token = t_data.json()['data']['token']
  19. token['token'] = g_token
  20. with open('./utils/LocalToken.py', 'w+', encoding='utf8') as f:
  21. f.write('token = {}'.format(token))
  22. except Exception as e:
  23. print(e)
  24. new_data = {"status": 0, "msg": "接口返回异常", "data": []}
  25. return new_data
  26. @staticmethod
  27. def genearte_MD5(params, pt):
  28. # 创建md5对象
  29. hl = hashlib.md5()
  30. pn = int(pt)
  31. if pn == 3:
  32. param = params + str(pt)
  33. else:
  34. param = params
  35. hl.update(param.encode(encoding='utf-8'))
  36. return hl.hexdigest()
  37. @staticmethod
  38. def genearte_uuid(params):
  39. # 简体
  40. line = Converter("zh-hans").convert(params).replace(' ', '')
  41. # 繁体
  42. # line = Converter("zh-hant").convert(params).replace(' ', '')
  43. hl = hashlib.md5()
  44. hl.update(line.encode(encoding='utf-8'))
  45. return hl.hexdigest()
  46. @staticmethod
  47. def change_time(ctime):
  48. time1 = time.mktime(time.strptime(ctime, '%Y-%m-%d %H:%M:%S')) + 43200
  49. time2 = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time1))
  50. match_date = time2.split(" ")[0]
  51. match_time = time2.split(" ")[1]
  52. return match_date, match_time, time2