askcompu

Untitled

Jul 23rd, 2015
336
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.24 KB | None | 0 0
  1. def wikipedia_search(query):
  2.     query = query.replace('!', '')
  3.     query = web.quote(query)
  4.     uri = 'https://en.wikipedia.org/w/api.php?action=query&list=search&continue=&srsearch=%s&format=json' % query
  5.     bytes = web.get(uri)
  6.     jsonstring = json.loads(bytes)
  7.     whits = jsonstring['query']['searchinfo']['totalhits']
  8.     if whits > 0:
  9.         wtitle = jsonstring['query']['search'][0]['title']
  10.         wwords = str(jsonstring['query']['search'][0]['wordcount'])
  11.         wsearch = wtitle.replace('!', '')
  12.         wsearch = web.quote(wsearch)
  13.         base_url = "https://en.wikipedia.org/wiki/"+wsearch
  14.         return (wtitle + " - " + wwords + " words " + base_url)
  15.  
  16. def wikipedia(phenny, input):
  17.     """Queries Wikipedia for the specified input."""
  18.     query = input.group(2)
  19.     if not query: return phenny.reply('.w what?')
  20.  
  21.     uri = wikipedia_search(query)
  22.     if uri:
  23.         phenny.say("Here's what I got, " + input.nick + ": " + uri)
  24.         if not hasattr(phenny.bot, 'last_seen_uri'):
  25.             phenny.bot.last_seen_uri = {}
  26.         phenny.bot.last_seen_uri[input.sender] = uri
  27.     else: phenny.say("Sorry " + input.nick + ", I couldn't find anything for '%s'." % query)
  28. wikipedia.commands = ['w', 'wikipedia']
  29. wikipedia.example = '.w swhack'
Advertisement
Add Comment
Please, Sign In to add comment