Initial commit

This commit is contained in:
almightyhak 2024-06-20 11:54:12 +07:00
commit 98ed7e8839
2263 changed files with 108711 additions and 0 deletions

View file

@ -0,0 +1,11 @@
ext {
extName = 'PutLocker'
extClass = '.PutLocker'
extVersionCode = 8
}
apply from: "$rootDir/common.gradle"
dependencies {
implementation project(':lib:playlist-utils')
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 38 KiB

View file

@ -0,0 +1,157 @@
package eu.kanade.tachiyomi.animeextension.en.putlocker
import android.util.Base64
import java.security.MessageDigest
import java.security.SecureRandom
import java.util.Arrays
import javax.crypto.Cipher
import javax.crypto.spec.IvParameterSpec
import javax.crypto.spec.SecretKeySpec
import kotlin.math.min
/**
* Conforming with CryptoJS AES method
* https://stackoverflow.com/questions/49384768/encrypt-from-android-and-decrypt-in-cryptojs#63701411
*
* see https://gist.github.com/thackerronak/554c985c3001b16810af5fc0eb5c358f
*/
@Suppress("unused", "FunctionName")
object CryptoAES {
private const val KEY_SIZE = 256
private const val IV_SIZE = 128
private const val HASH_CIPHER = "AES/CBC/PKCS7Padding"
private const val AES = "AES"
private const val KDF_DIGEST = "MD5"
// Seriously crypto-js, what's wrong with you?
private const val APPEND = "Salted__"
/**
* Encrypt
* @param password passphrase
* @param plainText plain string
*/
fun encrypt(password: String, plainText: String): CipherResult {
val saltBytes = generateSalt(8)
val key = ByteArray(KEY_SIZE / 8)
val iv = ByteArray(IV_SIZE / 8)
EvpKDF(password.toByteArray(), KEY_SIZE, IV_SIZE, saltBytes, key, iv)
val keyS = SecretKeySpec(key, AES)
val cipher = Cipher.getInstance(HASH_CIPHER)
val ivSpec = IvParameterSpec(iv)
cipher.init(Cipher.ENCRYPT_MODE, keyS, ivSpec)
val cipherText = cipher.doFinal(plainText.toByteArray())
// Thanks kientux for this: https://gist.github.com/kientux/bb48259c6f2133e628ad
// Create CryptoJS-like encrypted!
// val sBytes = APPEND.toByteArray()
// val b = ByteArray(sBytes.size + saltBytes.size + cipherText.size)
// System.arraycopy(sBytes, 0, b, 0, sBytes.size)
// System.arraycopy(saltBytes, 0, b, sBytes.size, saltBytes.size)
// System.arraycopy(cipherText, 0, b, sBytes.size + saltBytes.size, cipherText.size)
// val bEncode = Base64.encode(b, Base64.NO_WRAP)
// return String(bEncode)
// For our use case we don't need the first 16 bytes cause we are sending the salt and iv separately
// I'm leaving it as is and taking what is needed in case anyone want's to use the original code
val bEncode = Base64.encode(cipherText, Base64.NO_WRAP)
return CipherResult(
String(bEncode).toHex(),
password.toHex(),
saltBytes.toHex(),
iv.toHex(),
)
}
private fun ByteArray.toHex(): String = joinToString(separator = "") { eachByte -> "%02x".format(eachByte) }
private fun String.toHex(): String = toByteArray().toHex()
data class CipherResult(
val cipherText: String,
val password: String,
val salt: String,
val iv: String,
)
/**
* Decrypt
* Thanks Artjom B. for this: http://stackoverflow.com/a/29152379/4405051
* @param password passphrase
* @param cipherText encrypted string
*/
fun decrypt(password: String, cipherText: String): String {
val ctBytes = Base64.decode(cipherText.toByteArray(), Base64.NO_WRAP)
val saltBytes = Arrays.copyOfRange(ctBytes, 8, 16)
val cipherTextBytes = Arrays.copyOfRange(ctBytes, 16, ctBytes.size)
val key = ByteArray(KEY_SIZE / 8)
val iv = ByteArray(IV_SIZE / 8)
EvpKDF(password.toByteArray(), KEY_SIZE, IV_SIZE, saltBytes, key, iv)
val cipher = Cipher.getInstance(HASH_CIPHER)
val keyS = SecretKeySpec(key, AES)
cipher.init(Cipher.DECRYPT_MODE, keyS, IvParameterSpec(iv))
val plainText = cipher.doFinal(cipherTextBytes)
return String(plainText)
}
private fun EvpKDF(
password: ByteArray,
keySize: Int,
ivSize: Int,
salt: ByteArray,
resultKey: ByteArray,
resultIv: ByteArray,
): ByteArray {
return EvpKDF(password, keySize, ivSize, salt, 1, KDF_DIGEST, resultKey, resultIv)
}
private fun EvpKDF(
password: ByteArray,
keySize: Int,
ivSize: Int,
salt: ByteArray,
iterations: Int,
hashAlgorithm: String,
resultKey: ByteArray,
resultIv: ByteArray,
): ByteArray {
val keySize = keySize / 32
val ivSize = ivSize / 32
val targetKeySize = keySize + ivSize
val derivedBytes = ByteArray(targetKeySize * 4)
var numberOfDerivedWords = 0
var block: ByteArray? = null
val hash = MessageDigest.getInstance(hashAlgorithm)
while (numberOfDerivedWords < targetKeySize) {
if (block != null) {
hash.update(block)
}
hash.update(password)
block = hash.digest(salt)
hash.reset()
// Iterations
for (i in 1 until iterations) {
block = hash.digest(block!!)
hash.reset()
}
System.arraycopy(
block!!,
0,
derivedBytes,
numberOfDerivedWords * 4,
min(block.size, (targetKeySize - numberOfDerivedWords) * 4),
)
numberOfDerivedWords += block.size / 4
}
System.arraycopy(derivedBytes, 0, resultKey, 0, keySize * 4)
System.arraycopy(derivedBytes, keySize * 4, resultIv, 0, ivSize * 4)
return derivedBytes // key + iv
}
private fun generateSalt(length: Int): ByteArray {
return ByteArray(length).apply {
SecureRandom().nextBytes(this)
}
}
}

View file

@ -0,0 +1,76 @@
package eu.kanade.tachiyomi.animeextension.en.putlocker
object JSONUtil {
fun escape(input: String): String {
val output = StringBuilder()
for (ch in input) {
// let's not put any nulls in our strings
val charInt = ch.code
assert(charInt != 0)
// 0x10000 = 65536 = 2^16 = u16 max value
assert(charInt < 0x10000) { "Java stores as u16, so it should never give us a character that's bigger than 2 bytes. It literally can't." }
val escapedChar = when (ch) {
'\b' -> "\\b"
'\u000C' -> "\\f" // '\u000C' == '\f', Kotlin doesnt support \f
'\n' -> "\\n"
'\r' -> "\\r"
'\t' -> "\\t"
'\\' -> "\\\\"
'"' -> "\\\""
else -> {
if (charInt > 127) {
String.format("\\u%04x", charInt)
} else {
ch
}
}
}
output.append(escapedChar)
}
return output.toString()
}
fun unescape(input: String): String {
val builder = StringBuilder()
var index = 0
while (index < input.length) {
val delimiter = input.get(index) // consume letter or backslash
index++
if (delimiter == '\\' && index < input.length) {
// consume first after backslash
val ch = input.get(index)
index++
val unescaped = when (ch) {
'\\', '/', '"', '\'' -> ch // "
'b' -> '\b'
'f' -> '\u000C' // '\f' in java
'n' -> '\n'
'r' -> '\r'
't' -> '\t'
'u' -> {
if (index + 4 > input.length) {
throw RuntimeException("Not enough unicode digits!")
}
val hex = input.substring(index, index + 4)
if (hex.any { !it.isLetterOrDigit() }) {
throw RuntimeException("Bad character in unicode escape.")
}
hex.toInt(16).toChar()
}
else -> throw RuntimeException("Illegal escape sequence: \\" + ch)
}
builder.append(unescaped)
} else {
builder.append(delimiter)
}
}
return builder.toString()
}
}

View file

@ -0,0 +1,274 @@
package eu.kanade.tachiyomi.animeextension.en.putlocker
import android.app.Application
import android.content.SharedPreferences
import androidx.preference.ListPreference
import androidx.preference.PreferenceScreen
import eu.kanade.tachiyomi.animeextension.en.putlocker.extractors.PutServerExtractor
import eu.kanade.tachiyomi.animesource.ConfigurableAnimeSource
import eu.kanade.tachiyomi.animesource.model.AnimeFilterList
import eu.kanade.tachiyomi.animesource.model.SAnime
import eu.kanade.tachiyomi.animesource.model.SEpisode
import eu.kanade.tachiyomi.animesource.model.Video
import eu.kanade.tachiyomi.animesource.online.ParsedAnimeHttpSource
import eu.kanade.tachiyomi.network.GET
import eu.kanade.tachiyomi.util.asJsoup
import eu.kanade.tachiyomi.util.parallelCatchingFlatMap
import eu.kanade.tachiyomi.util.parallelCatchingFlatMapBlocking
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.jsonPrimitive
import okhttp3.Request
import okhttp3.Response
import org.jsoup.Jsoup
import org.jsoup.nodes.Document
import org.jsoup.nodes.Element
import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get
import uy.kohesive.injekt.injectLazy
@ExperimentalSerializationApi
class PutLocker : ConfigurableAnimeSource, ParsedAnimeHttpSource() {
override val name = "PutLocker"
override val baseUrl = "https://ww7.putlocker.vip"
override val lang = "en"
override val supportsLatest = true
private val json: Json by injectLazy()
private val preferences: SharedPreferences by lazy {
Injekt.get<Application>().getSharedPreferences("source_$id", 0x0000)
}
private val putServerExtractor by lazy { PutServerExtractor(client) }
// ============================== Popular ===============================
override fun popularAnimeRequest(page: Int): Request = GET("$baseUrl/putlocker/")
override fun popularAnimeSelector(): String =
"div#tab-movie > div.ml-item, div#tab-tv-show > div.ml-item"
override fun popularAnimeNextPageSelector(): String? = null
override fun popularAnimeFromElement(element: Element): SAnime {
return SAnime.create().apply {
setUrlWithoutDomain(
element.select("div.mli-poster > a")
.attr("abs:href"),
)
title = element.select("div.mli-info h3").text()
thumbnail_url = element.select("div.mli-poster > a > img")
.attr("abs:data-original")
}
}
// =============================== Latest ===============================
override fun latestUpdatesRequest(page: Int): Request =
GET("$baseUrl/filter/$page?genre=all&country=all&types=all&year=all&sort=updated")
override fun latestUpdatesSelector(): String = "div.movies-list > div.ml-item"
override fun latestUpdatesNextPageSelector(): String = "div#pagination li.active ~ li"
override fun latestUpdatesFromElement(element: Element): SAnime = popularAnimeFromElement(element)
// =============================== Search ===============================
override fun searchAnimeRequest(page: Int, query: String, filters: AnimeFilterList): Request {
return "[^A-Za-z0-9 ]".toRegex()
.replace(query, "")
.replace(" ", "+")
.lowercase()
.let {
GET("$baseUrl/movie/search/$it/$page/")
}
}
override fun searchAnimeSelector(): String = latestUpdatesSelector()
override fun searchAnimeNextPageSelector(): String = latestUpdatesNextPageSelector()
override fun searchAnimeFromElement(element: Element): SAnime = popularAnimeFromElement(element)
// =========================== Anime Details ============================
override fun animeDetailsParse(document: Document): SAnime = SAnime.create().apply {
document.select("div.mvic-desc").let { descElement ->
val mLeft = descElement
.select("div.mvic-info > div.mvici-left")
val mRight = descElement
.select("div.mvic-info > div.mvici-right")
status = SAnime.COMPLETED
genre = mLeft.select("p:contains(Genre) a").joinToString { it.text() }
author = mLeft.select("p:contains(Production) a").first()?.text()
description = buildString {
appendLine(descElement.select("div.desc").text())
appendLine()
appendLine(mLeft.select("p:contains(Production) a").joinToString { it.text() })
appendLine(mLeft.select("p:contains(Country)").text())
mRight.select("p").mapNotNull { appendLine(it.text()) }
}
}
}
// ============================== Episodes ==============================
override fun episodeListRequest(anime: SAnime): Request =
GET("$baseUrl${anime.url}/watching.html")
override fun episodeListParse(response: Response): List<SEpisode> {
val doc = response.asJsoup()
val (type, mediaId) = doc.selectFirst("script:containsData(total_episode)")
?.data()
?.let {
val t = it
.substringBefore("name:")
.substringAfter("type:")
.replace(",", "")
.trim()
val mId = it
.substringAfter("id:")
.substringBefore(",")
.replace("\"", "")
.trim()
Pair(t, mId)
} ?: return emptyList()
return when (type) {
"1" -> {
listOf(
SEpisode.create().apply {
url = EpLinks(
dataId = "1_full",
mediaId = mediaId,
).toJson()
name = "Movie"
episode_number = 1F
},
)
}
else -> {
client.newCall(
GET("$baseUrl/ajax/movie/seasons/$mediaId"),
).execute()
.body.string()
.parseHtml()
.select("div.dropdown-menu > a")
.mapNotNull { it.attr("data-id") }
.sortedDescending()
.parallelCatchingFlatMapBlocking { season ->
client.newCall(
GET("$baseUrl/ajax/movie/season/episodes/${mediaId}_$season"),
).execute()
.body.string()
.parseHtml()
.select("a")
.mapNotNull { elem ->
val dataId = elem.attr("data-id")
val epFloat = dataId
.substringAfter("_")
.toFloatOrNull()
?: 0F
SEpisode.create().apply {
url = EpLinks(
dataId = dataId,
mediaId = mediaId,
).toJson()
name = "Season $season ${elem.text()}"
episode_number = epFloat
}
}.sortedByDescending { it.episode_number }
}
}
}
}
override fun episodeListSelector(): String = throw UnsupportedOperationException()
override fun episodeFromElement(element: Element): SEpisode = throw UnsupportedOperationException()
// ============================ Video Links =============================
override suspend fun getVideoList(episode: SEpisode): List<Video> {
val media = json.decodeFromString<EpLinks>(episode.url)
return client.newCall(
GET("$baseUrl/ajax/movie/episode/servers/${media.mediaId}_${media.dataId}"),
).execute()
.body.string()
.parseHtml()
.select("a")
.mapNotNull { elem ->
Triple(
elem.attr("data-name"),
elem.attr("data-id"),
elem.text(),
)
}
.parallelCatchingFlatMap { putServerExtractor.extractVideo(it, baseUrl) }
.sort()
}
override fun videoFromElement(element: Element): Video = throw UnsupportedOperationException()
override fun videoListSelector(): String = throw UnsupportedOperationException()
override fun videoUrlParse(document: Document): String = throw UnsupportedOperationException()
// ============================= Utilities ==============================
private fun EpLinks.toJson(): String = json.encodeToString(this)
private fun String.parseHtml(): Document =
json.decodeFromString<JsonObject>(this@parseHtml)["html"]!!
.jsonPrimitive.content.run {
Jsoup.parse(JSONUtil.unescape(this@run))
}
override fun List<Video>.sort(): List<Video> {
val quality = preferences.getString("preferred_quality", null)
val newList = mutableListOf<Video>()
if (quality != null) {
var preferred = 0
for (video in this) {
if (video.quality.contains(quality)) {
newList.add(preferred, video)
preferred++
} else {
newList.add(video)
}
}
return newList
}
return this
}
override fun setupPreferenceScreen(screen: PreferenceScreen) {
val videoQualityPref = ListPreference(screen.context).apply {
key = "preferred_quality"
title = "Preferred quality"
entries = arrayOf("1080p", "720p", "480p", "360p")
entryValues = arrayOf("1080", "720", "480", "360")
setDefaultValue("1080")
summary = "%s"
setOnPreferenceChangeListener { _, newValue ->
val selected = newValue as String
val index = findIndexOfValue(selected)
val entry = entryValues[index] as String
preferences.edit().putString(key, entry).commit()
}
}
screen.addPreference(videoQualityPref)
}
}

View file

@ -0,0 +1,34 @@
package eu.kanade.tachiyomi.animeextension.en.putlocker
import kotlinx.serialization.Serializable
@Serializable
data class EpLinks(
val dataId: String,
val mediaId: String,
)
@Serializable
data class EpResp(
val status: Boolean,
val src: String,
)
@Serializable
data class VidSource(
val file: String,
val type: String?,
)
@Serializable
data class SubTrack(
val file: String,
val label: String?,
)
@Serializable
data class Sources(
val sources: List<VidSource>,
val tracks: List<SubTrack>?,
val backupLink: String?,
)

View file

@ -0,0 +1,114 @@
package eu.kanade.tachiyomi.animeextension.en.putlocker.extractors
import eu.kanade.tachiyomi.animeextension.en.putlocker.CryptoAES
import eu.kanade.tachiyomi.animeextension.en.putlocker.EpResp
import eu.kanade.tachiyomi.animeextension.en.putlocker.Sources
import eu.kanade.tachiyomi.animeextension.en.putlocker.SubTrack
import eu.kanade.tachiyomi.animeextension.en.putlocker.VidSource
import eu.kanade.tachiyomi.animesource.model.Track
import eu.kanade.tachiyomi.animesource.model.Video
import eu.kanade.tachiyomi.lib.playlistutils.PlaylistUtils
import eu.kanade.tachiyomi.network.GET
import eu.kanade.tachiyomi.util.asJsoup
import eu.kanade.tachiyomi.util.parseAs
import kotlinx.serialization.json.Json
import okhttp3.Headers
import okhttp3.HttpUrl.Companion.toHttpUrl
import okhttp3.OkHttpClient
import uy.kohesive.injekt.injectLazy
class PutServerExtractor(private val client: OkHttpClient) {
private val json: Json by injectLazy()
private val playlistUtils by lazy { PlaylistUtils(client) }
fun extractVideo(videoData: Triple<String, String, String>, baseUrl: String): List<Video> {
val embedUrl = client.newCall(
GET("$baseUrl/ajax/movie/episode/server/sources/${videoData.second}_${videoData.first}"),
).execute()
.parseAs<EpResp>()
.src
val vidReferer = Headers.headersOf("Referer", "$baseUrl/")
val vidResponse = extractVideoEmbed(embedUrl, vidReferer)
if (!vidResponse.contains("sources")) return emptyList()
val vidJson = json.decodeFromString<Sources>(vidResponse)
return vidJson.sources.flatMap { source ->
extractVideoLinks(
source,
"$baseUrl/",
extractSubs(vidJson.tracks),
videoData.third,
)
}.ifEmpty {
if (!vidJson.backupLink.isNullOrBlank()) {
vidJson.backupLink.let { bakUrl ->
val bakResponse = extractVideoEmbed(bakUrl, vidReferer)
if (bakResponse.contains("sources")) {
val bakJson = json.decodeFromString<Sources>(bakResponse)
bakJson.sources.flatMap { bakSource ->
extractVideoLinks(
bakSource,
"$baseUrl/",
extractSubs(bakJson.tracks),
"${videoData.third} - Backup",
)
}
} else {
emptyList()
}
}
} else {
emptyList()
}
}
}
private fun extractSubs(tracks: List<SubTrack>?): List<Track> {
return tracks?.mapNotNull { sub ->
Track(
sub.file,
sub.label ?: return@mapNotNull null,
)
} ?: emptyList()
}
private fun extractVideoEmbed(embedUrl: String, referer: Headers): String {
val embedHost = embedUrl.substringBefore("/embed-player")
val playerResp = client.newCall(GET(embedUrl, referer)).execute().asJsoup()
val player = playerResp.select("div#player")
val vidId = "\"${player.attr("data-id")}\""
val vidHash = player.attr("data-hash")
val cipher = CryptoAES.encrypt(vidHash, vidId)
val vidUrl = "$embedHost/ajax/getSources/".toHttpUrl().newBuilder()
.addQueryParameter("id", cipher.cipherText)
.addQueryParameter("h", cipher.password)
.addQueryParameter("a", cipher.iv)
.addQueryParameter("t", cipher.salt)
.build().toString()
return client.newCall(GET(vidUrl, referer)).execute().body.string()
}
private fun extractVideoLinks(source: VidSource, vidReferer: String, subsList: List<Track>, serverId: String): List<Video> {
return if (source.file.endsWith(".m3u8")) {
playlistUtils.extractFromHls(
source.file,
vidReferer,
videoNameGen = { q -> "$serverId - $q" },
subtitleList = subsList,
)
} else {
listOf(
Video(
source.file,
"$serverId (${source.type})",
source.file,
subtitleTracks = subsList,
),
)
}
}
}