public
Last active

  • Download Gist
curatorial-poetry.py
Python
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
#!/usr/bin/env python
 
import sys
import os
import os.path
import sqlite3 as lite
import urlparse
import oauth2 as oauth
import urllib
 
consumer_key = 'your key'
consumer_secret = 'your secret'
oauth_key = 'your other key'
oauth_secret = 'your other secret'
 
request_token_url = 'http://www.tumblr.com/oauth/request_token'
access_token_url = 'http://www.tumblr.com/oauth/access_token'
authorize_url = 'http://www.tumblr.com/oauth/authorize'
 
import logging
logging.basicConfig(level=logging.INFO)
 
if __name__ == '__main__':
 
whoami = os.path.abspath(sys.argv[0])
bindir = os.path.dirname(whoami)
rootdir = os.path.dirname(bindir)
 
datadir = os.path.join(rootdir, 'data')
 
datafile = os.path.join(datadir, 'objects.sqlite3')
logging.info("file %s" % datafile)
# grab top row where it has a description and has NOT been published
con = None
 
con = lite.connect(datafile)
 
with con:
cur = con.cursor()
cur.execute("SELECT * FROM objects WHERE description IS NOT NULL AND published IS NULL ORDER BY RANDOM() LIMIT 1")
row = cur.fetchall()
for f in row:
object_id = f[0]
description = f[1]
url = f[2]
published = f[3]
the_body = description + "\n" + "\n" + url
# post to tumblr blog
consumer = oauth.Consumer(consumer_key, consumer_secret)
client = oauth.Client(consumer)
 
resp, content = client.request(request_token_url, "GET")
if resp['status'] != '200':
raise Exception("Invalid response %s." % resp['status'])
 
request_token = dict(urlparse.parse_qsl(content))
 
token = oauth.Token(oauth_key, oauth_secret)
client = oauth.Client(consumer, token)
params = {
'type': 'text',
'title': object_id,
'body': the_body,
}
print client.request("http://api.tumblr.com/v2/blog/yourtumblrblog.tumblr.com/post", method="POST", body=urllib.urlencode(params))
with con:
con = con.cursor()
sql = "UPDATE objects SET published = 1 WHERE id=" + str(object_id)
cur.execute(sql)
 
data = cur.fetchone()
 
print data

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.