forked from thepaul/adium-hipchat-emoticons
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate.py
executable file
·96 lines (81 loc) · 2.92 KB
/
update.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/python
import sys
import os
import json
import urllib
from xml.sax.saxutils import unescape
from StringIO import StringIO
from collections import namedtuple
hipchat_emoticons_github = 'https://github.com/henrik/hipchat-emoticons'
hipchat_emoticons_dl = 'https://dujrsrsgsd3nh.cloudfront.net/img/emoticons'
plist_header = '''\
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>AdiumSetVersion</key>
<real>1.3</real>
<key>Emoticons</key>
<dict>
'''
plist_footer = '''\
</dict>
</dict>
</plist>''' # no eol- this is important
plist_item = '''\
<key>%(imgbase)s</key>
<dict>
<key>Equivalents</key>
<array>
%(shortcuts)s
</array>
<key>Name</key>
<string>%(name)s</string>
</dict>
'''
Emot = namedtuple('Emot', ('name', 'imgbase', 'imgpath', 'shortcuts'))
def fetch_emoticons_json(github_repo):
raw_github = github_repo.replace('//github.com', '//raw.github.com')
stream = urllib.urlopen(raw_github + '/master/emoticons.json')
contents = stream.read()
return contents
def merge_identical_emoticons(emoticons):
emotedict = {}
for emot in emoticons:
imgname = os.path.basename(emot['image'])
emotobj = emotedict.get(imgname)
shortcut = unescape(emot['shortcut'])
# special case, work around bug(?) in henrik code
if shortcut == ':':
shortcut = ':/'
if emotobj is None:
emotedict[imgname] = emotobj = Emot(shortcut, imgname, emot['image'], [])
emotobj.shortcuts.append(shortcut)
return sorted(emotedict.values())
def write_plist_key(f, emot):
shortcuts = '\n '.join(['<string>%s</string>' % (s,) for s in emot.shortcuts])
f.write(plist_item % {'imgbase': emot.imgbase,
'shortcuts': shortcuts,
'name': emot.name})
def write_plist_stream(f, emoticons):
f.write(plist_header)
for emot in emoticons:
write_plist_key(f, emot)
f.write(plist_footer)
def write_plist(plist_path, emoticons):
with open(plist_path, 'w') as f:
return write_plist_stream(f, emoticons)
def update_icon(imgpath, destfile):
urllib.urlretrieve(hipchat_emoticons_dl + '/' + imgpath, destfile)
def update_bundle(bundle_path, emoticons):
write_plist(os.path.join(bundle_path, 'Emoticons.plist'), emoticons)
for emot in emoticons:
update_icon(emot.imgpath, os.path.join(bundle_path, emot.imgbase))
def main(args):
emoticons_json = fetch_emoticons_json(hipchat_emoticons_github)
emoticons = json.loads(emoticons_json)
emoticons = merge_identical_emoticons(emoticons)
adium_bundle = os.path.join(os.path.dirname(__file__), 'Hipchat.AdiumEmoticonSet')
update_bundle(adium_bundle, emoticons)
if __name__ == "__main__":
main(sys.argv[1:])