|
| 1 | +package eu.kanade.tachiyomi.animeextension.es.ennovelas |
| 2 | + |
| 3 | +import android.app.Application |
| 4 | +import android.content.SharedPreferences |
| 5 | +import androidx.preference.ListPreference |
| 6 | +import androidx.preference.PreferenceScreen |
| 7 | +import eu.kanade.tachiyomi.animesource.ConfigurableAnimeSource |
| 8 | +import eu.kanade.tachiyomi.animesource.model.AnimeFilterList |
| 9 | +import eu.kanade.tachiyomi.animesource.model.SAnime |
| 10 | +import eu.kanade.tachiyomi.animesource.model.SEpisode |
| 11 | +import eu.kanade.tachiyomi.animesource.model.Video |
| 12 | +import eu.kanade.tachiyomi.animesource.online.ParsedAnimeHttpSource |
| 13 | +import eu.kanade.tachiyomi.network.GET |
| 14 | +import eu.kanade.tachiyomi.util.asJsoup |
| 15 | +import okhttp3.OkHttpClient |
| 16 | +import okhttp3.Request |
| 17 | +import okhttp3.Response |
| 18 | +import org.jsoup.nodes.Document |
| 19 | +import org.jsoup.nodes.Element |
| 20 | +import uy.kohesive.injekt.Injekt |
| 21 | +import uy.kohesive.injekt.api.get |
| 22 | +import java.lang.Exception |
| 23 | +import java.text.Normalizer |
| 24 | + |
| 25 | +class EnNovelas : ConfigurableAnimeSource, ParsedAnimeHttpSource() { |
| 26 | + |
| 27 | + override val name = "EnNovelas" |
| 28 | + |
| 29 | + override val baseUrl = "https://www.ennovelas.com" |
| 30 | + |
| 31 | + override val lang = "es" |
| 32 | + |
| 33 | + override val supportsLatest = false |
| 34 | + |
| 35 | + override val client: OkHttpClient = network.cloudflareClient |
| 36 | + |
| 37 | + private val preferences: SharedPreferences by lazy { |
| 38 | + Injekt.get<Application>().getSharedPreferences("source_$id", 0x0000) |
| 39 | + } |
| 40 | + |
| 41 | + override fun popularAnimeSelector(): String = "#container section.search-videos div.section-content div.row div div.col-xs-6 div.video-post" |
| 42 | + |
| 43 | + override fun popularAnimeRequest(page: Int): Request = GET("$baseUrl/?op=categories_all&per_page=60&page=$page") |
| 44 | + |
| 45 | + override fun popularAnimeFromElement(element: Element): SAnime { |
| 46 | + val anime = SAnime.create() |
| 47 | + anime.setUrlWithoutDomain(changeUrlFormat(element.select("a").attr("href"))) |
| 48 | + anime.title = element.select("a p").text() |
| 49 | + anime.thumbnail_url = element.select("a div.thumb").attr("style") |
| 50 | + .substringAfter("background-image:url(").substringBefore(")") |
| 51 | + anime.description = "" |
| 52 | + return anime |
| 53 | + } |
| 54 | + |
| 55 | + private fun changeUrlFormat(link: String): String { |
| 56 | + val novel = link.substringAfter("/category/").replace("+", "%20") |
| 57 | + return "$baseUrl/?cat_name=$novel&op=search&per_page=all" |
| 58 | + } |
| 59 | + |
| 60 | + override fun popularAnimeNextPageSelector(): String = "#container section div.section-content div.paging a:last-of-type" |
| 61 | + |
| 62 | + override fun episodeListParse(response: Response): List<SEpisode> { |
| 63 | + val document = response.asJsoup() |
| 64 | + val episodeList = mutableListOf<SEpisode>() |
| 65 | + document.select("#col3 div.videobox").forEach { element -> |
| 66 | + val ep = SEpisode.create() |
| 67 | + val noEpisode = getNumberFromEpsString( |
| 68 | + element.selectFirst("a:nth-child(2)").text().substringAfter("Cap") |
| 69 | + .substringBefore("FIN").substringBefore("fin") |
| 70 | + ) |
| 71 | + ep.setUrlWithoutDomain(element.selectFirst("a.video200").attr("href")) |
| 72 | + ep.name = "Cap" + element.selectFirst("a:nth-child(2)").text().substringAfter("Cap") |
| 73 | + ep.episode_number = noEpisode.toFloat() |
| 74 | + episodeList.add(ep) |
| 75 | + } |
| 76 | + return episodeList.sortedByDescending { x -> x.episode_number } |
| 77 | + } |
| 78 | + |
| 79 | + override fun episodeListSelector() = "uwu" |
| 80 | + |
| 81 | + override fun episodeFromElement(element: Element) = throw Exception("not used") |
| 82 | + |
| 83 | + private fun getNumberFromEpsString(epsStr: String): String { |
| 84 | + return epsStr.filter { it.isDigit() } |
| 85 | + } |
| 86 | + |
| 87 | + override fun videoListParse(response: Response): List<Video> { |
| 88 | + val document = response.asJsoup() |
| 89 | + val videoList = mutableListOf<Video>() |
| 90 | + document.select("script").forEach { script -> |
| 91 | + if (script.data().contains("window.hola_player({")) { |
| 92 | + val url = script.data().substringAfter("sources: [{src: \"").substringBefore("\",") |
| 93 | + val quality = script.data().substringAfter("res: ").substringBefore(",") |
| 94 | + videoList.add(Video(url, "${quality}p", url, headers = null)) |
| 95 | + } |
| 96 | + } |
| 97 | + return videoList |
| 98 | + } |
| 99 | + |
| 100 | + override fun videoListSelector() = throw Exception("not used") |
| 101 | + |
| 102 | + override fun videoUrlParse(document: Document) = throw Exception("not used") |
| 103 | + |
| 104 | + override fun videoFromElement(element: Element) = throw Exception("not used") |
| 105 | + |
| 106 | + override fun List<Video>.sort(): List<Video> { |
| 107 | + val quality = preferences.getString("preferred_quality", "720p") |
| 108 | + if (quality != null) { |
| 109 | + val newList = mutableListOf<Video>() |
| 110 | + var preferred = 0 |
| 111 | + for (video in this) { |
| 112 | + if (video.quality == quality) { |
| 113 | + newList.add(preferred, video) |
| 114 | + preferred++ |
| 115 | + } else { |
| 116 | + newList.add(video) |
| 117 | + } |
| 118 | + } |
| 119 | + return newList |
| 120 | + } |
| 121 | + return this |
| 122 | + } |
| 123 | + |
| 124 | + override fun searchAnimeRequest(page: Int, query: String, filters: AnimeFilterList): Request { |
| 125 | + return when { |
| 126 | + query.isNotBlank() -> GET("$baseUrl/?name=$query&op=categories_all&page=$page") |
| 127 | + else -> popularAnimeRequest(page) |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + override fun searchAnimeFromElement(element: Element): SAnime { |
| 132 | + return popularAnimeFromElement(element) |
| 133 | + } |
| 134 | + |
| 135 | + override fun searchAnimeNextPageSelector(): String = popularAnimeNextPageSelector() |
| 136 | + |
| 137 | + override fun searchAnimeSelector(): String = popularAnimeSelector() |
| 138 | + |
| 139 | + override fun animeDetailsParse(document: Document): SAnime { |
| 140 | + val descriptionElement = document.selectFirst("#inwg").text() |
| 141 | + .substringAfter("Notifications:") |
| 142 | + .substringBefore("Capitulo") |
| 143 | + .substringBefore("Capítulo") |
| 144 | + val anime = SAnime.create() |
| 145 | + val title = document.selectFirst("#inwg h3 span.first-word").text() |
| 146 | + val idx = idxDescription(descriptionElement, title) |
| 147 | + val description = descriptionElement.substring(0, idx).trim() |
| 148 | + |
| 149 | + anime.title = title.trim() |
| 150 | + anime.description = description.ifEmpty { title } |
| 151 | + anime.genre = "novela" |
| 152 | + anime.status = SAnime.UNKNOWN |
| 153 | + return anime |
| 154 | + } |
| 155 | + |
| 156 | + private fun String.removeNonSpacingMarks() = Normalizer.normalize(this, Normalizer.Form.NFD).replace("\\p{Mn}+".toRegex(), "") |
| 157 | + |
| 158 | + private fun idxDescription(text: String, title: String): Int { |
| 159 | + val descriptionLowercase = text.lowercase().removeNonSpacingMarks() |
| 160 | + val titleLowercase = title.lowercase().removeNonSpacingMarks() |
| 161 | + val idx = descriptionLowercase.lastIndexOf(titleLowercase) |
| 162 | + return if (idx == -1) descriptionLowercase.length - 1 else idx |
| 163 | + } |
| 164 | + |
| 165 | + override fun latestUpdatesNextPageSelector() = popularAnimeNextPageSelector() |
| 166 | + |
| 167 | + override fun latestUpdatesFromElement(element: Element) = popularAnimeFromElement(element) |
| 168 | + |
| 169 | + override fun latestUpdatesRequest(page: Int) = GET("$baseUrl/browse?order=added&page=$page") |
| 170 | + |
| 171 | + override fun latestUpdatesSelector() = popularAnimeSelector() |
| 172 | + |
| 173 | + override fun setupPreferenceScreen(screen: PreferenceScreen) { |
| 174 | + val videoQualityPref = ListPreference(screen.context).apply { |
| 175 | + key = "preferred_quality" |
| 176 | + title = "Preferred quality" |
| 177 | + entries = arrayOf("1080p", "720p", "480p") |
| 178 | + entryValues = arrayOf("1080p", "720p", "480p") |
| 179 | + setDefaultValue("720p") |
| 180 | + summary = "%s" |
| 181 | + |
| 182 | + setOnPreferenceChangeListener { _, newValue -> |
| 183 | + val selected = newValue as String |
| 184 | + val index = findIndexOfValue(selected) |
| 185 | + val entry = entryValues[index] as String |
| 186 | + preferences.edit().putString(key, entry).commit() |
| 187 | + } |
| 188 | + } |
| 189 | + screen.addPreference(videoQualityPref) |
| 190 | + } |
| 191 | +} |
0 commit comments