forked from dixudx/tumblr-crawler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtumblr-photo-video-ripper.py
213 lines (178 loc) · 6.86 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
# -*- coding: utf-8 -*-
import os
import sys
import requests
import xmltodict
import urllib
import socket
from six.moves import queue as Queue
from threading import Thread
import re
# 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
# Proxy
PROXY = {'http': "socks5://127.0.0.1:1080"}
class DownloadWorker(Thread):
def __init__(self, queue):
Thread.__init__(self)
self.queue = queue
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"]
pattern = re.compile(r'[\S\s]*src="(\S*)" ')
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):
socket.setdefaulttimeout(TIMEOUT)
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_folder = target_folder
if medium_type == "video":
file_folder = os.path.join(target_folder, "video")
if medium_type == "photo":
file_folder = os.path.join(target_folder, "photo")
if not os.path.isdir(file_folder):
os.mkdir(file_folder)
file_path = os.path.join(file_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:
r = requests.get(medium_url, stream=True, proxies=PROXY, timeout=10)
with open(file_path, 'wb') as f:
for chunk in r.iter_content(chunk_size=1024):
f.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):
self.sites = sites
self.queue = Queue.Queue()
self.scheduling()
def scheduling(self):
# create workers
for x in range(THREADS):
worker = DownloadWorker(self.queue)
# 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=PROXY)
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("Please create file sites.txt under this same directory")
print("in sites.txt, specify tumblr sites, separated by comma and no space")
print("save the file and retry")
print("Sample: site1,site2")
print("\n")
print("Or use command line options")
print("Sample: python tumblr-photo-video-ripper.py site1,site2")
print("\n")
print(u"未找到sites.txt文件,请创建")
print(u"请在文件中指定Tumblr站点名,并以逗号分割,不要有空格")
print(u"保存文件并重试")
print(u"例子: site1,site2")
print("\n")
print(u"或者使用命令行参数指定站点")
print(u"例子: python tumblr-photo-video-ripper.py site1,site2")
if __name__ == "__main__":
sites = None
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:
print usage()
sys.exit(1)
else:
sites = sys.argv[1].split(",")
if len(sites) == 0 or sites[0] == "":
usage()
sys.exit(1)
CrawlerScheduler(sites)