Add preliminary Python 3 support (some things are still quite broken and need testing)

This commit is contained in:
GLolol 2014-11-01 11:36:56 -07:00
parent 40fe73e544
commit e23895b94e
2 changed files with 24 additions and 27 deletions

View File

@ -54,8 +54,9 @@ __contributors__ = {
# This is a url where the most recent plugin package can be downloaded. # This is a url where the most recent plugin package can be downloaded.
__url__ = 'https://github.com/krf/supybot-lastfm' __url__ = 'https://github.com/krf/supybot-lastfm'
import config from . import config
import plugin from . import plugin
from imp import reload
reload(plugin) # In case we're being reloaded. reload(plugin) # In case we're being reloaded.
# Add more reloads here if you add third-party modules and want them to be # 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! # reloaded when this plugin is reloaded. Don't forget to import them as well!

View File

@ -29,6 +29,7 @@
### ###
from __future__ import unicode_literals
import supybot.utils as utils import supybot.utils as utils
from supybot.commands import * from supybot.commands import *
import supybot.conf as conf import supybot.conf as conf
@ -38,11 +39,10 @@ import supybot.callbacks as callbacks
import supybot.world as world import supybot.world as world
import supybot.log as log import supybot.log as log
import urllib2
from xml.dom import minidom from xml.dom import minidom
from time import time from time import time
from LastFMDB import * from .LastFMDB import *
class LastFMParser: class LastFMParser:
@ -110,15 +110,13 @@ class LastFM(callbacks.Plugin):
url = "%s/%s/%s.txt" % (self.APIURL_1_0, id, method) url = "%s/%s/%s.txt" % (self.APIURL_1_0, id, method)
try: try:
f = urllib2.urlopen(url) f = utils.web.getUrlFd(url)
except urllib2.HTTPError: except utils.web.Error:
irc.error("Unknown ID (%s) or unknown method (%s)" irc.error("Unknown ID (%s) or unknown method (%s)"
% (msg.nick, method)) % (msg.nick, method), Raise=True)
return
lines = f.readlines()
lines = f.read().split("\n") content = list(map(lambda s: s.split(",")[-1], lines))
content = map(lambda s: s.split(",")[-1], lines)
irc.reply("%s's %s: %s (with a total number of %i entries)" irc.reply("%s's %s: %s (with a total number of %i entries)"
% (id, method, ", ".join(content[0:maxResults]), % (id, method, ", ".join(content[0:maxResults]),
@ -139,8 +137,8 @@ class LastFM(callbacks.Plugin):
# see http://www.lastfm.de/api/show/user.getrecenttracks # see http://www.lastfm.de/api/show/user.getrecenttracks
url = "%s&method=user.getrecenttracks&user=%s" % (self.APIURL_2_0, id) url = "%s&method=user.getrecenttracks&user=%s" % (self.APIURL_2_0, id)
try: try:
f = urllib2.urlopen(url) f = utils.web.getUrlFd(url)
except urllib2.HTTPError: except utils.web.Error:
irc.error("Unknown ID (%s)" % id) irc.error("Unknown ID (%s)" % id)
return return
@ -148,12 +146,12 @@ class LastFM(callbacks.Plugin):
(user, isNowPlaying, artist, track, album, time) = parser.parseRecentTracks(f) (user, isNowPlaying, artist, track, album, time) = parser.parseRecentTracks(f)
albumStr = "[" + album + "]" if album else "" albumStr = "[" + album + "]" if album else ""
if isNowPlaying: if isNowPlaying:
irc.reply(('%s is listening to "%s" by %s %s' irc.reply('%s is listening to "%s" by %s %s'
% (user, track, artist, albumStr)).encode("utf8")) % (user, track, artist, albumStr))
else: else:
irc.reply(('%s listened to "%s" by %s %s more than %s' irc.reply('%s listened to "%s" by %s %s more than %s'
% (user, track, artist, albumStr, % (user, track, artist, albumStr,
self._formatTimeago(time))).encode("utf-8")) self._formatTimeago(time)))
np = wrap(nowPlaying, [optional("something")]) np = wrap(nowPlaying, [optional("something")])
@ -182,8 +180,8 @@ class LastFM(callbacks.Plugin):
url = "%s/%s/profile.xml" % (self.APIURL_1_0, id) url = "%s/%s/profile.xml" % (self.APIURL_1_0, id)
try: try:
f = urllib2.urlopen(url) f = utils.web.getUrlFd(url)
except urllib2.HTTPError: except utils.web.Error:
irc.error("Unknown user (%s)" % id) irc.error("Unknown user (%s)" % id)
return return
@ -191,8 +189,8 @@ class LastFM(callbacks.Plugin):
keys = "realname registered age gender country playcount".split() keys = "realname registered age gender country playcount".split()
profile = tuple([self._parse(xml, node) for node in keys]) profile = tuple([self._parse(xml, node) for node in keys])
irc.reply(("%s (realname: %s) registered on %s; age: %s / %s; \ irc.reply("%s (realname: %s) registered on %s; age: %s / %s; "
Country: %s; Tracks played: %s" % ((id,) + profile)).encode("utf8")) "Country: %s; Tracks played: %s" % ((id,) + profile))
profile = wrap(profile, [optional("something")]) profile = wrap(profile, [optional("something")])
@ -212,8 +210,8 @@ Country: %s; Tracks played: %s" % ((id,) + profile)).encode("utf8"))
self.APIURL_2_0, user1, user2, maxResults self.APIURL_2_0, user1, user2, maxResults
) )
try: try:
f = urllib2.urlopen(url) f = utils.web.getUrlFd(url)
except urllib2.HTTPError, e: except utils.web.Error as e:
irc.error("Failure: %s" % (e)) irc.error("Failure: %s" % (e))
return return
@ -224,10 +222,8 @@ Country: %s; Tracks played: %s" % ((id,) + profile)).encode("utf8"))
# Note: XPath would be really cool here... # Note: XPath would be really cool here...
artists = [el for el in resultNode.getElementsByTagName("artist")] artists = [el for el in resultNode.getElementsByTagName("artist")]
artistNames = [el.getElementsByTagName("name")[0].firstChild.data for el in artists] artistNames = [el.getElementsByTagName("name")[0].firstChild.data for el in artists]
irc.reply(("Result of comparison between %s and %s: score: %s, common artists: %s" \ irc.reply("Result of comparison between %s and %s: score: %s, common artists: %s" \
% (user1, user2, scoreStr, ", ".join(artistNames)) % (user1, user2, scoreStr, ", ".join(artistNames)))
).encode("utf-8")
)
compare = wrap(compareUsers, ["something", optional("something")]) compare = wrap(compareUsers, ["something", optional("something")])