2020-10-08 22:20:20 +02:00
package org.mosad.teapod.parser
2020-10-09 15:18:52 +02:00
import kotlinx.coroutines.*
import org.json.JSONObject
2020-10-08 22:20:20 +02:00
import org.jsoup.Connection
import org.jsoup.Jsoup
import org.mosad.teapod.util.GUIMedia
class AoDParser {
private val baseURL = " https://www.anime-on-demand.de "
private val loginPath = " /users/sign_in "
2020-10-09 15:18:52 +02:00
// TODO
2020-10-08 22:20:20 +02:00
private val login = " "
private val pwd = " "
2020-10-09 15:18:52 +02:00
private var sessionCookies = mutableMapOf < String , String > ( )
private var loginSuccess = false
2020-10-08 22:20:20 +02:00
2020-10-09 15:18:52 +02:00
private fun login ( ) = runBlocking {
2020-10-08 22:20:20 +02:00
val userAgent = " Mozilla/5.0 (X11; Linux x86_64; rv:80.0) Gecko/20100101 Firefox/80.0 "
withContext ( Dispatchers . Default ) {
// get the authenticity token
2020-10-09 15:18:52 +02:00
val resAuth = Jsoup . connect ( baseURL + loginPath )
2020-10-08 22:20:20 +02:00
. header ( " User-Agent " , userAgent )
. execute ( )
val authenticityToken = resAuth . parse ( ) . select ( " meta[name=csrf-token] " ) . attr ( " content " )
println ( " Authenticity token is: $authenticityToken " )
val cookies = resAuth . cookies ( )
println ( " cookies: $cookies " )
val data = mapOf (
Pair ( " user[login] " , login ) ,
Pair ( " user[password] " , pwd ) ,
Pair ( " user[remember_me] " , " 1 " ) ,
Pair ( " commit " , " Einloggen " ) ,
Pair ( " authenticity_token " , authenticityToken )
)
val resLogin = Jsoup . connect ( baseURL + loginPath )
. method ( Connection . Method . POST )
. data ( data )
. postDataCharset ( " UTF-8 " )
. cookies ( cookies )
. execute ( )
//println(resLogin.body())
2020-10-09 15:18:52 +02:00
loginSuccess = resLogin . body ( ) . contains ( " Hallo, du bist jetzt angemeldet. " )
2020-10-08 22:20:20 +02:00
println ( " Status: ${resLogin.statusCode()} ( ${resLogin.statusMessage()} ), login successful: $loginSuccess " )
2020-10-09 15:18:52 +02:00
sessionCookies = resLogin . cookies ( )
2020-10-08 22:20:20 +02:00
}
}
// https://www.anime-on-demand.de/animes
2020-10-09 15:18:52 +02:00
fun listAnimes ( ) : ArrayList < GUIMedia > = runBlocking {
if ( sessionCookies . isEmpty ( ) ) login ( )
2020-10-08 22:20:20 +02:00
2020-10-09 15:18:52 +02:00
withContext ( Dispatchers . Default ) {
2020-10-08 22:20:20 +02:00
val res = Jsoup . connect ( " $baseURL /animes " )
2020-10-09 15:18:52 +02:00
. cookies ( sessionCookies )
2020-10-08 22:20:20 +02:00
. get ( )
//println(res)
2020-10-09 15:18:52 +02:00
val animes = arrayListOf < GUIMedia > ( )
2020-10-08 22:20:20 +02:00
res . select ( " div.animebox " ) . forEach {
val media = GUIMedia (
it . select ( " h3.animebox-title " ) . text ( ) ,
it . select ( " p.animebox-image " ) . select ( " img " ) . attr ( " src " ) ,
2020-10-09 15:18:52 +02:00
it . select ( " p.animebox-shorttext " ) . text ( ) ,
it . select ( " p.animebox-link " ) . select ( " a " ) . attr ( " href " )
2020-10-08 22:20:20 +02:00
)
2020-10-09 15:18:52 +02:00
animes . add ( media )
2020-10-08 22:20:20 +02:00
}
2020-10-09 15:18:52 +02:00
println ( " got ${animes.size} anime " )
return @withContext animes
}
}
2020-10-08 22:20:20 +02:00
2020-10-09 15:18:52 +02:00
fun loadDetails ( mediaPath : String ) = runBlocking {
if ( sessionCookies . isEmpty ( ) ) login ( )
if ( ! loginSuccess ) {
println ( " please log in " )
return @runBlocking
}
withContext ( Dispatchers . Default ) {
println ( baseURL + mediaPath )
val res = Jsoup . connect ( baseURL + mediaPath )
. cookies ( sessionCookies )
. get ( )
//println(res)
val playlists = res . select ( " input.streamstarter_html5 " ) . eachAttr ( " data-playlist " )
println ( playlists . first ( ) )
val csrfToken = res . select ( " meta[name=csrf-token] " ) . attr ( " content " )
println ( " csrf token is: $csrfToken " )
loadStreamInfo ( playlists . first ( ) , csrfToken )
}
}
private fun loadStreamInfo ( playlistPath : String , csrfToken : String ) = runBlocking {
withContext ( Dispatchers . Default ) {
println ( baseURL + playlistPath )
val headers = mutableMapOf (
Pair ( " Accept " , " application/json, text/javascript, */*; q=0.01 " ) ,
Pair ( " Accept-Language " , " de,en-US;q=0.7,en;q=0.3 " ) ,
Pair ( " Accept-Encoding " , " gzip, deflate, br " ) ,
Pair ( " X-CSRF-Token " , csrfToken ) ,
Pair ( " X-Requested-With " , " XMLHttpRequest " ) ,
)
val res = Jsoup . connect ( baseURL + playlistPath )
. ignoreContentType ( true )
. cookies ( sessionCookies )
. headers ( headers )
. execute ( )
//println(res.body())
// TODO replace with gson
val jsonObject = JSONObject ( res . body ( ) )
val sourcesObject = jsonObject . getJSONArray ( " playlist " ) . get ( 0 ) . toString ( )
val sourcesArray = JSONObject ( sourcesObject ) . getJSONArray ( " sources " )
for ( i in 0 until sourcesArray . length ( ) ) {
println ( sourcesArray [ i ] . toString ( ) )
}
2020-10-08 22:20:20 +02:00
}
}
}