Trac is being migrated to new services! Issues can be found in our new YouTrack instance and WIKI pages can be found on our website.

Changes between Initial Version and Version 1 of ImportCustomSmileysFromEmesene


Ignore:
Timestamp:
Jan 19, 2009, 11:57:17 PM (15 years ago)
Author:
taralluccio
Comment:

script to import custom smileys from emesene

Legend:

Unmodified
Added
Removed
Modified
  • ImportCustomSmileysFromEmesene

    v1 v1  
     1= Import Custom Smileys from Emesene =
     2
     3[http://emesene.org/ Emesene] is a client for the MSN protocol. It supports custom smileys, which can be imported into Pidgin using the following script (requires Python 2.5 or later).
     4
     5
     6{{{
     7#!/usr/bin/python
     8#
     9# Import custom smileys into Pidgin from Emesene 1.0.
     10# Requires Python 2.5 or later.
     11#
     12# Copyright (C) 2009, Danilo Piazzalunga <danilopiazza@gmail.com>
     13# This file is distributed under the same license as the Pidgin package.
     14#
     15# Usage:
     16# pidgin-import-emesene-smileys.py EMESENE_EMOTICON_MAP [PURPLE_USER_DIR]
     17#
     18# EMESENE_EMOTICON_MAP: the custom_emoticons/map file in your emesene
     19# account directory, to be used as the input file
     20# For example, $HOME/.config/emesene1.0/*/custom_emoticons/map.
     21#
     22# PURPLE_USER_DIR: the libpurple user directory, to be used as output
     23# directory. Defaults to $HOME/.purple.
     24#
     25
     26from __future__ import with_statement
     27
     28import hashlib
     29import os.path
     30import shutil
     31import sys
     32import xml.dom.minidom
     33
     34def usage():
     35    return ("usage: %s EMESENE_EMOTICON_MAP [PURPLE_USER_DIR]" % sys.argv[0])
     36
     37def purple_user_dir():
     38    for dir in [ "$PURPLEHOME", "%APPDATA%", "$HOME", "~" ]:
     39        purple_dir = os.path.join(
     40            os.path.expandvars(os.path.expanduser(dir)),
     41            ".purple")
     42        if os.path.isdir(purple_dir):
     43            return purple_dir
     44
     45def open_cmdline_arg(filename, input=True):
     46    if filename == "-":
     47        if input: return sys.stdin
     48        else: return sys.stdout
     49    else:
     50        if input: return open(filename, "U")
     51        else: return open(filename, "w")
     52
     53def parse_emesene_map(map):
     54    smileys = {}
     55
     56    while True:
     57        shortcut = map.readline().rstrip("\n")
     58        if not shortcut: break
     59
     60        picture_name = map.readline().rstrip("\n")
     61        smileys[shortcut] = picture_name
     62
     63    return smileys
     64
     65def sha1sum(filename):
     66    with open(filename, "rb") as file:
     67        data = file.read()
     68
     69    sha1sum = hashlib.sha1()
     70    sha1sum.update(data)
     71    return sha1sum.hexdigest()
     72
     73def import_smileys(smileys, purple_dir):
     74    purple_custom_smiley_dir = os.path.join(purple_dir, "custom_smiley")
     75    purple_smileys_xml = os.path.join(purple_dir, "smileys.xml")
     76
     77    if not os.path.isdir(purple_custom_smiley_dir):
     78        os.mkdir(purple_custom_smiley_dir)
     79
     80    smileys_purple = []
     81
     82    for shortcut, picture_name in smileys.iteritems():
     83        checksum = sha1sum(picture_name)
     84        ext = os.path.splitext(picture_name)[1]
     85        filename = "".join([checksum, ext])
     86
     87        shutil.copyfile(picture_name,
     88                        os.path.join(purple_custom_smiley_dir, filename))
     89
     90        smileys_purple.append({"shortcut": shortcut,
     91                               "checksum": checksum,
     92                               "filename": filename })
     93
     94    write_purple_smileys_xml(smileys_purple, purple_smileys_xml)
     95
     96def write_purple_smileys_xml(smileys_purple, purple_smileys_xml):
     97    dom = xml.dom.minidom.getDOMImplementation()
     98    doc = dom.createDocument(None, "smileys", None)
     99
     100    try:
     101        top_element = doc.documentElement
     102        top_element.setAttribute("version", "1.0")
     103
     104        profile_element = doc.createElement("profile")
     105        profile_element.setAttribute("name", "Default")
     106        top_element.appendChild(profile_element)
     107
     108        smiley_set_element = doc.createElement("smiley_set")
     109        profile_element.appendChild(smiley_set_element)
     110
     111        for s in smileys_purple:
     112            smiley_element = doc.createElement("smiley")
     113            smiley_element.setAttribute("shortcut", s["shortcut"])
     114            smiley_element.setAttribute("checksum", s["checksum"])
     115            smiley_element.setAttribute("filename", s["filename"])
     116            smiley_set_element.appendChild(smiley_element)
     117
     118        with open(purple_smileys_xml, "w") as writer:
     119            doc.writexml(writer, addindent="\t", newl="\n")
     120    finally:
     121        doc.unlink()
     122
     123if __name__ == "__main__":
     124    if len(sys.argv) < 2 or len(sys.argv) > 3:
     125        sys.exit(usage())
     126
     127    input_filename = sys.argv[1]
     128    purple_dir = sys.argv[2] if len(sys.argv) > 2 else purple_user_dir()
     129
     130    with open_cmdline_arg(input_filename) as map:
     131        smileys = parse_emesene_map(map)
     132
     133    import_smileys(smileys, purple_dir)
     134
     135}}}
All information, including names and email addresses, entered onto this website or sent to mailing lists affiliated with this website will be public. Do not post confidential information, especially passwords!