From 028354b283f8c7373708f308fb51926445c25c3b Mon Sep 17 00:00:00 2001 From: Christian Schabesberger Date: Mon, 29 Feb 2016 20:33:28 +0100 Subject: [PATCH] fixed some bugs --- .../org/schabi/newpipe/ErrorActivity.java | 1 + .../schabi/newpipe/VideoItemListFragment.java | 31 +++++++++++++------ .../newpipe/extractor/SearchEngine.java | 6 ++++ .../newpipe/extractor/SearchResult.java | 9 +++--- .../services/youtube/YoutubeSearchEngine.java | 20 +++++++----- app/src/main/res/values/strings.xml | 2 +- 6 files changed, 46 insertions(+), 23 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/ErrorActivity.java b/app/src/main/java/org/schabi/newpipe/ErrorActivity.java index 5ddb9b988..5c34fbced 100644 --- a/app/src/main/java/org/schabi/newpipe/ErrorActivity.java +++ b/app/src/main/java/org/schabi/newpipe/ErrorActivity.java @@ -114,6 +114,7 @@ public class ErrorActivity extends AppCompatActivity { ActivityCommunicator ac = ActivityCommunicator.getCommunicator(); ac.errorList = el; ac.returnActivity = returnAcitivty; + ac.errorInfo = errorInfo; Intent intent = new Intent(context, ErrorActivity.class); context.startActivity(intent); } diff --git a/app/src/main/java/org/schabi/newpipe/VideoItemListFragment.java b/app/src/main/java/org/schabi/newpipe/VideoItemListFragment.java index fc931f2f7..fc20aadb9 100644 --- a/app/src/main/java/org/schabi/newpipe/VideoItemListFragment.java +++ b/app/src/main/java/org/schabi/newpipe/VideoItemListFragment.java @@ -134,13 +134,14 @@ public class VideoItemListFragment extends ListFragment { } // hard errors: } catch(IOException e) { - postNewErrorToast(h, R.string.network_error); + postNewNothingFoundToast(h, R.string.network_error); e.printStackTrace(); + } catch(SearchEngine.NothingFoundException e) { + postNewErrorToast(h, e.getMessage()); } catch(ExtractionException e) { ErrorActivity.reportError(h, getActivity(), e, null, null, ErrorActivity.ErrorInfo.make(ErrorActivity.SEARCHED, /* todo: this shoudl not be assigned static */ "Youtube", query, R.string.parsing_error)); - //postNewErrorToast(h, R.string.parsing_error); e.printStackTrace(); @@ -197,12 +198,11 @@ public class VideoItemListFragment extends ListFragment { private void updateListOnResult(SearchResult result, int requestId) { if(requestId == currentRequestId) { setListShown(true); - if (!result.resultList.isEmpty()) { - if (!result.suggestion.isEmpty()) { - Toast.makeText(getActivity(), getString(R.string.did_you_mean) + result.suggestion + " ?", - Toast.LENGTH_LONG).show(); - } - updateList(result.resultList); + updateList(result.resultList); + if(!result.suggestion.isEmpty()) { + Toast.makeText(getActivity(), + String.format(getString(R.string.did_you_mean), result.suggestion), + Toast.LENGTH_LONG).show(); } } } @@ -344,13 +344,24 @@ public class VideoItemListFragment extends ListFragment { mActivatedPosition = position; } - private void postNewErrorToast(Handler h, final int stringResource) { + private void postNewErrorToast(Handler h, final String message) { + h.post(new Runnable() { + @Override + public void run() { + setListShown(true); + Toast.makeText(getActivity(), message, + Toast.LENGTH_SHORT).show(); + } + }); + } + + private void postNewNothingFoundToast(Handler h, final int stringResource) { h.post(new Runnable() { @Override public void run() { setListShown(true); Toast.makeText(getActivity(), getString(stringResource), - Toast.LENGTH_SHORT).show(); + Toast.LENGTH_LONG).show(); } }); } diff --git a/app/src/main/java/org/schabi/newpipe/extractor/SearchEngine.java b/app/src/main/java/org/schabi/newpipe/extractor/SearchEngine.java index 0c230a59b..677287afe 100644 --- a/app/src/main/java/org/schabi/newpipe/extractor/SearchEngine.java +++ b/app/src/main/java/org/schabi/newpipe/extractor/SearchEngine.java @@ -27,6 +27,12 @@ import java.util.Vector; @SuppressWarnings("ALL") public interface SearchEngine { + public class NothingFoundException extends ExtractionException { + public NothingFoundException(String message) { + super(message); + } + } + ArrayList suggestionList(String query,String contentCountry, Downloader dl) throws ExtractionException, IOException; diff --git a/app/src/main/java/org/schabi/newpipe/extractor/SearchResult.java b/app/src/main/java/org/schabi/newpipe/extractor/SearchResult.java index 4aac7aaf1..887b94564 100644 --- a/app/src/main/java/org/schabi/newpipe/extractor/SearchResult.java +++ b/app/src/main/java/org/schabi/newpipe/extractor/SearchResult.java @@ -28,11 +28,12 @@ public class SearchResult { public static SearchResult getSearchResult(SearchEngine engine, String query, int page, String languageCode, Downloader dl) throws ExtractionException, IOException { - try { - return engine.search(query, page, languageCode, dl).getSearchResult(); - } catch (Exception e) { - throw new ExtractionException("Could not get any search result", e); + + SearchResult result = engine.search(query, page, languageCode, dl).getSearchResult(); + if(result.resultList.isEmpty()) { + throw new ExtractionException("Empty result despite no error"); } + return result; } public String suggestion = ""; diff --git a/app/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeSearchEngine.java b/app/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeSearchEngine.java index 6afed94eb..648cbd749 100644 --- a/app/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeSearchEngine.java +++ b/app/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeSearchEngine.java @@ -7,6 +7,7 @@ import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.schabi.newpipe.extractor.Downloader; +import org.schabi.newpipe.extractor.ExtractionException; import org.schabi.newpipe.extractor.Parser; import org.schabi.newpipe.extractor.ParsingException; import org.schabi.newpipe.extractor.SearchEngine; @@ -52,7 +53,7 @@ public class YoutubeSearchEngine implements SearchEngine { @Override public StreamPreviewInfoCollector search(String query, int page, String languageCode, Downloader downloader) - throws IOException, ParsingException { + throws IOException, ExtractionException { StreamPreviewInfoCollector collector = new StreamPreviewInfoCollector( new YoutubeStreamUrlIdHandler()); Uri.Builder builder = new Uri.Builder(); @@ -98,7 +99,7 @@ public class YoutubeSearchEngine implements SearchEngine { // search message item } else if (!((el = item.select("div[class*=\"search-message\"]").first()) == null)) { //result.errorMessage = el.text(); - throw new StreamExtractor.ContentNotAvailableException(el.text()); + throw new NothingFoundException(el.text()); // video item type } else if (!((el = item.select("div[class*=\"yt-lockup-video\"").first()) == null)) { @@ -211,8 +212,6 @@ public class YoutubeSearchEngine implements SearchEngine { @Override public long getViewCount() throws ParsingException { - throw new ParsingException("blabla"); - /* String output; String input = item.select("div[class=\"yt-lockup-meta\"]").first() .select("li").get(1) @@ -222,11 +221,16 @@ public class YoutubeSearchEngine implements SearchEngine { .replace(".", "") .replace(",", ""); - if(Long.parseLong(output) == 30) { - Log.d(TAG, "bla"); + try { + return Long.parseLong(output); + } catch (NumberFormatException e) { + // if this happens the video probably has no views + if(!input.isEmpty()) { + return 0; + } else { + throw new ParsingException("Could not handle input: " + input, e); + } } - return Long.parseLong(output); - */ } @Override diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index cbaa45111..d91841c66 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -15,7 +15,7 @@ Download Search Settings - Did you mean: + Did you mean: %1$s ? Search page: Share with: Choose browser: