Android 2.0.5: ListenBrainz and a scrobble queue that can't get stuck
- Connect ListenBrainz with a user token (checked), disconnect, send now - Listens carry MusicBrainz ids, ISRC, disc, tags, release artist read from the file - Last.fm: dead sessions sign out and keep the queue; rejected data is dropped; >14-day-old scrobbles dropped; recording MBID sent - Each service flushes on its own Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
parent
ec4ed9a73a
commit
c0dc2c68c3
8 changed files with 599 additions and 44 deletions
|
|
@ -1 +1 @@
|
|||
2.0.4
|
||||
2.0.5
|
||||
|
|
|
|||
|
|
@ -1 +1 @@
|
|||
6
|
||||
7
|
||||
|
|
|
|||
|
|
@ -23,6 +23,31 @@ public final class LastFm {
|
|||
void done(String str);
|
||||
}
|
||||
|
||||
/** Last.fm API error with its code (0 = network/HTTP problem). */
|
||||
public static final class LfmError extends Exception {
|
||||
public final int code;
|
||||
|
||||
LfmError(int code, String msg) {
|
||||
super(msg);
|
||||
this.code = code;
|
||||
}
|
||||
}
|
||||
|
||||
/** Session no longer valid (auth failed, invalid session key, unauthorized token). */
|
||||
static boolean authError(int code) {
|
||||
return code == 4 || code == 9 || code == 14;
|
||||
}
|
||||
|
||||
/** Worth retrying later (service offline, temporary error, rate limit, network). */
|
||||
static boolean retryError(int code) {
|
||||
return code == 0 || code == 11 || code == 16 || code == 29;
|
||||
}
|
||||
|
||||
/** Either scrobbler is on. */
|
||||
public static boolean anyOn() {
|
||||
return enabled() || ListenBrainz.enabled();
|
||||
}
|
||||
|
||||
public static boolean loggedIn() {
|
||||
return App.I.prefs.getStr(Prefs.LFM_SESSION, FrameBodyCOMM.DEFAULT).length() > 0;
|
||||
}
|
||||
|
|
@ -53,9 +78,24 @@ public final class LastFm {
|
|||
}
|
||||
treeMap.put("api_sig", md5(((Object) sb) + SECRET));
|
||||
treeMap.put("format", "json");
|
||||
JSONObject jSONObject = new JSONObject(Net.post(API, Net.query(treeMap)));
|
||||
String text;
|
||||
try {
|
||||
text = Net.post(API, Net.query(treeMap));
|
||||
} catch (Exception e) {
|
||||
throw new LfmError(0, "Last.fm: " + e.getMessage());
|
||||
}
|
||||
JSONObject jSONObject;
|
||||
try {
|
||||
jSONObject = new JSONObject(text);
|
||||
} catch (Exception e) {
|
||||
throw new LfmError(0, "Last.fm: bad reply");
|
||||
}
|
||||
if (jSONObject.has("error")) {
|
||||
throw new Exception(jSONObject.optString("message", "Last.fm error " + jSONObject.optInt("error")));
|
||||
int code = jSONObject.optInt("error");
|
||||
if (z && authError(code)) {
|
||||
sessionExpired();
|
||||
}
|
||||
throw new LfmError(code, jSONObject.optString("message", "Last.fm error " + code));
|
||||
}
|
||||
return jSONObject;
|
||||
}
|
||||
|
|
@ -90,6 +130,18 @@ public final class LastFm {
|
|||
public static void logout() {
|
||||
App.I.prefs.setStr(Prefs.LFM_SESSION, FrameBodyCOMM.DEFAULT);
|
||||
App.I.prefs.setStr(Prefs.LFM_USER, FrameBodyCOMM.DEFAULT);
|
||||
purge("lastfm");
|
||||
}
|
||||
|
||||
/** Last.fm dropped our session: forget it but keep the queue for the next sign-in. */
|
||||
static void sessionExpired() {
|
||||
App.I.prefs.setStr(Prefs.LFM_SESSION, FrameBodyCOMM.DEFAULT);
|
||||
App.ui(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
MainActivity.toast("Last.fm signed Mulberry out. Sign in again in Settings; scrobbles are kept.");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
static TreeMap<String, String> trackParams(Song song, String str) {
|
||||
|
|
@ -108,10 +160,18 @@ public final class LastFm {
|
|||
if (song.track > 0) {
|
||||
treeMap.put("trackNumber" + str, String.valueOf(song.track));
|
||||
}
|
||||
String mbid = ListenBrainz.fileInfo(song).optString("recording_mbid", "");
|
||||
if (mbid.length() > 0) {
|
||||
treeMap.put("mbid" + str, mbid);
|
||||
}
|
||||
if (str.length() > 0) {
|
||||
treeMap.put("chosenByUser" + str, song.isStream() ? "0" : "1");
|
||||
}
|
||||
return treeMap;
|
||||
}
|
||||
|
||||
public static void nowPlaying(final Song song) {
|
||||
ListenBrainz.nowPlaying(song);
|
||||
if (enabled() && song != null && song.displayArtist().length() != 0) {
|
||||
App.IO.execute(new Runnable() { // from class: com.mulberry.player.LastFm.2
|
||||
@Override // java.lang.Runnable
|
||||
|
|
@ -149,9 +209,9 @@ public final class LastFm {
|
|||
}
|
||||
|
||||
public static void scrobble(final Song song, final long j) {
|
||||
if (enabled() && song.displayArtist().length() != 0) {
|
||||
App.IO.execute(new Runnable() { // from class: com.mulberry.player.LastFm.3
|
||||
@Override // java.lang.Runnable
|
||||
if (anyOn() && song.displayArtist().length() != 0) {
|
||||
App.IO.execute(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
LastFm.queueScrobble(song, j);
|
||||
}
|
||||
|
|
@ -161,11 +221,16 @@ public final class LastFm {
|
|||
|
||||
static void queueScrobble(Song song, long j) {
|
||||
try {
|
||||
JSONObject json = song.toJson();
|
||||
json.put("ts", j);
|
||||
synchronized (LastFm.class) {
|
||||
JSONArray queue = readQueue();
|
||||
queue.put(json);
|
||||
for (String svc : new String[]{"lastfm", "listenbrainz"}) {
|
||||
if ("lastfm".equals(svc) ? enabled() : ListenBrainz.enabled()) {
|
||||
JSONObject json = song.toJson();
|
||||
json.put("ts", j);
|
||||
json.put("svc", svc);
|
||||
queue.put(json);
|
||||
}
|
||||
}
|
||||
writeQueue(queue);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
|
|
@ -173,50 +238,155 @@ public final class LastFm {
|
|||
flush();
|
||||
}
|
||||
|
||||
static String svcOf(JSONObject o) {
|
||||
return o.optString("svc", "lastfm");
|
||||
}
|
||||
|
||||
/** Drop the queue of a service the user signed out of. */
|
||||
public static void purge(String svc) {
|
||||
synchronized (LastFm.class) {
|
||||
JSONArray q = readQueue();
|
||||
JSONArray keep = new JSONArray();
|
||||
for (int i = 0; i < q.length(); i++) {
|
||||
JSONObject o = q.optJSONObject(i);
|
||||
if (o != null && !svc.equals(svcOf(o))) {
|
||||
keep.put(o);
|
||||
}
|
||||
}
|
||||
writeQueue(keep);
|
||||
}
|
||||
}
|
||||
|
||||
/** Remove sent (or rejected) entries, matched by service + timestamp + uri. */
|
||||
static void removeDone(List<JSONObject> done) {
|
||||
if (done.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
java.util.Set<String> keys = new java.util.HashSet<>();
|
||||
for (JSONObject o : done) {
|
||||
keys.add(svcOf(o) + "|" + o.optLong("ts") + "|" + o.optString("uri"));
|
||||
}
|
||||
synchronized (LastFm.class) {
|
||||
JSONArray q = readQueue();
|
||||
JSONArray keep = new JSONArray();
|
||||
for (int i = 0; i < q.length(); i++) {
|
||||
JSONObject o = q.optJSONObject(i);
|
||||
if (o != null && !keys.contains(svcOf(o) + "|" + o.optLong("ts") + "|" + o.optString("uri"))) {
|
||||
keep.put(o);
|
||||
}
|
||||
}
|
||||
writeQueue(keep);
|
||||
}
|
||||
}
|
||||
|
||||
/** Send queued scrobbles. Each service goes on its own, so one failing never blocks the other. */
|
||||
public static void flush() {
|
||||
if (enabled()) {
|
||||
App.IO.execute(new Runnable() { // from class: com.mulberry.player.LastFm.4
|
||||
@Override // java.lang.Runnable
|
||||
public void run() {
|
||||
JSONArray queue;
|
||||
synchronized (LastFm.class) {
|
||||
queue = LastFm.readQueue();
|
||||
if (!anyOn()) {
|
||||
return;
|
||||
}
|
||||
App.IO.execute(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
JSONArray queue;
|
||||
synchronized (LastFm.class) {
|
||||
queue = LastFm.readQueue();
|
||||
}
|
||||
List<JSONObject> lfm = new ArrayList<>(), lb = new ArrayList<>(), done = new ArrayList<>();
|
||||
long now = System.currentTimeMillis() / 1000;
|
||||
for (int i = 0; i < queue.length(); i++) {
|
||||
JSONObject o = queue.optJSONObject(i);
|
||||
if (o == null) {
|
||||
continue;
|
||||
}
|
||||
if (queue.length() != 0) {
|
||||
if ("listenbrainz".equals(svcOf(o))) {
|
||||
lb.add(o);
|
||||
} else if (now - o.optLong("ts", now) > 14 * 86400) {
|
||||
done.add(o); // Last.fm refuses scrobbles older than 14 days
|
||||
} else {
|
||||
lfm.add(o);
|
||||
}
|
||||
}
|
||||
if (enabled()) {
|
||||
for (int s = 0; s < lfm.size(); s += 50) {
|
||||
List<JSONObject> chunk = lfm.subList(s, Math.min(s + 50, lfm.size()));
|
||||
TreeMap<String, String> params = new TreeMap<>();
|
||||
for (int i = 0; i < chunk.size(); i++) {
|
||||
params.putAll(trackParams(Song.fromJson(chunk.get(i)), "[" + i + "]"));
|
||||
params.put("timestamp[" + i + "]", String.valueOf(chunk.get(i).optLong("ts")));
|
||||
}
|
||||
try {
|
||||
int iMin = Math.min(50, queue.length());
|
||||
TreeMap treeMap = new TreeMap();
|
||||
for (int i = 0; i < iMin; i++) {
|
||||
JSONObject jSONObject = queue.getJSONObject(i);
|
||||
treeMap.putAll(LastFm.trackParams(Song.fromJson(jSONObject), "[" + i + "]"));
|
||||
treeMap.put("timestamp[" + i + "]", String.valueOf(jSONObject.getLong("ts")));
|
||||
}
|
||||
LastFm.call("track.scrobble", treeMap, true);
|
||||
synchronized (LastFm.class) {
|
||||
JSONArray queue2 = LastFm.readQueue();
|
||||
JSONArray jSONArray = new JSONArray();
|
||||
while (iMin < queue2.length()) {
|
||||
jSONArray.put(queue2.get(iMin));
|
||||
iMin++;
|
||||
}
|
||||
LastFm.writeQueue(jSONArray);
|
||||
call("track.scrobble", params, true);
|
||||
done.addAll(chunk);
|
||||
} catch (LfmError e) {
|
||||
if (authError(e.code) || retryError(e.code)) {
|
||||
break;
|
||||
}
|
||||
done.addAll(chunk); // Last.fm rejected the data itself; retrying would loop forever
|
||||
} catch (Exception e) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
if (ListenBrainz.enabled()) {
|
||||
outer:
|
||||
for (int s = 0; s < lb.size(); s += 100) {
|
||||
List<JSONObject> chunk = lb.subList(s, Math.min(s + 100, lb.size()));
|
||||
JSONArray payload = new JSONArray();
|
||||
try {
|
||||
for (JSONObject o : chunk) {
|
||||
payload.put(ListenBrainz.payload(Song.fromJson(o), o.optLong("ts")));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
break;
|
||||
}
|
||||
try {
|
||||
ListenBrainz.submit(payload.length() > 1 ? "import" : "single", payload);
|
||||
done.addAll(chunk);
|
||||
} catch (ListenBrainz.HttpError e) {
|
||||
if (e.status != 400) {
|
||||
break;
|
||||
}
|
||||
// One bad listen spoils a batch: send them one by one and drop the bad ones.
|
||||
for (int i = 0; i < chunk.size(); i++) {
|
||||
try {
|
||||
ListenBrainz.submit("single", new JSONArray().put(payload.opt(i)));
|
||||
done.add(chunk.get(i));
|
||||
} catch (ListenBrainz.HttpError e2) {
|
||||
if (e2.status != 400) {
|
||||
break outer;
|
||||
}
|
||||
done.add(chunk.get(i));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
removeDone(done);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static int pending() {
|
||||
return readQueue().length();
|
||||
}
|
||||
|
||||
public static int pending(String svc) {
|
||||
JSONArray q = readQueue();
|
||||
int n = 0;
|
||||
for (int i = 0; i < q.length(); i++) {
|
||||
JSONObject o = q.optJSONObject(i);
|
||||
if (o != null && svc.equals(svcOf(o))) {
|
||||
n++;
|
||||
}
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
public static void love(final Song song, final boolean z) {
|
||||
ListenBrainz.love(song, z);
|
||||
if (enabled()) {
|
||||
App.IO.execute(new Runnable() { // from class: com.mulberry.player.LastFm.5
|
||||
@Override // java.lang.Runnable
|
||||
App.IO.execute(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
TreeMap treeMap = new TreeMap();
|
||||
treeMap.put("artist", song.displayArtist());
|
||||
|
|
|
|||
317
android/src/com/mulberry/player/ListenBrainz.java
Normal file
317
android/src/com/mulberry/player/ListenBrainz.java
Normal file
|
|
@ -0,0 +1,317 @@
|
|||
package com.mulberry.player;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.io.RandomAccessFile;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import org.jaudiotagger.audio.AudioFileIO;
|
||||
import org.jaudiotagger.tag.FieldKey;
|
||||
import org.jaudiotagger.tag.Tag;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
|
||||
/** ListenBrainz listens, "now playing" and loves, plus the MusicBrainz ids both scrobblers send (desktop scrobble.rs). */
|
||||
public final class ListenBrainz {
|
||||
static final String API = "https://api.listenbrainz.org/1/";
|
||||
|
||||
/** HTTP failure with its status code (0 = no connection). */
|
||||
public static final class HttpError extends Exception {
|
||||
public final int status;
|
||||
|
||||
HttpError(int status, String msg) {
|
||||
super(msg);
|
||||
this.status = status;
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean connected() {
|
||||
return App.I.prefs.getStr(Prefs.LB_TOKEN, "").length() > 0;
|
||||
}
|
||||
|
||||
public static boolean enabled() {
|
||||
return connected() && App.I.prefs.getBool(Prefs.LB_ENABLED, true);
|
||||
}
|
||||
|
||||
static JSONObject http(String method, String path, String token, JSONObject body) throws HttpError {
|
||||
HttpURLConnection c = null;
|
||||
try {
|
||||
c = (HttpURLConnection) new URL(API + path).openConnection();
|
||||
c.setConnectTimeout(10000);
|
||||
c.setReadTimeout(20000);
|
||||
c.setRequestMethod(method);
|
||||
c.setRequestProperty("User-Agent", Net.UA);
|
||||
c.setRequestProperty("Authorization", "Token " + token);
|
||||
if (body != null) {
|
||||
c.setDoOutput(true);
|
||||
c.setRequestProperty("Content-Type", "application/json; charset=utf-8");
|
||||
OutputStream o = c.getOutputStream();
|
||||
o.write(body.toString().getBytes("UTF-8"));
|
||||
o.close();
|
||||
}
|
||||
int st = c.getResponseCode();
|
||||
InputStream in = st >= 400 ? c.getErrorStream() : c.getInputStream();
|
||||
String text = in == null ? "" : new String(Net.readAll(in), "UTF-8");
|
||||
JSONObject res;
|
||||
try {
|
||||
res = new JSONObject(text);
|
||||
} catch (Exception e) {
|
||||
res = new JSONObject();
|
||||
}
|
||||
if (st >= 400) {
|
||||
throw new HttpError(st, "ListenBrainz: " + res.optString("error", "HTTP " + st));
|
||||
}
|
||||
return res;
|
||||
} catch (HttpError e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new HttpError(0, "ListenBrainz: " + e.getMessage());
|
||||
} finally {
|
||||
if (c != null) {
|
||||
c.disconnect();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Checks a token; gives the user name. */
|
||||
public static String validate(String token) throws Exception {
|
||||
JSONObject r = http("GET", "validate-token", token.trim(), null);
|
||||
if (!r.optBoolean("valid", false)) {
|
||||
throw new Exception(r.optString("message", "Token not valid"));
|
||||
}
|
||||
return r.optString("user_name", "");
|
||||
}
|
||||
|
||||
public static void connect(final String token, final LastFm.Cb cb) {
|
||||
App.IO.execute(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
String err = null;
|
||||
try {
|
||||
String name = validate(token);
|
||||
App.I.prefs.setStr(Prefs.LB_TOKEN, token.trim());
|
||||
App.I.prefs.setStr(Prefs.LB_USER, name);
|
||||
App.I.prefs.setBool(Prefs.LB_ENABLED, true);
|
||||
} catch (Exception e) {
|
||||
err = e.getMessage();
|
||||
}
|
||||
final String msg = err;
|
||||
App.ui(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
cb.done(msg);
|
||||
}
|
||||
});
|
||||
if (msg == null) {
|
||||
LastFm.flush();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static void disconnect() {
|
||||
App.I.prefs.setStr(Prefs.LB_TOKEN, "");
|
||||
App.I.prefs.setStr(Prefs.LB_USER, "");
|
||||
App.I.prefs.setBool(Prefs.LB_ENABLED, false);
|
||||
LastFm.purge("listenbrainz");
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- ids from the file's tags
|
||||
|
||||
static String clean(String s) {
|
||||
return s == null ? "" : s.trim();
|
||||
}
|
||||
|
||||
static void putIds(JSONObject out, String name, String raw, boolean many) throws Exception {
|
||||
JSONArray arr = new JSONArray();
|
||||
for (String p : clean(raw).split("[/;,\\u0000]")) {
|
||||
p = p.trim();
|
||||
if (p.length() == 36) {
|
||||
arr.put(p);
|
||||
}
|
||||
}
|
||||
if (arr.length() > 0) {
|
||||
out.put(name, many ? arr : arr.get(0));
|
||||
}
|
||||
}
|
||||
|
||||
/** MusicBrainz ids, ISRC, disc and genre from a local file, named like ListenBrainz additional_info. */
|
||||
public static JSONObject fileInfo(Song s) {
|
||||
JSONObject out = new JSONObject();
|
||||
try {
|
||||
if (s.isStream() || s.path == null || s.path.length() == 0) {
|
||||
return out;
|
||||
}
|
||||
File f = new File(s.path);
|
||||
if (!f.canRead()) {
|
||||
return out;
|
||||
}
|
||||
String rec, rel, rg, rtrack, art, relart, work, isrc, disc, genre;
|
||||
String ext = TagCore.extOf(f);
|
||||
if (TagCore.isOpus(f, ext)) {
|
||||
java.util.Map<String, String> v = new java.util.HashMap<>();
|
||||
RandomAccessFile raf = new RandomAccessFile(f, "r");
|
||||
try {
|
||||
for (String item : OggOpus.parse(OggOpus.layout(raf).tags).items) {
|
||||
int i = item.indexOf('=');
|
||||
if (i > 0) {
|
||||
String k = item.substring(0, i).toUpperCase();
|
||||
String old = v.get(k);
|
||||
v.put(k, old == null ? item.substring(i + 1) : old + "/" + item.substring(i + 1));
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
raf.close();
|
||||
}
|
||||
rec = v.get("MUSICBRAINZ_TRACKID");
|
||||
rel = v.get("MUSICBRAINZ_ALBUMID");
|
||||
rg = v.get("MUSICBRAINZ_RELEASEGROUPID");
|
||||
rtrack = v.get("MUSICBRAINZ_RELEASETRACKID");
|
||||
art = v.get("MUSICBRAINZ_ARTISTID");
|
||||
relart = v.get("MUSICBRAINZ_ALBUMARTISTID");
|
||||
work = v.get("MUSICBRAINZ_WORKID");
|
||||
isrc = v.get("ISRC");
|
||||
disc = v.get("DISCNUMBER");
|
||||
genre = v.get("GENRE");
|
||||
} else {
|
||||
Tag t = AudioFileIO.read(f).getTag();
|
||||
if (t == null) {
|
||||
return out;
|
||||
}
|
||||
rec = TagCore.first(t, FieldKey.MUSICBRAINZ_TRACK_ID);
|
||||
rel = TagCore.first(t, FieldKey.MUSICBRAINZ_RELEASEID);
|
||||
rg = TagCore.first(t, FieldKey.MUSICBRAINZ_RELEASE_GROUP_ID);
|
||||
rtrack = null;
|
||||
art = TagCore.first(t, FieldKey.MUSICBRAINZ_ARTISTID);
|
||||
relart = TagCore.first(t, FieldKey.MUSICBRAINZ_RELEASEARTISTID);
|
||||
work = TagCore.first(t, FieldKey.MUSICBRAINZ_WORK_ID);
|
||||
isrc = TagCore.first(t, FieldKey.ISRC);
|
||||
disc = TagCore.first(t, FieldKey.DISC_NO);
|
||||
genre = TagCore.first(t, FieldKey.GENRE);
|
||||
}
|
||||
putIds(out, "recording_mbid", rec, false);
|
||||
putIds(out, "release_mbid", rel, false);
|
||||
putIds(out, "release_group_mbid", rg, false);
|
||||
putIds(out, "track_mbid", rtrack, false);
|
||||
putIds(out, "artist_mbids", art, true);
|
||||
putIds(out, "release_artist_mbids", relart, true);
|
||||
putIds(out, "work_mbids", work, true);
|
||||
if (clean(isrc).length() > 0) {
|
||||
out.put("isrc", clean(isrc));
|
||||
}
|
||||
String d = clean(disc).split("/")[0];
|
||||
if (d.matches("\\d+") && Integer.parseInt(d) > 0) {
|
||||
out.put("discnumber", Integer.parseInt(d));
|
||||
}
|
||||
JSONArray tags = new JSONArray();
|
||||
for (String g : clean(genre).split("[;/,]")) {
|
||||
if (g.trim().length() > 0) {
|
||||
tags.put(g.trim());
|
||||
}
|
||||
}
|
||||
if (tags.length() > 0) {
|
||||
out.put("tags", tags);
|
||||
}
|
||||
} catch (Throwable e) {
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
static String version() {
|
||||
try {
|
||||
return App.I.getPackageManager().getPackageInfo(App.I.getPackageName(), 0).versionName;
|
||||
} catch (Exception e) {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
/** One listen: https://listenbrainz.readthedocs.io/en/latest/users/json.html */
|
||||
public static JSONObject payload(Song s, long ts) throws Exception {
|
||||
JSONObject info = fileInfo(s);
|
||||
String v = version();
|
||||
info.put("media_player", "Mulberry Android");
|
||||
info.put("media_player_version", v);
|
||||
info.put("submission_client", "Mulberry Android");
|
||||
info.put("submission_client_version", v);
|
||||
if (s.duration > 0) {
|
||||
info.put("duration_ms", s.duration);
|
||||
}
|
||||
if (s.track > 0) {
|
||||
info.put("tracknumber", s.track);
|
||||
}
|
||||
if (s.albumArtist.length() > 0) {
|
||||
info.put("release_artist_name", s.albumArtist);
|
||||
}
|
||||
if (s.isStream()) {
|
||||
info.put("origin_url", s.uri);
|
||||
String host = s.uri.replaceFirst("^[a-z]+://", "").split("[/:?]")[0].replaceFirst("^www\\.", "");
|
||||
if (host.length() > 0) {
|
||||
info.put("music_service", host);
|
||||
}
|
||||
}
|
||||
JSONObject meta = new JSONObject();
|
||||
meta.put("artist_name", s.displayArtist());
|
||||
meta.put("track_name", s.displayTitle());
|
||||
if (s.album.length() > 0 && !s.isStream()) {
|
||||
meta.put("release_name", s.album);
|
||||
}
|
||||
meta.put("additional_info", info);
|
||||
JSONObject p = new JSONObject();
|
||||
p.put("track_metadata", meta);
|
||||
if (ts > 0) {
|
||||
p.put("listened_at", ts);
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
public static void submit(String type, JSONArray payload) throws HttpError {
|
||||
try {
|
||||
JSONObject body = new JSONObject();
|
||||
body.put("listen_type", type);
|
||||
body.put("payload", payload);
|
||||
http("POST", "submit-listens", App.I.prefs.getStr(Prefs.LB_TOKEN, ""), body);
|
||||
} catch (HttpError e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new HttpError(0, e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public static void nowPlaying(final Song song) {
|
||||
if (!enabled() || song == null || song.displayArtist().length() == 0) {
|
||||
return;
|
||||
}
|
||||
App.IO.execute(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
submit("playing_now", new JSONArray().put(payload(song, 0)));
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static void love(final Song song, final boolean on) {
|
||||
if (!enabled()) {
|
||||
return;
|
||||
}
|
||||
App.IO.execute(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
String mbid = fileInfo(song).optString("recording_mbid", "");
|
||||
if (mbid.length() == 0) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
JSONObject b = new JSONObject();
|
||||
b.put("recording_mbid", mbid);
|
||||
b.put("score", on ? 1 : 0);
|
||||
http("POST", "feedback/recording-feedback", App.I.prefs.getStr(Prefs.LB_TOKEN, ""), b);
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
@ -1753,7 +1753,7 @@ public class MainActivity extends Activity implements MusicService.Listener, Lib
|
|||
Song songCurrent = this.svc.current();
|
||||
boolean z = !PlayStats.fav(songCurrent);
|
||||
PlayStats.setFav(songCurrent, z);
|
||||
if (z && LastFm.enabled()) {
|
||||
if (z && LastFm.anyOn()) {
|
||||
LastFm.love(songCurrent, true);
|
||||
}
|
||||
this.svc.refreshUi();
|
||||
|
|
@ -1795,12 +1795,12 @@ public class MainActivity extends Activity implements MusicService.Listener, Lib
|
|||
MainActivity.this.sleepTimer();
|
||||
}
|
||||
}));
|
||||
if (LastFm.enabled()) {
|
||||
arrayList.add(new Lists.Item(R.drawable.ic_favorite, "Love on Last.fm", new Runnable() { // from class: com.mulberry.player.MainActivity.85
|
||||
if (LastFm.anyOn()) {
|
||||
arrayList.add(new Lists.Item(R.drawable.ic_favorite, LastFm.enabled() ? "Love on Last.fm" : "Love on ListenBrainz", new Runnable() { // from class: com.mulberry.player.MainActivity.85
|
||||
@Override // java.lang.Runnable
|
||||
public void run() {
|
||||
LastFm.love(songCurrent, true);
|
||||
MainActivity.toast("Loved on Last.fm");
|
||||
MainActivity.toast("Loved");
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1263,7 +1263,7 @@ public class MusicService extends MediaBrowserService implements AudioManager.On
|
|||
this.countedPlay = false;
|
||||
this.startedSec = System.currentTimeMillis() / 1000;
|
||||
this.lastTick = SystemClock.elapsedRealtime();
|
||||
if (song == null || !LastFm.enabled()) {
|
||||
if (song == null || !LastFm.anyOn()) {
|
||||
return;
|
||||
}
|
||||
LastFm.nowPlaying(song);
|
||||
|
|
|
|||
|
|
@ -14,6 +14,9 @@ public class Prefs {
|
|||
public static final String LFM_ENABLED = "lfm_enabled";
|
||||
public static final String LFM_SESSION = "lfm_session";
|
||||
public static final String LFM_USER = "lfm_user";
|
||||
public static final String LB_ENABLED = "lb_enabled";
|
||||
public static final String LB_TOKEN = "lb_token";
|
||||
public static final String LB_USER = "lb_user";
|
||||
public static final String LIB_TAB = "lib_tab";
|
||||
public static final String LOW_POWER = "low_power";
|
||||
public static final String LYRICS_PLAIN = "lyrics_plain";
|
||||
|
|
|
|||
|
|
@ -200,9 +200,9 @@ public class SettingsPage extends ScrollView {
|
|||
section("Last.fm");
|
||||
LinearLayout linearLayoutCard4 = card();
|
||||
if (LastFm.loggedIn()) {
|
||||
info(linearLayoutCard4, R.drawable.ic_person, "Signed in as " + this.p.getStr(Prefs.LFM_USER, FrameBodyCOMM.DEFAULT), LastFm.pending() > 0 ? LastFm.pending() + " scrobbles waiting to be sent" : "All scrobbles sent");
|
||||
info(linearLayoutCard4, R.drawable.ic_person, "Signed in as " + this.p.getStr(Prefs.LFM_USER, FrameBodyCOMM.DEFAULT), LastFm.pending("lastfm") > 0 ? LastFm.pending("lastfm") + " scrobbles waiting to be sent" : "All scrobbles sent");
|
||||
toggle(linearLayoutCard4, R.drawable.ic_scrobble, "Scrobble", "Send “now playing” and scrobbles (after half the song or 4 minutes)", Prefs.LFM_ENABLED, true, null);
|
||||
if (LastFm.pending() > 0) {
|
||||
if (LastFm.pending("lastfm") > 0) {
|
||||
button(linearLayoutCard4, R.drawable.ic_upload, "Send waiting scrobbles now", null, new Runnable() { // from class: com.mulberry.player.SettingsPage.13
|
||||
@Override // java.lang.Runnable
|
||||
public void run() {
|
||||
|
|
@ -228,6 +228,38 @@ public class SettingsPage extends ScrollView {
|
|||
});
|
||||
}
|
||||
toggle(linearLayoutCard4, R.drawable.ic_radio, "Tell scrobbler apps", "Pano Scrobbler reads the media session automatically; Simple Last.fm Scrobbler and Scrobble Droid get broadcasts. Found: " + ScrobbleBridge.detected(this.a), Prefs.SCROBBLER_APPS, true, null);
|
||||
section("ListenBrainz");
|
||||
LinearLayout lbCard = card();
|
||||
if (ListenBrainz.connected()) {
|
||||
int lbPending = LastFm.pending("listenbrainz");
|
||||
String lbUser = this.p.getStr(Prefs.LB_USER, FrameBodyCOMM.DEFAULT);
|
||||
info(lbCard, R.drawable.ic_person, lbUser.length() > 0 ? "Connected as " + lbUser : "Connected", lbPending > 0 ? lbPending + " listens waiting to be sent" : "All listens sent");
|
||||
toggle(lbCard, R.drawable.ic_scrobble, "Submit listens", "Send “now playing” and listens with MusicBrainz ids, ISRC and more", Prefs.LB_ENABLED, true, null);
|
||||
if (lbPending > 0) {
|
||||
button(lbCard, R.drawable.ic_upload, "Send waiting listens now", null, new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
LastFm.flush();
|
||||
MainActivity.toast("Sending…");
|
||||
}
|
||||
});
|
||||
}
|
||||
button(lbCard, R.drawable.ic_close, "Disconnect", null, new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
ListenBrainz.disconnect();
|
||||
SettingsPage.this.refresh();
|
||||
}
|
||||
});
|
||||
} else {
|
||||
info(lbCard, R.drawable.ic_scrobble, "Not connected", "Paste your user token from listenbrainz.org/settings");
|
||||
button(lbCard, R.drawable.ic_person, "Connect ListenBrainz", null, new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
SettingsPage.this.lbConnect();
|
||||
}
|
||||
});
|
||||
}
|
||||
section("Permissions & background");
|
||||
LinearLayout linearLayoutCard5 = card();
|
||||
button(linearLayoutCard5, R.drawable.ic_library_music, "Music access", this.a.hasAudioPermission() ? "Allowed" : "Not allowed — tap to allow", new Runnable() { // from class: com.mulberry.player.SettingsPage.16
|
||||
|
|
@ -362,6 +394,39 @@ public class SettingsPage extends ScrollView {
|
|||
alertDialogArr[0] = Ui.show(Ui.dialog(this.a).setTitle("Pick a colour").setView(linearLayoutCol).setNegativeButton("Cancel", (DialogInterface.OnClickListener) null));
|
||||
}
|
||||
|
||||
void lbConnect() {
|
||||
LinearLayout col = Ui.col(this.a);
|
||||
col.setPadding(Ui.dp(this.a, 22.0f), Ui.dp(this.a, 8.0f), Ui.dp(this.a, 22.0f), 0);
|
||||
final EditText field = Ui.field(this.a, "User token", FrameBodyCOMM.DEFAULT);
|
||||
field.setSingleLine(true);
|
||||
field.setInputType(129);
|
||||
col.addView(field);
|
||||
TextView hint = Ui.text(this.a, "Find it at listenbrainz.org/settings (“User token”).", 12.0f, 12, false);
|
||||
hint.setPadding(0, Ui.dp(this.a, 10.0f), 0, 0);
|
||||
col.addView(hint);
|
||||
Ui.show(Ui.dialog(this.a).setTitle("Connect ListenBrainz").setView(col).setPositiveButton("Connect", new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface dialogInterface, int i) {
|
||||
MainActivity.toast("Checking token…");
|
||||
ListenBrainz.connect(field.getText().toString(), new LastFm.Cb() {
|
||||
@Override
|
||||
public void done(String str) {
|
||||
MainActivity.toast(str == null ? "Connected to ListenBrainz" : "ListenBrainz: " + str);
|
||||
SettingsPage.this.refresh();
|
||||
}
|
||||
});
|
||||
}
|
||||
}).setNeutralButton("Get token", new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface dialogInterface, int i) {
|
||||
try {
|
||||
SettingsPage.this.a.startActivity(new Intent("android.intent.action.VIEW", Uri.parse("https://listenbrainz.org/settings/")));
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
}).setNegativeButton("Cancel", (DialogInterface.OnClickListener) null));
|
||||
}
|
||||
|
||||
void login() {
|
||||
LinearLayout linearLayoutCol = Ui.col(this.a);
|
||||
linearLayoutCol.setPadding(Ui.dp(this.a, 22.0f), Ui.dp(this.a, 8.0f), Ui.dp(this.a, 22.0f), 0);
|
||||
|
|
|
|||
Loading…
Reference in a new issue