2018-07-13 16:02:40 +00:00
|
|
|
package org.schabi.newpipe.extractor.services.youtube.linkHandler;
|
2017-03-01 17:47:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
import org.schabi.newpipe.extractor.exceptions.ParsingException;
|
|
|
|
|
2019-01-13 11:52:07 +00:00
|
|
|
import java.net.URL;
|
|
|
|
|
2017-06-29 18:12:55 +00:00
|
|
|
/*
|
2017-03-01 17:47:52 +00:00
|
|
|
* 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 {
|
|
|
|
|
|
|
|
private YoutubeParsingHelper() {
|
|
|
|
}
|
|
|
|
|
2019-01-13 11:52:07 +00:00
|
|
|
private static boolean isHTTP(URL url) {
|
|
|
|
// make sure its http or https
|
|
|
|
String protocol = url.getProtocol();
|
|
|
|
if (!protocol.equals("http") && !protocol.equals("https")) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
boolean usesDefaultPort = url.getPort() == url.getDefaultPort();
|
|
|
|
boolean setsNoPort = url.getPort() == -1;
|
|
|
|
|
|
|
|
return setsNoPort || usesDefaultPort;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean isYoutubeURL(URL url) {
|
|
|
|
// make sure its http or https
|
|
|
|
if (!isHTTP(url))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// make sure its a known youtube url
|
|
|
|
String host = url.getHost();
|
|
|
|
return host.equalsIgnoreCase("youtube.com") || host.equalsIgnoreCase("www.youtube.com")
|
|
|
|
|| host.equalsIgnoreCase("m.youtube.com");
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean isYoutubeALikeURL(URL url) {
|
|
|
|
// make sure its http or https
|
|
|
|
if (!isHTTP(url))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// make sure its a known youtube url
|
|
|
|
String host = url.getHost();
|
|
|
|
return host.equalsIgnoreCase("youtube.com") || host.equalsIgnoreCase("www.youtube.com")
|
|
|
|
|| host.equalsIgnoreCase("m.youtube.com") || host.equalsIgnoreCase("www.youtube-nocookie.com")
|
2019-01-24 09:48:29 +00:00
|
|
|
|| host.equalsIgnoreCase("youtu.be") || host.equalsIgnoreCase("hooktube.com")
|
|
|
|
|| host.equalsIgnoreCase("invidio.us");
|
2019-01-13 11:52:07 +00:00
|
|
|
}
|
|
|
|
|
2017-08-11 18:21:49 +00:00
|
|
|
public static long parseDurationString(String input)
|
2017-03-01 17:47:52 +00:00
|
|
|
throws ParsingException, NumberFormatException {
|
2018-09-09 09:53:10 +00:00
|
|
|
|
|
|
|
// If time separator : is not detected, try . instead
|
2018-09-09 12:01:39 +00:00
|
|
|
|
|
|
|
final String[] splitInput = input.contains(":")
|
|
|
|
? input.split(":")
|
|
|
|
: input.split("\\.");
|
|
|
|
|
2017-03-01 17:47:52 +00:00
|
|
|
String days = "0";
|
|
|
|
String hours = "0";
|
|
|
|
String minutes = "0";
|
2018-09-09 12:01:39 +00:00
|
|
|
final String seconds;
|
2017-03-01 17:47:52 +00:00
|
|
|
|
2017-06-29 18:12:55 +00:00
|
|
|
switch (splitInput.length) {
|
2017-03-01 17:47:52 +00:00
|
|
|
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);
|
|
|
|
}
|
2017-08-11 18:21:49 +00:00
|
|
|
return ((((Long.parseLong(days) * 24)
|
|
|
|
+ Long.parseLong(hours) * 60)
|
|
|
|
+ Long.parseLong(minutes)) * 60)
|
|
|
|
+ Long.parseLong(seconds);
|
2017-03-01 17:47:52 +00:00
|
|
|
}
|
|
|
|
}
|