-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommit-msg.while-listening
executable file
·47 lines (36 loc) · 1.4 KB
/
commit-msg.while-listening
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
#!/usr/bin/env python
''' This file append what you are listening to, to your git commit messages.
First thing to do is to rename it to commit-msg and put it in .git/hooks in your repo
Then edit your lastfm_key and lastfm_user strings using info here, http://www.last.fm/api/accounts
Now all your commits in that repo will have the new message appended to them.
'''
import sys
import json
import urllib
from unidecode import unidecode
# Enter your Last.fm info here
lastfm_key = 'YOUR-LASTFM-KEY'
lastfm_user = 'YOUR-LASTFM-USERNAME'
def listening_msg():
params = urllib.urlencode({
'method': 'user.getrecenttracks',
'api_key': lastfm_key,
'format': 'json',
'user': lastfm_user
})
f = urllib.urlopen("http://ws.audioscrobbler.com/2.0/?%s" % params)
data = json.loads(f.read())
#print data
msg = ' [While not listening to anything]'
#print data['recenttracks']['track'][0]
recent_track = data['recenttracks']['track'][0]
if '@attr' in recent_track and 'nowplaying' in recent_track['@attr'] and recent_track['@attr']['nowplaying'] == 'true':
msg = ' [While listening to: %s - %s]' % (recent_track['artist']['#text'], recent_track['name'])
return unidecode(msg)
def append_msg(msg=' Toz'):
filename = sys.argv[1]
fd = open(filename,'a')
fd.write(msg)
fd.close()
msg = listening_msg()
append_msg(msg)