Skip to content

Instantly share code, notes, and snippets.

@davechristian
Last active January 2, 2016 12:48
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 davechristian/8305454 to your computer and use it in GitHub Desktop.
Save davechristian/8305454 to your computer and use it in GitHub Desktop.
Script to download and display the Urban Dictionary word of the day. Used Feedparser to process the RSS feed - see https://code.google.com/p/feedparser for source).
# Simple script to get the Urban Dictionary Word of the Day via RSS
# Code is released under MIT license. Please see http://opensource.org/licenses/MIT for full license
import feedparser # 3rd party: https://code.google.com/p/feedparser/
from HTMLParser import HTMLParser # Python stdlib
import os
class HTMLStripper(HTMLParser, object):
def __init__(self):
HTMLParser.__init__(self)
self.html = ''
def handle_data(self, d):
self.html += str(d.lstrip()) # Python should 'special case' this, resulting in O(n) complexity :)
def get_processed(self):
return self.html
def get_word_of_the_day():
try:
feed = feedparser.parse('http://feeds.urbandictionary.com/UrbanWordOfTheDay')
hstrip = HTMLStripper()
if (len(feed.entries) > 0):
desc = feed.entries[0].description # Only interested in most recent entry
desc = desc.replace('<br>', os.linesep)
desc = desc.replace('<br />', os.linesep)
hstrip.feed(desc)
print('\"' + feed.entries[0].title + '\"' + os.linesep + hstrip.get_processed())
else:
print('No WOTD entries were present in the RSS feed.')
except:
print('Something went wrong connecting to the WOTD RSS feed.')
if __name__ == "__main__":
get_word_of_the_day()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment