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

Added highly experimental last.fm proxy scrobbling

This commit is contained in:
Krateng 2019-06-24 15:43:38 +02:00
parent 8cbc7a8e56
commit b9ae163132
5 changed files with 147 additions and 0 deletions

View File

@ -6,6 +6,7 @@ import utilities
from malojatime import register_scrobbletime, time_stamps, ranges from malojatime import register_scrobbletime, time_stamps, ranges
from urihandler import uri_to_internal, internal_to_uri, compose_querystring from urihandler import uri_to_internal, internal_to_uri, compose_querystring
import compliant_api import compliant_api
from external import proxy_scrobble
# doreah toolkit # doreah toolkit
from doreah.logging import log from doreah.logging import log
from doreah import tsv from doreah import tsv
@ -116,6 +117,9 @@ def createScrobble(artists,title,time,volatile=False):
invalidate_caches() invalidate_caches()
dblock.release() dblock.release()
proxy_scrobble(artists,title,time)
return get_track_dict(TRACKS[obj.track]) return get_track_dict(TRACKS[obj.track])

View File

@ -3,6 +3,9 @@ import json
import base64 import base64
from doreah.settings import get_settings from doreah.settings import get_settings
from doreah.logging import log from doreah.logging import log
import hashlib
### PICTURES
apis_artists = [] apis_artists = []
@ -130,3 +133,43 @@ def api_request_track(track):
pass pass
return None return None
### SCROBBLING
# creates signature and returns full query string
def lfmbuild(parameters):
m = hashlib.md5()
keys = sorted(str(k) for k in parameters)
m.update(utf("".join(str(k) + str(parameters[k]) for k in keys)))
m.update(utf(get_settings("LASTFM_API_SECRET")))
sig = m.hexdigest()
return "&".join(str(k) + "=" + str(parameters[k]) for k in parameters) + "&api_sig=" + sig
def utf(st):
return st.encode(encoding="UTF-8")
apis_scrobble = []
if get_settings("LASTFM_API_SK") not in [None,"ASK"] and get_settings("LASTFM_API_SECRET") not in [None,"ASK"] and get_settings("LASTFM_API_KEY") not in [None,"ASK"]:
apis_scrobble.append({
"name":"LastFM",
"scrobbleurl":"http://ws.audioscrobbler.com/2.0/",
"requestbody":lambda artists,title,timestamp: lfmbuild({"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")})
})
def proxy_scrobble(artists,title,timestamp):
for api in apis_scrobble:
response = urllib.request.urlopen(api["scrobbleurl"],data=utf(api["requestbody"](artists,title,timestamp)))
xml = response.read()

View File

@ -5,6 +5,7 @@ WEB_PORT = 42010
[Third Party Services] [Third Party Services]
LASTFM_API_KEY = "ASK" # 'ASK' signifies that the user has not yet indicated to not use any key at all. LASTFM_API_KEY = "ASK" # 'ASK' signifies that the user has not yet indicated to not use any key at all.
LASTFM_API_SECRET = "ASK"
FANARTTV_API_KEY = "ASK" FANARTTV_API_KEY = "ASK"
SPOTIFY_API_ID = "ASK" SPOTIFY_API_ID = "ASK"
SPOTIFY_API_SECRET = "ASK" SPOTIFY_API_SECRET = "ASK"

46
website/proxy.html Normal file
View File

@ -0,0 +1,46 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Maloja - Proxyscrobble</title>
<script src="javascript/cookies.js"></script>
<script>
window.addEventListener("load",function(){
try {
document.getElementById("lastfmlink").href += window.location.href;
}
catch (e) {
}
});
</script>
</head>
<body>
<table class="top_info">
<tr>
<td class="image">
<div style="background-image:url('/favicon.png')"></div>
</td>
<td class="text">
<h1>Proxyscrobble</h1>
<p class="desc">Duplicate your scrobbles to another service.
Your API key is required to make any changes to the server: <input id='apikey' onchange='checkAPIkey()' style='width:300px;'/></p>
</td>
</tr>
</table>
<table class="list">
<tr>
<td>Last.fm</td>
KEY_STATUS_LASTFM
</tr>
</table>
</body>
</html>

53
website/proxy.py Normal file
View File

@ -0,0 +1,53 @@
from doreah.settings import get_settings, update_settings
import urllib.request
import hashlib
import xml.etree.ElementTree as ET
from bottle import redirect, request
from database import checkAPIkey
from external import lfmbuild
def instructions(keys):
authenticated = False
if "Cookie" in request.headers:
cookies = request.headers["Cookie"].split(";")
for c in cookies:
if c.strip().startswith("apikey="):
authenticated = checkAPIkey(c.strip()[7:])
if "token" in keys and authenticated:
token = keys.get("token")
parameters = {
"method":"auth.getSession",
"token":token,
"api_key":get_settings("LASTFM_API_KEY")
}
response = urllib.request.urlopen("http://ws.audioscrobbler.com/2.0/?" + lfmbuild(parameters))
xml = response.read()
data = ET.fromstring(xml)
if data.attrib.get("status") == "ok":
username = data.find("session").find("name").text
sessionkey = data.find("session").find("key").text
update_settings("settings/settings.ini",{"LASTFM_API_SK":sessionkey,"LASTFM_USERNAME":username},create_new=True)
return "/proxy"
else:
key,secret,sessionkey,name = get_settings("LASTFM_API_KEY","LASTFM_API_SECRET","LASTFM_API_SK","LASTFM_USERNAME")
if key is None:
lastfm = "<td>No Last.fm key provided</td>"
elif secret is None:
lastfm = "<td>No Last.fm secret provided</td>"
elif sessionkey is None and authenticated:
url = "http://www.last.fm/api/auth/?api_key=" + key + "&cb="
lastfm = "<td class='button'><a id='lastfmlink' href='" + url + "'><div>Connect</div></a></td>"
elif sessionkey is None:
lastfm = "<td>Not active</td>"
else:
lastfm = "<td>Account: " + name + "</td>"
return {"KEY_STATUS_LASTFM":lastfm},[]