diff --git a/StremioX/build.gradle.kts b/StremioX/build.gradle.kts new file mode 100644 index 00000000..57b33f83 --- /dev/null +++ b/StremioX/build.gradle.kts @@ -0,0 +1,26 @@ +// use an integer for version numbers +version = 1 + + +cloudstream { + language = "en" + // All of these properties are optional, you can safely remove them + + description = "Allow you to use Stremio addons as sources such as torrentio. (!) Requires setup" + authors = listOf("Hexated") + + /** + * Status int as the following: + * 0: Down + * 1: Ok + * 2: Slow + * 3: Beta only + * */ + status = 1 // will be 3 if unspecified + tvTypes = listOf( + "TvSeries", + "Movie", + ) + + iconUrl = "https://www.google.com/s2/favicons?domain=www.stremio.com&sz=%size%" +} \ No newline at end of file diff --git a/StremioX/icon.png b/StremioX/icon.png new file mode 100644 index 00000000..6ca05ae6 Binary files /dev/null and b/StremioX/icon.png differ diff --git a/StremioX/src/main/AndroidManifest.xml b/StremioX/src/main/AndroidManifest.xml new file mode 100644 index 00000000..c98063f8 --- /dev/null +++ b/StremioX/src/main/AndroidManifest.xml @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/StremioX/src/main/kotlin/com/hexated/StremioX.kt b/StremioX/src/main/kotlin/com/hexated/StremioX.kt new file mode 100644 index 00000000..cd973fc9 --- /dev/null +++ b/StremioX/src/main/kotlin/com/hexated/StremioX.kt @@ -0,0 +1,594 @@ +package com.hexated + +import android.util.Log +import com.fasterxml.jackson.annotation.JsonProperty +import com.lagradost.cloudstream3.* +import com.lagradost.cloudstream3.LoadResponse.Companion.addTrailer +import com.lagradost.cloudstream3.utils.* +import com.lagradost.cloudstream3.utils.AppUtils.parseJson +import com.lagradost.cloudstream3.utils.AppUtils.toJson +import org.json.JSONObject +import java.net.URI +import java.util.ArrayList +import kotlin.math.roundToInt + +open class StremioX : MainAPI() { + override var mainUrl = "https://torrentio.strem.fun" + override var name = "StremioX" + override val hasMainPage = true + override val hasQuickSearch = true + override val supportedTypes = setOf( + TvType.Others, + ) + + companion object { + const val TRACKER_LIST_URL = + "https://raw.githubusercontent.com/ngosang/trackerslist/master/trackers_best.txt" + const val openSubAPI = "https://opensubtitles.strem.io/stremio/v1" + const val watchSomuchAPI = "https://watchsomuch.tv" + private const val tmdbAPI = "https://api.themoviedb.org/3" + private val apiKey = + base64DecodeAPI("ZTM=NTg=MjM=MjM=ODc=MzI=OGQ=MmE=Nzk=Nzk=ZjI=NTA=NDY=NDA=MzA=YjA=") // PLEASE DON'T STEAL + + fun getType(t: String?): TvType { + return when (t) { + "movie" -> TvType.Movie + else -> TvType.TvSeries + } + } + + fun getStatus(t: String?): ShowStatus { + return when (t) { + "Returning Series" -> ShowStatus.Ongoing + else -> ShowStatus.Completed + } + } + + private fun base64DecodeAPI(api: String): String { + return api.chunked(4).map { base64Decode(it) }.reversed().joinToString("") + } + + } + + override val mainPage = mainPageOf( + "$tmdbAPI/trending/all/day?api_key=$apiKey®ion=US" to "Trending", + "$tmdbAPI/movie/popular?api_key=$apiKey®ion=US" to "Popular Movies", + "$tmdbAPI/tv/popular?api_key=$apiKey®ion=US&with_original_language=en" to "Popular TV Shows", + "$tmdbAPI/tv/airing_today?api_key=$apiKey®ion=US&with_original_language=en" to "Airing Today TV Shows", + "$tmdbAPI/discover/tv?api_key=$apiKey&with_networks=213" to "Netflix", + "$tmdbAPI/discover/tv?api_key=$apiKey&with_networks=1024" to "Amazon", + "$tmdbAPI/discover/tv?api_key=$apiKey&with_networks=2739" to "Disney+", + "$tmdbAPI/discover/tv?api_key=$apiKey&with_networks=453" to "Hulu", + "$tmdbAPI/discover/tv?api_key=$apiKey&with_networks=2552" to "Apple TV+", + "$tmdbAPI/discover/tv?api_key=$apiKey&with_networks=49" to "HBO", + "$tmdbAPI/discover/tv?api_key=$apiKey&with_networks=4330" to "Paramount+", + "$tmdbAPI/movie/top_rated?api_key=$apiKey®ion=US" to "Top Rated Movies", + "$tmdbAPI/tv/top_rated?api_key=$apiKey®ion=US" to "Top Rated TV Shows", + "$tmdbAPI/movie/upcoming?api_key=$apiKey®ion=US" to "Upcoming Movies", + "$tmdbAPI/discover/tv?api_key=$apiKey&with_original_language=ko" to "Korean Shows", + ) + + private fun getImageUrl(link: String?): String? { + if (link == null) return null + return if (link.startsWith("/")) "https://image.tmdb.org/t/p/w500/$link" else link + } + + private fun getOriImageUrl(link: String?): String? { + if (link == null) return null + return if (link.startsWith("/")) "https://image.tmdb.org/t/p/original/$link" else link + } + + override suspend fun getMainPage( + page: Int, request: MainPageRequest + ): HomePageResponse { + val adultQuery = + if (settingsForProvider.enableAdult) "" else "&without_keywords=190370|13059|226161|195669|190370" + val type = if (request.data.contains("/movie")) "movie" else "tv" + val home = app.get("${request.data}$adultQuery&page=$page") + .parsedSafe()?.results?.mapNotNull { media -> + media.toSearchResponse(type) + } ?: throw ErrorLoadingException("Invalid Json reponse") + return newHomePageResponse(request.name, home) + } + + private fun Media.toSearchResponse(type: String? = null): SearchResponse? { + return newMovieSearchResponse( + title ?: name ?: originalTitle ?: return null, + Data(id = id, type = mediaType ?: type).toJson(), + TvType.Movie, + ) { + this.posterUrl = getImageUrl(posterPath) + } + } + + override suspend fun quickSearch(query: String): List? = search(query) + + override suspend fun search(query: String): List? { + return app.get( + "$tmdbAPI/search/multi?api_key=$apiKey&language=en-US&query=$query&page=1&include_adult=${settingsForProvider.enableAdult}" + ).parsedSafe()?.results?.mapNotNull { media -> + media.toSearchResponse() + } + } + + override suspend fun load(url: String): LoadResponse? { + val data = parseJson(url) + val type = getType(data.type) + val resUrl = if (type == TvType.Movie) { + "$tmdbAPI/movie/${data.id}?api_key=$apiKey&append_to_response=keywords,credits,external_ids,videos,recommendations" + } else { + "$tmdbAPI/tv/${data.id}?api_key=$apiKey&append_to_response=keywords,credits,external_ids,videos,recommendations" + } + val res = app.get(resUrl).parsedSafe() + ?: throw ErrorLoadingException("Invalid Json Response") + + val title = res.title ?: res.name ?: return null + val poster = getOriImageUrl(res.posterPath) + val bgPoster = getOriImageUrl(res.backdropPath) + val year = (res.releaseDate ?: res.firstAirDate)?.split("-")?.first()?.toIntOrNull() + val rating = res.vote_average.toString().toRatingInt() + val genres = res.genres?.mapNotNull { it.name } + val isAnime = + genres?.contains("Animation") == true && (res.original_language == "zh" || res.original_language == "ja") + val keywords = res.keywords?.results?.mapNotNull { it.name }.orEmpty() + .ifEmpty { res.keywords?.keywords?.mapNotNull { it.name } } + + val actors = res.credits?.cast?.mapNotNull { cast -> + ActorData( + Actor( + cast.name ?: cast.originalName ?: return@mapNotNull null, + getImageUrl(cast.profilePath) + ), roleString = cast.character + ) + } ?: return null + val recommendations = + res.recommendations?.results?.mapNotNull { media -> media.toSearchResponse() } + + val trailer = + res.videos?.results?.map { "https://www.youtube.com/watch?v=${it.key}" }?.randomOrNull() + + return if (type == TvType.TvSeries) { + val episodes = res.seasons?.mapNotNull { season -> + app.get("$tmdbAPI/${data.type}/${data.id}/season/${season.seasonNumber}?api_key=$apiKey") + .parsedSafe()?.episodes?.map { eps -> + Episode( + LoadData( + res.external_ids?.imdb_id, + eps.seasonNumber, + eps.episodeNumber + ).toJson(), + name = eps.name, + season = eps.seasonNumber, + episode = eps.episodeNumber, + posterUrl = getImageUrl(eps.stillPath), + rating = eps.voteAverage?.times(10)?.roundToInt(), + description = eps.overview + ).apply { + this.addDate(eps.airDate) + } + } + }?.flatten() ?: listOf() + newTvSeriesLoadResponse( + title, url, if (isAnime) TvType.Anime else TvType.TvSeries, episodes + ) { + this.posterUrl = poster + this.backgroundPosterUrl = bgPoster + this.year = year + this.plot = res.overview + this.tags = if (isAnime) keywords else genres + this.rating = rating + this.showStatus = getStatus(res.status) + this.recommendations = recommendations + this.actors = actors + addTrailer(trailer) + } + } else { + newMovieLoadResponse( + title, + url, + TvType.Movie, + LoadData(res.external_ids?.imdb_id).toJson() + ) { + this.posterUrl = poster + this.backgroundPosterUrl = bgPoster + this.year = year + this.plot = res.overview + this.duration = res.runtime + this.tags = if (isAnime) keywords else genres + this.rating = rating + this.recommendations = recommendations + this.actors = actors + addTrailer(trailer) + } + } + } + + override suspend fun loadLinks( + data: String, + isCasting: Boolean, + subtitleCallback: (SubtitleFile) -> Unit, + callback: (ExtractorLink) -> Unit + ): Boolean { + val res = parseJson(data) + + argamap( + { + invokeMainSource(res.imdbId, res.season, res.episode, subtitleCallback, callback) + }, + { + invokeWatchsomuch(res.imdbId, res.season, res.episode, subtitleCallback) + }, + { + invokeOpenSubs(res.imdbId, res.season, res.episode, subtitleCallback) + }, + ) + + return true + } + + private suspend fun invokeMainSource( + imdbId: String? = null, + season: Int? = null, + episode: Int? = null, + subtitleCallback: (SubtitleFile) -> Unit, + callback: (ExtractorLink) -> Unit + ) { + val fixMainUrl = mainUrl.fixSourceUrl() + val url = if(season == null) { + "$fixMainUrl/stream/movie/$imdbId.json" + } else { + "$fixMainUrl/stream/series/$imdbId:$season:$episode.json" + } + val res = AppUtils.tryParseJson(app.get(url).text) ?: return + res.streams.forEach { stream -> + stream.runCallback(subtitleCallback, callback) + } + } + + private suspend fun invokeOpenSubs( + imdbId: String? = null, + season: Int? = null, + episode: Int? = null, + subtitleCallback: (SubtitleFile) -> Unit, + ) { + val id = if(season == null) { + imdbId + } else { + "$imdbId $season $episode" + } + val data = base64Encode("""{"id":1,"jsonrpc":"2.0","method":"subtitles.find","params":[null,{"query":{"itemHash":"$id"}}]}""".toByteArray()) + app.get("$openSubAPI/q.json?b=$data").parsedSafe()?.result?.all?.map { sub -> + subtitleCallback.invoke( + SubtitleFile( + SubtitleHelper.fromThreeLettersToLanguage(sub.lang ?: "") ?: sub.lang + ?: "", + sub.url ?: return@map + ) + ) + } + + } + + private suspend fun invokeWatchsomuch( + imdbId: String? = null, + season: Int? = null, + episode: Int? = null, + subtitleCallback: (SubtitleFile) -> Unit, + ) { + val id = imdbId?.removePrefix("tt") + val epsId = app.post( + "${watchSomuchAPI}/Watch/ajMovieTorrents.aspx", data = mapOf( + "index" to "0", + "mid" to "$id", + "wsk" to "f6ea6cde-e42b-4c26-98d3-b4fe48cdd4fb", + "lid" to "", + "liu" to "" + ), headers = mapOf("X-Requested-With" to "XMLHttpRequest") + ).parsedSafe()?.movie?.torrents?.let { eps -> + if (season == null) { + eps.firstOrNull()?.id + } else { + eps.find { it.episode == episode && it.season == season }?.id + } + } ?: return + + val (seasonSlug, episodeSlug) = getEpisodeSlug(season, episode) + + val subUrl = if (season == null) { + "${watchSomuchAPI}/Watch/ajMovieSubtitles.aspx?mid=$id&tid=$epsId&part=" + } else { + "${watchSomuchAPI}/Watch/ajMovieSubtitles.aspx?mid=$id&tid=$epsId&part=S${seasonSlug}E${episodeSlug}" + } + + app.get(subUrl).parsedSafe()?.subtitles?.map { sub -> + subtitleCallback.invoke( + SubtitleFile( + sub.label ?: "", fixUrl(sub.url ?: return@map null, watchSomuchAPI) + ) + ) + } + + + } + + private fun String.fixSourceUrl() : String { + return this.replace("/manifest.json", "").replace("stremio://", "https://") + } + + private fun getEpisodeSlug( + season: Int? = null, + episode: Int? = null, + ): Pair { + return if (season == null && episode == null) { + "" to "" + } else { + (if (season!! < 10) "0$season" else "$season") to (if (episode!! < 10) "0$episode" else "$episode") + } + } + + private fun fixUrl(url: String, domain: String): String { + if (url.startsWith("http")) { + return url + } + if (url.isEmpty()) { + return "" + } + + val startsWithNoHttp = url.startsWith("//") + if (startsWithNoHttp) { + return "https:$url" + } else { + if (url.startsWith('/')) { + return domain + url + } + return "$domain/$url" + } + } + + private data class StreamsResponse(val streams: List) + private data class Subtitle( + val url: String?, + val lang: String?, + val id: String?, + ) + + private data class Stream( + val name: String?, + val title: String?, + val url: String?, + val description: String?, + val ytId: String?, + val externalUrl: String?, + val behaviorHints: JSONObject?, + val infoHash: String?, + val sources: List = emptyList(), + val subtitles: List = emptyList() + ) { + suspend fun runCallback( + subtitleCallback: (SubtitleFile) -> Unit, + callback: (ExtractorLink) -> Unit + ) { + if (url != null) { + var referer: String? = null + try { + val headers = ((behaviorHints?.get("proxyHeaders") as? JSONObject) + ?.get("request") as? JSONObject) + referer = + headers?.get("referer") as? String ?: headers?.get("origin") as? String + } catch (ex: Throwable) { + Log.e("Stremio", Log.getStackTraceString(ex)) + } + callback.invoke( + ExtractorLink( + name ?: "", + title ?: name ?: "", + url, + referer ?: "", + getQualityFromName(description), + isM3u8 = URI(url).path.endsWith(".m3u8") + ) + ) + subtitles.map { sub -> + subtitleCallback.invoke( + SubtitleFile( + SubtitleHelper.fromThreeLettersToLanguage(sub.lang ?: "") ?: sub.lang + ?: "", + sub.url ?: return@map + ) + ) + } + } + if (ytId != null) { + loadExtractor("https://www.youtube.com/watch?v=$ytId", subtitleCallback, callback) + } + if (externalUrl != null) { + loadExtractor(externalUrl, subtitleCallback, callback) + } + if (infoHash != null) { + val resp = app.get(TRACKER_LIST_URL).text + val otherTrackers = resp + .split("\n") + .filterIndexed { i, _ -> i % 2 == 0 } + .filter { s -> s.isNotEmpty() }.joinToString("") { "&tr=$it" } + + val sourceTrackers = sources + .filter { it.startsWith("tracker:") } + .map { it.removePrefix("tracker:") } + .filter { s -> s.isNotEmpty() }.joinToString("") { "&tr=$it" } + + val magnet = "magnet:?xt=urn:btih:${infoHash}${sourceTrackers}${otherTrackers}" + callback.invoke( + ExtractorLink( + name ?: "", + title ?: name ?: "", + magnet, + "", + Qualities.Unknown.value + ) + ) + } + } + } + + data class OsSubtitles( + @JsonProperty("url") val url: String? = null, + @JsonProperty("lang") val lang: String? = null, + ) + + data class OsAll( + @JsonProperty("all") val all: ArrayList? = arrayListOf(), + ) + + data class OsResult( + @JsonProperty("result") val result: OsAll? = null, + ) + + data class WatchsomuchTorrents( + @JsonProperty("id") val id: Int? = null, + @JsonProperty("movieId") val movieId: Int? = null, + @JsonProperty("season") val season: Int? = null, + @JsonProperty("episode") val episode: Int? = null, + ) + + data class WatchsomuchMovies( + @JsonProperty("torrents") val torrents: ArrayList? = arrayListOf(), + ) + + data class WatchsomuchResponses( + @JsonProperty("movie") val movie: WatchsomuchMovies? = null, + ) + + data class WatchsomuchSubtitles( + @JsonProperty("url") val url: String? = null, + @JsonProperty("label") val label: String? = null, + ) + + data class WatchsomuchSubResponses( + @JsonProperty("subtitles") val subtitles: ArrayList? = arrayListOf(), + ) + + data class LoadData( + val imdbId: String? = null, + val season: Int? = null, + val episode: Int? = null, + ) + + data class Data( + val id: Int? = null, + val type: String? = null, + val aniId: String? = null, + val malId: Int? = null, + ) + + data class Results( + @JsonProperty("results") val results: ArrayList? = arrayListOf(), + ) + + data class Media( + @JsonProperty("id") val id: Int? = null, + @JsonProperty("name") val name: String? = null, + @JsonProperty("title") val title: String? = null, + @JsonProperty("original_title") val originalTitle: String? = null, + @JsonProperty("media_type") val mediaType: String? = null, + @JsonProperty("poster_path") val posterPath: String? = null, + ) + + data class Genres( + @JsonProperty("id") val id: Int? = null, + @JsonProperty("name") val name: String? = null, + ) + + data class Keywords( + @JsonProperty("id") val id: Int? = null, + @JsonProperty("name") val name: String? = null, + ) + + data class KeywordResults( + @JsonProperty("results") val results: ArrayList? = arrayListOf(), + @JsonProperty("keywords") val keywords: ArrayList? = arrayListOf(), + ) + + data class Seasons( + @JsonProperty("id") val id: Int? = null, + @JsonProperty("name") val name: String? = null, + @JsonProperty("season_number") val seasonNumber: Int? = null, + @JsonProperty("air_date") val airDate: String? = null, + ) + + data class Cast( + @JsonProperty("id") val id: Int? = null, + @JsonProperty("name") val name: String? = null, + @JsonProperty("original_name") val originalName: String? = null, + @JsonProperty("character") val character: String? = null, + @JsonProperty("known_for_department") val knownForDepartment: String? = null, + @JsonProperty("profile_path") val profilePath: String? = null, + ) + + data class Episodes( + @JsonProperty("id") val id: Int? = null, + @JsonProperty("name") val name: String? = null, + @JsonProperty("overview") val overview: String? = null, + @JsonProperty("air_date") val airDate: String? = null, + @JsonProperty("still_path") val stillPath: String? = null, + @JsonProperty("vote_average") val voteAverage: Double? = null, + @JsonProperty("episode_number") val episodeNumber: Int? = null, + @JsonProperty("season_number") val seasonNumber: Int? = null, + ) + + data class MediaDetailEpisodes( + @JsonProperty("episodes") val episodes: ArrayList? = arrayListOf(), + ) + + data class Trailers( + @JsonProperty("key") val key: String? = null, + ) + + data class ResultsTrailer( + @JsonProperty("results") val results: ArrayList? = arrayListOf(), + ) + + data class ExternalIds( + @JsonProperty("imdb_id") val imdb_id: String? = null, + @JsonProperty("tvdb_id") val tvdb_id: String? = null, + ) + + data class Credits( + @JsonProperty("cast") val cast: ArrayList? = arrayListOf(), + ) + + data class ResultsRecommendations( + @JsonProperty("results") val results: ArrayList? = arrayListOf(), + ) + + data class LastEpisodeToAir( + @JsonProperty("episode_number") val episode_number: Int? = null, + @JsonProperty("season_number") val season_number: Int? = null, + ) + + data class MediaDetail( + @JsonProperty("id") val id: Int? = null, + @JsonProperty("imdb_id") val imdbId: String? = null, + @JsonProperty("title") val title: String? = null, + @JsonProperty("name") val name: String? = null, + @JsonProperty("original_title") val originalTitle: String? = null, + @JsonProperty("original_name") val originalName: String? = null, + @JsonProperty("poster_path") val posterPath: String? = null, + @JsonProperty("backdrop_path") val backdropPath: String? = null, + @JsonProperty("release_date") val releaseDate: String? = null, + @JsonProperty("first_air_date") val firstAirDate: String? = null, + @JsonProperty("overview") val overview: String? = null, + @JsonProperty("runtime") val runtime: Int? = null, + @JsonProperty("vote_average") val vote_average: Any? = null, + @JsonProperty("original_language") val original_language: String? = null, + @JsonProperty("status") val status: String? = null, + @JsonProperty("genres") val genres: ArrayList? = arrayListOf(), + @JsonProperty("keywords") val keywords: KeywordResults? = null, + @JsonProperty("last_episode_to_air") val last_episode_to_air: LastEpisodeToAir? = null, + @JsonProperty("seasons") val seasons: ArrayList? = arrayListOf(), + @JsonProperty("videos") val videos: ResultsTrailer? = null, + @JsonProperty("external_ids") val external_ids: ExternalIds? = null, + @JsonProperty("credits") val credits: Credits? = null, + @JsonProperty("recommendations") val recommendations: ResultsRecommendations? = null, + ) + +} diff --git a/StremioX/src/main/kotlin/com/hexated/StremioXPlugin.kt b/StremioX/src/main/kotlin/com/hexated/StremioXPlugin.kt new file mode 100644 index 00000000..329d8ca3 --- /dev/null +++ b/StremioX/src/main/kotlin/com/hexated/StremioXPlugin.kt @@ -0,0 +1,14 @@ + +package com.hexated + +import com.lagradost.cloudstream3.plugins.CloudstreamPlugin +import com.lagradost.cloudstream3.plugins.Plugin +import android.content.Context + +@CloudstreamPlugin +class StremioXPlugin: Plugin() { + override fun load(context: Context) { + // All providers should be added in this manner. Please don't edit the providers list directly. + registerMainAPI(StremioX()) + } +} \ No newline at end of file