def wikiSearch(query, url, results=5): """Use MediaWikis API to search for values from wiktionary and wikipedia""" # First, we need to grab the data, and serialize it in JSON url_query = urlify(query) data = jsonify(get(full_search % (lang, url, url_query)).read()) # Check to see if we have #(results as arg form) results, then make a list if not data[1]: return False if len(data[1]) > results: return data[1][:results] else: # Assume it's smaller than or = 5 return data[1]
def wikiDefine(term, url): """Use MediaWikis API to define a value from wiktionary and wikipedia""" # First, we need to grab the data, and serialize it in JSON url_query = urlify(term) data = jsonify(get(full_define % (lang, url, maxlen, url_query)).read())['query']['pages'] # We need to see if it was found. If it wasn't it'll be a -1 page for pageNumber, pageData in data.iteritems(): if pageNumber == '-1': # Assume failed to find return False else: # Assume found a result. Now, find and return the title/contents. if pageData['extract'].startswith('REDIRECT'): return False # This means it's a redirect page according to MediaWiki API title = pageData['title'] content = pageData['extract'].encode('ascii', 'ignore').replace('\n', ' ') while ' ' in content: content = content.replace(' ', ' ') return [title, content]