Commits

Benoit Boissinot  committed 1f0f6c5

add mylifeisaverage.com support

  • Participants
  • Parent commits dabdfda

Comments (0)

Files changed (1)

 
 import re
 import time
+import random as modrandom
 from xml.dom.minidom import parseString
 
 import supybot.utils as utils
                             'bash.org':[],
                             'viedemerde.fr':[],
                             'fmylife.com':[],
+                            'mylifeisaverage.com':[],
                             'textsfromlastnight.com':[],
                             }
 
                     r'<div class="post_content">(?P<text>.*?)</div>.*?'\
                     r'<span id="postingGoodCount_.*?">(?P<up>\d+)</span>.*?'\
                     r'<span id="postingBadCount_.*?">(?P<down>\d+)</span>'
+    _mliaReString = r'<div id="s_(?P<id>\d+)" class="story s">\s*'\
+                    r'<div class="sc">(?P<text>.*?)</div>.*?'\
+                    r'<span class="v_pos">(?P<up>\d+)</span>.*?'\
+                    r'<span class="v_neg">(?P<down>\d+)</span>'
     _qdbReString = r'<tr><td bgcolor="#(?:ffffff|e8e8e8)"><a href="/\d*?">'\
                     r'#\d*?</a>.*?<p>(?P<text>.*?)</p></td></tr>'
     _gkREDict = {'bash.org': re.compile(r'<p class="qt">(?P<text>.*?)</p>',
                     re.M | re.DOTALL),
                 'qdb.us': re.compile(_qdbReString, re.M | re.DOTALL),
+                'mylifeisaverage.com':
+                    re.compile(_mliaReString, re.M | re.DOTALL),
                 'textsfromlastnight.com':
                     re.compile(_tflnReString, re.M | re.DOTALL),}
     _betacieUrl = ('http://api.betacie.com/view/'
             url = 'http://%s/%s%s' % (site,
                                       not random and 'view/' or '',
                                       id)
+        elif site == 'mylifeisaverage.com':
+            if random:
+                url = 'http://%s/' % site
+                try:
+                    html = utils.web.getUrl(url)
+                except utils.web.Error, e:
+                    self.log.info('%u server returned the error: %s',
+                                  site, utils.web.strError(e))
+                last = re.search(r'<li class="last"><a href="(\d+)">', html)
+                last = int(last.group(1))
+                url = 'http://%s/%s' % (site, modrandom.randint(1, last))
+            else:
+                url = 'http://%s/story/%s' % (site, id)
         else:
             url = 'http://%s/?%s' % (site, id)
         html = ''
                 s = "%s #%s (+%s,-%s)" % (t, id, up, down)
                 if random and s not in self.randomData[site]:
                     self.randomData[site].append(s)
-        elif site == 'textsfromlastnight.com':
+        elif site in ('mylifeisaverage.com', 'textsfromlastnight.com'):
             for item in self._gkREDict[site].finditer(html):
                 d = item.groupdict()
                 t = d['text']
             return s
 
     def geekSnarfer(self, irc, msg, match):
-        r'http://(?:www\.)?(?P<site>bash\.org|qdb\.us|viedemerde\.fr|fmylife\.com|textsfromlastnight\.com)/\??(?P<id>\d+)'
+        r'http://(?:www\.)?(?P<site>bash\.org|qdb\.us|viedemerde\.fr|fmylife\.com|textsfromlastnight\.com)/([?]|story/)?(?P<id>\d+)'
         if not self.registryValue('geekSnarfer', msg.args[0]):
             return
         id = match.groupdict()['id']
         self._gkBackend(irc, msg, site, id)
     tfln = wrap(tfln, [additional(('id', 'tfln'))])
 
+    def mlia(self, irc, msg, args, id):
+        """[<id>]
+
+        Returns a random quote from mylifeisaverage.com; the optional
+        argument <id> specifies which quote to retrieve.
+        """
+        site = 'mylifeisaverage.com'
+        self._gkBackend(irc, msg, site, id)
+    mlia = wrap(mlia, [additional(('id', 'tfln'))])
+
 Class = Geekquote