diff --git a/CHANGES.txt b/CHANGES.txt new file mode 100644 index 0000000..c7f8f76 --- /dev/null +++ b/CHANGES.txt @@ -0,0 +1,10 @@ +0.3 (unreleased): +* Implement now playing method +* Split up plugin.py to plugin.py and LastFMDB.py + +0.2 (08 Sep 2008): +* You can now specify and save your LastFM ID +* You can limit the number of results (plugins.LastFM.#maxResults) + +0.1: +* Initial release diff --git a/LastFMDB.py b/LastFMDB.py new file mode 100644 index 0000000..9bf2b6b --- /dev/null +++ b/LastFMDB.py @@ -0,0 +1,73 @@ +### +# Copyright (c) 2008, Kevin Funk +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are met: +# +# * Redistributions of source code must retain the above copyright notice, +# this list of conditions, and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright notice, +# this list of conditions, and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# * Neither the name of the author of this software nor the name of +# contributors to this software may be used to endorse or promote products +# derived from this software without specific prior written consent. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. + +### + +import supybot.utils as utils +from supybot.commands import * +import supybot.conf as conf +import supybot.plugins as plugins +import supybot.ircutils as ircutils +import supybot.callbacks as callbacks +import supybot.world as world +import supybot.conf as conf +import supybot.plugins as plugins + +class LastFMDB(plugins.ChannelUserDB): + """Holds the LastFM IDs of all known nicks + + (This database is case insensitive and channel independent) + """ + + def __init__(self, *args, **kwargs): + plugins.ChannelUserDB.__init__(self, *args, **kwargs) + + def serialize(self, v): + + return list(v) + + def deserialize(self, channel, id, L): + (id,) = L + return (id,) + + def set(self, nick, id): + """ + if nick.lower() == id.lower(): + del self['x', nick.lower()] # FIXME: Bug in supybot(?) + else:""" + self['x', nick.lower()] = (id,) + + def getId(self, nick): + try: + return self['x', nick.lower()][0] + except: + return # entry does not exist + +filename = conf.supybot.directories.data.dirize("LastFM.db") + +# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79: diff --git a/README.md b/README.md index af9f8c0..529e95e 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,31 @@ supybot-lastfm ============== -Supybot LastFM Plugin \ No newline at end of file +A plugin for supybot that displays various information about a LastFM ID in IRC. + +Usage +----- + +Showing now playing information: + [09:53:33] $np + [09:53:34] KRF listened to “Behind Closed Doors” by Rise Against [The Sufferer & The Witness] more than 1 days ago + +Showing profile information: + [09:53:36] $profile + [09:53:37] KRF (realname: Kevin Funk) registered on May 28, 2006; 23 years old / m; Country: Germany; Tracks played: 32870 + +Showing recent tracks: + [10:29:16] $lastfm recenttracks + [10:29:17] KRF’s recenttracks: Zebrahead – The Set-Up, Good Charlotte – Girls & Boys, The All-American Rejects – Another Heart Calls, Angels & Airwaves – Do It For Me Now, Bowling For Soup – The Bitch Song, Yellowcard – Down On My Head, Sum 41 – Confusion And Frustration In Modern Times, Sum 41 – With Me, Goldfinger – Bro, The Offspring – Americana (with a total number of 11 entries) + +Showing help: + [10:28:29] $help lastfm + [10:28:29] (lastfm method [id]) — Lists LastFM info where method is in [friends, neighbours, profile, recenttracks, tags, topalbums, topartists, toptracks]. Set your LastFM ID with the set method (default is your current nick) or specify id to switch for one call. + + +Development +----------- + +Feel free to suggest enhancements, I'm happy to receive code contributions + +The files __init__.py and plugin.py provide some documentation. diff --git a/TODO.txt b/TODO.txt new file mode 100644 index 0000000..4028991 --- /dev/null +++ b/TODO.txt @@ -0,0 +1 @@ +* Use some Scrobbler 2.0 functions (needs a valid license key, though), e.g. now playing, etc. diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..984175a --- /dev/null +++ b/__init__.py @@ -0,0 +1,70 @@ +### +# -*- coding: utf-8 -*- +# +# Copyright (c) 2008, Kevin Funk +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are met: +# +# * Redistributions of source code must retain the above copyright notice, +# this list of conditions, and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright notice, +# this list of conditions, and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# * Neither the name of the author of this software nor the name of +# contributors to this software may be used to endorse or promote products +# derived from this software without specific prior written consent. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. + +### + +""" +Shows some information about a LastFM account (see: www.last.fm) +""" + +import supybot +import supybot.world as world + +# Use this for the version of this plugin. You may wish to put a CVS keyword +# in here if you're keeping the plugin in CVS or some similar system. +__version__ = "0.3rc" + +# Replace this with an appropriate author or supybot.Author instance. +__author__ = supybot.Author("Kevin Funk", "KRF", "krf@electrostorm.net") +# This is a dictionary mapping supybot.Author instances to lists of +# contributions. +__contributors__ = { + supybot.Author("Ilya Kuznetsov", "worklez", "worklez@gmail.com"): ["profile"], + supybot.Author("Pavel Dvořák", "czshadow", "czshadow@gmail.com"): + ["misc"], + } + +# This is a url where the most recent plugin package can be downloaded. +__url__ = 'http://supybot.com/Members/krf/LastFM/' + +import config +import plugin +reload(plugin) # In case we're being reloaded. +# Add more reloads here if you add third-party modules and want them to be +# reloaded when this plugin is reloaded. Don't forget to import them as well! + +if world.testing: + import test + +Class = plugin.Class +configure = config.configure + + +# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79: diff --git a/config.py b/config.py new file mode 100644 index 0000000..f1c06da --- /dev/null +++ b/config.py @@ -0,0 +1,51 @@ +### +# Copyright (c) 2008, Kevin Funk +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are met: +# +# * Redistributions of source code must retain the above copyright notice, +# this list of conditions, and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright notice, +# this list of conditions, and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# * Neither the name of the author of this software nor the name of +# contributors to this software may be used to endorse or promote products +# derived from this software without specific prior written consent. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. + +### + +import supybot.conf as conf +import supybot.registry as registry + +def configure(advanced): + # This will be called by supybot to configure this module. advanced is + # a bool that specifies whether the user identified himself as an advanced + # user or not. You should effect your configuration by manipulating the + # registry as appropriate. + from supybot.questions import expect, anything, something, yn + conf.registerPlugin('LastFM', True) + + +LastFM = conf.registerPlugin('LastFM') +# This is where your configuration variables (if any) should go. For example: +# conf.registerGlobalValue(LastFM, 'someConfigVariableName', +# registry.Boolean(False, """Help for someConfigVariableName.""")) +conf.registerChannelValue(LastFM, "maxResults", + registry.NonNegativeInteger(5, """Limits the number of results that will be + displayed in the channel.""")) + +# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79: diff --git a/plugin.py b/plugin.py new file mode 100644 index 0000000..cc549c3 --- /dev/null +++ b/plugin.py @@ -0,0 +1,194 @@ +### +# Copyright (c) 2006, Ilya Kuznetsov +# Copyright (c) 2008, Kevin Funk +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are met: +# +# * Redistributions of source code must retain the above copyright notice, +# this list of conditions, and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright notice, +# this list of conditions, and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# * Neither the name of the author of this software nor the name of +# contributors to this software may be used to endorse or promote products +# derived from this software without specific prior written consent. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. + +### + +import supybot.utils as utils +from supybot.commands import * +import supybot.conf as conf +import supybot.plugins as plugins +import supybot.ircutils as ircutils +import supybot.callbacks as callbacks +import supybot.world as world + +import urllib2 +from xml.dom import minidom +from time import time + +from LastFMDB import * + +class LastFM(callbacks.Plugin): + BASEURL = "http://ws.audioscrobbler.com/1.0/user" + APIKEY = "b25b959554ed76058ac220b7b2e0a026" # FIXME: Get own key + APIURL = "http://ws.audioscrobbler.com/2.0/?api_key=%s&" % APIKEY + + def __init__(self, irc): + self.__parent = super(LastFM, self) + self.__parent.__init__(irc) + self.db = LastFMDB(dbfilename) + world.flushers.append(self.db.flush) + + def die(self): + if self.db.flush in world.flushers: + world.flushers.remove(self.db.flush) + self.db.close() + self.__parent.die() + + def lastfm(self, irc, msg, args, method, optionalId): + """ [] + + Lists LastFM info where is in + [friends, neighbours, profile, recenttracks, tags, topalbums, + topartists, toptracks]. + Set your LastFM ID with the set method (default is your current nick) + or specify to switch for one call. + """ + + id = (optionalId or self.db.getId(msg.nick) or msg.nick) + channel = msg.args[0] + maxResults = self.registryValue("maxResults", channel) + method = method.lower() + + try: + f = urllib2.urlopen("%s/%s/%s.txt" % (self.BASEURL, id, method)) + except urllib2.HTTPError: + irc.error("Unknown ID (%s) or unknown method (%s)" + % (msg.nick, method)) + return + + + lines = f.read().split("\n") + content = map(lambda s: s.split(",")[-1], lines) + + irc.reply("%s's %s: %s (with a total number of %i entries)" + % (id, method, ", ".join(content[0:maxResults]), + len(content))) + + lastfm = wrap(lastfm, ["something", optional("something")]) + + def np(self, irc, msg, args, optionalId): + """[] + + Announces the now playing track of the specified LastFM ID. + Set your LastFM ID with the set method (default is your current nick) + or specify to switch for one call. + """ + + id = (optionalId or self.db.getId(msg.nick) or msg.nick) + + try: + f = urllib2.urlopen("%s&method=user.getrecenttracks&user=%s" + % (self.APIURL, id)) + except urllib2.HTTPError: + irc.error("Unknown ID (%s)" % id) + return + + xml = minidom.parse(f).getElementsByTagName("recenttracks")[0] + user = xml.getAttribute("user") + t = xml.getElementsByTagName("track")[0] # most recent track + isNowplaying = (t.getAttribute("nowplaying") == "true") + artist = t.getElementsByTagName("artist")[0].firstChild.data + track = t.getElementsByTagName("name")[0].firstChild.data + try: + album = "["+t.getElementsByTagName("album")[0].firstChild.data+"]" + except: + album = "" + + if isNowplaying: + irc.reply(('%s is listening to "%s" by %s %s' + % (user, track, artist, album)).encode("utf8")) + else: + time = int(t.getElementsByTagName("date")[0].getAttribute("uts")) + irc.reply(('%s listened to "%s" by %s %s more than %s' + % (user, track, artist, album, + self._formatTimeago(time))).encode("utf-8")) + + np = wrap(np, [optional("something")]) + + def set(self, irc, msg, args, newId): + """ + + Sets the LastFM ID for the caller and saves it in a database. + """ + + self.db.set(msg.nick, newId) + + irc.reply("LastFM ID changed.") + self.profile(irc, msg, args) + + set = wrap(set, ["something"]) + + def profile(self, irc, msg, args, optionalId): + """[] + + Prints the profile info for the specified LastFM ID. + Set your LastFM ID with the set method (default is your current nick) + or specify to switch for one call. + """ + + id = (optionalId or self.db.getId(msg.nick) or msg.nick) + + try: + f = urllib2.urlopen("%s/%s/profile.xml" % (self.BASEURL, id)) + except urllib2.HTTPError: + irc.error("Unknown user (%s)" % id) + return + + xml = minidom.parse(f).getElementsByTagName("profile")[0] + keys = "realname registered age gender country playcount".split() + profile = tuple([self._parse(xml, node) for node in keys]) + + irc.reply(("%s (realname: %s) registered on %s; age: %s / %s; \ +Country: %s; Tracks played: %s" % ((id,) + profile)).encode("utf8")) + + profile = wrap(profile, [optional("something")]) + + def _parse(self, data, node, exceptMsg="not specified"): + try: + return data.getElementsByTagName(node)[0].firstChild.data + except IndexError: + return exceptMsg + + def _formatTimeago(self, unixtime): + t = int(time()-unixtime) + if t/86400 > 0: + return "%i days ago" % (t/86400) + if t/3600 > 0: + return "%i hours ago" % (t/3600) + if t/60 > 0: + return "%i minutes ago" % (t/60) + if t > 0: + return "%i seconds ago" % (t) + +dbfilename = conf.supybot.directories.data.dirize("LastFM.db") + +Class = LastFM + + +# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79: diff --git a/test.py b/test.py new file mode 100644 index 0000000..ef070cf --- /dev/null +++ b/test.py @@ -0,0 +1,46 @@ +### +# Copyright (c) 2008, Kevin Funk +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are met: +# +# * Redistributions of source code must retain the above copyright notice, +# this list of conditions, and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright notice, +# this list of conditions, and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# * Neither the name of the author of this software nor the name of +# contributors to this software may be used to endorse or promote products +# derived from this software without specific prior written consent. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. + +### + +from supybot.test import * + +class LastFMTestCase(PluginTestCase): + plugins = ('LastFM',) + + def testLastfm(self): + print self.assertNotError("lastfm recenttracks") + print self.assertError("lastfm TESTEXCEPTION") + print self.assertNotError("lastfm profile czshadow") + print self.assertNotError("lastfm recenttracks czshadow") + print self.assertNotError("lastfm np krf") + print self.assertNotError("lastfm profile test") + print self.assertNotError("lastfm set nick") # test db + print self.assertNotError("lastfm set test") # test db unset + +# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79: