-
Notifications
You must be signed in to change notification settings - Fork 1
/
drag.py
58 lines (44 loc) · 1.75 KB
/
drag.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/local/bin/python3
# coding: utf-8
# BagAndDrag - drag.py
# 1/10/21 15:38
#
__author__ = "Benny <[email protected]>"
import argparse
import logging
import time
import requests
import traceback
from concurrent.futures import ThreadPoolExecutor
from tqdm import tqdm
from bag import SHARE_URL, API_DATA, load_cookies, is_cookie_valid, login, insert_db, insert_error
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(filename)s [%(levelname)s]: %(message)s')
s = requests.session()
ua = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36"
s.headers.update({"User-Agent": ua})
parser = argparse.ArgumentParser()
# start_id, end_id, interval, concurrency
parser.add_argument("-s", help="Start id", type=int, default=1)
parser.add_argument("-e", help="End id", type=int, default=10)
parser.add_argument("-i", help="Interval", default=5, type=int)
parser.add_argument("-c", help="Concurrency", default=2, type=int)
args = parser.parse_args()
executor = ThreadPoolExecutor(max_workers=args.c)
def get_api_json(resource_id):
try:
time.sleep(args.i)
if not is_cookie_valid():
login()
logging.info("resource id is %s", resource_id)
res = s.post(SHARE_URL, data={"rid": resource_id}, cookies=load_cookies()).json()
share_code = res['data'].split('/')[-1]
logging.info("Share code is %s", share_code)
data = s.get(API_DATA.format(code=share_code)).json()
insert_db(data)
except Exception:
insert_error(resource_id, traceback.format_exc())
def main():
total = args.e + 1 - args.s
list(tqdm(executor.map(get_api_json, range(args.s, args.e + 1)), total=total))
if __name__ == '__main__':
main()