forked from dixudx/tumblr-crawler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtumblr-photo-video-ripper.py
232 lines (196 loc) · 7.96 KB
/
tumblr-photo-video-ripper.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# -*- coding: utf-8 -*-
import os
import sys
import requests
import xmltodict
from six.moves import queue as Queue
from threading import Thread
import re
import json
# Setting timeout
TIMEOUT = 10
# Retry times
RETRY = 5
# Medium Index Number that Starts from
START = 0
# Numbers of photos/videos per page
MEDIA_NUM = 50
# Numbers of downloading threads concurrently
THREADS = 10
class DownloadWorker(Thread):
def __init__(self, queue, proxies=None):
Thread.__init__(self)
self.queue = queue
self.proxies = proxies
def run(self):
while True:
medium_type, post, target_folder = self.queue.get()
self.download(medium_type, post, target_folder)
self.queue.task_done()
def download(self, medium_type, post, target_folder):
try:
medium_url = self._handle_medium_url(medium_type, post)
if medium_url is not None:
self._download(medium_type, medium_url, target_folder)
except TypeError:
pass
def _handle_medium_url(self, medium_type, post):
try:
if medium_type == "photo":
return post["photo-url"][0]["#text"]
if medium_type == "video":
video_player = post["video-player"][1]["#text"]
hd_pattern = re.compile(r'.*"hdUrl":("([^\s,]*)"|false),')
hd_match = hd_pattern.match(video_player)
try:
if hd_match is not None and hd_match.group(1) != 'false':
return hd_match.group(2).replace('\\', '')
except IndexError:
pass
pattern = re.compile(r'.*src="(\S*)" ', re.DOTALL)
match = pattern.match(video_player)
if match is not None:
try:
return match.group(1)
except IndexError:
return None
except:
raise TypeError("Unable to find the right url for downloading. "
"Please open a new issue on "
"https://github.com/dixudx/tumblr-crawler/"
"issues/new attached with below information:\n\n"
"%s" % post)
def _download(self, medium_type, medium_url, target_folder):
medium_name = medium_url.split("/")[-1].split("?")[0]
if medium_type == "video":
if not medium_name.startswith("tumblr"):
medium_name = "_".join([medium_url.split("/")[-2],
medium_name])
medium_name += ".mp4"
file_path = os.path.join(target_folder, medium_name)
if not os.path.isfile(file_path):
print("Downloading %s from %s.\n" % (medium_name,
medium_url))
retry_times = 0
while retry_times < RETRY:
try:
resp = requests.get(medium_url,
stream=True,
proxies=self.proxies,
timeout=TIMEOUT)
with open(file_path, 'wb') as fh:
for chunk in resp.iter_content(chunk_size=1024):
fh.write(chunk)
break
except:
# try again
pass
retry_times += 1
else:
try:
os.remove(file_path)
except OSError:
pass
print("Failed to retrieve %s from %s.\n" % (medium_type,
medium_url))
class CrawlerScheduler(object):
def __init__(self, sites, proxies=None):
self.sites = sites
self.proxies = proxies
self.queue = Queue.Queue()
self.scheduling()
def scheduling(self):
# create workers
for x in range(THREADS):
worker = DownloadWorker(self.queue,
proxies=self.proxies)
# Setting daemon to True will let the main thread exit
# even though the workers are blocking
worker.daemon = True
worker.start()
for site in self.sites:
self.download_media(site)
def download_media(self, site):
self.download_photos(site)
self.download_videos(site)
def download_videos(self, site):
self._download_media(site, "video", START)
# wait for the queue to finish processing all the tasks from one
# single site
self.queue.join()
print("Finish Downloading All the videos from %s" % site)
def download_photos(self, site):
self._download_media(site, "photo", START)
# wait for the queue to finish processing all the tasks from one
# single site
self.queue.join()
print("Finish Downloading All the photos from %s" % site)
def _download_media(self, site, medium_type, start):
current_folder = os.getcwd()
target_folder = os.path.join(current_folder, site)
if not os.path.isdir(target_folder):
os.mkdir(target_folder)
base_url = "http://{0}.tumblr.com/api/read?type={1}&num={2}&start={3}"
start = START
while True:
media_url = base_url.format(site, medium_type, MEDIA_NUM, start)
response = requests.get(media_url,
proxies=self.proxies)
data = xmltodict.parse(response.content)
try:
posts = data["tumblr"]["posts"]["post"]
for post in posts:
# select the largest resolution
# usually in the first element
self.queue.put((medium_type, post, target_folder))
start += MEDIA_NUM
except KeyError:
break
def usage():
print("1. Please create file sites.txt under this same directory.\n"
"2. In sites.txt, you can specify tumblr sites separated only by "
"comma without any blank spaces.\n"
"3. Save the file and retry.\n\n"
"Sample File Content:\nsite1,site2\n\n"
"Or use command line options:\n\n"
"Sample:\npython tumblr-photo-video-ripper.py site1,site2\n\n\n")
print(u"未找到sites.txt文件,请创建.\n"
u"请在文件中指定Tumblr站点名,并以逗号分割,不要有空格.\n"
u"保存文件并重试.\n\n"
u"例子: site1,site2\n\n"
u"或者直接使用命令行参数指定站点\n"
u"例子: python tumblr-photo-video-ripper.py site1,site2")
def illegal_json():
print("Illegal JSON format in file 'proxies.json'.\n"
"Please refer to 'proxies_sample1.json' and 'proxies_sample2.json'.\n"
"And go to http://jsonlint.com/ for validation.\n\n\n")
print(u"文件proxies.json格式非法.\n"
u"请参照示例文件'proxies_sample1.json'和'proxies_sample2.json'.\n"
u"然后去 http://jsonlint.com/ 进行验证.")
if __name__ == "__main__":
sites = None
proxies = None
if os.path.exists("./proxies.json"):
with open("./proxies.json", "r") as fj:
try:
proxies = json.load(fj)
if proxies is not None and len(proxies) > 0:
print("You are using proxies.\n%s" % proxies)
except:
illegal_json()
sys.exit(1)
if len(sys.argv) < 2:
# check the sites file
filename = "sites.txt"
if os.path.exists(filename):
with open(filename, "r") as f:
sites = f.read().rstrip().lstrip().split(",")
else:
usage()
sys.exit(1)
else:
sites = sys.argv[1].split(",")
if len(sites) == 0 or sites[0] == "":
usage()
sys.exit(1)
CrawlerScheduler(sites, proxies=proxies)