added multiple search engines

This commit is contained in:
deniscerri 2023-04-17 22:07:38 +02:00
parent 6b83fd7f46
commit 5b1992a554
No known key found for this signature in database
GPG key ID: 95C43D517D830350
8 changed files with 71 additions and 26 deletions

View file

@ -96,7 +96,7 @@ class ActiveDownloadAdapter(onItemClickListener: OnItemClickListener, activity:
} }
val formatNote = card.findViewById<Chip>(R.id.format_note) val formatNote = card.findViewById<Chip>(R.id.format_note)
formatNote.text = item.format.format_note formatNote.text = item.format.format_note.uppercase()
val codec = card.findViewById<Chip>(R.id.codec) val codec = card.findViewById<Chip>(R.id.codec)
val codecText = val codecText =

View file

@ -57,7 +57,7 @@ class DownloadViewModel(application: Application) : AndroidViewModel(application
erroredDownloads = repository.erroredDownloads erroredDownloads = repository.erroredDownloads
val videoFormat = getApplication<App>().resources.getStringArray(R.array.video_formats) val videoFormat = getApplication<App>().resources.getStringArray(R.array.video_formats)
val videoContainer = sharedPreferences.getString("video_format", getApplication<App>().resources.getString(R.string.defaultValue)) val videoContainer = sharedPreferences.getString("video_format", "Default")
defaultVideoFormats = mutableListOf() defaultVideoFormats = mutableListOf()
videoFormat.forEach { videoFormat.forEach {

View file

@ -41,6 +41,7 @@ class SettingsFragment : PreferenceFragmentCompat() {
private var clearCache: Preference? = null private var clearCache: Preference? = null
private var incognito: SwitchPreferenceCompat? = null private var incognito: SwitchPreferenceCompat? = null
private var preferredDownloadType : ListPreference? = null private var preferredDownloadType : ListPreference? = null
private var searchEngine : ListPreference? = null
private var downloadCard: SwitchPreferenceCompat? = null private var downloadCard: SwitchPreferenceCompat? = null
private var meteredNetwork: SwitchPreferenceCompat? = null private var meteredNetwork: SwitchPreferenceCompat? = null
private var apiKey: EditTextPreference? = null private var apiKey: EditTextPreference? = null
@ -108,6 +109,7 @@ class SettingsFragment : PreferenceFragmentCompat() {
clearCache = findPreference("clear_cache") clearCache = findPreference("clear_cache")
incognito = findPreference("incognito") incognito = findPreference("incognito")
preferredDownloadType = findPreference("preferred_download_type") preferredDownloadType = findPreference("preferred_download_type")
searchEngine = findPreference("search_engine")
downloadCard = findPreference("download_card") downloadCard = findPreference("download_card")
meteredNetwork = findPreference("metered_networks") meteredNetwork = findPreference("metered_networks")
apiKey = findPreference("api_key") apiKey = findPreference("api_key")
@ -174,6 +176,7 @@ class SettingsFragment : PreferenceFragmentCompat() {
editor.putBoolean("incognito", incognito!!.isChecked) editor.putBoolean("incognito", incognito!!.isChecked)
editor.putString("preferred_download_type", preferredDownloadType!!.value) editor.putString("preferred_download_type", preferredDownloadType!!.value)
editor.putString("search_engine", searchEngine!!.value)
editor.putBoolean("download_card", downloadCard!!.isChecked) editor.putBoolean("download_card", downloadCard!!.isChecked)
editor.putBoolean("metered_networks", meteredNetwork!!.isChecked) editor.putBoolean("metered_networks", meteredNetwork!!.isChecked)
editor.putString("api_key", apiKey!!.text) editor.putString("api_key", apiKey!!.text)
@ -294,6 +297,12 @@ class SettingsFragment : PreferenceFragmentCompat() {
editor.apply() editor.apply()
true true
} }
searchEngine!!.onPreferenceChangeListener =
Preference.OnPreferenceChangeListener { _: Preference?, newValue: Any ->
editor.putString("search_engine", newValue.toString())
editor.apply()
true
}
downloadCard!!.onPreferenceChangeListener = downloadCard!!.onPreferenceChangeListener =
Preference.OnPreferenceChangeListener { _: Preference?, newValue: Any -> Preference.OnPreferenceChangeListener { _: Preference?, newValue: Any ->
val enable = newValue as Boolean val enable = newValue as Boolean

View file

@ -10,6 +10,7 @@ import com.deniscerri.ytdlnis.R
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.JsonArray
import com.google.gson.JsonObject import com.google.gson.JsonObject
import com.google.gson.reflect.TypeToken import com.google.gson.reflect.TypeToken
import com.yausername.youtubedl_android.YoutubeDL import com.yausername.youtubedl_android.YoutubeDL
@ -64,8 +65,9 @@ class InfoUtil(private val context: Context) {
fun search(query: String): ArrayList<ResultItem?> { fun search(query: String): ArrayList<ResultItem?> {
init() init()
items = ArrayList() items = ArrayList()
val searchEngine = sharedPreferences.getString("search_engine", "ytsearch")
return if (key!!.isEmpty()) { return if (key!!.isEmpty()) {
if (useInvidous) searchFromInvidous(query) else getFromYTDL(query) if (useInvidous && searchEngine == "ytsearch") searchFromInvidous(query) else getFromYTDL(query)
} else searchFromKey(query) } else searchFromKey(query)
} }
@ -555,6 +557,7 @@ class InfoUtil(private val context: Context) {
fun getFromYTDL(query: String): ArrayList<ResultItem?> { fun getFromYTDL(query: String): ArrayList<ResultItem?> {
init() init()
items = ArrayList() items = ArrayList()
val searchEngine = sharedPreferences.getString("search_engine", "ytsearch")
try { try {
val request = YoutubeDLRequest(query) val request = YoutubeDLRequest(query)
request.addOption("--flat-playlist") request.addOption("--flat-playlist")
@ -562,7 +565,7 @@ class InfoUtil(private val context: Context) {
request.addOption("--skip-download") request.addOption("--skip-download")
request.addOption("-R", "1") request.addOption("-R", "1")
request.addOption("--socket-timeout", "5") 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") val cookiesFile = File(context.cacheDir, "cookies.txt")
if (cookiesFile.exists()){ if (cookiesFile.exists()){
@ -607,7 +610,7 @@ class InfoUtil(private val context: Context) {
var playlistTitle: String? = "" var playlistTitle: String? = ""
if (jsonObject.has("playlist_title")) playlistTitle = jsonObject.getString("playlist_title") if (jsonObject.has("playlist_title")) playlistTitle = jsonObject.getString("playlist_title")
if(playlistTitle.equals(query)) playlistTitle = "" 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<Format> = ArrayList() val formats : ArrayList<Format> = ArrayList()
if (formatsInJSON != null) { if (formatsInJSON != null) {
for (f in 0 until formatsInJSON.length()){ for (f in 0 until formatsInJSON.length()){

View file

@ -143,29 +143,25 @@ class DownloadWorker(
if (audioQualityId.isBlank() || audioQualityId == "0" || audioQualityId == context.getString(R.string.best_quality)) audioQualityId = "" if (audioQualityId.isBlank() || audioQualityId == "0" || audioQualityId == context.getString(R.string.best_quality)) audioQualityId = ""
else if (audioQualityId == context.getString(R.string.worst_quality)) audioQualityId = "worstaudio" else if (audioQualityId == context.getString(R.string.worst_quality)) audioQualityId = "worstaudio"
if (audioQualityId.isNotBlank()){ val ext = downloadItem.format.container
request.addOption("-f", audioQualityId) if (audioQualityId.isBlank() || ext == "Default") request.addOption("-x")
request.addOption("-f", audioQualityId)
val ext = downloadItem.format.container if(ext != "webm"){
if(ext != "Default" && ext != "webm"){ val codec = when(downloadItem.format.container){
val codec = when(downloadItem.format.container){ "aac" -> "aac"
"aac" -> "aac" "mp3" -> "libmp3lame"
"mp3" -> "libmp3lame" "flac" -> "flac"
"flac" -> "flac" "opus" -> "libopus"
"opus" -> "libopus" else -> ""
else -> "" }
} Log.e("aa", codec)
Log.e("aa", codec) if (codec.isEmpty()){
if (codec.isEmpty()){ request.addOption("-x")
request.addOption("-x") }else{
}else{ request.addOption("--remux-video", ext)
request.addOption("--remux-video", ext) request.addOption("--ppa", "VideoRemuxer:-vn -c:a $codec")
request.addOption("--ppa", "VideoRemuxer:-vn -c:a $codec")
}
} }
}else{
request.addOption("-x")
} }
request.addOption("--embed-metadata") request.addOption("--embed-metadata")

View file

@ -125,4 +125,30 @@
<item>invidious</item> <item>invidious</item>
<item>yt-dlp</item> <item>yt-dlp</item>
</array> </array>
<array name="search_engines">
<item>YouTube</item>
<item>Nico video</item>
<item>PRX Series</item>
<item>PRX Stories</item>
<item>Rokfin</item>
<item>Yahoo screen</item>
<item>Soundcloud</item>
<item>Google Video</item>
<item>Bilibili</item>
<item>Netverse</item>
</array>
<array name="search_engines_values">
<item>ytsearch</item>
<item>nicosearch</item>
<item>PRX Series</item>
<item>PRX Stories</item>
<item>rkfnsearch</item>
<item>yvsearch</item>
<item>scsearch</item>
<item>gvsearch</item>
<item>bilisearch</item>
<item>netsearch</item>
</array>
</resources> </resources>

View file

@ -214,4 +214,6 @@
<string name="format_source">Formats Source</string> <string name="format_source">Formats Source</string>
<string name="video_recommendations">Video Recommendations</string> <string name="video_recommendations">Video Recommendations</string>
<string name="video_recommendations_summary">Get Recommended Youtube Videos on the Home Screen from Invidious</string> <string name="video_recommendations_summary">Get Recommended Youtube Videos on the Home Screen from Invidious</string>
<string name="preferred_search_engine">Preferred Search Engine</string>
<string name="preferred_search_engine_summary">The search engine to use for in-app searches</string>
</resources> </resources>

View file

@ -83,6 +83,15 @@
app:summary="@string/preferred_download_type_summary" app:summary="@string/preferred_download_type_summary"
app:title="@string/preferred_download_type" /> app:title="@string/preferred_download_type" />
<ListPreference
android:defaultValue="ytsearch"
android:entries="@array/search_engines"
android:entryValues="@array/search_engines_values"
android:icon="@drawable/baseline_manage_search_24"
app:key="search_engine"
app:summary="@string/preferred_search_engine_summary"
app:title="@string/preferred_search_engine" />
<EditTextPreference <EditTextPreference
android:icon="@drawable/ic_key" android:icon="@drawable/ic_key"
app:key="api_key" app:key="api_key"