-
Notifications
You must be signed in to change notification settings - Fork 74
/
Tumblrimage.py
85 lines (75 loc) · 1.89 KB
/
Tumblrimage.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
# -*- coding: utf-8 -*-
"""
Author: Sparrow
Purpose: downloading one entire blog from Tumblr once.
Created: 2017-1.1
"""
import re
import urllib.request
import os
import traceback
from urllib.parse import quote
import ExceptionOptions
def getHtml(url):
url = quote(url, safe='/:?=')
try:
page = urllib.request.urlopen(url)
html = page.read().decode('utf-8')
return html
except:
# traceback.print_exc()
print('The URL you requested could not be found in Module image')
return 'Html'
def getPostname(posturl):
reg = r'https*://.*?\/post\/(.*)'
postname = re.compile(reg)
postnamelist = re.findall(postname, posturl)
# print(postnamelist)
if postnamelist:
return postnamelist[0]
else:
postnamelist = ['page1']
return postnamelist[0]
def getImg(url):
html = getHtml(url)
reg = r'<meta property="og:image" content="(https*://68.media.tumblr.com/.*?\.(jpg|gif|png))" />'
imgre = re.compile(reg)
imglist_none = re.findall(imgre, html)
imglist = list(set(imglist_none))
if imglist:
PrePostname = getPostname(url)
txt = re.search('/', PrePostname)
if txt:
Postnames = PrePostname.split('/')
Postname =Postnames[0]
# print(PrePostname,Postnames)
else:
Postname = PrePostname
print(len(imglist))
print(imglist)
i = 0
path = 'Tumblrimgdownload/'
if not os.path.exists(path):
os.makedirs(path)
for imgurls in imglist:
Name = Postname + '_' + str(i)
imgurl = imgurls[0]
Postfix = imgurls[1]
target = path + '%s.%s' % (Name,Postfix)
i += 1
print("Downloading %s \n" % target)
try:
urllib.request.urlretrieve(imgurl, target)
except:
print('The image is lost.')
return True
else:
ExceptionOptions.ImgDownloadException(url)
# print('There is no image!')
return False
if __name__ == '__main__':
select = 'Y'
while (select == 'Y'):
URL = input('Input url: ')
getImg(URL)
select = input("Do you want to Continue? [Y/N]")