fixed audio downloads

This commit is contained in:
Denis Çerri 2023-01-30 23:04:51 +01:00
parent 544a8eab14
commit c653edb387
No known key found for this signature in database
GPG key ID: 95C43D517D830350
7 changed files with 22 additions and 33 deletions

View file

@ -112,7 +112,6 @@ class HistoryAdapter(onItemClickListener: OnItemClickListener, activity: Activit
//IS IN THE FILE SYSTEM? //IS IN THE FILE SYSTEM?
val path = item.downloadPath val path = item.downloadPath
val file = File(path) val file = File(path)
Log.e("aa", file.absolutePath + " " + file.isFile)
if (!file.exists() && path.isNotEmpty()) { if (!file.exists() && path.isNotEmpty()) {
filePresent = false filePresent = false
thumbnail.colorFilter = ColorMatrixColorFilter(object : ColorMatrix() { thumbnail.colorFilter = ColorMatrixColorFilter(object : ColorMatrix() {

View file

@ -72,17 +72,18 @@ class ResultRepository(private val resultDao: ResultDao, private val commandTemp
var nextPageToken = "" var nextPageToken = ""
if (resetResults) deleteAll() if (resetResults) deleteAll()
val infoUtil = InfoUtil(context) val infoUtil = InfoUtil(context)
val items : ArrayList<ResultItem?> = arrayListOf()
do { do {
val tmp = infoUtil.getPlaylist(query, nextPageToken) val tmp = infoUtil.getPlaylist(query, nextPageToken)
val tmpVids = tmp.videos items.addAll(tmp.videos)
val tmpToken = tmp.nextPageToken val tmpToken = tmp.nextPageToken
tmpVids.forEach {
resultDao.insert(it!!)
}
if (tmpToken.isEmpty()) break if (tmpToken.isEmpty()) break
if (tmpToken == nextPageToken) break if (tmpToken == nextPageToken) break
nextPageToken = tmpToken nextPageToken = tmpToken
} while (true) } while (true)
items.forEach {
resultDao.insert(it!!)
}
} }
suspend fun getDefault(inputQuery: String, resetResults: Boolean){ suspend fun getDefault(inputQuery: String, resetResults: Boolean){
@ -114,5 +115,4 @@ class ResultRepository(private val resultDao: ResultDao, private val commandTemp
fun getItemByURL(url: String): ResultItem { fun getItemByURL(url: String): ResultItem {
return resultDao.getResultByURL(url) return resultDao.getResultByURL(url)
} }
} }

View file

@ -61,16 +61,17 @@ class ResultViewModel(application: Application) : AndroidViewModel(application)
try { try {
when (type) { when (type) {
"Search" -> { "Search" -> {
repository.search(inputQuery, resetResults); repository.search(inputQuery, resetResults)
} }
"Video" -> { "Video" -> {
repository.getOne(inputQuery, resetResults); repository.getOne(inputQuery, resetResults)
} }
"Playlist" -> { "Playlist" -> {
repository.getPlaylist(inputQuery, resetResults); repository.getPlaylist(inputQuery, resetResults)
} }
"Default" -> { "Default" -> {
repository.getDefault(inputQuery, resetResults); repository.getDefault(inputQuery, resetResults)
} }
} }
} catch (e: Exception) { } catch (e: Exception) {
@ -93,8 +94,6 @@ class ResultViewModel(application: Application) : AndroidViewModel(application)
repository.update(item); repository.update(item);
} }
fun getItemByURL(url: String) : ResultItem { fun getItemByURL(url: String) : ResultItem {
return repository.getItemByURL(url) return repository.getItemByURL(url)
} }

View file

@ -96,9 +96,6 @@ class DownloadAudioFragment(private val downloadItem: DownloadItem) : Fragment()
override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {} override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {}
override fun afterTextChanged(p0: Editable?) { override fun afterTextChanged(p0: Editable?) {
downloadItem.title = p0.toString() downloadItem.title = p0.toString()
resultItem.title = p0.toString()
//resultViewModel.update(resultItem)
//downloadViewModel.updateDownload(downloadItem)
} }
}) })
@ -109,9 +106,6 @@ class DownloadAudioFragment(private val downloadItem: DownloadItem) : Fragment()
override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {} override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {}
override fun afterTextChanged(p0: Editable?) { override fun afterTextChanged(p0: Editable?) {
downloadItem.author = p0.toString() downloadItem.author = p0.toString()
resultItem.author = p0.toString()
//resultViewModel.update(resultItem)
//downloadViewModel.updateDownload(downloadItem)
} }
}) })
saveDir = view.findViewById(R.id.outputPath) saveDir = view.findViewById(R.id.outputPath)
@ -169,7 +163,6 @@ class DownloadAudioFragment(private val downloadItem: DownloadItem) : Fragment()
AdapterView.OnItemClickListener { _: AdapterView<*>?, _: View?, index: Int, _: Long -> AdapterView.OnItemClickListener { _: AdapterView<*>?, _: View?, index: Int, _: Long ->
downloadItem.format.format_note = formats[index].format_note downloadItem.format.format_note = formats[index].format_note
downloadItem.format.format_id = formats[index].format_id downloadItem.format.format_id = formats[index].format_id
//downloadViewModel.updateDownload(downloadItem)
} }
} }
@ -188,14 +181,13 @@ class DownloadAudioFragment(private val downloadItem: DownloadItem) : Fragment()
) )
val selectedContainer: String = formats.find { downloadItem.format.format_note == it.format_note }?.container val selectedContainer: String = formats.find { downloadItem.format.format_note == it.format_note }?.container
?: sharedPreferences.getString("audio_format", "mp3")!! ?: sharedPreferences.getString("audio_format", "mp3")!!
downloadItem.format.container = selectedContainer
containerAutoCompleteTextView!!.setText(selectedContainer, false) containerAutoCompleteTextView!!.setText(selectedContainer, false)
(container!!.editText as AutoCompleteTextView?)!!.onItemClickListener = (container!!.editText as AutoCompleteTextView?)!!.onItemClickListener =
AdapterView.OnItemClickListener { _: AdapterView<*>?, _: View?, index: Int, _: Long -> AdapterView.OnItemClickListener { _: AdapterView<*>?, _: View?, index: Int, _: Long ->
downloadItem.format.container = containers[index] downloadItem.format.container = containers[index]
//downloadViewModel.updateDownload(downloadItem)
} }
//downloadViewModel.updateDownload(downloadItem)
}catch (e : Exception){ }catch (e : Exception){
e.printStackTrace() e.printStackTrace()
} }

View file

@ -58,7 +58,7 @@ class FileUtil() {
return formattedPath.toString() return formattedPath.toString()
} }
fun scanMedia(destDir: String, context: Context) : String { private fun scanMedia(destDir: String, context: Context) : String {
val files = ArrayList<File>() val files = ArrayList<File>()
val path = File(formatPath(destDir)) val path = File(formatPath(destDir))
@ -79,7 +79,7 @@ class FileUtil() {
return context.getString(R.string.unfound_file); return context.getString(R.string.unfound_file);
} }
fun moveFile(originDir: File, context: Context, destDir: String, progress: (p: Int) -> Unit){ fun moveFile(originDir: File, context: Context, destDir: String, progress: (p: Int) -> Unit) : String {
originDir.listFiles()?.forEach { originDir.listFiles()?.forEach {
if (it.name.equals("rList")){ if (it.name.equals("rList")){
it.delete() it.delete()
@ -131,7 +131,7 @@ class FileUtil() {
} }
} }
originDir.delete() originDir.delete()
scanMedia(destDir, context) return scanMedia(destDir, context)
} }
fun convertFileSize(s: Long): String{ fun convertFileSize(s: Long): String{

View file

@ -45,8 +45,9 @@ class InfoUtil(context: Context) {
"US" "US"
} }
} }
if (useInvidous == false){ if (!useInvidous){
val res = genericRequest(invidousURL + "stats") val res = genericRequest(invidousURL + "stats")
Log.e("Assa", res.toString())
useInvidous = res.length() != 0 useInvidous = res.length() != 0
} }
} }
@ -524,7 +525,6 @@ class InfoUtil(context: Context) {
} }
fun getSearchSuggestions(query: String): ArrayList<String> { fun getSearchSuggestions(query: String): ArrayList<String> {
if (!useInvidous) return ArrayList()
val url = invidousURL + "search/suggestions?q=" + query val url = invidousURL + "search/suggestions?q=" + query
val res = genericRequest(url) val res = genericRequest(url)
if (res.length() == 0) return ArrayList() if (res.length() == 0) return ArrayList()

View file

@ -94,8 +94,8 @@ class DownloadWorker(
"audio" -> { "audio" -> {
request.addOption("-x") request.addOption("-x")
var audioQualityId : String = downloadItem.format.format_id var audioQualityId : String = downloadItem.format.format_id
if (audioQualityId == "0") audioQualityId = "ba" if (audioQualityId == "0" || audioQualityId.isEmpty()) audioQualityId = "ba"
var ext = downloadItem.format.container val ext = downloadItem.format.container
request.addOption("-f", audioQualityId) request.addOption("-f", audioQualityId)
request.addOption("--audio-format", ext) request.addOption("--audio-format", ext)
request.addOption("--embed-metadata") request.addOption("--embed-metadata")
@ -181,15 +181,13 @@ class DownloadWorker(
}.onSuccess { }.onSuccess {
//move file from internal to set download directory //move file from internal to set download directory
moveFile(tempFileDir.absoluteFile, downloadLocation){ progress -> val finalPath = moveFile(tempFileDir.absoluteFile, downloadLocation){ progress ->
setProgressAsync(workDataOf("progress" to progress)) setProgressAsync(workDataOf("progress" to progress))
} }
//put download in history //put download in history
val incognito = sharedPreferences.getBoolean("incognito", false) val incognito = sharedPreferences.getBoolean("incognito", false)
if (!incognito) { if (!incognito) {
val unixtime = System.currentTimeMillis() / 1000 val unixtime = System.currentTimeMillis() / 1000
val fileUtil = FileUtil()
val finalPath = fileUtil.formatPath(downloadItem.downloadPath) + downloadItem.author + " - " + downloadItem.title + "." + downloadItem.format.container
val historyItem = HistoryItem(0, downloadItem.url, downloadItem.title, downloadItem.author, downloadItem.duration, downloadItem.thumb, downloadItem.type, unixtime, finalPath, downloadItem.website, downloadItem.format) val historyItem = HistoryItem(0, downloadItem.url, downloadItem.title, downloadItem.author, downloadItem.duration, downloadItem.thumb, downloadItem.type, unixtime, finalPath, downloadItem.website, downloadItem.format)
runBlocking { runBlocking {
historyDao.insert(historyItem) historyDao.insert(historyItem)
@ -232,14 +230,15 @@ class DownloadWorker(
} }
private fun moveFile(originDir: File, downLocation: String, progress: (progress: Int) -> Unit){ private fun moveFile(originDir: File, downLocation: String, progress: (progress: Int) -> Unit) : String{
val destDir = Uri.parse(downLocation).run { val destDir = Uri.parse(downLocation).run {
DocumentsContract.buildChildDocumentsUriUsingTree(this, DocumentsContract.getTreeDocumentId(this)) DocumentsContract.buildChildDocumentsUriUsingTree(this, DocumentsContract.getTreeDocumentId(this))
} }
val fileUtil = FileUtil() val fileUtil = FileUtil()
fileUtil.moveFile(originDir, context, downLocation){ p -> val path = fileUtil.moveFile(originDir, context, downLocation){ p ->
progress(p) progress(p)
} }
return path
} }
companion object { companion object {