Skip to content

Commit

Permalink
v 1.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
appujet committed Oct 13, 2024
1 parent c24324e commit 00cc19f
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 243 deletions.
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

- This is a plugin for [Lavalink](https://github.com/lavalink-devs/Lavalink)
- This plugin allows you to play songs from JioSaavn in your discord server.
- NO API KEY REQUIRED
- This plugin uses the [JioSaavn API](https://github.com/appujet/jiosaavn-rest-api) to fetch songs.
- Special thanks to [topi314](https://github.com/topi314/LavaSrc) and [duncte123](https://github.com/duncte123) because most of the code for this plugin is based on [Lavasrc](https://github.com/topi314/LavaSrc) and [skybot-lavalink-plugin](https://github.com/DuncteBot/skybot-lavalink-plugin).

## Lavalink Usage
Expand Down Expand Up @@ -59,6 +59,7 @@ lavalink:
plugins:
jiosaavn:
apiURL: "https://jiosaavn-plugin-api.vercel.app" # JioSaavn API URL
playlistTrackLimit: 50 # The maximum number of tracks to return from given playlist (default 50 tracks)
recommendationsTrackLimit: 10 # The maximum number of track to return from recommendations (default 10 tracks)
Expand Down Expand Up @@ -103,3 +104,14 @@ logging:
- <https://www.jiosaavn.com/album/bhediya/wSM2AOubajk>_
- <https://www.jiosaavn.com/artist/arijit-singh-songs/LlRWpHzy3Hk>_
- <https://www.jiosaavn.com/featured/jai-hanuman/8GIEhrr8clSO0eMLZZxqsA>__

## How to get API URL ?

- You can host the api locally using [this guide](https://github.com/appujet/jiosaavn-rest-api)

- You can easily deploy your own instance of the API by clicking the button below:

[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/clone?repository-url=https://github.com/appujet/jiosaavn-rest-api)

> [!TIP]
> To ensure the API provides results in the intended language, configure the [Serverless Function Region](https://vercel.com/docs/concepts/functions/serverless-functions/regions) in Vercel to `Mumbai, India (South) - > bom1`.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version=1.0.0
version=1.0.1
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.HttpClientBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.URI;
import java.net.URISyntaxException;
import java.io.IOException;

import java.nio.charset.StandardCharsets;
import java.util.function.Consumer;
import java.util.function.Function;
Expand All @@ -33,7 +32,7 @@
public abstract class ExtendedAudioSourceManager implements AudioSourceManager, HttpConfigurable {
public static final Logger log = LoggerFactory.getLogger(ExtendedAudioSourceManager.class);
protected final HttpInterfaceManager httpInterfaceManager;
private static final String BASE_API_URL = "https://www.jiosaavn.com/api.php?";
protected static String BASE_API_URL = null;
public ExtendedAudioSourceManager () {
this(true);
}
Expand All @@ -54,10 +53,21 @@ public HttpInterface getHttpInterface() {
return httpInterfaceManager.getInterface();
}

public JsonBrowser fetchJson(String pageURl) {
final HttpGet httpGet = new HttpGet(BASE_API_URL + pageURl);
try (final CloseableHttpResponse response = this.getHttpInterface().execute(httpGet)) {
final String content = IOUtils.toString(response.getEntity().getContent(), StandardCharsets.UTF_8);
//log.info("Response from API: {}", content);
return JsonBrowser.parse(content);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

public JsonBrowser fetchJson(String endpoint, String[] params, String context) {
try {
URI uri = getUri(endpoint, params, context);
final HttpGet httpGet = new HttpGet(uri);

final HttpGet httpGet = new HttpGet();
try (final CloseableHttpResponse response = this.getHttpInterface().execute(httpGet)) {
final String content = IOUtils.toString(response.getEntity().getContent(), StandardCharsets.UTF_8);
//log.info("Response from API: {}", content);
Expand All @@ -67,24 +77,6 @@ public JsonBrowser fetchJson(String endpoint, String[] params, String context) {
throw new RuntimeException("Error fetching JSON from JioSaavn API", e);
}
}

private static URI getUri(String endpoint, String[] params, String context) throws URISyntaxException {
URIBuilder uriBuilder = new URIBuilder(BASE_API_URL);
uriBuilder.addParameter("__call", endpoint);
uriBuilder.addParameter("_format", "json");
uriBuilder.addParameter("_marker", "0");
uriBuilder.addParameter("api_version", "4");
uriBuilder.addParameter("ctx", context != null ? context : "web6dot0");

if (params != null) {
for (int i = 0; i < params.length; i += 2) {
if (i + 1 < params.length) {
uriBuilder.addParameter(params[i], params[i + 1]);
}
}
}
return uriBuilder.build();
}

@Override
public void shutdown() {
Expand Down
88 changes: 0 additions & 88 deletions main/src/main/java/com/github/appujet/jiosaavn/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,103 +3,15 @@

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.security.Key;
import java.util.Base64;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Utils {

private static final String KEY = "38346591";
private static final String ALGORITHM = "DES/ECB/NoPadding";
private static final String[] QUALITIES = { "_320", "_160", "_96", "_48", "_12" };

// Regular expressions for extracting tokens
private static final Pattern SONG_PATTERN = Pattern.compile("jiosaavn\\.com/song/[^/]+/([^/]+)$");
private static final Pattern ARTIST_PATTERN = Pattern.compile("jiosaavn\\.com/artist/[^/]+/([^/]+)$");
private static final Pattern ALBUM_PATTERN = Pattern.compile("jiosaavn\\.com/album/[^/]+/([^/]+)$");
private static final Pattern PLAYLIST_PATTERN = Pattern.compile("jiosaavn\\.com/(?:featured|s/playlist)/[^/]+/([^/]+)$");

/**
* Extracts a song token from a JioSaavn song URL.
*
* @param url The song URL.
* @return The song token or null if not found.
*/
public static String extractSongToken(String url) {
return extractToken(url, SONG_PATTERN);
}

/**
* Extracts an artist token from a JioSaavn artist URL.
*
* @param url The artist URL.
* @return The artist token or null if not found.
*/
public static String extractArtistToken(String url) {
return extractToken(url, ARTIST_PATTERN);
}

/**
* Extracts an album token from a JioSaavn album URL.
*
* @param url The album URL.
* @return The album token or null if not found.
*/
public static String extractAlbumToken(String url) {
return extractToken(url, ALBUM_PATTERN);
}

/**
* Extracts a playlist token from a JioSaavn playlist URL.
*
* @param url The playlist URL.
* @return The playlist token or null if not found.
*/
public static String extractPlaylistToken(String url) {
return extractToken(url, PLAYLIST_PATTERN);
}

/**
* Extracts a token using the provided pattern.
*
* @param url The URL to extract the token from.
* @param pattern The regex pattern to match the token.
* @return The token if found, or null if not.
*/
private static String extractToken(String url, Pattern pattern) {
if (url == null || url.isEmpty()) {
return null;
}

Matcher matcher = pattern.matcher(url);
if (matcher.find()) {
return matcher.group(1); // Return the first captured group
}
return null;
}

/**
* Encodes a string using URL encoding and replaces certain characters.
* @param input The string to encode.
* @return The encoded string.
*/
public static String encodeURIComponent(String input) {
String encoded = URLEncoder.encode(input, StandardCharsets.UTF_8);
encoded = encoded.replace("+", "%20");
encoded = encoded.replace("'", "%27");
encoded = encoded.replace("%", "%25");
encoded = encoded.replace("#", "%23");
encoded = encoded.replace("&", "%26");
encoded = encoded.replace("=", "%3D");
encoded = encoded.replace("?", "%3F");

return encoded;

}

/**
* Generates a download link by decrypting the encrypted media URL.
*
Expand Down
Loading

0 comments on commit 00cc19f

Please sign in to comment.