Advertisement
Markk

wp.py

Jun 20th, 2012
17
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.38 KB | None | 0 0
  1. # coding: utf-8
  2. import re
  3.  
  4. import utility
  5. from commands import Command
  6.  
  7. class WikipediaCommand(Command):
  8.     def wp_get(self, language, item):
  9.         url = "http://%s.wikipedia.org/wiki/%s" % (language, utility.escape(item.replace(" ", "_")))
  10.  
  11.         response = utility.read_url(url)
  12.  
  13.         if not response:
  14.             return (None, None)
  15.  
  16.         data = response["data"]
  17.         url = response["url"]
  18.  
  19.         # sometimes there is a nasty table containing the first <p>. we can't allow this to happen!
  20.         pattern = re.compile("<table.*?>.+?<\/table>", re.MULTILINE)
  21.  
  22.         data = re.sub(pattern, "", data)
  23.  
  24.         m = re.search("<p>(.+?)<\/p>", data)
  25.         if m:
  26.             data = utility.unescape(m.group(1))
  27.             data = re.sub("<.+?>", "", data)
  28.             data = re.sub("\[\d+\]", "", data)
  29.  
  30.             index = data.rfind(".", 0, 300)
  31.  
  32.             if index == -1:
  33.                 index = 300
  34.  
  35.             if index+1 < len(data) and data[index+1] == '"':
  36.                 index += 1
  37.  
  38.             data = data[0:index+1]
  39.  
  40.             if "Wikipedia does not have an article with this exact name." in data:
  41.                 data = None
  42.         else:
  43.             data = None
  44.  
  45.         return (url, data)
  46.  
  47.     def trig_wp(self, bot, source, target, trigger, argument):
  48.         languages = ["simple", "en", "sv", "de", "dk", "nn", "no", "fi", "fr", "it", "nl", "it", "es", "pt", "ru"]
  49.         for language in languages:
  50.             url, data = self.wp_get(language, argument)
  51.             if data:
  52.                 return "%s - %s" % (data, url)
  53.  
  54.         return "I couldn't find an article... :("
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement