My older twitter mercurial hook relied on twitter’s basic authentication which, as of August 31, 2010, is no longer supported. This script is an updated version that now works with twitter’s new OAuth system.
For this hook, you will need to create consumer keys for the script and access keys for the twitter account from which you wish to tweet. If you don’t know how to create and obtain these keys, don’t worry, Jeff Miller has written a nice article that covers how to generate them. After following his tutorial, you should have a python script that successfully posts to your twitter account from the command line and contains a CONSUMER_KEY, CONSUMER_SECRET, ACCESS_KEY, and ACCESS_SECRET. Simply use those keys for the corresponding configuration variables in the twitter section of your hgrc file. If you paste the block of code containing the consumer and access key strings into your config, the key values should not be surrounded by single or double quotes like they are in python code. Remember to remove the quotes or they will be sent as part of the keys and you will receive an “Invalid Token” exception.
This updated version of the hook requires tweepy.
More information on installing and configuring hooks can be found in mercurial’s documentation.
File: hgtwitter.py
''' [extensions] hgext.hgtwitter= [hooks] incoming.notify = python:hgext.hgtwitter.hook [twitter] CONSUMER_KEY = your Consumer Key CONSUMER_SECRET = your Consumer Secret ACCESS_KEY = your Access Key ACCESS_SECRET = your Access Secret ''' from mercurial import cmdutil, templater import tweepy tweet_template = ''' |{root|basename}:{rev}| {desc|strip} '''.strip() def hook(ui, repo, hooktype, node=None, source=None, **kwargs): ctx = repo[node] CONSUMER_KEY = ui.config('twitter', 'CONSUMER_KEY') CONSUMER_SECRET = ui.config('twitter', 'CONSUMER_SECRET') ACCESS_KEY = ui.config('twitter', 'ACCESS_KEY') ACCESS_SECRET = ui.config('twitter', 'ACCESS_SECRET') t = cmdutil.changeset_templater(ui=ui, repo=repo, patch=False, diffopts=None, mapfile=None, buffered=False) t.use_template(templater.parsestring(tweet_template, quoted=False)) ui.pushbuffer() t.show(ctx, changes=ctx.changeset(), root=repo.root) tweet = ui.popbuffer() if len(tweet) > 140: tweet = tweet[:139] + u"\u2026" auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET) auth.set_access_token(ACCESS_KEY, ACCESS_SECRET) api = tweepy.API(auth) api.update_status(tweet) |
If you have problems with mercurial 1.1 and lower, try removing the diffopts argument from changeset_templater() so it looks like:
t = cmdutil.changeset_templater(ui=ui, repo=repo, patch=False, mapfile=None, buffered=False) |
One Trackback/Pingback
black friday iphone sales 2014
Tony's Blog › simple twitter oauth mercurial hook
Post a Comment