From 5b1992a554c63c5e06bf2c3f0e8eb1aee2fc6c27 Mon Sep 17 00:00:00 2001 From: deniscerri <64997243+deniscerri@users.noreply.github.com> Date: Mon, 17 Apr 2023 22:07:38 +0200 Subject: [PATCH] added multiple search engines --- .../ytdlnis/adapter/ActiveDownloadAdapter.kt | 2 +- .../database/viewmodel/DownloadViewModel.kt | 2 +- .../ui/more/settings/SettingsFragment.kt | 9 +++++ .../com/deniscerri/ytdlnis/util/InfoUtil.kt | 9 +++-- .../deniscerri/ytdlnis/work/DownloadWorker.kt | 38 +++++++++---------- app/src/main/res/values/arrays.xml | 26 +++++++++++++ app/src/main/res/values/strings.xml | 2 + app/src/main/res/xml/root_preferences.xml | 9 +++++ 8 files changed, 71 insertions(+), 26 deletions(-) diff --git a/app/src/main/java/com/deniscerri/ytdlnis/adapter/ActiveDownloadAdapter.kt b/app/src/main/java/com/deniscerri/ytdlnis/adapter/ActiveDownloadAdapter.kt index f263cf1d..044685d1 100644 --- a/app/src/main/java/com/deniscerri/ytdlnis/adapter/ActiveDownloadAdapter.kt +++ b/app/src/main/java/com/deniscerri/ytdlnis/adapter/ActiveDownloadAdapter.kt @@ -96,7 +96,7 @@ class ActiveDownloadAdapter(onItemClickListener: OnItemClickListener, activity: } val formatNote = card.findViewById(R.id.format_note) - formatNote.text = item.format.format_note + formatNote.text = item.format.format_note.uppercase() val codec = card.findViewById(R.id.codec) val codecText = diff --git a/app/src/main/java/com/deniscerri/ytdlnis/database/viewmodel/DownloadViewModel.kt b/app/src/main/java/com/deniscerri/ytdlnis/database/viewmodel/DownloadViewModel.kt index 49b5b43f..7182a340 100644 --- a/app/src/main/java/com/deniscerri/ytdlnis/database/viewmodel/DownloadViewModel.kt +++ b/app/src/main/java/com/deniscerri/ytdlnis/database/viewmodel/DownloadViewModel.kt @@ -57,7 +57,7 @@ class DownloadViewModel(application: Application) : AndroidViewModel(application erroredDownloads = repository.erroredDownloads val videoFormat = getApplication().resources.getStringArray(R.array.video_formats) - val videoContainer = sharedPreferences.getString("video_format", getApplication().resources.getString(R.string.defaultValue)) + val videoContainer = sharedPreferences.getString("video_format", "Default") defaultVideoFormats = mutableListOf() videoFormat.forEach { diff --git a/app/src/main/java/com/deniscerri/ytdlnis/ui/more/settings/SettingsFragment.kt b/app/src/main/java/com/deniscerri/ytdlnis/ui/more/settings/SettingsFragment.kt index 6268066d..e5d0764a 100644 --- a/app/src/main/java/com/deniscerri/ytdlnis/ui/more/settings/SettingsFragment.kt +++ b/app/src/main/java/com/deniscerri/ytdlnis/ui/more/settings/SettingsFragment.kt @@ -41,6 +41,7 @@ class SettingsFragment : PreferenceFragmentCompat() { private var clearCache: Preference? = null private var incognito: SwitchPreferenceCompat? = null private var preferredDownloadType : ListPreference? = null + private var searchEngine : ListPreference? = null private var downloadCard: SwitchPreferenceCompat? = null private var meteredNetwork: SwitchPreferenceCompat? = null private var apiKey: EditTextPreference? = null @@ -108,6 +109,7 @@ class SettingsFragment : PreferenceFragmentCompat() { clearCache = findPreference("clear_cache") incognito = findPreference("incognito") preferredDownloadType = findPreference("preferred_download_type") + searchEngine = findPreference("search_engine") downloadCard = findPreference("download_card") meteredNetwork = findPreference("metered_networks") apiKey = findPreference("api_key") @@ -174,6 +176,7 @@ class SettingsFragment : PreferenceFragmentCompat() { editor.putBoolean("incognito", incognito!!.isChecked) editor.putString("preferred_download_type", preferredDownloadType!!.value) + editor.putString("search_engine", searchEngine!!.value) editor.putBoolean("download_card", downloadCard!!.isChecked) editor.putBoolean("metered_networks", meteredNetwork!!.isChecked) editor.putString("api_key", apiKey!!.text) @@ -294,6 +297,12 @@ class SettingsFragment : PreferenceFragmentCompat() { editor.apply() true } + searchEngine!!.onPreferenceChangeListener = + Preference.OnPreferenceChangeListener { _: Preference?, newValue: Any -> + editor.putString("search_engine", newValue.toString()) + editor.apply() + true + } downloadCard!!.onPreferenceChangeListener = Preference.OnPreferenceChangeListener { _: Preference?, newValue: Any -> val enable = newValue as Boolean diff --git a/app/src/main/java/com/deniscerri/ytdlnis/util/InfoUtil.kt b/app/src/main/java/com/deniscerri/ytdlnis/util/InfoUtil.kt index db354b5f..6ef521be 100644 --- a/app/src/main/java/com/deniscerri/ytdlnis/util/InfoUtil.kt +++ b/app/src/main/java/com/deniscerri/ytdlnis/util/InfoUtil.kt @@ -10,6 +10,7 @@ import com.deniscerri.ytdlnis.R import com.deniscerri.ytdlnis.database.models.Format import com.deniscerri.ytdlnis.database.models.ResultItem import com.google.gson.Gson +import com.google.gson.JsonArray import com.google.gson.JsonObject import com.google.gson.reflect.TypeToken import com.yausername.youtubedl_android.YoutubeDL @@ -64,8 +65,9 @@ class InfoUtil(private val context: Context) { fun search(query: String): ArrayList { init() items = ArrayList() + val searchEngine = sharedPreferences.getString("search_engine", "ytsearch") return if (key!!.isEmpty()) { - if (useInvidous) searchFromInvidous(query) else getFromYTDL(query) + if (useInvidous && searchEngine == "ytsearch") searchFromInvidous(query) else getFromYTDL(query) } else searchFromKey(query) } @@ -555,6 +557,7 @@ class InfoUtil(private val context: Context) { fun getFromYTDL(query: String): ArrayList { init() items = ArrayList() + val searchEngine = sharedPreferences.getString("search_engine", "ytsearch") try { val request = YoutubeDLRequest(query) request.addOption("--flat-playlist") @@ -562,7 +565,7 @@ class InfoUtil(private val context: Context) { request.addOption("--skip-download") request.addOption("-R", "1") request.addOption("--socket-timeout", "5") - if (!query.contains("http")) request.addOption("--default-search", "ytsearch25") + if (!query.contains("http")) request.addOption("--default-search", "${searchEngine}25") val cookiesFile = File(context.cacheDir, "cookies.txt") if (cookiesFile.exists()){ @@ -607,7 +610,7 @@ class InfoUtil(private val context: Context) { var playlistTitle: String? = "" if (jsonObject.has("playlist_title")) playlistTitle = jsonObject.getString("playlist_title") if(playlistTitle.equals(query)) playlistTitle = "" - val formatsInJSON = if (jsonObject.has("formats")) jsonObject.getJSONArray("formats") else null + val formatsInJSON = if (jsonObject.has("formats") && jsonObject.get("formats") is JsonArray) jsonObject.getJSONArray("formats") else null val formats : ArrayList = ArrayList() if (formatsInJSON != null) { for (f in 0 until formatsInJSON.length()){ diff --git a/app/src/main/java/com/deniscerri/ytdlnis/work/DownloadWorker.kt b/app/src/main/java/com/deniscerri/ytdlnis/work/DownloadWorker.kt index 31c10484..839e5388 100644 --- a/app/src/main/java/com/deniscerri/ytdlnis/work/DownloadWorker.kt +++ b/app/src/main/java/com/deniscerri/ytdlnis/work/DownloadWorker.kt @@ -143,29 +143,25 @@ class DownloadWorker( if (audioQualityId.isBlank() || audioQualityId == "0" || audioQualityId == context.getString(R.string.best_quality)) audioQualityId = "" else if (audioQualityId == context.getString(R.string.worst_quality)) audioQualityId = "worstaudio" - if (audioQualityId.isNotBlank()){ - request.addOption("-f", audioQualityId) + val ext = downloadItem.format.container + if (audioQualityId.isBlank() || ext == "Default") request.addOption("-x") + request.addOption("-f", audioQualityId) - val ext = downloadItem.format.container - if(ext != "Default" && ext != "webm"){ - val codec = when(downloadItem.format.container){ - "aac" -> "aac" - "mp3" -> "libmp3lame" - "flac" -> "flac" - "opus" -> "libopus" - else -> "" - } - Log.e("aa", codec) - if (codec.isEmpty()){ - request.addOption("-x") - }else{ - request.addOption("--remux-video", ext) - request.addOption("--ppa", "VideoRemuxer:-vn -c:a $codec") - } + if(ext != "webm"){ + val codec = when(downloadItem.format.container){ + "aac" -> "aac" + "mp3" -> "libmp3lame" + "flac" -> "flac" + "opus" -> "libopus" + else -> "" + } + Log.e("aa", codec) + if (codec.isEmpty()){ + request.addOption("-x") + }else{ + request.addOption("--remux-video", ext) + request.addOption("--ppa", "VideoRemuxer:-vn -c:a $codec") } - - }else{ - request.addOption("-x") } request.addOption("--embed-metadata") diff --git a/app/src/main/res/values/arrays.xml b/app/src/main/res/values/arrays.xml index 8ef702ec..d6fb1c6f 100644 --- a/app/src/main/res/values/arrays.xml +++ b/app/src/main/res/values/arrays.xml @@ -125,4 +125,30 @@ invidious yt-dlp + + + YouTube + Nico video + PRX Series + PRX Stories + Rokfin + Yahoo screen + Soundcloud + Google Video + Bilibili + Netverse + + + + ytsearch + nicosearch + PRX Series + PRX Stories + rkfnsearch + yvsearch + scsearch + gvsearch + bilisearch + netsearch + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 0c424dd2..d5880ccb 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -214,4 +214,6 @@ Formats Source Video Recommendations Get Recommended Youtube Videos on the Home Screen from Invidious + Preferred Search Engine + The search engine to use for in-app searches \ No newline at end of file diff --git a/app/src/main/res/xml/root_preferences.xml b/app/src/main/res/xml/root_preferences.xml index 3e66345e..46d5f06c 100644 --- a/app/src/main/res/xml/root_preferences.xml +++ b/app/src/main/res/xml/root_preferences.xml @@ -83,6 +83,15 @@ app:summary="@string/preferred_download_type_summary" app:title="@string/preferred_download_type" /> + +