-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPinterestDownloader.py
146 lines (128 loc) · 4.89 KB
/
PinterestDownloader.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
import py3pin.Pinterest as Pinterest
import json, os, requests, time
mainpath = os.path.dirname(os.path.abspath(__file__))
username = ""
try:
username = input("please input user name")
except:
input("invalid user name")
exit(0)
# 检查url是否是200
def isValidUrl(url):
i = 0
while i < 3:
try:
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'}
if requests.options(url, timeout=5, headers=headers).status_code == 200:
return True
if requests.get(url, timeout=5, headers=headers).status_code == 200:
return True
raise ConnectionError
except requests.exceptions.RequestException:
i += 1
except:
return False
return False
# 从原始网站下载
def downloadFromUrl(url, DownloadPath, name='', type='jpg'):
type = type.strip('.')
# remove 因为下面做了try
#if not isValidUrl(url):
#return None
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'}
file_path = DownloadPath
zip_url = url
try:
os.mkdir(file_path)
except Exception:
pass
if not os.path.exists(DownloadPath):
os.makedirs(DownloadPath)
if name == '':
name = 'temp'
zip_path = os.path.join(file_path, name+'.'+type)
print(DownloadPath+name, "begin downloading")
#if not os.path.exists(zip_path):
try:
r = requests.get(zip_url, stream=True, headers=headers, timeout=15)
length = float(r.headers['content-length'])
f = open(zip_path, 'wb+')
count = 0
count_tmp = 0
time1 = time.time()
for chunk in r.iter_content(chunk_size=512):
if chunk:
f.write(chunk)
count += len(chunk)
if time.time() - time1 > 2:
p = count / length * 100
speed = (count - count_tmp) / 1024 / 1024 / 2
count_tmp = count
print('\r'+zip_path + ': ' + '{:.2f}'.format(p) + '%' + ' Speed: ' + '{:.2f}'.format(speed) + 'M/S',end = '', flush=True)
time1 = time.time()
f.close()
print(DownloadPath+name, "download finished")
except:
try:
if os.path.exists(zip_path):
os.remove(zip_path)
except:
pass
if not isValidUrl(url):
print(url + " invalid url")
print(DownloadPath+name, "download failed")
if os.path.exists(zip_path):
return zip_path
else:
return None
def readOldJson(boardname='', name='result'):
if not os.path.exists(mainpath + '/' + username + '/'+boardname+'/'):
os.makedirs(mainpath + '/' + username + '/'+boardname+'/')
if os.path.exists(mainpath + '/' + username + '/'+boardname+'/' + name+'.json'):
with open(mainpath + '/' + username + '/' +boardname+'/'+ name+'.json', 'r', encoding='utf-8') as result_file:
try:
json_result = json.load(result_file)
except:
json_result = {}
else:
json_result = {}
return json_result
def writeJson(data,boardname='', name='result'):
with open(mainpath + '/' + username + '/' +boardname+'/'+ name+'.json', 'w+', encoding='utf-8') as result_file:
json.dump(data, result_file, ensure_ascii=False)
def downloadBoard(Boardid):
pins = p.board_feed(board_id=Boardid)
d = readOldJson(pins[0]['board']["name"])
f = readOldJson(pins[0]['board']["name"], name='fail')
for pin in pins:
try:
if pin["image_signature"] in d.keys():
continue
else:
try:
images = pin["images"]
k = sorted(images.values(), key=lambda x: (-x['width']))
url = k[0]['url']
except:
url = ''
returncode = None
if url != '':
if not os.path.exists(mainpath + '/' + username + '/' + pin['board']["name"] + '/'):
os.makedirs(mainpath + '/' + username + '/' + pin['board']["name"] + '/')
returncode = downloadFromUrl(url, DownloadPath=mainpath + '/' + username + '/' + pin['board'][
"name"] + '/', name=pin["image_signature"])
if returncode != None:
d[pin["image_signature"]] = url
else:
f[pin["image_signature"]] = pin
except:
continue
writeJson(d, pins[0]['board']["name"])
writeJson(f, pins[0]['board']["name"], 'fail')
p = Pinterest.Pinterest()
boards = p.boards(username=username)
pins = []
for b in boards:
downloadBoard(b['id'])
input("download finished")