1
0
mirror of https://github.com/TeamNewPipe/NewPipe synced 2025-10-25 20:37:40 +00:00

replaced content tountry selection with search language selection.Should now allows users to search for videos in their preferred language. Needs testing before pushing to origin.

This commit is contained in:
Adam Howard
2015-11-08 01:09:03 +00:00
parent 4a938b81df
commit 8aff134c56
6 changed files with 210 additions and 195 deletions

View File

@@ -1,6 +1,7 @@
package org.schabi.newpipe;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
@@ -29,12 +30,25 @@ import java.net.UnknownHostException;
public class Downloader {
private static final String USER_AGENT = "Mozilla/5.0";
public static String download(String siteUrl) {
StringBuffer response = new StringBuffer();
public static String download(String siteUrl, String language) {
String ret = "";
try {
URL url = new URL(siteUrl);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestProperty("Accept-Language", language);
ret = dl(con);
}
catch(Exception e) {
e.printStackTrace();
}
return ret;
}
private static String dl(HttpURLConnection con) {
StringBuffer response = new StringBuffer();
try {
con.setRequestMethod("GET");
con.setRequestProperty("User-Agent", USER_AGENT);
@@ -57,4 +71,20 @@ public class Downloader {
}
return response.toString();
}
public static String download(String siteUrl) {
String ret = "";
try {
URL url = new URL(siteUrl);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
ret = dl(con);
}
catch(Exception e) {
e.printStackTrace();
}
return ret;
}
}

View File

@@ -93,10 +93,10 @@ public class VideoItemListFragment extends ListFragment {
public void run() {
try {
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+"\"");
String searchLanguageKey = getContext().getString(R.string.searchLanguage);
String searchLanguage = sp.getString(searchLanguageKey, "");
SearchEngine.Result result = engine.search(query, page, searchLanguage);
Log.i(TAG, "countryCode passed:\""+searchLanguage+"\"");
if(run) {
h.post(new ResultRunnable(result, requestId));
}

View File

@@ -49,7 +49,7 @@ public class YoutubeSearchEngine implements SearchEngine {
private static final String TAG = YoutubeSearchEngine.class.toString();
@Override
public Result search(String query, int page, String countryCode) {
public Result search(String query, int page, String languageCode) {
//String contentCountry = PreferenceManager.getDefaultSharedPreferences(this).getString(getString(R.string., "");
Uri.Builder builder = new Uri.Builder();
builder.scheme("https")
@@ -59,20 +59,25 @@ public class YoutubeSearchEngine implements SearchEngine {
.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");
String site;
String url = builder.build().toString();
//if we've been passed a valid, non-empty language code, append it to the URL
if(languageCode.length() > 0) {
if(languageCode.length() == 2) {
Log.i(TAG, "URI: \""+builder+"\"");
site = Downloader.download(url, languageCode);
}
else {
Log.e(TAG, "invalid country code passed to search(): \""+countryCode+"\"");
Log.e(TAG, "invalid language code passed to search(): \""+languageCode+"\"");
site = Downloader.download(url);
}
}
String url = builder.build().toString();
else {
site = Downloader.download(url);
}
String site = Downloader.download(url);
//String site = Downloader.download(url);
Document doc = Jsoup.parse(site, url);
Result result = new Result();
Element list = doc.select("ol[class=\"item-section\"]").first();