Skip to content

Instantly share code, notes, and snippets.

@gkoehler
Created January 3, 2014 01:14
Show Gist options
  • Save gkoehler/8230718 to your computer and use it in GitHub Desktop.
Save gkoehler/8230718 to your computer and use it in GitHub Desktop.
Posts a Pelican article link & title to Twitter.
import os, re, sys, getopt, twitter, pelicanconf, publishconf
from urlparse import urljoin
# Posts a Pelican article to Twitter.
# usage: post_to_twitter.py -a articlename
# (articlename is the filename, minus the extension)
# The script will look for articlename.md inside /content, parse out Title & Slug,
# then tweet out "%title% %siteurl%/%slug%.html" under your account.
# Requirements:
# * Pelican.
# * At least one Pelican article, already published.
# * `pip install twitter`.
# Usage:
# Put this script in your Pelican directory, add the
# required keys to your pelicanconf.py, then
# `python post_to_twitter.py -a articlename`. Confirm the tweet text by hitting enter.
# Warnings:
# This is VERY specific to my setup... I use .md and keep all my articles
# in /contents, and I use the same slug as my article name. Also, my site
# is setup as sitename.com/slug.html.
# Maybe this would be a good Penguin plugin, if Penguin gave us a way to know about new articles.
def get_meta_value(tagname, str):
results = re.search(tagname + ':\s*(.*)\n', str)
if results:
return results.group(1)
else:
print '{0} not found for article'.format(tagname)
sys.exit()
def main(argv):
articlename = ''
# verify arguments are specified
try:
opts, args = getopt.getopt(argv,"a:",["articlename="])
except getopt.GetoptError:
print 'post_to_twitter.py -a <articlename>'
sys.exit(2)
for opt, arg in opts:
if opt in ("-a", "--articlename"):
articlename = arg
if len(articlename) == 0:
print 'post_to_twitter.py -a <articlename>'
sys.exit()
# verify settings exist
if not (hasattr(pelicanconf, 'TWITTER_CONSUMER_KEY')
and hasattr(pelicanconf, 'TWITTER_CONSUMER_SECRET')
and hasattr(pelicanconf, 'TWITTER_ACCESS_TOKEN_KEY')
and hasattr(pelicanconf, 'TWITTER_ACCESS_TOKEN_SECRET')):
print 'Add these strings to pelicanconf.py:'
print 'TWITTER_CONSUMER_KEY, '
print 'TWITTER_CONSUMER_SECRET, '
print 'TWITTER_ACCESS_TOKEN_KEY, '
print 'TWITTER_ACCESS_TOKEN_SECRET'
sys.exit()
# verify specified file exists
articlepath = 'content/{0}.md'.format(articlename)
if not os.path.exists(articlepath):
print 'Article doesn\'t exist at {0}'.format(articlepath)
sys.exit()
# get the content, and parse out the title, url
with open(articlepath) as f:
articletext = f.read()
title = get_meta_value('Title', articletext)
slug = get_meta_value('Slug', articletext)
tweet_text = '{0} {1}.html'.format(title, urljoin(publishconf.SITEURL, slug))
# connect to the API and verify credentials
api = twitter.Api(consumer_key = pelicanconf.TWITTER_CONSUMER_KEY,
consumer_secret = pelicanconf.TWITTER_CONSUMER_SECRET,
access_token_key= pelicanconf.TWITTER_ACCESS_TOKEN_KEY,
access_token_secret = pelicanconf.TWITTER_ACCESS_TOKEN_SECRET)
api.VerifyCredentials()
print('Here is the tweet we are going to post:\n{0}'.format(tweet_text))
userinput = raw_input('OK to send it? ([y]/n)')
if userinput == '' or userinput == 'y':
print('OK, sending tweet')
api.PostUpdate(tweet_text)
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