From fc707b6c7ee94caf1386e4b31b3cbc33b600f6cd Mon Sep 17 00:00:00 2001 From: Adam Howard Date: Wed, 4 Nov 2015 12:42:17 +0000 Subject: [PATCH] Added Content Country selection setting. This changes the SearchEngine interface slightly. Still need a reliable test to prove whether YouTube is serving different search results based on the country code passed in the search query. --- .../org/schabi/newpipe/ActionBarHandler.java | 2 +- .../java/org/schabi/newpipe/SearchEngine.java | 3 +- .../schabi/newpipe/VideoItemListFragment.java | 8 +- .../newpipe/youtube/YoutubeSearchEngine.java | 17 +- app/src/main/res/values/settings_keys.xml | 179 ++++++++++++++++++ app/src/main/res/values/strings.xml | 1 + app/src/main/res/xml/settings_screen.xml | 6 + 7 files changed, 212 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/ActionBarHandler.java b/app/src/main/java/org/schabi/newpipe/ActionBarHandler.java index 36c6772c8..5cdecef66 100644 --- a/app/src/main/java/org/schabi/newpipe/ActionBarHandler.java +++ b/app/src/main/java/org/schabi/newpipe/ActionBarHandler.java @@ -321,7 +321,7 @@ public class ActionBarHandler { } }); builder.create().show(); - Log.d(TAG, "Either no Streaming player for audio was installed, or something importand crashed:"); + Log.d(TAG, "Either no Streaming player for audio was installed, or something important crashed:"); e.printStackTrace(); } } diff --git a/app/src/main/java/org/schabi/newpipe/SearchEngine.java b/app/src/main/java/org/schabi/newpipe/SearchEngine.java index 5d986f2a4..eb50cc45e 100644 --- a/app/src/main/java/org/schabi/newpipe/SearchEngine.java +++ b/app/src/main/java/org/schabi/newpipe/SearchEngine.java @@ -31,5 +31,6 @@ public interface SearchEngine { public Vector resultList = new Vector<>(); } - Result search(String query, int page); + //Result search(String query, int page); + Result search(String query, int page, String contentCountry); } diff --git a/app/src/main/java/org/schabi/newpipe/VideoItemListFragment.java b/app/src/main/java/org/schabi/newpipe/VideoItemListFragment.java index 3ba90c6db..8de38c102 100644 --- a/app/src/main/java/org/schabi/newpipe/VideoItemListFragment.java +++ b/app/src/main/java/org/schabi/newpipe/VideoItemListFragment.java @@ -1,10 +1,12 @@ package org.schabi.newpipe; import android.content.Context; +import android.content.SharedPreferences; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.os.Handler; +import android.preference.PreferenceManager; import android.support.v4.app.ListFragment; import android.util.Log; import android.view.View; @@ -90,7 +92,11 @@ public class VideoItemListFragment extends ListFragment { @Override public void run() { try { - SearchEngine.Result result = engine.search(query, page); + SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getContext()); + String contentCountryKey = getContext().getString(R.string.contentCountry); + String contentCountry = sp.getString(contentCountryKey, ""); + SearchEngine.Result result = engine.search(query, page, contentCountry); + Log.i(TAG, "countryCode passed:\""+contentCountry+"\""); if(run) { h.post(new ResultRunnable(result, requestId)); } diff --git a/app/src/main/java/org/schabi/newpipe/youtube/YoutubeSearchEngine.java b/app/src/main/java/org/schabi/newpipe/youtube/YoutubeSearchEngine.java index a59c67295..75af219ad 100644 --- a/app/src/main/java/org/schabi/newpipe/youtube/YoutubeSearchEngine.java +++ b/app/src/main/java/org/schabi/newpipe/youtube/YoutubeSearchEngine.java @@ -1,6 +1,7 @@ package org.schabi.newpipe.youtube; import android.net.Uri; +import android.preference.PreferenceManager; import android.util.Log; import org.jsoup.Jsoup; @@ -38,7 +39,8 @@ public class YoutubeSearchEngine implements SearchEngine { private static final String TAG = YoutubeSearchEngine.class.toString(); @Override - public Result search(String query, int page) { + public Result search(String query, int page, String countryCode) { + //String contentCountry = PreferenceManager.getDefaultSharedPreferences(this).getString(getString(R.string., ""); Uri.Builder builder = new Uri.Builder(); builder.scheme("https") .authority("www.youtube.com") @@ -46,6 +48,18 @@ public class YoutubeSearchEngine implements SearchEngine { .appendQueryParameter("search_query", query) .appendQueryParameter("page", Integer.toString(page)) .appendQueryParameter("filters", "video"); + + //if we've been passed a valid, non-empty country code, append it to the URL + if(countryCode.length() > 0) { + if(countryCode.length() == 2) { + builder.appendQueryParameter("gl", countryCode); + builder.appendQueryParameter("persist_gl", "1"); + Log.i(TAG, "URI: \""+builder+"\""); + } + else { + Log.e(TAG, "invalid country code passed to search(): \""+countryCode+"\""); + } + } String url = builder.build().toString(); String site = Downloader.download(url); @@ -115,4 +129,5 @@ public class YoutubeSearchEngine implements SearchEngine { } return result; } + } diff --git a/app/src/main/res/values/settings_keys.xml b/app/src/main/res/values/settings_keys.xml index e7c520aa4..d427b7022 100644 --- a/app/src/main/res/values/settings_keys.xml +++ b/app/src/main/res/values/settings_keys.xml @@ -24,4 +24,183 @@ m4a show_next_video + content_country + + + + + US + DZ + AR + AU + AT + AZ + BH + BY + BE + BA + BR + BG + CA + CL + CO + HR + CZ + DK + EG + EE + FI + FR + GE + DE + GH + GR + HK + HU + IS + IN + ID + IE + IL + IT + JP + JO + KZ + KE + KW + LV + LB + LY + LT + LU + MK + MY + MX + ME + MA + NL + NZ + NG + NO + OM + PE + PH + PL + PT + PR + QA + RO + RU + SA + SN + RS + SG + SK + SI + ZA + KR + ES + SE + CH + TW + TZ + TH + TN + TR + UG + UA + AE + GB + VN + YE + ZW + + + (None) + Worldwide (USA) + Algeria + Argentina + Australia + Austria + Azerbaijan + Bahrain + Belarus + Belgium + Bosnia and Herzegovina + Brazil + Bulgaria + Canada + Chile + Colombia + Croatia + Czech Republic + Denmark + Egypt + Estonia + Finland + France + Georgia + Germany + Ghana + Greece + Hong Kong + Hungary + Iceland + India + Indonesia + Ireland + Israel + Italy + Japan + Jordan + Kazakhstan + Kenya + Kuwait + Latvia + Lebanon + Libya + Lithuania + Luxembourg + Macedonia + Malaysia + Mexico + Montenegro + Morocco + Netherlands + New Zealand + Nigeria + Norway + Oman + Peru + Philippines + Poland + Portugal + Puerto Rico + Qatar + Romania + Russia + Saudi Arabia + Senegal + Serbia + Singapore + Slovakia + Slovenia + South Africa + South Korea + Spain + Sweden + Switzerland + Taiwan + Tanzania + Thailand + Tunisia + Turkey + Uganda + Ukraine + United Arab Emirates + United Kingdom + Vietnam + Yemen + Zimbabwe + \ 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 3c4faeb88..7fafc3104 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -49,4 +49,5 @@ Show \"Next video\" item. Url not Supported. Similar Videos + Video Content Country \ No newline at end of file diff --git a/app/src/main/res/xml/settings_screen.xml b/app/src/main/res/xml/settings_screen.xml index e9250d4c9..bee161060 100644 --- a/app/src/main/res/xml/settings_screen.xml +++ b/app/src/main/res/xml/settings_screen.xml @@ -51,4 +51,10 @@ android:title="@string/showNextVideoTitle" android:defaultValue="true" /> + \ No newline at end of file