Skip to content

Instantly share code, notes, and snippets.

@kyhau
Created February 28, 2016 03:31
Show Gist options
  • Save kyhau/d91c7a9fdb5b9a455fca to your computer and use it in GitHub Desktop.
Save kyhau/d91c7a9fdb5b9a455fca to your computer and use it in GitHub Desktop.
Random dilbert image url
"""
Dilbert
"""
import random
import urllib
import re
def random_dilbert(logger):
"""
Retrieve url of a random dilbert
"""
logger.debug('Running random_dilbert ...')
year = random.choice(['2013', '2014', '2015'])
month = random.choice(range(1, 13))
day = random.choice(range(1, 29))
try:
url_to_dilbert_page = 'http://www.dilbert.com/%s-%s-%s/' % (year, month, day)
page_contents = urllib.urlopen(url_to_dilbert_page).read()
image_url = re.search('<meta name="twitter:image" content="(.*)">', page_contents).group(1) + '.png'
return image_url
except Exception as e:
logger.error(e)
return None
if __name__ == "__main__":
# debug-only
import logging
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
random_dilbert(logger)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment