Skip to content

Instantly share code, notes, and snippets.

@lepture
Created March 10, 2012 15:54
Show Gist options
  • Save lepture/2011858 to your computer and use it in GitHub Desktop.
Save lepture/2011858 to your computer and use it in GitHub Desktop.
emoji support in python
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (c) 2012, lepture.com
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials provided
# with the distribution.
# * Neither the name of the author nor the names of its contributors
# may be used to endorse or promote products derived from this
# software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import re
emoji_url = 'http://a248.e.akamai.net/assets.github.com/images/icons/emoji/'
emoji_list = [
"-1", "0", "1", "109", "2", "3", "4", "5", "6", "7", "8", "8ball", "9",
"a", "ab", "airplane", "alien", "ambulance", "angel", "anger", "angry",
"apple", "aquarius", "aries", "arrow_backward", "arrow_down",
"arrow_forward", "arrow_left", "arrow_lower_left", "arrow_lower_right",
"arrow_right", "arrow_up", "arrow_upper_left", "arrow_upper_right",
"art", "astonished", "atm", "b", "baby", "baby_chick", "baby_symbol",
"balloon", "bamboo", "bank", "barber", "baseball", "basketball", "bath",
"bear", "beer", "beers", "beginner", "bell", "bento", "bike", "bikini",
"bird", "birthday", "black_square", "blue_car", "blue_heart", "blush",
"boar", "boat", "bomb", "book", "boot", "bouquet", "bow", "bowtie",
"boy", "bread", "briefcase", "broken_heart", "bug", "bulb",
"bullettrain_front", "bullettrain_side", "bus", "busstop", "cactus",
"cake", "calling", "camel", "camera", "cancer", "capricorn", "car",
"cat", "cd", "chart", "checkered_flag", "cherry_blossom", "chicken",
"christmas_tree", "church", "cinema", "city_sunrise", "city_sunset",
"clap", "clapper", "clock1", "clock10", "clock11", "clock12", "clock2",
"clock3", "clock4", "clock5", "clock6", "clock7", "clock8", "clock9",
"closed_umbrella", "cloud", "clubs", "cn", "cocktail", "coffee",
"cold_sweat", "computer", "confounded", "congratulations",
"construction", "construction_worker", "convenience_store", "cool",
"cop", "copyright", "couple", "couple_with_heart", "couplekiss", "cow",
"crossed_flags", "crown", "cry", "cupid", "currency_exchange", "curry",
"cyclone", "dancer", "dancers", "dango", "dart", "dash", "de",
"department_store", "diamonds", "disappointed", "dog", "dolls",
"dolphin", "dress", "dvd", "ear", "ear_of_rice", "egg", "eggplant",
"egplant", "eight_pointed_black_star", "eight_spoked_asterisk",
"elephant", "email", "es", "european_castle", "exclamation", "eyes",
"factory", "fallen_leaf", "fast_forward", "fax", "fearful", "feelsgood",
"feet", "ferris_wheel", "finnadie", "fire", "fire_engine", "fireworks",
"fish", "fist", "flags", "flushed", "football", "fork_and_knife",
"fountain", "four_leaf_clover", "fr", "fries", "frog", "fuelpump", "gb",
"gem", "gemini", "ghost", "gift", "gift_heart", "girl", "goberserk",
"godmode", "golf", "green_heart", "grey_exclamation", "grey_question",
"grin", "guardsman", "guitar", "gun", "haircut", "hamburger", "hammer",
"hamster", "hand", "handbag", "hankey", "hash", "headphones", "heart",
"heart_decoration", "heart_eyes", "heartbeat", "heartpulse", "hearts",
"hibiscus", "high_heel", "horse", "hospital", "hotel", "hotsprings",
"house", "hurtrealbad", "icecream", "id", "ideograph_advantage", "imp",
"information_desk_person", "iphone", "it", "jack_o_lantern",
"japanese_castle", "joy", "jp", "key", "kimono", "kiss", "kissing_face",
"kissing_heart", "koala", "koko", "kr", "leaves", "leo", "libra", "lips",
"lipstick", "lock", "loop", "loudspeaker", "love_hotel", "mag",
"mahjong", "mailbox", "man", "man_with_gua_pi_mao", "man_with_turban",
"maple_leaf", "mask", "massage", "mega", "memo", "mens", "metal",
"metro", "microphone", "minidisc", "mobile_phone_off", "moneybag",
"monkey", "monkey_face", "moon", "mortar_board", "mount_fuji", "mouse",
"movie_camera", "muscle", "musical_note", "nail_care", "necktie", "new",
"no_good", "no_smoking", "nose", "notes", "o", "o2", "ocean", "octocat",
"octopus", "oden", "office", "ok", "ok_hand", "ok_woman", "older_man",
"older_woman", "open_hands", "ophiuchus", "palm_tree", "parking",
"part_alternation_mark", "pencil", "penguin", "pensive", "persevere",
"person_with_blond_hair", "phone", "pig", "pill", "pisces", "plus1",
"point_down", "point_left", "point_right", "point_up", "point_up_2",
"police_car", "poop", "post_office", "postbox", "pray", "princess",
"punch", "purple_heart", "question", "rabbit", "racehorse", "radio",
"rage", "rage1", "rage2", "rage3", "rage4", "rainbow", "raised_hands",
"ramen", "red_car", "red_circle", "registered", "relaxed", "relieved",
"restroom", "rewind", "ribbon", "rice", "rice_ball", "rice_cracker",
"rice_scene", "ring", "rocket", "roller_coaster", "rose", "ru", "runner",
"sa", "sagittarius", "sailboat", "sake", "sandal", "santa", "satellite",
"satisfied", "saxophone", "school", "school_satchel", "scissors",
"scorpius", "scream", "seat", "secret", "shaved_ice", "sheep", "shell",
"ship", "shipit", "shirt", "shit", "shoe", "signal_strength",
"six_pointed_star", "ski", "skull", "sleepy", "slot_machine", "smile",
"smiley", "smirk", "smoking", "snake", "snowman", "sob", "soccer",
"space_invader", "spades", "spaghetti", "sparkler", "sparkles",
"speaker", "speedboat", "squirrel", "star", "star2", "stars", "station",
"statue_of_liberty", "stew", "strawberry", "sunflower", "sunny",
"sunrise", "sunrise_over_mountains", "surfer", "sushi", "suspect",
"sweat", "sweat_drops", "swimmer", "syringe", "tada", "tangerine",
"taurus", "taxi", "tea", "telephone", "tennis", "tent", "thumbsdown",
"thumbsup", "ticket", "tiger", "tm", "toilet", "tokyo_tower", "tomato",
"tongue", "top", "tophat", "traffic_light", "train", "trident",
"trollface", "trophy", "tropical_fish", "truck", "trumpet", "tshirt",
"tulip", "tv", "u5272", "u55b6", "u6307", "u6708", "u6709", "u6e80",
"u7121", "u7533", "u7a7a", "umbrella", "unamused", "underage", "unlock",
"up", "us", "v", "vhs", "vibration_mode", "virgo", "vs", "walking",
"warning", "watermelon", "wave", "wc", "wedding", "whale", "wheelchair",
"white_square", "wind_chime", "wink", "wink2", "wolf", "woman",
"womans_hat", "womens", "x", "yellow_heart", "zap", "zzz", "+1"
]
def emoji(text):
pattern = re.compile(':([a-z0-9\+\-_]+):')
def make_emoji(m):
name = m.group(1)
if name not in emoji_list:
return ':%s:' % name
tpl = ('<img class="emoji" title="%(name)s" alt="%(name)s" height="20"'
' width="20" src="%(url)s%(name)s.png" align="top">')
return tpl % {'name': name, 'url': emoji_url}
text = pattern.sub(make_emoji, text)
return text
@linnchord
Copy link

great!

@lepture
Copy link
Author

lepture commented Mar 12, 2012

@linnchord hi, I just fixed a regex bug.

@nitely
Copy link

nitely commented Oct 20, 2013

@lepture that emoji_list should be a set for faster lookups. ("if name not in emoji_list")

@michaeljoseph
Copy link

@lepture, I'd really like to turn this into a pypi installable- is this on the cards for you? Totally up to doing the legwork and maintaining the project / package.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment