added StremioX

This commit is contained in:
hexated 2023-03-31 04:36:51 +07:00
parent e69a0f141d
commit 43fdd9573f
5 changed files with 636 additions and 0 deletions

26
StremioX/build.gradle.kts Normal file
View File

@ -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%"
}

BIN
StremioX/icon.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 52 KiB

View File

@ -0,0 +1,2 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.hexated"/>

View File

@ -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&region=US" to "Trending",
"$tmdbAPI/movie/popular?api_key=$apiKey&region=US" to "Popular Movies",
"$tmdbAPI/tv/popular?api_key=$apiKey&region=US&with_original_language=en" to "Popular TV Shows",
"$tmdbAPI/tv/airing_today?api_key=$apiKey&region=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&region=US" to "Top Rated Movies",
"$tmdbAPI/tv/top_rated?api_key=$apiKey&region=US" to "Top Rated TV Shows",
"$tmdbAPI/movie/upcoming?api_key=$apiKey&region=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>()?.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<SearchResponse>? = search(query)
override suspend fun search(query: String): List<SearchResponse>? {
return app.get(
"$tmdbAPI/search/multi?api_key=$apiKey&language=en-US&query=$query&page=1&include_adult=${settingsForProvider.enableAdult}"
).parsedSafe<Results>()?.results?.mapNotNull { media ->
media.toSearchResponse()
}
}
override suspend fun load(url: String): LoadResponse? {
val data = parseJson<Data>(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<MediaDetail>()
?: 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<MediaDetailEpisodes>()?.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<LoadData>(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<StreamsResponse>(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<OsResult>()?.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<WatchsomuchResponses>()?.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<WatchsomuchSubResponses>()?.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<String, String> {
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<Stream>)
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<String> = emptyList(),
val subtitles: List<Subtitle> = 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<OsSubtitles>? = 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<WatchsomuchTorrents>? = 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<WatchsomuchSubtitles>? = 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<Media>? = 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<Keywords>? = arrayListOf(),
@JsonProperty("keywords") val keywords: ArrayList<Keywords>? = 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<Episodes>? = arrayListOf(),
)
data class Trailers(
@JsonProperty("key") val key: String? = null,
)
data class ResultsTrailer(
@JsonProperty("results") val results: ArrayList<Trailers>? = 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<Cast>? = arrayListOf(),
)
data class ResultsRecommendations(
@JsonProperty("results") val results: ArrayList<Media>? = 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<Genres>? = arrayListOf(),
@JsonProperty("keywords") val keywords: KeywordResults? = null,
@JsonProperty("last_episode_to_air") val last_episode_to_air: LastEpisodeToAir? = null,
@JsonProperty("seasons") val seasons: ArrayList<Seasons>? = 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,
)
}

View File

@ -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())
}
}