/
notify-mublog.py
executable file
·53 lines (37 loc) · 1.25 KB
/
notify-mublog.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/env python
import sys
import urllib, urllib2
import githook
CONFIG_URL = 'hooks.mublogurl'
CONFIG_USER = 'hooks.mubloguser'
CONFIG_PASS = 'hooks.mublogpass'
CONFIG_COMMIT_URL = 'hooks.commiturl'
SHORTEN_URL = r'http://is.gd/api.php?longurl=%s'
def make_tiny_url(url):
u = urllib2.urlopen(SHORTEN_URL % url)
return u.read()
def post(opener, url, status):
u = opener.open(url, urllib.urlencode({'status': status}))
u.read()
u.close()
if __name__ == '__main__':
url = githook.get_config(CONFIG_URL)
# make urlopen do basic auth
auth_hand = urllib2.HTTPBasicAuthHandler(
urllib2.HTTPPasswordMgrWithDefaultRealm())
auth_hand.add_password(user=githook.get_config(CONFIG_USER),
passwd=githook.get_config(CONFIG_PASS),
realm=None,
uri=url)
opener = urllib2.build_opener(auth_hand)
commit_url = githook.get_config(CONFIG_COMMIT_URL)
data = githook.parse_input(sys.stdin)
for d in data:
for rev in githook.get_revisions(d['old'], d['new']):
msg = 'commit: ' + rev['message']
link = make_tiny_url(commit_url % rev['id'])
avail = 140 - len(link) - 1
if len(msg) > avail:
msg = msg[:avail-3] + '...'
status = '%s %s' % (msg, link)
post(opener, url, status)