1
0
mirror of https://github.com/TeamNewPipe/NewPipe synced 2024-12-25 01:20:34 +00:00

made duration an integer, and made youtubesearch engine throw more parsingexceptions

This commit is contained in:
Christian Schabesberger 2016-03-02 02:36:45 +01:00
parent 3aecd15916
commit 68b7d9cdff
8 changed files with 164 additions and 34 deletions

View File

@ -68,7 +68,7 @@ public class YoutubeSearchEngineTest extends AndroidTestCase {
public void testItemsHaveRightDuration() { public void testItemsHaveRightDuration() {
for(StreamPreviewInfo i : result.resultList) { for(StreamPreviewInfo i : result.resultList) {
assertTrue(i.duration, i.duration.contains(":")); assertTrue(i.duration >= 0);
} }
} }

View File

@ -1,6 +1,7 @@
package org.schabi.newpipe; package org.schabi.newpipe;
import android.content.Context; import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
@ -74,8 +75,8 @@ class VideoInfoItemViewCreator {
} else { } else {
holder.itemDurationView.setVisibility(View.INVISIBLE); holder.itemDurationView.setVisibility(View.INVISIBLE);
} }
if(info.duration != null && !info.duration.isEmpty()) { if(info.duration > 0) {
holder.itemDurationView.setText(info.duration); holder.itemDurationView.setText(getDurationString(info.duration));
} else { } else {
holder.itemDurationView.setVisibility(View.GONE); holder.itemDurationView.setVisibility(View.GONE);
} }
@ -115,4 +116,50 @@ class VideoInfoItemViewCreator {
} }
} }
public static String getDurationString(int duration) {
Log.d("asfd", Integer.toString(duration) + " ----------------------");
String output = "";
int days = duration / (24 * 60 * 60); /* greater than a day */
duration %= (24 * 60 * 60);
int hours = duration / (60 * 60); /* greater than an hour */
duration %= (60 * 60);
int minutes = duration / 60;
int seconds = duration % 60;
if(days > 0) {
output = Integer.toString(days) + ":";
}
if(hours > 0 || !output.isEmpty()) {
if(hours > 0) {
if(hours >= 10) {
output += Integer.toString(minutes);
} else {
output += "0" + Integer.toString(minutes);
}
} else {
output += "00";
}
output += ":";
}
if(minutes > 0 || !output.isEmpty()) {
if(minutes > 0) {
if(minutes >= 10) {
output += Integer.toString(minutes);
} else {
output += "0" + Integer.toString(minutes);
}
} else {
output += "00";
}
output += ":";
}
if(seconds >= 10) {
output += Integer.toString(seconds);
} else {
output += "0" + Integer.toString(seconds);
}
return output;
}
} }

View File

@ -254,10 +254,12 @@ public class StreamInfo extends AbstractVideoInfo {
//todo: better than this //todo: better than this
if(avi instanceof StreamPreviewInfo) { if(avi instanceof StreamPreviewInfo) {
//shitty String to convert code //shitty String to convert code
/*
String dur = ((StreamPreviewInfo)avi).duration; String dur = ((StreamPreviewInfo)avi).duration;
int minutes = Integer.parseInt(dur.substring(0, dur.indexOf(":"))); int minutes = Integer.parseInt(dur.substring(0, dur.indexOf(":")));
int seconds = Integer.parseInt(dur.substring(dur.indexOf(":")+1, dur.length())); int seconds = Integer.parseInt(dur.substring(dur.indexOf(":")+1, dur.length()));
this.duration = (minutes*60)+seconds; */
this.duration = ((StreamPreviewInfo)avi).duration;
} }
} }

View File

@ -22,5 +22,5 @@ package org.schabi.newpipe.extractor;
/**Info object for previews of unopened videos, eg search results, related videos*/ /**Info object for previews of unopened videos, eg search results, related videos*/
public class StreamPreviewInfo extends AbstractVideoInfo { public class StreamPreviewInfo extends AbstractVideoInfo {
public String duration = ""; public int duration = 0;
} }

View File

@ -23,7 +23,7 @@ package org.schabi.newpipe.extractor;
public interface StreamPreviewInfoExtractor { public interface StreamPreviewInfoExtractor {
String getWebPageUrl() throws ParsingException; String getWebPageUrl() throws ParsingException;
String getTitle() throws ParsingException; String getTitle() throws ParsingException;
String getDuration() throws ParsingException; int getDuration() throws ParsingException;
String getUploader() throws ParsingException; String getUploader() throws ParsingException;
String getUploadDate() throws ParsingException; String getUploadDate() throws ParsingException;
long getViewCount() throws ParsingException; long getViewCount() throws ParsingException;

View File

@ -0,0 +1,61 @@
package org.schabi.newpipe.extractor.services.youtube;
import org.schabi.newpipe.extractor.ParsingException;
/**
* Created by Christian Schabesberger on 02.03.16.
*
* Copyright (C) Christian Schabesberger 2016 <chris.schabesberger@mailbox.org>
* YoutubeParsingHelper.java is part of NewPipe.
*
* NewPipe is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* NewPipe is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with NewPipe. If not, see <http://www.gnu.org/licenses/>.
*/
public class YoutubeParsingHelper {
public static int parseDurationString(String input)
throws ParsingException, NumberFormatException {
String[] splitInput = input.split(":");
String days = "0";
String hours = "0";
String minutes = "0";
String seconds = "0";
switch(splitInput.length) {
case 4:
days = splitInput[0];
hours = splitInput[1];
minutes = splitInput[2];
seconds = splitInput[3];
break;
case 3:
hours = splitInput[0];
minutes = splitInput[1];
seconds = splitInput[2];
break;
case 2:
minutes = splitInput[0];
seconds = splitInput[1];
break;
case 1:
seconds = splitInput[0];
break;
default:
throw new ParsingException("Error duration string with unknown format: " + input);
}
return ((((Integer.parseInt(days) * 24)
+ Integer.parseInt(hours) * 60)
+ Integer.parseInt(minutes)) * 60)
+ Integer.parseInt(seconds);
}
}

View File

@ -172,40 +172,56 @@ public class YoutubeSearchEngine implements SearchEngine {
return new StreamPreviewInfoExtractor() { return new StreamPreviewInfoExtractor() {
@Override @Override
public String getWebPageUrl() throws ParsingException { public String getWebPageUrl() throws ParsingException {
try {
Element el = item.select("div[class*=\"yt-lockup-video\"").first(); Element el = item.select("div[class*=\"yt-lockup-video\"").first();
Element dl = el.select("h3").first().select("a").first(); Element dl = el.select("h3").first().select("a").first();
return dl.attr("abs:href"); return dl.attr("abs:href");
} catch (Exception e) {
throw new ParsingException("Could not get web page url for the video", e);
}
} }
@Override @Override
public String getTitle() throws ParsingException { public String getTitle() throws ParsingException {
try {
Element el = item.select("div[class*=\"yt-lockup-video\"").first(); Element el = item.select("div[class*=\"yt-lockup-video\"").first();
Element dl = el.select("h3").first().select("a").first(); Element dl = el.select("h3").first().select("a").first();
return dl.text(); return dl.text();
} catch (Exception e) {
throw new ParsingException("Could not get title", e);
}
} }
@Override @Override
public String getDuration() throws ParsingException { public int getDuration() throws ParsingException {
try { try {
return item.select("span[class=\"video-time\"]").first().text(); return YoutubeParsingHelper.parseDurationString(
item.select("span[class=\"video-time\"]").first().text());
} catch(Exception e) { } catch(Exception e) {
e.printStackTrace(); throw new ParsingException("Could not get Duration", e);
} }
return "";
} }
@Override @Override
public String getUploader() throws ParsingException { public String getUploader() throws ParsingException {
try {
return item.select("div[class=\"yt-lockup-byline\"]").first() return item.select("div[class=\"yt-lockup-byline\"]").first()
.select("a").first() .select("a").first()
.text(); .text();
} catch (Exception e) {
throw new ParsingException("Could not get uploader", e);
}
} }
@Override @Override
public String getUploadDate() throws ParsingException { public String getUploadDate() throws ParsingException {
try {
return item.select("div[class=\"yt-lockup-meta\"]").first() return item.select("div[class=\"yt-lockup-meta\"]").first()
.select("li").first() .select("li").first()
.text(); .text();
} catch(Exception e) {
throw new ParsingException("Could not get uplaod date", e);
}
} }
@Override @Override
@ -233,6 +249,7 @@ public class YoutubeSearchEngine implements SearchEngine {
@Override @Override
public String getThumbnailUrl() throws ParsingException { public String getThumbnailUrl() throws ParsingException {
try {
String url; String url;
Element te = item.select("div[class=\"yt-thumb video-thumb\"]").first() Element te = item.select("div[class=\"yt-thumb video-thumb\"]").first()
.select("img").first(); .select("img").first();
@ -243,8 +260,10 @@ public class YoutubeSearchEngine implements SearchEngine {
if (url.contains(".gif")) { if (url.contains(".gif")) {
url = te.attr("abs:data-thumb"); url = te.attr("abs:data-thumb");
} }
return url; return url;
} catch (Exception e) {
throw new ParsingException("Could not get thumbnail url", e);
}
} }
}; };
} }

View File

@ -701,7 +701,8 @@ public class YoutubeStreamExtractor implements StreamExtractor {
} }
info.uploader = li.select("span.g-hovercard").first().text(); info.uploader = li.select("span.g-hovercard").first().text();
info.duration = li.select("span.video-time").first().text(); info.duration = YoutubeParsingHelper.parseDurationString(
li.select("span.video-time").first().text());
Element img = li.select("img").first(); Element img = li.select("img").first();
info.thumbnail_url = img.attr("abs:src"); info.thumbnail_url = img.attr("abs:src");