From 4a004971c69dca59ad11f6359d58d50a2edf61b2 Mon Sep 17 00:00:00 2001 From: gdlbo <41114720+gdlbo@users.noreply.github.com> Date: Tue, 30 May 2023 19:25:30 +0300 Subject: [PATCH] Update CatalogJsonInjector.java --- .../catalog/inject/CatalogJsonInjector.java | 130 +++++++----------- 1 file changed, 53 insertions(+), 77 deletions(-) diff --git a/app/src/main/java/ru/vtosters/lite/music/catalog/inject/CatalogJsonInjector.java b/app/src/main/java/ru/vtosters/lite/music/catalog/inject/CatalogJsonInjector.java index 03b039947..bf572e7fc 100644 --- a/app/src/main/java/ru/vtosters/lite/music/catalog/inject/CatalogJsonInjector.java +++ b/app/src/main/java/ru/vtosters/lite/music/catalog/inject/CatalogJsonInjector.java @@ -30,79 +30,22 @@ public class CatalogJsonInjector { public static JSONObject music(JSONObject json) throws JSONException { var catalog = json.optJSONObject("catalog"); - - JSONArray oldItems = null; - - if (catalog != null) { - oldItems = catalog.optJSONArray("sections"); - } - + var oldItems = catalog != null ? catalog.optJSONArray("sections") : null; var useOldAppVer = getBoolValue("useOldAppVer", false); var isUsersCatalog = oldItems.optJSONObject(0).optString("url").equals("https://vk.com/audios" + getUserId() + "?section=" + (useOldAppVer ? "all" : "general")); + var blocks = oldItems.optJSONObject(0).optJSONArray("blocks"); - if (isUsersCatalog) { - var blocks = oldItems.optJSONObject(0).optJSONArray("blocks"); - + if (isUsersCatalog && blocks != null) { if (!useOldAppVer) { fixDailyMix(blocks); - if (blocks != null) { - for (int i = 0; i < blocks.length(); i++) { - var block = blocks.optJSONObject(i); - var layout = block.optJSONObject("layout"); - if (layout != null) { - var name = layout.optString("name"); - if (name.equals("header_extended")) { - if (layout.has("top_title")) blocks.remove(i); - try { - layout.put("name", "header"); - } catch (JSONException e) { - e.printStackTrace(); - } - } - } - } - } - var playlists = fetchCatalogId("https://vk.com/audio?section=my_playlists"); - if (playlists != null) { - var catalogarr = playlists.optJSONObject("catalog").optJSONArray("sections").optJSONObject(0); + removeUnsupportedLayouts(blocks); - var title = catalogarr.optString("title"); - var id = catalogarr.optString("id"); - var url = catalogarr.optString("url"); + fetchCatalogId("https://vk.com/audio?section=my_playlists", oldItems); - Log.d("VKMusic", "Added " + title + " in music sections"); + fetchCatalogId("https://vk.com/audio?section=albums", oldItems); - oldItems.put(new JSONObject().put("id", id).put("title", title).put("url", url)); - } - - var albums = fetchCatalogId("https://vk.com/audio?section=albums"); - if (albums != null) { - var catalogarr = albums.optJSONObject("catalog").optJSONArray("sections").optJSONObject(0); - - var title = catalogarr.optString("title"); - var id = catalogarr.optString("id"); - var url = catalogarr.optString("url"); - - Log.d("VKMusic", "Added " + title + " in music sections"); - - oldItems.put(new JSONObject().put("id", id).put("title", title).put("url", url)); - } - - for (int i = 0; i < oldItems.length(); i++) { - var item = oldItems.optJSONObject(i); - var title = item.optString("title"); - var id = item.optString("id"); - var url = item.optString("url"); - var value = Preferences.getString("musicdefcatalog"); - - Log.d("VKMusic", "title: " + title + " id: " + id + " url: " + url + " value: " + value); - - if (url.contains(value) && !value.isEmpty()) { - catalog.put("default_section", id); - Log.d("VKMusic", "Added " + title + " as default music section"); - } - } + setDefaultAudioPage(oldItems, catalog); } if (CacheDatabaseDelegate.hasTracks() && !LibVKXClient.isIntegrationEnabled()) { // inj in playlist list @@ -112,15 +55,11 @@ public class CatalogJsonInjector { json.put("playlists", new JSONArray().put(getPlaylist())); Log.d("catalogInjector", "added new pl"); } else { - try { - json.optJSONArray("playlists").put(getPlaylist()); - Log.d("catalogInjector", "added to exist pl"); - } catch (Exception e) { - Log.d("catalogInjector", e.toString()); - } + json.optJSONArray("playlists").put(getPlaylist()); + Log.d("catalogInjector", "added to exist pl"); } - if (blocks != null && (!useOldAppVer || noPlaylists)) { + if (!useOldAppVer || noPlaylists) { var newBlocks = new JSONArray(); newBlocks @@ -155,6 +94,7 @@ public class CatalogJsonInjector { if (!isUsersCatalog || !CacheDatabaseDelegate.hasTracks() || LibVKXClient.isIntegrationEnabled()) { return; // early return if not users catalog or no tracks or integration enabled } + var noPlaylists = !json.has("playlists"); if (noPlaylists) { @@ -278,9 +218,38 @@ public class CatalogJsonInjector { } } - private static JSONObject fetchCatalogId(String section) { - if (section.isEmpty()) return null; + private static void removeUnsupportedLayouts(JSONArray blocks) throws JSONException { + for (int i = 0; i < blocks.length(); i++) { + var block = blocks.optJSONObject(i); + var layout = block.optJSONObject("layout"); + if (layout != null) { + var name = layout.optString("name"); + if (name.equals("header_extended")) { + if (layout.has("top_title")) blocks.remove(i); + layout.put("name", "header"); + } + } + } + } + private static void setDefaultAudioPage(JSONArray jsonArray, JSONObject catalog) throws JSONException { + for (int i = 0; i < jsonArray.length(); i++) { + var item = jsonArray.optJSONObject(i); + var title = item.optString("title"); + var id = item.optString("id"); + var url = item.optString("url"); + var value = Preferences.getString("musicdefcatalog"); + + Log.d("VKMusic", "title: " + title + " id: " + id + " url: " + url + " value: " + value); + + if (url.contains(value) && !value.isEmpty()) { + catalog.put("default_section", id); + Log.d("VKMusic", "Added " + title + " as default music section"); + } + } + } + + private static void fetchCatalogId(String sectionUrl, JSONArray jsonArray) { var request = new Request.a() .b("https://" + getApi() + "/method/" + "catalog.getAudio" + "?v=5.119" @@ -291,16 +260,23 @@ public class CatalogJsonInjector { + "&device_id=" + DeviceIdProvider.d(AndroidUtils.getGlobalContext()) + "&url=" - + section + + sectionUrl + "&access_token=" + AccountManagerUtils.getUserToken()) .a(Headers.a("User-Agent", Network.l.c().a(), "Content-Type", "application/x-www-form-urlencoded; charset=utf-8")).a(); try { - return new JSONObject(mClient.a(request).execute().a().g()).getJSONObject("response"); + var json = new JSONObject(mClient.a(request).execute().a().g()).getJSONObject("response"); + var catalogarr = json.optJSONObject("catalog").optJSONArray("sections").optJSONObject(0); + + var title = catalogarr.optString("title"); + var id = catalogarr.optString("id"); + var url = catalogarr.optString("url"); + + Log.d("VKMusic", "Added " + title + " in music sections"); + + jsonArray.put(new JSONObject().put("id", id).put("title", title).put("url", url)); } catch (JSONException | IOException e) { Log.d("VTLMusic", "Error: " + e.getMessage()); } - - return null; } }