Skip to content

Instantly share code, notes, and snippets.

@wcaleb
Last active August 29, 2015 14:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wcaleb/60d52759f6d0c5bc270d to your computer and use it in GitHub Desktop.
Save wcaleb/60d52759f6d0c5bc270d to your computer and use it in GitHub Desktop.
Post a tweet when my research wiki is updated
#!/usr/bin/python
# I run a wiki on Gitit at http://wiki.wcaleb.rice.edu. It's usually
# updated by pushing to the wiki's remote git repo from my local machine.
# I have a `post-receive` hook in that repo that runs this script, like so:
#
# unset GIT_DIR
# python /path/to/wikitweet.py
#
# Now, whenever I push to the repo, the script looks to see which files have
# been changed and tweets about the update to https://twitter.com/wcaleb
import os
import twitter # https://github.com/bear/python-twitter
import git # http://gitpython.readthedocs.org/en/stable/index.html
from OAuthSettings import wcaleb
api_key = wcaleb['api_key']
api_secret = wcaleb['api_secret']
access_token = wcaleb['access_token']
access_token_secret = wcaleb['access_token_secret']
base_url = 'http://wiki.wcaleb.rice.edu/'
repo = git.Repo('/home/wcm1/wiki/wikidata')
most_recent_diff = repo.head.commit.diff('HEAD~1')
changed_files = []
for x in most_recent_diff:
if x.a_blob.path not in changed_files:
changed_files.append(x.a_blob.path)
if x.b_blob is not None and x.b_blob.path not in changed_files:
changed_files.append(x.b_blob.path)
# Filter out changed discussion pages
def isNotDiscuss(file):
return file[0] != '@'
filter_files = [os.path.splitext(n)[0] for n in filter(isNotDiscuss, changed_files)]
if len(filter_files) == 1:
pageUrl = urllib.pathname2url(filter_files[0])
url = base_url + '_diff/' + pageUrl + '?to=' + repo.head.commit.hexsha
else:
url = base_url + '_activity'
file_names = ', '.join(filter_files)
if len(file_names) < 80:
tweet = 'Updated notes on ' + file_names + ' in my research wiki. ' + url
else:
tweet = 'Updated notes in my research wiki. ' + url
try:
api = twitter.Api(consumer_key = api_key, consumer_secret = api_secret, access_token_key = access_token, access_token_secret = access_token_secret)
status = api.PostUpdate(tweet)
except twitter.TwitterError, error:
print(error.message)
exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment