Skip to content

Commit

Permalink
Extract Wikipedia plugin into a separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
Merola committed Nov 1, 2015
1 parent b38e776 commit 5dba217
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 50 deletions.
2 changes: 1 addition & 1 deletion plugins/__init__.py
Expand Up @@ -7,4 +7,4 @@
'tenta', 'prisjakt', 'spotify', 'stava', 'stock', 'down', 'metacritic',
'notes', 'fml', 'systembolaget', 'randombuy', 'festern_bbq',
'compliment', 'roulette', 'tyda', 'yrno', 'tweet', 'calc', 'degoogle',
'temperatur_nu', 'google_search']
'temperatur_nu', 'google_search', 'wikipedia']
49 changes: 0 additions & 49 deletions plugins/standard.py
Expand Up @@ -138,55 +138,6 @@ def trig_help(self, bot, source, target, trigger, argument):
else:
return "That's not a command! Try `help <command>`"

class WikipediaCommand(Command):
def wp_get(self, language, item):
url = "http://%s.wikipedia.org/wiki/%s" % (language, utility.escape(item.replace(" ", "_")))

response = utility.read_url(url)

if not response:
return (None, None)

data = response["data"]
url = response["url"]

# sometimes there is a nasty table containing the first <p>. we can't allow this to happen!
pattern = re.compile("<table.*?>.+?<\/table>", re.MULTILINE)

data = re.sub(pattern, "", data)

m = re.search("<p>(.+?)<\/p>", data)
if m:
data = utility.unescape(m.group(1))
data = re.sub("<.+?>", "", data)
data = re.sub("\[\d+\]", "", data)

index = data.rfind(".", 0, 300)

if index == -1:
index = 300

if index+1 < len(data) and data[index+1] == '"':
index += 1

data = data[0:index+1]

if "Wikipedia does not have an article with this exact name." in data:
data = None
else:
data = None

return (url, data)

def trig_wp(self, bot, source, target, trigger, argument):
languages = ["simple", "en", "sv"]
for language in languages:
url, data = self.wp_get(language, argument)
if data:
return "%s - %s" % (data, url)

return "I couldn't find an article... :("

class AAOCommand(Command):
triggers = ['}{|', 'åäö', 'åäö']

Expand Down
54 changes: 54 additions & 0 deletions plugins/wikipedia.py
@@ -0,0 +1,54 @@
# coding: utf-8

from commands import Command
import re
import utility

class WikipediaCommand(Command):
def wp_get(self, language, item):
url = "http://%s.wikipedia.org/wiki/%s" % (language, utility.escape(item.replace(" ", "_")))

response = utility.read_url(url)

if not response:
return (None, None)

data = response["data"]
url = response["url"]

# sometimes there is a nasty table containing the first <p>. we can't allow this to happen!
pattern = re.compile("<table.*?>.+?<\/table>", re.MULTILINE)

data = re.sub(pattern, "", data)

m = re.search("<p>(.+?)<\/p>", data)
if m:
data = utility.unescape(m.group(1))
data = re.sub("<.+?>", "", data)
data = re.sub("\[\d+\]", "", data)

index = data.rfind(".", 0, 300)

if index == -1:
index = 300

if index+1 < len(data) and data[index+1] == '"':
index += 1

data = data[0:index+1]

if "Wikipedia does not have an article with this exact name." in data:
data = None
else:
data = None

return (url, data)

def trig_wp(self, bot, source, target, trigger, argument):
languages = ["simple", "en", "sv"]
for language in languages:
url, data = self.wp_get(language, argument)
if data:
return "%s - %s" % (data, url)

return "I couldn't find an article... :("

0 comments on commit 5dba217

Please sign in to comment.