Skip to content

Instantly share code, notes, and snippets.

@fcy
Created March 25, 2013 21:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fcy/5240825 to your computer and use it in GitHub Desktop.
Save fcy/5240825 to your computer and use it in GitHub Desktop.
My first Python script. It integrates Urban Dictionary with the new Alfred 2 Workflow. Download the bundle here: https://www.dropbox.com/s/edrab4lyzi5gorj/Urban%20Dictionary.alfredworkflow
import urllib2
from HTMLParser import HTMLParser
import xml.etree.ElementTree as ET
class UrbanDictionaryHTMLParser(HTMLParser):
def __init__(self):
HTMLParser.__init__(self)
self.is_parsing_entries = False
self.definitions = []
self.is_word_open = False
self.is_word_span = False
self.is_index_open = False
self.is_index_a = False
self.is_definition = False
self.current_data = []
self.tables_in_entries = 0
self.current_definition = {}
def handle_starttag(self, tag, attrs):
attrD = dict(attrs)
if tag == 'table':
if attrD.get('id', '') == 'entries':
self.is_parsing_entries = True
elif self.is_parsing_entries:
self.tables_in_entries += 1
if self.is_parsing_entries:
tag_class = attrD.get('class', '')
if tag == 'td' and tag_class == 'index':
self.is_index_open = True
elif self.is_index_open and tag == 'a':
self.current_definition['link'] = attrD['href']
elif tag == 'td' and tag_class == 'word':
self.is_word_open = True
elif self.is_word_open and tag == 'span':
self.is_word_span = True
elif tag == 'div' and tag_class == 'definition':
self.is_definition = True
def handle_endtag(self, tag):
if tag == 'table':
if self.tables_in_entries > 0:
self.tables_in_entries -= 1
elif self.is_parsing_entries:
self.is_parsing_entries = False
if self.is_parsing_entries:
if tag == 'td' and self.is_index_open:
self.is_index_open = False
elif tag == 'span' and self.is_word_span:
self.is_word_span = False
self.current_definition['word'] = ''.join(self.current_data).strip()
self.current_data = []
elif tag == 'td' and self.is_word_open:
self.is_word_open = False
elif tag == 'div' and self.is_definition:
self.is_definition = False
self.current_definition['definition'] = ''.join(self.current_data)
self.definitions.append(self.current_definition)
self.current_data = []
self.current_definition = {}
def handle_data(self, data):
if self.is_definition or self.is_word_span:
self.current_data.append(data)
f = urllib2.urlopen('http://www.urbandictionary.com/define.php?term={query}')
html = f.read()
parser = UrbanDictionaryHTMLParser()
parser.feed(html)
# Generate Alfred's XML
root = ET.Element('items')
for d in parser.definitions:
item = ET.SubElement(root, 'item')
item.set('uid', 'urbandictionarydefinition')
item.set('arg', d['link'])
title = ET.SubElement(item, 'title')
title.text = d['word']
subtitle = ET.SubElement(item, 'subtitle')
subtitle.text = d['definition']
icon = ET.SubElement(item, 'icon')
icon.set('type', 'fileicon')
icon.text = '/Applications/Dictionary.app'
print ET.tostring(root)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment