@ -12,13 +12,13 @@ import com.deniscerri.ytdlnis.database.models.ChapterItem
import com.deniscerri.ytdlnis.database.models.Format
import com.deniscerri.ytdlnis.database.models.Format
import com.deniscerri.ytdlnis.database.models.ResultItem
import com.deniscerri.ytdlnis.database.models.ResultItem
import com.google.gson.Gson
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import com.google.gson.reflect.TypeToken
import com.google.gson.reflect.TypeToken
import com.yausername.youtubedl_android.YoutubeDL
import com.yausername.youtubedl_android.YoutubeDL
import com.yausername.youtubedl_android.YoutubeDLRequest
import com.yausername.youtubedl_android.YoutubeDLRequest
import org.json.JSONArray
import org.json.JSONArray
import org.json.JSONException
import org.json.JSONException
import org.json.JSONObject
import org.json.JSONObject
import retrofit2.Retrofit
import java.io.BufferedReader
import java.io.BufferedReader
import java.io.File
import java.io.File
import java.io.InputStreamReader
import java.io.InputStreamReader
@ -32,7 +32,6 @@ import java.util.regex.Pattern
class InfoUtil ( private val context : Context ) {
class InfoUtil ( private val context : Context ) {
private var items : ArrayList < ResultItem ? >
private var items : ArrayList < ResultItem ? >
private lateinit var sharedPreferences : SharedPreferences
private lateinit var sharedPreferences : SharedPreferences
private lateinit var retrofit : Retrofit
private var key : String ? = null
private var key : String ? = null
init {
init {
@ -146,7 +145,9 @@ class InfoUtil(private val context: Context) {
var nextpage = res . getString ( " nextpage " )
var nextpage = res . getString ( " nextpage " )
for ( i in 0 until dataArray . length ( ) ) {
for ( i in 0 until dataArray . length ( ) ) {
val obj = dataArray . getJSONObject ( i )
val obj = dataArray . getJSONObject ( i )
items . add ( createVideoFromPipedJSON ( obj , obj . getString ( " url " ) . removePrefix ( " /watch?v= " ) ) )
val itm = createVideoFromPipedJSON ( obj , obj . getString ( " url " ) . removePrefix ( " /watch?v= " ) )
itm ?. playlistTitle = " YTDLnis "
items . add ( itm )
}
}
if ( nextpage == " null " ) nextpage = " "
if ( nextpage == " null " ) nextpage = " "
return PlaylistTuple ( nextpage , items )
return PlaylistTuple ( nextpage , items )
@ -211,20 +212,20 @@ class InfoUtil(private val context: Context) {
}
}
@Throws ( JSONException :: class )
@Throws ( JSONException :: class )
fun getVideo ( id : String ) : ResultItem? {
fun getVideo ( url : String ) : List< ResultItem? > {
try {
try {
val id = getIDFromYoutubeURL ( url )
if ( key !! . isEmpty ( ) ) {
if ( key !! . isEmpty ( ) ) {
val res = genericRequest ( " $pipedURL /streams/ $id " )
val res = genericRequest ( " $pipedURL /streams/ $id " )
return if ( res . length ( ) == 0 ) getFromYTDL ( " https://www.youtube.com/watch?v= $id " ) [ 0 ] else createVideoFromPipedJSON (
return if ( res . length ( ) == 0 ) getFromYTDL ( url ) else listOf ( createVideoFromPipedJSON (
res , id
res , id
)
) )
}
}
//short data
//short data
var res =
var res =
genericRequest ( " https://youtube.googleapis.com/youtube/v3/videos?part=snippet,contentDetails&id= $id &key= $key " )
genericRequest ( " https://youtube.googleapis.com/youtube/v3/videos?part=snippet,contentDetails&id= $id &key= $key " )
if ( ! res . has ( " items " ) ) return getFromYTDL ( " https://www.youtube.com/watch?v= $id " ) [ 0 ]
if ( ! res . has ( " items " ) ) return getFromYTDL ( url )
var duration = res . getJSONArray ( " items " ) . getJSONObject ( 0 ) . getJSONObject ( " contentDetails " )
var duration = res . getJSONArray ( " items " ) . getJSONObject ( 0 ) . getJSONObject ( " contentDetails " )
. getString ( " duration " )
. getString ( " duration " )
duration = formatDuration ( duration )
duration = formatDuration ( duration )
@ -232,9 +233,9 @@ class InfoUtil(private val context: Context) {
res . put ( " videoID " , id )
res . put ( " videoID " , id )
res . put ( " duration " , duration )
res . put ( " duration " , duration )
fixThumbnail ( res )
fixThumbnail ( res )
return createVideofromJSON( res )
return listOf( createVideofromJSON( res ) )
} catch ( e : Exception ) {
} catch ( e : Exception ) {
return getFromYTDL ( " https://www.youtube.com/watch?v= $id " ) [ 0 ]
return getFromYTDL ( url )
}
}
}
}
@ -697,9 +698,12 @@ class InfoUtil(private val context: Context) {
} else {
} else {
jsonObject . getString ( " webpage_url_basename " )
jsonObject . getString ( " webpage_url_basename " )
}
}
var author : String ? = " "
var author : String = if ( jsonObject . has ( " uploader " ) ) jsonObject . getString ( " uploader " ) else " "
if ( jsonObject . has ( " uploader " ) ) {
if ( author . isEmpty ( ) || author == " null " ) {
author = jsonObject . getString ( " uploader " )
author = if ( jsonObject . has ( " channel " ) ) jsonObject . getString ( " channel " ) else " "
if ( author . isEmpty ( ) || author == " null " ) {
author = if ( jsonObject . has ( " playlist_uploader " ) ) jsonObject . getString ( " playlist_uploader " ) else " "
}
}
}
var duration = " "
var duration = " "
runCatching {
runCatching {
@ -747,7 +751,7 @@ class InfoUtil(private val context: Context) {
}
}
}
}
val chaptersInJSON = if ( jsonObject . has ( " format s" ) && jsonObject . get ( " format s" ) is JSONArray ) jsonObject . getJSONArray ( " format s" ) else null
val chaptersInJSON = if ( jsonObject . has ( " chapter s" ) && jsonObject . get ( " chapter s" ) is JSONArray ) jsonObject . getJSONArray ( " chapter s" ) else null
val listType : Type = object : TypeToken < List < ChapterItem > > ( ) { } . type
val listType : Type = object : TypeToken < List < ChapterItem > > ( ) { } . type
var chapters : ArrayList < ChapterItem > = arrayListOf ( )
var chapters : ArrayList < ChapterItem > = arrayListOf ( )
@ -803,9 +807,12 @@ class InfoUtil(private val context: Context) {
val youtubeDLResponse = YoutubeDL . getInstance ( ) . execute ( request )
val youtubeDLResponse = YoutubeDL . getInstance ( ) . execute ( request )
val jsonObject = JSONObject ( youtubeDLResponse . out )
val jsonObject = JSONObject ( youtubeDLResponse . out )
var author : String ? = " "
var author : String = if ( jsonObject . has ( " uploader " ) ) jsonObject . getString ( " uploader " ) else " "
if ( jsonObject . has ( " uploader " ) ) {
if ( author . isEmpty ( ) || author == " null " ) {
author = jsonObject . getString ( " uploader " )
author = if ( jsonObject . has ( " channel " ) ) jsonObject . getString ( " channel " ) else " "
if ( author . isEmpty ( ) || author == " null " ) {
author = if ( jsonObject . has ( " playlist_uploader " ) ) jsonObject . getString ( " playlist_uploader " ) else " "
}
}
}
var duration = " "
var duration = " "