-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelper.py
213 lines (191 loc) · 5.33 KB
/
helper.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import requests
import time
import platform
import datetime
import subprocess
import inspect
from pyquery import PyQuery
from requests.adapters import HTTPAdapter
headers = {'user-agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.85 Safari/537.36'}
def now():
return time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
def today():
return time.strftime('%Y-%m-%d', time.localtime(time.time()))
def mkDir(path):
if not os.path.exists(path):
os.makedirs(path)
return path
def writeFile(content, path, mode='w'):
try:
f = open(path, mode)
f.write(content)
f.close()
return True
except Exception as e:
print(e)
return False
def readFile(path):
if os.path.exists(path):
f = open(path)
txt = f.read()
f.close()
return txt
def log(content, platform):
content = '[%s] %s\n' % (now(), content)
if platform:
mkDir(os.path.join('.', 'logs'))
log_path = os.path.join('.', 'logs', '%s-%s.log' % (today(), platform))
writeFile(content, log_path, 'a' if os.path.exists(log_path) else 'w')
print('[%s] => %s' % (platform or 'None', content))
# 开始下载图片
def downloadImg(url, imgPath):
if url != None:
if os.path.exists(imgPath):
print('%s is exists, jump it!' % imgPath)
return 2
else:
parent = os.sep.join(imgPath.split(os.sep)[: -1])
mkDir(parent)
print('[%s] download image: %s' % (now(), url))
try:
global headers
r = requests.get(url, stream=True, headers=headers)
except Exception as e:
print(e)
return -2
with open(imgPath, 'wb') as f:
for chunk in r.iter_content(chunk_size = 1024):
if chunk:
f.write(chunk)
f.flush()
return 1
else:
print('download img error => url is None')
return -1
# def get_ak_bmsc_cookie(url):
# s = requests.Session()
# s.mount('http://', HTTPAdapter(max_retries=10))
# s.mount('https://', HTTPAdapter(max_retries=10))
# print('get url => ' + url)
# s.get(url, headers={'User-Agent': 'Mozilla/5.0'}, timeout=10)
# return s.cookies.get_dict().get('ak_bmsc')
def get(url, cookies={}, myHeaders=None, sleep=0, returnText=False, withCookie=False, platform=None):
s = requests.Session()
s.mount('http://', HTTPAdapter(max_retries=10))
s.mount('https://', HTTPAdapter(max_retries=10))
if sleep > 0:
time.sleep(sleep)
log('get url => ' + url, platform)
global headers
response = None
try:
response = s.get(url, headers=myHeaders or headers, cookies=cookies, timeout=30)
except Exception as err:
# log('get url error!!! repeat again!!!', platform)
log(err, platform)
# return get(url, cookies, myHeaders, sleep or 3, returnText)
return None
if response.status_code == 200:
pq = None
try:
pq = PyQuery(response.text)
except:
pq = None
if withCookie:
return response.text if returnText else PyQuery(response.text), s.cookies.get_dict()
return response.text if returnText else pq
else:
log('response.status_code: %d' % response.status_code, platform)
return None
def post(url, data={'imgContinue': 'Continue to image ... '}, myHeaders=None, cookies={}, sleep=0, returnText=False, platform=None, json=None, timeout=30):
'''post'''
s = requests.Session()
s.mount('http://', HTTPAdapter(max_retries=10))
s.mount('https://', HTTPAdapter(max_retries=10))
if sleep > 0:
time.sleep(sleep)
log('post url => ' + url, platform)
global headers
response = None
try:
if data:
response = s.post(url, headers=myHeaders or headers, cookies=cookies, data=data, timeout=timeout)
else:
response = s.post(url, headers=myHeaders or headers, cookies=cookies, json=json, timeout=timeout)
# response = s.post('http://httpbin.org/post', headers=myHeaders or headers, cookies=cookies, data=data)
except Exception as e:
print(e)
response = None
if response:
# log('post status => ', response.status_code)
if response.status_code == 200:
return response.text if returnText else PyQuery(response.text)
log('post url not OK => ' + url, platform)
return None
def lookUp(obj):
print(inspect.getmembers(obj, inspect.ismethod))
def getMonth(english):
'''从英文转成阿拉伯数字'''
if english == 'Jan':
return 1
if english == 'Feb':
return 2
if english == 'Mar':
return 3
if english == 'Apr':
return 4
if english == 'May':
return 5
if english == 'Jun':
return 6
if english == 'Jul':
return 7
if english == 'Aug':
return 8
if english == 'Sep':
return 9
if english == 'Oct':
return 10
if english == 'Nov':
return 11
if english == 'Dec':
return 12
return english
def runCmd(cmd, logfile='./aria2c.log', timeout=1200):
process = None
if logfile:
process = subprocess.Popen('%s >>%s 2>&1' % (cmd, logfile), shell=True)
else:
process = subprocess.Popen(cmd, shell=True)
# print(u'run cmd => %s' % cmd)
process.wait()
start = datetime.datetime.now()
while process.poll() is None:
time.sleep(0.1)
now = datetime.datetime.now()
if (now - start).seconds > timeout:
try:
process.terminate()
except Exception as e:
return None
return None
out = process.communicate()[0]
if process.stdin:
process.stdin.close()
if process.stdout:
process.stdout.close()
if process.stderr:
process.stderr.close()
try:
process.kill()
except OSError:
pass
return out
def delRepeat(list):
for x in list:
while list.count(x)>1:
del list[list.index(x)]
return list