1
0
mirror of https://github.com/krateng/maloja.git synced 2023-08-10 21:12:55 +03:00

Initial concept

This commit is contained in:
Krateng 2020-05-17 14:18:37 +02:00
parent 2cd5472751
commit 471a61f788
3 changed files with 60 additions and 0 deletions

14
maloja/thirdparty/common/lastfm.py vendored Normal file
View File

@ -0,0 +1,14 @@
import hashlib
import urllib
class LastFMInterface:
def query_compose(self,parameters):
m = hashlib.md5()
keys = sorted(str(k) for k in parameters)
m.update(self.utf("".join(str(k) + str(parameters[k]) for k in keys)))
m.update(self.utf(get_settings("LASTFM_API_SECRET")))
sig = m.hexdigest()
return urllib.parse.urlencode(parameters) + "&api_sig=" + sig
def utf(self,st):
return st.encode(encoding="UTF-8")

20
maloja/thirdparty/scrobble/__init__.py vendored Normal file
View File

@ -0,0 +1,20 @@
import xml.etree.ElementTree as ElementTree
import urllib
class ScrobbleInterface:
required_settings = []
activated_setting = ""
def active(self):
return (
all(get_settings(settingname) not in [None,"ASK"] for settingname in self.required_settings) and
get_settings(self.activated_setting)
)
def scrobble(self,artists,title,timestamp):
response = urllib.request.urlopen(self.scrobbleurl,data=self.postdata(artists,title,timestamp))
responsedata = response.read()
if self.response_type == "xml":
data = ElementTree.fromstring(responsedata)
return self.parse_response(data)

26
maloja/thirdparty/scrobble/lastfm.py vendored Normal file
View File

@ -0,0 +1,26 @@
from . import ScrobbleInterface
from doreah.settings import get_settings
class LastFMScrobbler(ScrobbleInterface,LastFMInterface):
scrobbleurl = "http://ws.audioscrobbler.com/2.0/"
required_settings = [
"LASTFM_API_KEY",
"LASTFM_API_SK",
"LASTFM_API_SECRET"
]
activated_setting = "SCROBBLE_LASTFM"
def parse_response(self,data):
return data.attrib.get("status") == "ok" and data.find("scrobbles").attrib.get("ignored") == "0"
def postdata(self,artists,title,timestamp):
return self.query_compose({
"method":"track.scrobble",
"artist[0]":", ".join(artists),
"track[0]":title,
"timestamp":timestamp,
"api_key":get_settings("LASTFM_API_KEY"),
"sk":get_settings("LASTFM_API_SK")
})