Skip to content

Instantly share code, notes, and snippets.

@tanelpuhu
Created June 27, 2011 18:10
Show Gist options
  • Save tanelpuhu/1049405 to your computer and use it in GitHub Desktop.
Save tanelpuhu/1049405 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
'Downloads tumblr images from dashboard'
import urllib
import os
from time import sleep
import simplejson as json
from threading import Thread
from Queue import Queue
MY_QUEUE = Queue()
PHOTOS_FOLDER = os.path.join(os.path.dirname(__file__), 'photos')
BASEURL = 'https://www.tumblr.com/api/dashboard/json'
PARAMS = {
'type' : 'photo',
'num' : 50,
'debug': 1,
'email': '[email protected]',
'password': '********************'
}
def work(worker_name, item):
'downloader'
filename = os.path.basename(item)
savename = os.path.join(PHOTOS_FOLDER, filename)
exists = os.path.exists(savename)
print '\n%(worker_name)s : %(filename)s %(exists)s' % {
'worker_name' : worker_name.rjust(10),
'filename' : filename,
'exists' : ['',' (exists)'][exists]
},
if not exists:
try:
fhan = urllib.urlopen(item)
phan = open(savename, 'wb')
phan.write(fhan.read())
fhan.close()
phan.close()
except IOError:
print 'Could not download: %s' % filename
else:
pass # exists
def worker(worker_name):
'thread'
while True:
item = MY_QUEUE.get()
work(worker_name, item)
MY_QUEUE.task_done()
if not os.path.exists(PHOTOS_FOLDER):
os.mkdir(PHOTOS_FOLDER)
for i in ['essa', 'tessa', 'kossa']:
t = Thread(target = worker, args = (i,))
t.daemon = True
t.start()
for start in range(0, 251, 50):
PARAMS.update({'start' : start})
TUMBLR_API_URL = '%s?%s' % (BASEURL, urllib.urlencode(PARAMS))
handler = urllib.urlopen(TUMBLR_API_URL)
data = json.load(handler)
handler.close()
if not data:
data = {}
for post in data.get('posts', []):
photos = []
for key in post.keys():
if key[:10] == 'photo-url-':
photos.append(int(key[10:]))
if len(photos):
photos.sort()
largephoto = 'photo-url-%s' % photos[-1]
MY_QUEUE.put(post[largephoto])
sleep(15)
MY_QUEUE.join()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment