Skip to content

Instantly share code, notes, and snippets.

@fish2000
Created September 30, 2010 04:55
Show Gist options
  • Save fish2000/604046 to your computer and use it in GitHub Desktop.
Save fish2000/604046 to your computer and use it in GitHub Desktop.
upload images en masse to a tumblr account
#!/usr/bin/env python
# encoding: utf-8
"""
_shootemup.py
Created by fish on 2009-06-04.
Copyright (c) 2009 OST LLC. All rights reserved.
I wrote this in 10min so YMMV; but you really don't need an over-the-top
API binding for Tumblr cuz it's so simple, which I appreciate.
"""
import os, sys, time, urllib, urllib2, httplib, random
from poster.encode import multipart_encode
from poster.streaminghttp import register_openers
# if this next bit looks awful it's because it is; I swiped it from a real old project
class CallableFloatRange(object):
def __init__(self, *args, **kwargs):
self.upper = float(kwargs.get('upper', 10.00))
self.lower = float(kwargs.get('lower', 0.01))
def __call__(self):
return random.uniform(self.upper, self.lower)
def __float__(self):
return float(self())
def __repr__(self):
return self.__float__()
# replace this with something on yr system you want to u/l
thedir = "/Users/fish/Screenshots/_hatchling/_good"
endpt = "http://www.tumblr.com/api/write"
sleeper = CallableFloatRange(upper=25.0, lower=3.22)
# 75 is how many posts I got to before I started getting 404'd on the API calls,
# and then found a polite but immutable "you're cut off till tomorrow" message in the dashboard.
maxposts = 75
def getpngs(path):
return [os.path.join(path, im) for im in os.listdir(path) if im.endswith('.png')]
def yieldpostdata(img):
return {
"email": "you@yours.com",
"password": "YOUR-SUPER-SECRET-PASS-WORD",
"type": "photo",
"group": "yourtumblr.tumblr.com",
"generator": "OST/2. It works, bitches.",
"data": open(img, "rb"),
}
def main(argv):
## with this, poster registers itself with urllib2, I am told
register_openers()
posts = 0
print "Posting a bunch of images from %s to your Tumblr..." % thedir
for pn in getpngs(thedir):
if posts >= maxposts:
print "Over the %s-post limit there." % maxposts
break
print ">>> %s" % pn
d, h = multipart_encode(yieldpostdata(pn))
urlin = urllib2.Request(endpt, d, h)
try:
urlout = urllib2.urlopen(urlin).read()
except urllib2.HTTPError, httperr:
print "<<< %s" % httperr
else:
print "<<< %s" % urlout
time.sleep(sleeper)
posts += 1
print "Ok I'm done now. I'm bored, let's go."
if __name__ == '__main__':
main(sys.argv[1:])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment