-
Notifications
You must be signed in to change notification settings - Fork 1
/
hello.py
134 lines (109 loc) · 3.74 KB
/
hello.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
'''
?: *********************************************************************
Author: Weidows
Date: 2022-03-30 15:58:40
LastEditors: Weidows
LastEditTime: 2023-07-14 01:04:15
FilePath: \Keeper\scripts\hello.py
Description: Hello 图床多线程增量备份脚本
命令行启动示例:
python "scripts\hello.py" "username" .\
参数说明:
第一个参数: 你的用户名
第二个参数: 备份路径 (注意末尾带个 '\')
注意:
要在开了代理的终端环境下运行, 否则可能报错
HTTPSConnectionPool(host='www.helloimg.com', port=443): Max retries exceeded
!: *********************************************************************
'''
from concurrent.futures import ThreadPoolExecutor
import json
import os
import sys
import requests
import threading
import multiprocessing
class Utils:
BASE_URL = "https://www.helloimg.com/get-user-images/"
# D:\Repos\Weidows-projects\Keeper\Programming-Configuration\backup\Weidows
BACKUP_PATH = ""
# Weidows
USER_NAME = ""
# 不带 agent 的话会被防火墙拦截: <h1><span>拒绝非浏览器请求</span></h1>
HEADER = {
'User-Agent': 'Apifox/1.0.0 (https://www.apifox.cn)',
'Accept': '*/*',
'Host': 'www.helloimg.com',
'Connection': 'keep-alive'
}
LOCK = threading.Lock()
DATAS = []
CPU_COUNT = multiprocessing.cpu_count()
@staticmethod
def getResponseJson(url, method):
try:
if method == "GET":
# url, headers=Utils.HEADER, verify=False)
responseData = requests.get(url, headers=Utils.HEADER)
elif method == "POST":
responseData = requests.post(url, headers=Utils.HEADER)
return json.loads(responseData.text)
except Exception as e:
print(e)
return None
def multi_downloader():
while True:
Utils.LOCK.acquire()
if Utils.DATAS.__len__() == 0:
Utils.LOCK.release()
return
# https://www.helloimg.com/images/2022/02/26/GVROC6.png
url = Utils.DATAS.pop()["image"]
Utils.LOCK.release()
# 2022-02
date = url[32:39].replace("/", "-")
# GVROC6.png
# 2022-02/GVROC6.png
path = date + '/' + url[43:]
is_exists = False
if not os.path.exists(path):
if not os.path.exists(date):
os.mkdir(date)
pic = requests.get(url, headers=Utils.HEADER)
with open(path, 'wb') as f:
f.write(pic.content)
f.flush()
else:
is_exists = True
Utils.LOCK.acquire()
print(
f"{path} is_exists:{is_exists} {threading.current_thread().name} backuped"
)
Utils.LOCK.release()
if __name__ == '__main__':
argv = sys.argv[1:]
# argv = [
# "Weidows", "D:/Repos/Weidows-projects/Keeper/Programming-Configuration/backup/"
# ]
try:
Utils.USER_NAME = argv[0]
Utils.BACKUP_PATH = argv[1] + "hello"
except IndexError:
print("请正确输入参数中: 用户名 备份路径")
if os.path.exists(Utils.BACKUP_PATH):
print("备份文件夹已存在: " + Utils.BACKUP_PATH)
else:
os.mkdir(Utils.BACKUP_PATH)
os.chdir(Utils.BACKUP_PATH)
Utils.DATAS = Utils.getResponseJson(Utils.BASE_URL + Utils.USER_NAME,
"GET")
try:
os.remove("response.json")
except FileNotFoundError:
pass
with open("response.json", 'wb') as f:
f.write(json.dumps(Utils.DATAS).encode())
f.flush()
with ThreadPoolExecutor(max_workers=Utils.CPU_COUNT) as executor:
for i in range(Utils.CPU_COUNT):
executor.submit(multi_downloader)