From fe1ed955cdf6b1746b9036712e42d0565d053193 Mon Sep 17 00:00:00 2001 From: krateng Date: Mon, 27 Dec 2021 20:18:56 +0100 Subject: [PATCH] Added username/pw authentication for last.fm --- maloja/globalconf.py | 2 ++ maloja/thirdparty/__init__.py | 11 +++++++++++ maloja/thirdparty/lastfm.py | 23 ++++++++++++++++++++++- 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/maloja/globalconf.py b/maloja/globalconf.py index 1efd32c..8bddd2e 100644 --- a/maloja/globalconf.py +++ b/maloja/globalconf.py @@ -166,6 +166,8 @@ malojaconfig = Configuration( "lastfm_api_key":(tp.String(), "Last.fm API Key", None), "lastfm_api_secret":(tp.String(), "Last.fm API Secret", None), "lastfm_api_sk":(tp.String(), "Last.fm API Session Key", None), + "lastfm_username":(tp.String(), "Last.fm Username", None), + "lastfm_password":(tp.String(), "Last.fm Password", None), "spotify_api_id":(tp.String(), "Spotify API ID", None), "spotify_api_secret":(tp.String(), "Spotify API Secret", None), "audiodb_api_key":(tp.String(), "TheAudioDB API Key", None), diff --git a/maloja/thirdparty/__init__.py b/maloja/thirdparty/__init__.py index c3ec838..9cc2647 100644 --- a/maloja/thirdparty/__init__.py +++ b/maloja/thirdparty/__init__.py @@ -92,6 +92,17 @@ class GenericInterface: return True # per default. no authorization is necessary + # wrapper method + def request(self,url,data,responsetype): + response = urllib.request.urlopen( + url, + data=utf(data) + ) + responsedata = response.read() + if responsetype == "xml": + data = ElementTree.fromstring(responsedata) + return data + # proxy scrobbler class ProxyScrobbleInterface(GenericInterface,abstract=True): diff --git a/maloja/thirdparty/lastfm.py b/maloja/thirdparty/lastfm.py index d3add7b..80f3c75 100644 --- a/maloja/thirdparty/lastfm.py +++ b/maloja/thirdparty/lastfm.py @@ -1,6 +1,7 @@ from . import MetadataInterface, ProxyScrobbleInterface, utf import hashlib import urllib.parse, urllib.request +from doreah.logging import log class LastFM(MetadataInterface, ProxyScrobbleInterface): name = "LastFM" @@ -9,7 +10,9 @@ class LastFM(MetadataInterface, ProxyScrobbleInterface): settings = { "apikey":"LASTFM_API_KEY", "sk":"LASTFM_API_SK", - "secret":"LASTFM_API_SECRET" + "secret":"LASTFM_API_SECRET", + "username":"LASTFM_USERNAME", + "password":"LASTFM_PASSWORD" } proxyscrobble = { @@ -42,6 +45,24 @@ class LastFM(MetadataInterface, ProxyScrobbleInterface): "sk":self.settings["sk"] }) + def authorize(self): + try: + result = self.request( + self.proxyscrobble['scrobbleurl'], + self.query_compose({ + "method":"auth.getMobileSession", + "username":self.settings["username"], + "password":self.settings["password"], + "api_key":self.settings["apikey"] + }), + responsetype="xml" + ) + self.settings["sk"] = result.find("session").findtext("key") + except Exception as e: + pass + #log("Error while authenticating with LastFM: " + repr(e)) + + # creates signature and returns full query string def query_compose(self,parameters): m = hashlib.md5()