12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- import json
- from pathlib import Path
- import requests
- from utils.log import logger
- from utils.tools import wait
- from utils.databases import mongo_table
- from bson.objectid import ObjectId
- ROOT_PATH = Path(__file__).parent.parent
- _headers = {"Authorization": "Basic amlhbnl1MDAxOjEyM3F3ZSFB"}
- JSON_ACCOUNT_RECORD = (ROOT_PATH / 'config/account_record.json').resolve()
- def account_record(uid, crawl_type):
- with open(JSON_ACCOUNT_RECORD, 'w+', encoding='utf-8') as wp:
- item = {"uid": uid, "crawl_type": crawl_type}
- wp.write(json.dumps(item, indent=4))
- def read_account():
- try:
- with open(JSON_ACCOUNT_RECORD, encoding='utf-8') as rp:
- cookies: dict = json.load(rp)
- return cookies
- except (json.decoder.JSONDecodeError, FileNotFoundError):
- pass
- def get_account(site, crawl_type):
- url = "http://cc.spdata.jianyu360.com/competing_goods/account/fetch"
- params = {"site": site, "crawl_type": crawl_type}
- try:
- response = requests.get(url, headers=_headers, params=params, timeout=60)
- data = response.json()['data']
- logger.info("当前账号状态:{}".format(data['crawl_detail']))
- except requests.RequestException:
- # 网络不通信时,无法获取账号
- logger.info("网络异常,获取账号失败")
- data = None
- return data
- def release_account(uid, crawl_type, disable_log=False):
- url = "http://cc.spdata.jianyu360.com/competing_goods/account/release"
- params = {"uid": uid, "crawl_type": crawl_type}
- while True:
- try:
- response = requests.get(url, headers=_headers, params=params, timeout=60)
- response.raise_for_status()
- if not disable_log:
- logger.info(f"release_account >>> {response.json()}")
- break
- except requests.RequestException:
- logger.error("网络异常,归还账号失败")
- wait(1)
|