Skip to content

Commit

Permalink
Merge pull request #433 from BOFHers/nache_dev
Browse files Browse the repository at this point in the history
Allow you specify lang on wikipedia command
  • Loading branch information
embolalia committed Jan 28, 2014
2 parents a708e4a + 29bccc9 commit 1be337c
Showing 1 changed file with 46 additions and 6 deletions.
52 changes: 46 additions & 6 deletions willie/modules/wikipedia.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,19 @@

REDIRECT = re.compile(r'^REDIRECT (.*)')

def configure(config):
"""
| [wikipedia] | example | purpose |
| ------------- | ------- | ------- |
| default_lang | en | Set the Global default wikipedia lang |
"""
if config.option('Configure wikipedia module', False):
config.add_section('wikipedia')
config.interactive_add('wikipedia', 'default_lang', 'Wikipedia default language', 'en')

if config.option('Would you like to configure individual default language per channel', False):
c = 'Enter #channel:lang, one at time. When done, hit enter again.'
config.add_list('wikipedia', 'lang_per_channel', c, 'Channel:')

def mw_search(server, query, num):
"""
Expand All @@ -23,16 +36,18 @@ def mw_search(server, query, num):
'&srsearch=') % (server, num)
search_url += web.quote(query.encode('utf-8'))
query = json.loads(web.get(search_url))
query = query['query']['search']
return [r['title'] for r in query]

if 'query' in query:
query = query['query']['search']
return [r['title'] for r in query]
else:
return None

def mw_snippet(server, query):
"""
Retrives a snippet of the specified length from the given page on the given
server.
"""
snippet_url = ('https://en.wikipedia.org/w/api.php?format=json'
snippet_url = ('https://'+server+'/w/api.php?format=json'
'&action=query&prop=extracts&exintro&explaintext'
'&exchars=300&redirects&titles=')
snippet_url += web.quote(query.encode('utf-8'))
Expand All @@ -49,11 +64,36 @@ def mw_snippet(server, query):
@commands('w', 'wiki', 'wik')
@example('.w San Francisco')
def wikipedia(bot, trigger):

#Set the global default lang. 'en' if not definded
if not bot.config.has_option('wikipedia', 'default_lang'):
lang = 'en'
else:
lang = bot.config.wikipedia.default_lang

#change lang if channel has custom language set
if (
trigger.sender and
trigger.sender.startswith('#') and
bot.config.has_option('wikipedia', 'lang_per_channel')
):
customlang = re.search(
'('+trigger.sender+'):(\w+)',
bot.config.wikipedia.lang_per_channel
)
if customlang is not None:
lang = customlang.group(2)

query = trigger.group(2)
args = re.search(r'^-([a-z]{2,12})\s(.*)', query)
if args is not None:
lang = args.group(1)
query = args.group(2)

if not query:
bot.reply('What do you want me to look up?')
return NOLIMIT
server = 'en.wikipedia.org'
server = lang + '.wikipedia.org'
query = mw_search(server, query, 1)
if not query:
bot.reply("I can't find any results for that.")
Expand All @@ -63,4 +103,4 @@ def wikipedia(bot, trigger):
snippet = mw_snippet(server, query)

query = query.replace(' ', '_')
bot.say('"%s" - http://en.wikipedia.org/wiki/%s' % (snippet, query))
bot.say('"%s" - http://%s.wikipedia.org/wiki/%s' % (snippet, lang, query))

0 comments on commit 1be337c

Please sign in to comment.