Esempio n. 1
0
    def cmdQdb(self, event):
        if len(event.args) == 1 and event.args[0].isdigit() \
        or len(event.args) >= 2 and event.args[0].lower() == "read":
            if event.args[0].isdigit():
                id = int(event.args[0])
            else:
                id = int(event.args[1])
            self.log.debug("Fetching qdb.us/%i", id)
            data = urlopener.open("http://qdb.us/%i" % id)
            if data.getcode() == 404:
                event.client.msg(event.channel, str("[QDB #%s] Quote not found." % id))
                return
            if data.getcode() == 200:
                html = data.read()
                soup = self.BeautifulSoup(html)
                quote = soup.find("span", {"class":"qt"}).text
                self.sendQuote(event.client, event.channel, (id, quote))
                return
            self.log.error("Got unknown HTTP error code %i when fetching qdb.us/%i", data.getcode(), id)
            event.client.msg(event.channel, str("[QDB] An unknown exception occurred. Please notify the bot master and try again later."))
            return

        if len(event.args) <= 0 or event.args[0].lower() == "random":
            self.cacheIfNeeded()
            quote = self.quotesCache.pop()
            self.sendQuote(event.client, event.channel, quote)
            return
Esempio n. 2
0
 def eventURLInfo_Spotify(self, event):
     if self.bs is not None:
         if "open.spotify" in event.msg and "http" in event.msg:
             data = self.reSpotifyLink.search(event.msg)
             if data:
                 url = data.group(0)
                 html = urlopener.open(url).read()
                 soup = self.bs(html)
                 type = data.group(2)
                 if type == "track":
                     songtitle = soup.find("meta", {"property":"og:title"})['content'].strip()
                     artist = soup.find("div", {"class":"player-header"}) \
                                  .find("h2").find("a").text.strip()
                     if data:
                         event.client.msg(event.channel, str("\x031,3Spotify\x03 Track \x033::\x03 %s \x033::\x03 %s" % (unescape(songtitle), unescape(artist))).replace("\n",""))
                 elif type == "album":
                     albumtitle = soup.find("meta", {"property":"og:title"})['content'].strip()
                     artist = soup.find("div", {"class":"player-header"}) \
                                  .find("h2").find("a").text.strip()
                     if data:
                         event.client.msg(event.channel, str("\x031,3Spotify\x03 Album \x033::\x03 %s \x033::\x03 %s" % (unescape(albumtitle), unescape(artist))).replace("\n",""))
                 elif type == "artist":
                     artist = soup.find("meta", {"property":"og:title"})['content'].strip()
                     if data:
                         event.client.msg(event.channel, str("\x031,3Spotify\x03 Artist \x033::\x03 %s" % (unescape(artist))).replace("\n",""))
                 elif type == "user" and data.group(3) is not None:
                     playlist = soup.find("meta", {"property":"og:title"})['content'].strip()
                     user = soup.find("div", {"class":"player-header"}) \
                                  .find("h2").find("a").text.strip()
                     if data:
                         event.client.msg(event.channel, str("\x031,3Spotify\x03 Playlist \x033::\x03 %s \x033::\x03 %s" % (unescape(playlist), unescape(user))).replace("\n",""))
                 elif type == "user":
                     user = soup.find("meta", {"property":"og:title"})['content'].strip()
                     if data:
                         event.client.msg(event.channel, str("\x031,3Spotify\x03 User \x033::\x03 %s" % (unescape(user))).replace("\n",""))
Esempio n. 3
0
 def cacheIfNeeded(self):
     if not self.quotesCache:
         self.log.debug("Fetching new quotes from qdb.us/random")
         html = urlopener.open("http://qdb.us/random").read()
         soup = self.BeautifulSoup(html)
         data = soup.findAll("span", {"class":"qt"})
         for item in data:
             id = item["id"].split("qt")[1]
             self.quotesCache.append((id, item.text))
         self.log.debug("Got %i new quotes", len(self.quotesCache))
         random.shuffle(self.quotesCache, random.random)
Esempio n. 4
0
 def eventURLInfo_YouTube(self, event):
     if self.bs is not None:
         if "youtu" in event.msg and "http" in event.msg:
             data = self.reYouTubeLink.search(event.msg)
             if data:
                 vid = data.group(5)
                 url = "http://youtu.be/%s" % vid
                 html = urlopener.open(url).read()
                 soup = self.bs(html)
                 title = soup.find("span", {"id":"eow-title"}).text.strip()
                 if title:
                     event.client.msg(event.channel, str("\x030,1You\x030,4Tube\x03 \x034::\x03 %s \x034::\x03 %s" % (unescape(title), url)).replace("\n", ""))
Esempio n. 5
0
 def eventURLInfo_Twitter(self, event):
     if self.bs is not None:
         if "twitter" in event.msg and "http" in event.msg:
             data = self.reTwitterLink.search(event.msg)
             if data:
                 url = data.group(0)
                 html = urlopener.open(url).read()
                 soup = self.bs(html)
                 tweet = soup.find("div", {"class":"permalink-inner permalink-tweet-container"}).find("p", {"class":"tweet-text"}).text
                 user = soup.find("div", {"class":"permalink-inner permalink-tweet-container"}).find("span", {"class":"username js-action-profile-name"}).text
                 msg = u"\x0310Twitter\x03 \x0311::\x03 %s \x0311––\x03 %s" % (tweet, user)
                 msg = unescape(msg)
                 msg = msg.encode("utf-8")
                 msg = str(msg)
                 event.client.msg(event.channel, msg)
Esempio n. 6
0
    def trigger(self, event):
        argc = len(event.args)
        action = None
        nickname = None
        if argc <= 0:
            nickname = event.user.nickname
        elif event.args[0] in ["-r", "-d"]:
            action = event.args[0]
            if len(event.args) >= 2:
                nickname = event.args[1]
        else:
            nickname = event.args[0].decode("utf-8")

        session = self.db.new_session()
        if action == None:
            user = session.query(User).filter(User.nickname==nickname).first()
            if not user:
                event.client.msg(event.channel, str("%s: No user registered for nick '%s'" % (event.user.nickname, nickname)))
                return
            params = urllib.urlencode({"method": "user.getRecentTracks", "user": user.username, "api_key": self.apikey, "format": "json", "extended": 1})
            try:
                data = urlopener.open("http://ws.audioscrobbler.com/2.0/?%s" % params).read()
                data = json.loads(data)
            except:
                event.client.msg(event.channel, "[Last.fm] An unexpected error occurred. Please tell the bot manager to file a bug report.")
                self.log.exception("An error occurred while fetching user.getRecentTracks for user %s", nickname)
                return
            if "error" in data:
                self.log.error("API error %i: %s", data["error"], data["message"])
                event.client.msg(event.channel, "[Last.fm] An error occurred while processing your request. Please notify the bot manager")
                return
            if "track" not in data["recenttracks"] or len(data['recenttracks']['track']) < 1:
                event.client.msg(event.channel, str("%s: No tracks found for user '%s'. Are you sure that the user exists?" % (event.user.nickname, user.username)))
                return
            track = data['recenttracks']['track'][0]
            artist = track["artist"]["name"]
            tracktitle = track["name"]
            if "@attr" in track and "nowplaying" in track["@attr"] and track["@attr"]["nowplaying"].lower() == "true":
                loved = ""
                if "loved" in track and track["loved"] == "1":
                    loved = "\x034<3\x03"
                msg = "'%s' is now playing: %s - %s %s" % (user.username, artist, tracktitle, loved)
            else:
                timestamp = track["date"]["uts"]
                date = []

                dateThen = datetime.fromtimestamp(float(timestamp))
                dateNow = datetime.now()
                diff = dateNow - dateThen

                if diff.days > 0:
                    if diff.days != 1:
                        suffix = "s"
                    else:
                        suffix = ""
                    date.append("%s day%s" % (diff.days, suffix))

                hours = (diff.seconds//3600)%24
                if hours > 0:
                    if hours != 1:
                        suffix = "s"
                    else:
                        suffix = ""
                    date.append("%s hour%s" % (hours, suffix))

                minutes = (diff.seconds//60)%60
                if minutes != 1:
                    suffix = "s"
                else:
                    suffix = ""
                date.append("%s minute%s" % (minutes, suffix))
                msg = "'%s' is not playing anything now, but played this %s ago: %s - %s" % (user.username, ", ".join(date), artist, tracktitle)
            event.client.msg(event.channel, str(unescape(msg).encode("utf-8")))
            return

        elif action == "-r":
            if not nickname:
                event.client.notice(event.user.nickname, str("[Last.fm] You need to provide a Last.fm username."))
                return
                
            user = session.query(User).filter(User.nickname==event.user.nickname).first()
            if not user:
                user = User(event.user.nickname)
            user.username = nickname
            session.begin()
            session.add(user)
            session.commit()
            event.client.notice(event.user.nickname, str("[Last.fm] Registered '%s' to your nick" % nickname))
            return

        elif action == "-d":
            user = session.query(User).filter(User.nickname==event.user.nickname).first()
            if not user:
                event.client.notice(event.user.nickname, str("[Last.fm] No user registered for nick '%s'" % nickname))
                return

            session.begin()
            session.delete(user)
            session.commit()
            event.client.notice(event.user.nickname, str("[Last.fm] Unregistered your nick from '%s'" % user.username))
            return