1
+ package com.lagradost.cloudstreamtest
2
+
3
+ import com.lagradost.cloudstream3.AnimeLoadResponse
4
+ import com.lagradost.cloudstream3.HomePageResponse
5
+ import com.lagradost.cloudstream3.LiveStreamLoadResponse
6
+ import com.lagradost.cloudstream3.LoadResponse
7
+ import com.lagradost.cloudstream3.MainAPI
8
+ import com.lagradost.cloudstream3.MainPageRequest
9
+ import com.lagradost.cloudstream3.MovieLoadResponse
10
+ import com.lagradost.cloudstream3.SearchResponse
11
+ import com.lagradost.cloudstream3.SubtitleFile
12
+ import com.lagradost.cloudstream3.TvSeriesLoadResponse
13
+ import com.lagradost.cloudstream3.utils.ExtractorLink
14
+ import java.lang.RuntimeException
15
+
16
+ /* *
17
+ * Simple and easy testing class for providers.
18
+ * Should really be expanded to be able to test all providers properly.
19
+ * @see testAll
20
+ */
21
+ open class ProviderTester (private val provider : MainAPI ) {
22
+ suspend fun testSearch (query : String , verbose : Boolean = false): List <SearchResponse > {
23
+ val responses = provider.search(query) ? : emptyList()
24
+ println (" Response count: ${responses.size} , Query: $query " )
25
+ printSearchResponses(responses, verbose)
26
+ return responses
27
+ }
28
+
29
+ private fun printSearchResponses (responses : List <SearchResponse >, verbose : Boolean ) {
30
+ if (verbose) {
31
+ println (" Responses:\n ${responses.joinToString(" \n " )} " )
32
+ } else {
33
+ println (" Responses:\n ${responses.map { it.name to it.url }.joinToString(" \n " )} " )
34
+ }
35
+ }
36
+
37
+ suspend fun testMainPage (verbose : Boolean = false): List <HomePageResponse > {
38
+ if (! provider.hasMainPage) {
39
+ throw RuntimeException (" Provider does not have a main page!" )
40
+ }
41
+ val responses = provider.mainPage.map { request ->
42
+ provider.getMainPage(1 , MainPageRequest (request.name, request.data, false ))
43
+ }.ifEmpty { listOf (provider.getMainPage(1 , MainPageRequest (" " , " " , false ))) }
44
+ .mapNotNull { it }
45
+
46
+ responses.map { it.items }.flatten().forEach {
47
+ println (" Main page: ${it.name} , Item count: ${it.list.size} " )
48
+ printSearchResponses(it.list, verbose)
49
+ }
50
+ return responses
51
+ }
52
+
53
+ suspend fun testLoad (url : String ): LoadResponse ? {
54
+ println (" Loading response from: $url " )
55
+ val response = provider.load(url)
56
+ println (" Loaded response: $response " )
57
+ return response
58
+ }
59
+
60
+ suspend fun testLoadLinks (data : String ): Pair <List <ExtractorLink >, List<SubtitleFile>> {
61
+ val subtitles = mutableListOf<SubtitleFile >()
62
+ val links = mutableListOf<ExtractorLink >()
63
+ provider.loadLinks(data, false , { file ->
64
+ subtitles.add(file)
65
+ }, { link ->
66
+ links.add(link)
67
+ })
68
+
69
+ println (" Links count: ${links.size} , Subtitles count: ${subtitles.size} " )
70
+ println (" Links: ${links.joinToString(" \n " )} " )
71
+ println (" Subtitles: ${subtitles.joinToString(" \n " )} " )
72
+
73
+ return links to subtitles
74
+ }
75
+
76
+ suspend fun testAll (query : String? = null) {
77
+ val response = if (provider.hasMainPage) {
78
+ println (" Testing Main Page: -------------------" )
79
+ val mainPage = testMainPage()
80
+ val item = mainPage.first().items.first().list.first()
81
+ println (" \n\n Testing Search: -------------------" )
82
+
83
+ val searchResponses = testSearch(item.name)
84
+ assert (searchResponses.isNotEmpty())
85
+
86
+ item
87
+ } else if (query != null ) {
88
+ println (" Testing Search: -------------------" )
89
+
90
+ testSearch(query).first()
91
+ } else {
92
+ throw RuntimeException (" Cannot test everything without a query or a homepage" )
93
+ }
94
+ println (" \n\n Testing load: -------------------" )
95
+
96
+ val loadResponse = testLoad(response.url)
97
+ assert (loadResponse != null )
98
+ if (loadResponse!! .url != response.url) {
99
+ println (" Testing bookmark functionality" )
100
+ val secondResponse = testLoad(loadResponse.url)
101
+ assert (secondResponse != null )
102
+ }
103
+
104
+ val data = when (loadResponse) {
105
+ is AnimeLoadResponse -> {
106
+ loadResponse.episodes.values.first().first().data
107
+ }
108
+
109
+ is MovieLoadResponse -> {
110
+ loadResponse.dataUrl
111
+ }
112
+
113
+ is LiveStreamLoadResponse -> {
114
+ loadResponse.dataUrl
115
+ }
116
+
117
+ is TvSeriesLoadResponse -> {
118
+ loadResponse.episodes.first().data
119
+ }
120
+
121
+ else -> {
122
+ throw RuntimeException (" Unknown load response: ${loadResponse::class .simpleName} " )
123
+ }
124
+ }
125
+
126
+ println (" \n\n Testing LoadLinks: -------------------" )
127
+ val (links, subs) = testLoadLinks(data)
128
+ }
129
+ }
0 commit comments