Last active
December 16, 2015 00:58
-
-
Save gunsch/5351159 to your computer and use it in GitHub Desktop.
Script to post Goodreads reviews to Tumblr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import argparse | |
import datetime | |
import oauth2 as oauth | |
import sys | |
import urllib | |
import urllib2 | |
import xml.etree.ElementTree as ET | |
GOODREADS_LIST_URL = 'http://www.goodreads.com/review/list_rss/10688366.xml' | |
TUMBLR_POST_URL = 'http://api.tumblr.com/v2/blog/j.qxlp.net/post' | |
TUMBLR_OAUTH_KEY = 'todo' | |
TUMBLR_OAUTH_SECRET = 'todo' | |
# Get one-off oauth access tokens from script at https://gist.github.com/codingjester/2603387 | |
TUMBLR_OAUTH_TOKEN = 'todo' | |
TUMBLR_OAUTH_TOKEN_SECRET = 'todo' | |
parser = argparse.ArgumentParser( | |
description='Sends reviews from Goodreads to Tumblr') | |
parser.add_argument('-l', '--list_size', | |
help='Number of recent reviews to list', type=int, default=10) | |
parser.add_argument('-s', '--include_score', action='store_false' | |
help='Include "Score: N/5" line at the top of the review?', default=True) | |
parser.add_argument('--state', help='State: draft, published', default='draft') | |
# Parse flags | |
args = parser.parse_args() | |
# Include Score | |
include_score = args.include_score | |
list_size = args.list_size | |
post_state = args.state | |
# Fetch RSS list for goodreads posts. | |
goodreads_contents = urllib2.urlopen(GOODREADS_LIST_URL).read() | |
# Parse XML into structured tree. | |
tree = ET.fromstring(goodreads_contents) | |
# Find all recent reviews | |
reviews = tree.findall('channel/item') | |
# Get printable username for output | |
printable_name = tree.findtext('channel/item/user_name') | |
print 'Recent Reviews for %s' % printable_name | |
# Print list of options. | |
for i, review in zip(range(list_size), reviews[:list_size]): | |
book_name = review.findtext('title') | |
# Print 1 to N, not 0 to N-1 | |
print str.rjust(str(i+1), 4) + '. ' + book_name | |
# Prompt user for review number (1 greater than array index) | |
review_number = int(raw_input('Review # to post: ')) - 1 | |
# Sanity check | |
if review_number > len(reviews): | |
print 'Only have ', len(reviews), 'reviews!' | |
sys.exit(-1) | |
review = reviews[review_number] | |
# Pull out instance variables for easy manipulation | |
book_title = review.findtext('title') | |
book_author = review.findtext('author_name') | |
book_isbn = int(review.findtext('isbn')) | |
book_thumbnail = review.findtext('book_medium_image_url') | |
review_date = review.findtext('user_read_at') | |
review_score = int(review.findtext('user_rating')) | |
review_contents = review.findtext('user_review') | |
# Amazon URL | |
amazon_url = 'http://www.amazon.com/gp/search/ref=sr_adv_b/?field-isbn=%d' % book_isbn | |
# Put review together | |
review_html = ''.join([ | |
('<a href="%s">' + | |
'<img style="float:right; margin:4px; border:1px solid #000;" src="%s" />' + | |
'</a>') % (amazon_url, book_thumbnail), | |
('Score: %d/5<br/><br/>' % review_score) if include_score else '', | |
'%s' % review_contents, | |
'<br/><br/>', | |
'<a href="%s">%s on Amazon</a>' % (amazon_url, book_title) | |
]) | |
review_html = review_html.encode('ascii','ignore') | |
parsed_date = datetime.datetime.strptime(review_date[:-6], | |
'%a, %d %b %Y %H:%M:%S') | |
formatted_date = parsed_date.strftime('%Y-%m-%d %H:%M:%S GMT') | |
post_data = { | |
'type': 'text', | |
'state': 'published', | |
'date': formatted_date, | |
'format': 'html', | |
'title': '%s - %s' % (book_title, book_author), | |
'body': review_html | |
} | |
# Print data for previewing before posting | |
print post_data | |
yes_or_no = raw_input('Are you sure? [y/n] ') | |
if yes_or_no.strip().lower() != 'y': | |
sys.exit() | |
print 'Setting up an Oauth request...' | |
# Using prefetched access tokens, since the API doesn't really support a CLI | |
token = oauth.Token(key=TUMBLR_OAUTH_TOKEN, secret=TUMBLR_OAUTH_TOKEN_SECRET) | |
consumer = oauth.Consumer(key=TUMBLR_OAUTH_KEY, secret=TUMBLR_OAUTH_SECRET) | |
client = oauth.Client(consumer, token) | |
# Create our request. | |
response, content = client.request( | |
TUMBLR_POST_URL, | |
'POST', | |
urllib.urlencode({ | |
'type': 'text', | |
'state': post_state, | |
'date': review_date, | |
'format': 'html', | |
'title': '%s - %s' % (book_title, book_author), | |
'body': review_html | |
})) | |
print 'Response:' | |
print response | |
print content |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment