Removed libs folder

This commit is contained in:
h4h13 2018-07-27 19:28:24 +05:30
parent f77b426617
commit 960b057e0a
80 changed files with 1192 additions and 1673 deletions

View file

@ -1,69 +0,0 @@
package code.name.monkey.retromusic.lyrics;
import android.os.Handler;
import code.name.monkey.retromusic.model.Song;
import code.name.monkey.retromusic.rest.KogouClient;
import code.name.monkey.retromusic.rest.model.KuGouSearchLyricResult;
import code.name.monkey.retromusic.util.LyricUtil;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import java.io.File;
/**
* @author Hemanth S (h4h13).
*/
public class KogouLyricsFetcher {
private KogouClient mKogouClient;
private Song mSong;
private KogouLyricsCallback mCallback;
public KogouLyricsFetcher(KogouLyricsCallback callback) {
mCallback = callback;
mKogouClient = new KogouClient();
}
public void loadLyrics(Song song, String duration) {
mSong = song;
mKogouClient.getApiService()
.searchLyric(mSong.title, duration)
.subscribe(this::parseKugouResult,
throwable -> mCallback.onNoLyrics());
}
private void parseKugouResult(KuGouSearchLyricResult kuGouSearchLyricResult) {
if (kuGouSearchLyricResult != null && kuGouSearchLyricResult.status == 200 &
kuGouSearchLyricResult.candidates != null &&
kuGouSearchLyricResult.candidates.size() != 0) {
KuGouSearchLyricResult.Candidates candidates = kuGouSearchLyricResult.candidates.get(0);
loadLyricsFile(candidates);
} else {
mCallback.onNoLyrics();
}
}
private void loadLyricsFile(KuGouSearchLyricResult.Candidates candidates) {
mKogouClient.getApiService().getRawLyric(candidates.id, candidates.accesskey)
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.subscribe(kuGouRawLyric -> {
if (kuGouRawLyric == null) {
mCallback.onNoLyrics();
return;
}
String rawLyric = LyricUtil.decryptBASE64(kuGouRawLyric.content);
LyricUtil.writeLrcToLoc(mSong.title, mSong.artistName, rawLyric);
new Handler().postDelayed(
() -> mCallback.onLyrics(LyricUtil.getLocalLyricFile(mSong.title, mSong.artistName)),
1);
});
}
public interface KogouLyricsCallback {
void onNoLyrics();
void onLyrics(File file);
}
}

View file

@ -1,6 +0,0 @@
package code.name.monkey.retromusic.lyrics;
public interface LyricsEngine {
String getLyrics(String artistName, String songTitle);
}

View file

@ -1,143 +0,0 @@
package code.name.monkey.retromusic.lyrics;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;
import org.json.JSONException;
import org.json.JSONObject;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.Node;
import org.jsoup.nodes.TextNode;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
public class LyricsWikiEngine implements LyricsEngine {
private static final String TAG = LyricsWikiEngine.class.getSimpleName();
// Reads an InputStream and converts it to a String.
private static String readIt(InputStream stream) throws IOException {
Reader reader = new InputStreamReader(stream, "UTF-8");
StringWriter sw = new StringWriter();
char[] buffer = new char[4096];
int count;
while ((count = reader.read(buffer)) != -1) {
sw.write(buffer, 0, count);
}
return sw.toString();
}
@Override
public String getLyrics(String artistName, String songTitle) {
try {
String lyricsUrl = makeApiCall(artistName, songTitle);
if (lyricsUrl == null) { // no URL in API answer or no correct answer at all
return null;
}
return parseFullLyricsPage(lyricsUrl);
} catch (IOException e) {
Log.w(TAG, "Couldn't connect to lyrics wiki REST endpoints", e);
return null;
} catch (JSONException e) {
Log.w(TAG, "Couldn't transform API answer to JSON entity", e);
return null;
}
}
/**
* First call
*/
private String makeApiCall(String artistName, String songTitle) throws IOException, JSONException {
HttpsURLConnection apiCall = null;
try {
// build query
// e.g. https://lyrics.wikia.com/api.php?func=getSong&artist=The%20Beatle&song=Girl&fmt=realjson
Uri link = new Uri.Builder()
.scheme("https")
.authority("lyrics.wikia.com")
.path("api.php")
.appendQueryParameter("func", "getSong")
.appendQueryParameter("fmt", "realjson")
.appendQueryParameter("artist", artistName)
.appendQueryParameter("song", songTitle)
.build();
// construct an http request
apiCall = (HttpsURLConnection) new URL(link.toString()).openConnection();
apiCall.setReadTimeout(10_000);
apiCall.setConnectTimeout(15_000);
// execute
apiCall.connect();
int response = apiCall.getResponseCode();
if (response != HttpsURLConnection.HTTP_OK) {
// redirects are handled internally, this is clearly an error
return null;
}
InputStream is = apiCall.getInputStream();
String reply = readIt(is);
JSONObject getSongAnswer = new JSONObject(reply);
return getLyricsUrl(getSongAnswer);
} finally {
if (apiCall != null) {
apiCall.disconnect();
}
}
}
/**
* Second call
*/
private String parseFullLyricsPage(String lyricsUrl) throws IOException {
Document page = Jsoup.parse(new URL(lyricsUrl), 10_000);
Element lyricsBox = page.select("div.lyricbox").first();
if (lyricsBox == null) { // no lyrics frame on page
return null;
}
// remove unneeded elements
lyricsBox.select("div.rtMatcher").remove();
lyricsBox.select("div.lyricsbreak").remove();
lyricsBox.select("script").remove();
StringBuilder builder = new StringBuilder();
for (Node curr : lyricsBox.childNodes()) {
if (curr instanceof TextNode) {
builder.append(((TextNode) curr).text());
} else {
builder.append("\n");
}
}
return builder.toString();
}
private String getLyricsUrl(JSONObject getSongAnswer) {
try {
String pageId = getSongAnswer.getString("page_id");
if (TextUtils.isEmpty(pageId)) {
return null; // empty page_id means page wasn't created
}
return getSongAnswer.getString("url");
} catch (JSONException e) {
Log.w(TAG, "Unknown format of getSong API call answer", e);
return null;
}
}
}

View file

@ -1,38 +0,0 @@
package code.name.monkey.retromusic.lyrics;
import android.os.AsyncTask;
import android.text.TextUtils;
/**
* @author Hemanth S (h4h13).
*/
public class ParseLyrics extends AsyncTask<String, Void, String> {
private LyricsCallback mLyricsCallback;
public ParseLyrics(LyricsCallback lyricsCallback) {
mLyricsCallback = lyricsCallback;
}
@Override
protected String doInBackground(String... strings) {
LyricsEngine lyricsEngine = new LyricsWikiEngine();
return lyricsEngine.getLyrics(strings[1], strings[0]);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
if (TextUtils.isEmpty(s)) {
mLyricsCallback.onError();
return;
}
mLyricsCallback.onShowLyrics(s);
}
public interface LyricsCallback {
void onShowLyrics(String lyrics);
void onError();
}
}

View file

@ -1,23 +0,0 @@
package code.name.monkey.retromusic.lyrics;
public class QueryResult {
public static final String ITEM_LRC = "lrc";
public static final String ATTRIBUTE_ID = "id";
public static final String ATTRIBUTE_ARTIST = "artist";
public static final String ATTRIBUTE_TITLE = "title";
public final int mId;
public final String mArtist;
public final String mTitle;
QueryResult(int id, String artist, String title) {
mId = id;
mArtist = artist;
mTitle = title;
}
@Override
public String toString() {
return mTitle + " - " + mArtist;
}
}