LastFM: prefer looking up account names; use ident@host only if the caller isn't registered

Closes #43.
This commit is contained in:
James Lu 2016-03-04 15:25:04 -08:00
parent 9c4ad9acda
commit f4f9bf6956

View File

@ -1,7 +1,7 @@
###
# Copyright (c) 2006, Ilya Kuznetsov
# Copyright (c) 2008,2012 Kevin Funk
# Copyright (c) 2014-2015 James Lu
# Copyright (c) 2014-2016 James Lu
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
@ -39,18 +39,25 @@ import supybot.ircutils as ircutils
import supybot.callbacks as callbacks
import supybot.world as world
import supybot.log as log
import supybot.ircdb as ircdb
import json
from datetime import datetime
import pickle
class LastFMDB():
"""Holds the LastFM IDs of all known nicks
"""
Holds the database LastFM IDs of all known LastFM IDs.
(This database is case insensitive and channel independent)
This stores users by their bot account first, falling back to their
ident@host if they are not logged in.
"""
def __init__(self, *args, **kwargs):
"""
Loads the existing database, creating a new one in memory if none
exists.
"""
self.db = {}
try:
with open(filename, 'rb') as f:
@ -60,6 +67,7 @@ class LastFMDB():
'a new one: %s', e)
def flush(self):
"""Exports the database to a file."""
try:
with open(filename, 'wb') as f:
pickle.dump(self.db, f, 2)
@ -67,15 +75,29 @@ class LastFMDB():
log.warning('LastFM: Unable to write database: %s', e)
def set(self, prefix, newId):
user = prefix.split('!', 1)[1]
"""Sets a user ID given the user's prefix."""
try: # Try to first look up the caller as a bot account.
userobj = ircdb.users.getUser(prefix)
except KeyError: # If that fails, store them by nick@host.
user = prefix.split('!', 1)[1]
else:
user = userobj.name
self.db[user] = newId
def get(self, prefix):
user = prefix.split('!', 1)[1]
try:
return self.db[user]
except:
return # entry does not exist
"""Sets a user ID given the user's prefix."""
try: # Try to first look up the caller as a bot account.
userobj = ircdb.users.getUser(prefix)
except KeyError: # If that fails, store them by nick@host.
user = prefix.split('!', 1)[1]
else:
user = userobj.name
# Automatically returns None if entry does not exist
return self.db.get(user)
class LastFM(callbacks.Plugin):
threaded = True