Skip to content

Commit 0b55f24

Browse files
committed
增加对超过5次采集的图片的处理
1 parent 98f6df3 commit 0b55f24

File tree

7 files changed

+37
-7
lines changed

7 files changed

+37
-7
lines changed

‎README.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
# HuabanBatchUpload
22
花瓣网图片批量上传工具
33

4+
## 功能说明
5+
这是一个用于向花瓣网批量上传图片的程序。
6+
+ 多线程并发,可以快速上传大量文件
7+
+ 自动传输错误重试
8+
+ 自动图片处理,避免因“图片已经采集超过5次”而上传失败
9+
410
## 使用方法 1
511
1. 下载代码到本地
612
2. 在dist目录中打开命令窗口
@@ -10,3 +16,4 @@
1016
1. 下载代码到本地
1117
2. 双击打开 /dist/main.exe,按提示操作即可。
1218

19+

‎dist/library.zip‎

946 Bytes
Binary file not shown.

‎dist/main.exe‎

0 Bytes
Binary file not shown.

‎lib/lib.py‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,11 @@ def post(url,data="",files="",cookies="",headers="",timeout=3,max_try=5):
9494
return requests.post(url, data=data, files=files, cookies=cookies, headers=headers)
9595

9696
def getHeaders():
97-
return {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36'}
97+
return {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36'}
98+
99+
def decode(str):
100+
try :
101+
uni = str.decode('utf-8')
102+
except:
103+
uni = str.decode('gbk')
104+
return uni

‎lib/recreateImg.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# coding=u8
2+
3+
import random
4+
5+
def recreate(imgPath):
6+
with open(imgPath, 'rb') as f1:
7+
bin = f1.read()
8+
bin = bytearray(bin)
9+
bin = bin[:-2] + str(random.randint(0,9)) + str(random.randint(0,9))
10+
with open(imgPath, 'wb') as f2:
11+
f2.write(bin)

‎lib/upload.py‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
# project: HuabanBatchUpload
44
# author: Pingze-github @ Github
55

6-
import os
7-
from lib import *
86
import time
7+
from lib import *
8+
import recreateImg
99

1010
# global variable
1111
cookies = {}
@@ -64,7 +64,9 @@ def upload(filepath, board_title, pinname=None, lock=None, tname=None):
6464
url = "http://huaban.com/pins/"
6565
res = post(url, data=data, cookies=cookies, headers=headers) # 添加图片文件到画板
6666
if('<i class="error">' in res.text):
67-
printWithLock(u'[{}] 上传失败: 图片 "{}" 已经被采集超过5次'.format(time.asctime()[11:19], filename), lock);
67+
printWithLock(u'[{}] 上传失败: 图片 "{}" 已经被采集超过5次,准备处理图片后重试...'.format(time.asctime()[11:19], filename), lock);
68+
recreateImg.recreate(filepath)
69+
upload(filepath, board_title, pinname, lock, tname)
6870
elif json_parse(res.text)!=None:
6971
data = json_parse(res.text)
7072
printWithLock (u'[{}] 上传成功: 图片 "{}" 到画板 "{}"'.format(time.asctime()[11:19], filename, board_title), lock);

‎main.py‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,17 @@
88
from lib.batch import batchUpload
99
from lib.auth import getCookie
1010
from lib.auth import testCookie
11-
from lib.lib import json_parse, json_stringify
11+
from lib.lib import json_parse, json_stringify, decode
1212
import lib.inputPre as inputPre
1313

14+
1415
# TODO 处理图片来避免因过多采集而上传失败
1516

1617
def main():
1718
account = '' # 账户名
1819
password = '' # 密码
19-
dirpath = '' # 要上传的图片所在目录
2020
boardName = '' # 要上传到的画板名
21+
dirpath = '' # 要上传的图片所在目录
2122
if len(sys.argv) < 2:
2223
paramMap = inputPre.get([
2324
{'key': 'account', 'name': u'账户名'},
@@ -27,17 +28,19 @@ def main():
2728
])
2829
account = paramMap['account']
2930
password = paramMap['password']
30-
dirpath = paramMap['dirpath']
3131
boardName = paramMap['boardname']
32+
dirpath = paramMap['dirpath']
3233
# 优先接受命令行参数
3334
if len(sys.argv) >= 2:
3435
account = sys.argv[1]
3536
if len(sys.argv) >= 3:
3637
password = sys.argv[2]
3738
if len(sys.argv) >= 4:
3839
boardName = sys.argv[3]
40+
boardName = decode(boardName)
3941
if len(sys.argv) >= 5:
4042
dirpath = sys.argv[4]
43+
dirpath = decode(dirpath)
4144
if not account or not password:
4245
print(u'未设定账密')
4346
return

0 commit comments

Comments
 (0)