account.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import json
  2. from pathlib import Path
  3. import requests
  4. from utils.log import logger
  5. from utils.tools import wait
  6. from utils.databases import mongo_table
  7. from bson.objectid import ObjectId
  8. ROOT_PATH = Path(__file__).parent.parent
  9. _headers = {"Authorization": "Basic amlhbnl1MDAxOjEyM3F3ZSFB"}
  10. JSON_ACCOUNT_RECORD = (ROOT_PATH / 'config/account_record.json').resolve()
  11. def account_record(uid, crawl_type):
  12. with open(JSON_ACCOUNT_RECORD, 'w+', encoding='utf-8') as wp:
  13. item = {"uid": uid, "crawl_type": crawl_type}
  14. wp.write(json.dumps(item, indent=4))
  15. def read_account():
  16. try:
  17. with open(JSON_ACCOUNT_RECORD, encoding='utf-8') as rp:
  18. cookies: dict = json.load(rp)
  19. return cookies
  20. except (json.decoder.JSONDecodeError, FileNotFoundError):
  21. pass
  22. def get_account(site, crawl_type):
  23. url = "http://cc.spdata.jianyu360.com/competing_goods/account/fetch"
  24. params = {"site": site, "crawl_type": crawl_type}
  25. try:
  26. response = requests.get(url, headers=_headers, params=params, timeout=60)
  27. data = response.json()['data']
  28. logger.info("当前账号状态:{}".format(data['crawl_detail']))
  29. except requests.RequestException:
  30. # 网络不通信时,无法获取账号
  31. logger.info("网络异常,获取账号失败")
  32. data = None
  33. return data
  34. def release_account(uid, crawl_type, disable_log=False):
  35. url = "http://cc.spdata.jianyu360.com/competing_goods/account/release"
  36. params = {"uid": uid, "crawl_type": crawl_type}
  37. while True:
  38. try:
  39. response = requests.get(url, headers=_headers, params=params, timeout=60)
  40. response.raise_for_status()
  41. if not disable_log:
  42. logger.info(f"release_account >>> {response.json()}")
  43. break
  44. except requests.RequestException:
  45. logger.error("网络异常,归还账号失败")
  46. wait(1)