diff --git a/database.py b/database.py index 401ba19..adca465 100644 --- a/database.py +++ b/database.py @@ -6,6 +6,7 @@ import utilities from malojatime import register_scrobbletime, time_stamps, ranges from urihandler import uri_to_internal, internal_to_uri, compose_querystring import compliant_api +from external import proxy_scrobble # doreah toolkit from doreah.logging import log from doreah import tsv @@ -116,6 +117,9 @@ def createScrobble(artists,title,time,volatile=False): invalidate_caches() dblock.release() + proxy_scrobble(artists,title,time) + + return get_track_dict(TRACKS[obj.track]) diff --git a/external.py b/external.py index c9c51fc..6e5e6ad 100644 --- a/external.py +++ b/external.py @@ -3,6 +3,9 @@ import json import base64 from doreah.settings import get_settings from doreah.logging import log +import hashlib + +### PICTURES apis_artists = [] @@ -130,3 +133,43 @@ def api_request_track(track): pass 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() diff --git a/settings/default.ini b/settings/default.ini index 6471ed7..fdc7bf4 100644 --- a/settings/default.ini +++ b/settings/default.ini @@ -5,6 +5,7 @@ WEB_PORT = 42010 [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_SECRET = "ASK" FANARTTV_API_KEY = "ASK" SPOTIFY_API_ID = "ASK" SPOTIFY_API_SECRET = "ASK" diff --git a/website/proxy.html b/website/proxy.html new file mode 100644 index 0000000..7a20fbb --- /dev/null +++ b/website/proxy.html @@ -0,0 +1,46 @@ + + + + + + Maloja - Proxyscrobble + + + + + + + + + + + +
+
+
+

Proxyscrobble

+ +

Duplicate your scrobbles to another service. + Your API key is required to make any changes to the server:

+
+ + + + + KEY_STATUS_LASTFM + +
Last.fm
+ + + + diff --git a/website/proxy.py b/website/proxy.py new file mode 100644 index 0000000..01ed70d --- /dev/null +++ b/website/proxy.py @@ -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 = "No Last.fm key provided" + elif secret is None: + lastfm = "No Last.fm secret provided" + elif sessionkey is None and authenticated: + url = "http://www.last.fm/api/auth/?api_key=" + key + "&cb=" + lastfm = "
Connect
" + elif sessionkey is None: + lastfm = "Not active" + else: + + lastfm = "Account: " + name + "" + + + + return {"KEY_STATUS_LASTFM":lastfm},[]