Skip to content

Commit

Permalink
Fix linter warnings and all around refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
flebel committed Feb 13, 2018
1 parent 0ac9919 commit 4fe4d39
Showing 1 changed file with 39 additions and 24 deletions.
63 changes: 39 additions & 24 deletions urban-word-of-the-day.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,59 +3,74 @@
import json
import requests

from collections import namedtuple

from flask import Flask
from flask.ext.cache import Cache

from bs4 import BeautifulSoup

APP = Flask(__name__)
CACHE = Cache(APP, config={'CACHE_TYPE': 'simple'})
CACHE_TIMEOUT = 1800 # 30 minutes
URL = 'https://www.urbandictionary.com'

def get_wod(day=0):
if day > 6:
raise LookupError('Words of the day older than one week from today are not available.')
CACHE_TIMEOUT = 1800 # 30 minutes
_UrbanWord = namedtuple('UrbanWord', 'word meaning example')

content = requests.get(URL).text
soup = BeautifulSoup(content)
words = []

for div in soup.findAll('a', attrs={'class': 'word'}):
words.append(div.text.strip())
class UrbanWord(_UrbanWord):
def jsonify(self):
return json.dumps({
'word': self.word,
'meaning': self.meaning,
'example': self.example
})

meanings = __get_elements_of_class('meaning', soup)
elements = __get_elements_of_class('example', soup)

return zip(words, meanings, elements)[day]
class UrbanWordRetriever(object):
DEFAULT_URL = 'https://www.urbandictionary.com'

def __init__(self, day, url=DEFAULT_URL):
if day > 6:
raise LookupError('Words of the day older than one week from today'
' are not available.')
self.day = day
self.url = url

def __get_elements_of_class(class_name, soup):
elements = []
@staticmethod
def __get_elements_of_class(class_name, soup):
elements = []
for div in soup.findAll('div', attrs={'class': class_name}):
elements.append('\n'.join(div.findAll(text=True)).strip())
return elements

for div in soup.findAll('div', attrs={'class': class_name}):
elements.append('\n'.join(div.findAll(text=True)).strip())
def retrieve(self):
content = requests.get(self.url).text
soup = BeautifulSoup(content)

return elements
words = []
for div in soup.findAll('a', attrs={'class': 'word'}):
words.append(div.text.strip())

meanings = UrbanWordRetriever.__get_elements_of_class('meaning', soup)
example = UrbanWordRetriever.__get_elements_of_class('example', soup)

def jsonize_wod(wod):
return json.dumps({ 'word': wod[0], 'meaning': wod[1], 'example': wod[2] })
return UrbanWord(words[self.day], meanings[self.day],
example[self.day])


@APP.route('/')
@APP.route('/today')
@CACHE.cached(timeout=CACHE_TIMEOUT)
def today():
return jsonize_wod(get_wod(0))
word = UrbanWordRetriever(0).retrieve()
return word.jsonify()


@APP.route('/yesterday')
@CACHE.cached(timeout=CACHE_TIMEOUT)
def yesterday():
return jsonize_wod(get_wod(1))
word = UrbanWordRetriever(1).retrieve()
return word.jsonify()


if __name__ == '__main__':
APP.run(debug=True)

0 comments on commit 4fe4d39

Please sign in to comment.