#!/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()