Last active

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Downloads a Twitch user's custom emoticons and integrates them into Pidgin as custom smileys. A restart of Pidgin is required after execution. A Windows binary is available here: http://www.mediafire.com/download/wxbzkbzci9v6ino/twitchy-pidgin.exe

View twitchy-pidgin.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 97 98 99 100 101 102 103 104 105 106 107
import hashlib
import json
import os
import time
try:
import xml.etree.cElementTree as ElementTree
except ImportError:
import xml.etree.ElementTree as ElementTree
 
import click
import requests
 
 
URL_TEMPLATE = 'http://api.twitch.tv/api/channels/%s/product'
 
# If system is not Windows, it's most likely another unix-like system and the
# .purple directory should be found in "${HOME}/.purple".
# (Source: https://developer.pidgin.im/wiki/ConfigurationFiles)
DOT_PURPLE_PATH = os.path.join(os.environ['APPDATA' if os.name == 'nt' else 'HOME'], '.purple')
SMILEYS_XML_PATH = os.path.join(DOT_PURPLE_PATH, 'smileys.xml')
CUSTOM_SMILEY_PATH = os.path.join(DOT_PURPLE_PATH, 'custom_smiley')
SMILEYS_XML_STUB = "<?xml version='1.0' encoding='UTF-8' ?><smileys version='1.0'><profile name='Default'><smiley_set></smiley_set></profile></smileys>"
 
 
@click.command()
@click.option('--username', prompt='Username')
@click.option('--active-only', default=True, show_default=True, is_flag=True, help='Decides whether to only download currently active smileys.')
@click.option('--shortcut-template', default='{shortcut}', show_default=True, help='The template used to create the shortcut string.')
@click.option('--create-backup', default=True, show_default=True, help='Decides whether to create a backup of the smileys.xml before modifying it.')
@click.option('--overwrite-existing', default=False, show_default=True, is_flag=True, help='Decides whether to overwrite existing custom smileys with the same shortcut.')
def main(username, active_only, shortcut_template, create_backup, overwrite_existing):
"""
Downloads a Twitch user's custom emoticons and integrates them into Pidgin
as custom smileys. A restart of Pidgin is required after execution.
"""
url = URL_TEMPLATE % username
data = json.loads(requests.get(url).text)
 
if 'error' in data:
print '%s, %s: %s' % (data['status'], data['error'], data['message'])
raw_input('\nPress enter to exit...')
return
 
if not 'emoticons' in data:
print 'The user has no custom emoticons.'
raw_input('\nPress enter to exit...')
return
 
if not os.path.exists(CUSTOM_SMILEY_PATH):
os.makedirs(CUSTOM_SMILEY_PATH)
 
# If the path doesn't exist Pidgin hasn't created it yet, since no custom
# smileys were defined previously
if not os.path.exists(SMILEYS_XML_PATH):
with open(SMILEYS_XML_PATH, 'w') as file_:
file_.write(SMILEYS_XML_STUB.encode('utf-8'))
 
element_tree = ElementTree.parse(SMILEYS_XML_PATH)
smiley_set = element_tree.find('.//smiley_set')
 
downloaded = 0
for emoticon in data['emoticons']:
if active_only and not emoticon['state'] == 'active':
print 'Ignoring disabled emoticon "%s".' % emoticon['regex']
continue
 
shortcut = shortcut_template.format(shortcut=emoticon['regex'])
existing_smiley_tag = smiley_set.find(".//*[@shortcut='%s']" % shortcut)
if existing_smiley_tag is not None and not overwrite_existing:
print 'Custom smiley "%s" already exists, ignoring.' % shortcut
continue
 
url = emoticon['url']
filename, extension = os.path.splitext(url.split('/')[-1])
 
content = requests.get(url).content
checksum = hashlib.sha1(content).hexdigest()
 
new_filename = checksum + extension
path = os.path.join(CUSTOM_SMILEY_PATH, new_filename)
with open(path, 'wb') as file_:
file_.write(content)
 
if existing_smiley_tag is not None:
print 'Overwriting existing custom smiley "%s".' % shortcut
existing_smiley_tag.set('checksum', checksum)
existing_smiley_tag.set('filename', new_filename)
else:
print 'Adding custom smiley "%s".' % shortcut
smiley_tag = ElementTree.SubElement(smiley_set, 'smiley', shortcut=shortcut, checksum=checksum, filename=new_filename)
smiley_set.append(smiley_tag)
 
downloaded += 1
 
if create_backup:
backup_path = ''.join((SMILEYS_XML_PATH, '.', str(time.time()).replace('.', '')))
os.rename(SMILEYS_XML_PATH, backup_path)
 
element_tree.write(SMILEYS_XML_PATH)
 
print 'Successfully integrated %d Twitch emoticons into Pidgin!' % downloaded
print 'Please restart Pidgin for the changes to take effect.'
raw_input('\nPress enter to exit...')
 
 
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.