-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathmain.py
More file actions
106 lines (98 loc) · 3.36 KB
/
Copy pathmain.py
File metadata and controls
106 lines (98 loc) · 3.36 KB
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
# coding=utf-8
# main.py
# project: HuabanBatchUpload
# author: Pingze-github @ Github
import sys
import os
from lib.batch import batchUpload
from lib.auth import getCookie
from lib.auth import testCookie
from lib.lib import json_parse, json_stringify, decode
import lib.inputPre as inputPre
# TODO 处理图片来避免因过多采集而上传失败
def main():
account = '' # 账户名
password = '' # 密码
boardName = '' # 要上传到的画板名
dirpath = '' # 要上传的图片所在目录
if len(sys.argv) < 2:
paramMap = inputPre.get([
{
'key': 'account',
'name': u'账户名'
},
{
'key': 'password',
'name': u'账户密码'
},
{
'key': 'boardname',
'name': u'画板名'
},
{
'key': 'dirpath',
'name': u'图片文件目录路径'
},
])
account = paramMap['account']
password = paramMap['password']
boardName = paramMap['boardname']
dirpath = paramMap['dirpath']
# 优先接受命令行参数
if len(sys.argv) >= 2:
account = sys.argv[1]
if len(sys.argv) >= 3:
password = sys.argv[2]
if len(sys.argv) >= 4:
boardName = sys.argv[3]
boardName = decode(boardName)
if len(sys.argv) >= 5:
dirpath = sys.argv[4]
dirpath = decode(dirpath)
if not account or not password:
print(u'未设定账密')
return
if not dirpath:
print(u'未设定图片目录路径')
return
if not (os.path.exists(dirpath) and os.path.isdir(dirpath)):
print(u'设定图片目录路径' + dirpath + u'无效')
return
dirpath = os.path.normpath(dirpath)
if not boardName:
print(u'未设定画板名')
return
# boardName = dirpath[dirpath.rfind('\\') + 1:] # 默认取文件夹名
print(u'你的设置: 账号: {}, 密码: {}, 图片目录路径: {}, 画板名: {}'.format(
account, password, dirpath, boardName))
cookie = None
cookieJsonPath = './cookie.json'
if os.path.exists(cookieJsonPath) and os.path.isfile(cookieJsonPath):
with open(cookieJsonPath, 'r') as f:
cookieMap = json_parse(f.read())
if not cookieMap or not cookieMap.get(account):
print(u'未找到缓存Cookie')
else:
print(u'找到缓存Cookie,正在验证...')
cookie = cookieMap[account]
testResult = testCookie(cookie)
if (testResult):
print(u'验证缓存Cookie成功,用户名为 ' + testResult["username"])
else:
print(u'验证缓存Cookie失败')
if not cookie:
print(u'使用账密登录...')
cookie = getCookie(account, password)
if not cookie:
return
print(u'账密登录成功,获取到Cookie:')
with open(cookieJsonPath, 'w+') as file:
cookieMap = json_parse(file.read())
if not cookieMap:
cookieMap = {}
cookieMap[account] = cookie
file.write(json_stringify(cookieMap))
print(u'缓存Cookie成功')
batchUpload(cookie, dirpath, boardName)
if __name__ == '__main__':
main()