From 57d881279c1267c6d2fa98b90fb1496f138f466a Mon Sep 17 00:00:00 2001 From: Joe Robinson Date: Tue, 23 Sep 2014 15:07:00 +0100 Subject: Added Urban Dictionary definitions --- bladictionary.py | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/bladictionary.py b/bladictionary.py index 6cdaa03..b5e3099 100755 --- a/bladictionary.py +++ b/bladictionary.py @@ -6,8 +6,9 @@ import shlex import optparse from lxml import etree import sqlite3 +import requests -VERSION = "2.2.2" +VERSION = "2.3.0" class Definition(object): #ID is relative to the word type, eg noun 1, noun 2, verb 1, verb 2, not to the entire list @@ -152,7 +153,7 @@ def parse_args(): options, args = parser.parse_args( args ) types = ["n", "noun", "v", "verb", "adj", "adjective", "adv", "adverb", "tech"] - dicts = ["wn", "wordnet", "oed", "db", "foldoc"] + dicts = ["wn", "wordnet", "oed", "db", "foldoc", "urban"] word = "" word_type = "" @@ -413,7 +414,20 @@ def parse_foldoc(word, refer = None): items.append(item) return items +def parse_urban(word): + r = requests.get("http://urbanscraper.herokuapp.com/search/" + word) + json = r.json() + items = [] + id = 1 + + for json_item in json: + if json_item['definition'] != "" and json_item['definition'] is not None: + item = Definition(word, id, "urban", "urban", json_item['definition'], [json_item['example']], [], []) + items.append(item) + id += 1 + + return items def main(): @@ -435,6 +449,8 @@ def main(): items = parse_xml(xml) elif word_dict == "foldoc" or word_type == "tech": items = parse_foldoc(word) + elif word_dict == "urban": + items = parse_urban(word) else: if word_dict is None or word_dict == "": word_dict = "db" @@ -514,7 +530,8 @@ def main(): #Print usage examples if they exist if len(item.uses) > 0: - definition += "; \""+item.uses[0]['quote']+"\" " + definition = definition.rstrip(". ") + definition += "; \""+item.uses[0]+"\" " if not all_types and line_length + len(definition) < (max_length * (page_num -1)): line_length += len(definition) +1 -- cgit v1.2.3