-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtumblr.py
71 lines (62 loc) · 2.38 KB
/
tumblr.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
#-*- coding:utf-8 -*-
import urllib
import sys
from xml.dom import minidom
import Image
from glob import glob
class Tumblr(object):
def __init__(self, site="http://bonjourmadame.fr/api/read", folder="/Users/rafael/Dropbox/Bonjour/"):
self.site = site
self.folder = folder
self.links = {}
self.current_page = '?type=photo'
import os
os.chdir(folder)
self._total_posts()
def _open(self):
raw_xml = urllib.urlretrieve(self.site+self.current_page)
xml = minidom.parse(raw_xml[0])
self.data = xml.getElementsByTagName('posts')[0]
def _total_posts(self):
self._open()
self.total_posts = int(self.data.getAttribute("total"))
self.original_posts = int(self.data.getAttribute("total"))
self._new_page()
def _process_links(self):
self._open()
for post in self.data.childNodes:
for tag in post.childNodes:
if tag.tagName == u'photo-url' and \
int(tag.getAttribute('max-width')) >= 500 and \
post.getAttribute('id') not in self.links.keys():
self.links[post.getAttribute('id')] = tag.firstChild.wholeText
break
self._new_page()
def _new_page(self):
self.current_page = '?start=%d&type=photo' % (self.total_posts-20)
if self.total_posts > 0:
self.total_posts -= 20
else:
self.total_posts = 0
def process_all_links(self):
print 'This might take a while'
while len(set(self.links.keys())) < self.original_posts:
self._process_links()
sys.stdout.flush()
print 'Current status: %d of %d' % (len(set(self.links.keys())), self.original_posts)
def save_all_images(self):
counter = 1
for name,link in self.links.items():
if len(glob(str(name)+'.*')):
print "Image with id:%s already saved." % (str(name))
else:
data = urllib.urlretrieve(link)
img = Image.open(data[0])
print "Saving: %d of %d images." % (counter, self.original_posts)
img.save(self.folder+name+"."+img.format.lower())
counter += 1
print counter
if __name__ == '__main__':
t = Tumblr()
t.process_all_links()
t.save_all_images()