Last active
January 2, 2016 12:48
-
-
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).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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