From 9005105e5244b107e5a0c2a0653d2db37609cf1e Mon Sep 17 00:00:00 2001 From: Christian Schabesberger Date: Wed, 1 Mar 2017 18:47:52 +0100 Subject: [PATCH] initial commit --- AbstractStreamInfo.java | 41 + DashMpdParser.java | 93 ++ Downloader.java | 52 ++ InfoItem.java | 33 + InfoItemCollector.java | 61 ++ LICENSE | 674 ++++++++++++++ MediaFormat.java | 83 ++ NewPipe.java | 88 ++ Parser.java | 77 ++ README.md | 5 + ServiceList.java | 13 + StreamingService.java | 78 ++ SuggestionExtractor.java | 43 + UrlIdHandler.java | 35 + channel/ChannelExtractor.java | 62 ++ channel/ChannelInfo.java | 86 ++ channel/ChannelInfoItem.java | 44 + channel/ChannelInfoItemCollector.java | 74 ++ channel/ChannelInfoItemExtractor.java | 32 + copyright | 15 + exceptions/ExtractionException.java | 33 + exceptions/FoundAdException.java | 30 + exceptions/ParsingException.java | 31 + exceptions/ReCaptchaException.java | 27 + search/InfoItemSearchCollector.java | 79 ++ search/SearchEngine.java | 53 ++ search/SearchResult.java | 56 ++ services/youtube/YoutubeChannelExtractor.java | 361 +++++++ .../YoutubeChannelInfoItemExtractor.java | 83 ++ .../youtube/YoutubeChannelUrlIdHandler.java | 47 + services/youtube/YoutubeParsingHelper.java | 66 ++ services/youtube/YoutubeSearchEngine.java | 119 +++ services/youtube/YoutubeService.java | 82 ++ services/youtube/YoutubeStreamExtractor.java | 878 ++++++++++++++++++ .../YoutubeStreamInfoItemExtractor.java | 186 ++++ .../youtube/YoutubeStreamUrlIdHandler.java | 163 ++++ .../youtube/YoutubeSuggestionExtractor.java | 99 ++ stream_info/AudioStream.java | 46 + stream_info/StreamExtractor.java | 104 +++ stream_info/StreamInfo.java | 294 ++++++ stream_info/StreamInfoItem.java | 41 + stream_info/StreamInfoItemCollector.java | 102 ++ stream_info/StreamInfoItemExtractor.java | 36 + stream_info/VideoStream.java | 44 + 44 files changed, 4749 insertions(+) create mode 100644 AbstractStreamInfo.java create mode 100644 DashMpdParser.java create mode 100644 Downloader.java create mode 100644 InfoItem.java create mode 100644 InfoItemCollector.java create mode 100644 LICENSE create mode 100644 MediaFormat.java create mode 100644 NewPipe.java create mode 100644 Parser.java create mode 100644 README.md create mode 100644 ServiceList.java create mode 100644 StreamingService.java create mode 100644 SuggestionExtractor.java create mode 100644 UrlIdHandler.java create mode 100644 channel/ChannelExtractor.java create mode 100644 channel/ChannelInfo.java create mode 100644 channel/ChannelInfoItem.java create mode 100644 channel/ChannelInfoItemCollector.java create mode 100644 channel/ChannelInfoItemExtractor.java create mode 100644 copyright create mode 100644 exceptions/ExtractionException.java create mode 100644 exceptions/FoundAdException.java create mode 100644 exceptions/ParsingException.java create mode 100644 exceptions/ReCaptchaException.java create mode 100644 search/InfoItemSearchCollector.java create mode 100644 search/SearchEngine.java create mode 100644 search/SearchResult.java create mode 100644 services/youtube/YoutubeChannelExtractor.java create mode 100644 services/youtube/YoutubeChannelInfoItemExtractor.java create mode 100644 services/youtube/YoutubeChannelUrlIdHandler.java create mode 100644 services/youtube/YoutubeParsingHelper.java create mode 100644 services/youtube/YoutubeSearchEngine.java create mode 100644 services/youtube/YoutubeService.java create mode 100644 services/youtube/YoutubeStreamExtractor.java create mode 100644 services/youtube/YoutubeStreamInfoItemExtractor.java create mode 100644 services/youtube/YoutubeStreamUrlIdHandler.java create mode 100644 services/youtube/YoutubeSuggestionExtractor.java create mode 100644 stream_info/AudioStream.java create mode 100644 stream_info/StreamExtractor.java create mode 100644 stream_info/StreamInfo.java create mode 100644 stream_info/StreamInfoItem.java create mode 100644 stream_info/StreamInfoItemCollector.java create mode 100644 stream_info/StreamInfoItemExtractor.java create mode 100644 stream_info/VideoStream.java diff --git a/AbstractStreamInfo.java b/AbstractStreamInfo.java new file mode 100644 index 00000000..bfa86b3f --- /dev/null +++ b/AbstractStreamInfo.java @@ -0,0 +1,41 @@ +package org.schabi.newpipe.extractor; + +/** + * Copyright (C) Christian Schabesberger 2016 + * AbstractStreamInfo.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 . + */ + +/**Common properties between StreamInfo and StreamInfoItem.*/ +public abstract class AbstractStreamInfo { + public enum StreamType { + NONE, // placeholder to check if stream type was checked or not + VIDEO_STREAM, + AUDIO_STREAM, + LIVE_STREAM, + AUDIO_LIVE_STREAM, + FILE + } + + public StreamType stream_type; + public int service_id = -1; + public String id = ""; + public String title = ""; + public String uploader = ""; + public String thumbnail_url = ""; + public String webpage_url = ""; + public String upload_date = ""; + public long view_count = -1; +} diff --git a/DashMpdParser.java b/DashMpdParser.java new file mode 100644 index 00000000..527750a8 --- /dev/null +++ b/DashMpdParser.java @@ -0,0 +1,93 @@ +package org.schabi.newpipe.extractor; + +import org.schabi.newpipe.extractor.exceptions.ParsingException; +import org.schabi.newpipe.extractor.exceptions.ReCaptchaException; +import org.schabi.newpipe.extractor.stream_info.AudioStream; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.NodeList; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.List; +import java.util.Vector; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; + +/** + * Created by Christian Schabesberger on 02.02.16. + * + * Copyright (C) Christian Schabesberger 2016 + * DashMpdParser.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 . + */ + +public class DashMpdParser { + + private DashMpdParser() { + } + + static class DashMpdParsingException extends ParsingException { + DashMpdParsingException(String message, Exception e) { + super(message, e); + } + } + + public static List getAudioStreams(String dashManifestUrl) + throws DashMpdParsingException, ReCaptchaException { + String dashDoc; + Downloader downloader = NewPipe.getDownloader(); + try { + dashDoc = downloader.download(dashManifestUrl); + } catch(IOException ioe) { + throw new DashMpdParsingException("Could not get dash mpd: " + dashManifestUrl, ioe); + } catch (ReCaptchaException e) { + throw new ReCaptchaException("reCaptcha Challenge needed"); + } + Vector audioStreams = new Vector<>(); + + try { + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + DocumentBuilder builder = factory.newDocumentBuilder(); + InputStream stream = new ByteArrayInputStream(dashDoc.getBytes()); + + Document doc = builder.parse(stream); + NodeList adaptationSetList = doc.getElementsByTagName("AdaptationSet"); + for(int i = 0; i < adaptationSetList.getLength(); i++) { + Element adaptationSet = (Element) adaptationSetList.item(i); + String memeType = adaptationSet.getAttribute("mimeType"); + if(memeType.contains("audio")) { + Element representation = (Element) adaptationSet.getElementsByTagName("Representation").item(0); + String url = representation.getElementsByTagName("BaseURL").item(0).getTextContent(); + int bandwidth = Integer.parseInt(representation.getAttribute("bandwidth")); + int samplingRate = Integer.parseInt(representation.getAttribute("audioSamplingRate")); + int format = -1; + if(memeType.equals(MediaFormat.WEBMA.mimeType)) { + format = MediaFormat.WEBMA.id; + } else if(memeType.equals(MediaFormat.M4A.mimeType)) { + format = MediaFormat.M4A.id; + } + audioStreams.add(new AudioStream(url, format, bandwidth, samplingRate)); + } + } + } + catch(Exception e) { + throw new DashMpdParsingException("Could not parse Dash mpd", e); + } + return audioStreams; + } +} diff --git a/Downloader.java b/Downloader.java new file mode 100644 index 00000000..fe85696b --- /dev/null +++ b/Downloader.java @@ -0,0 +1,52 @@ +package org.schabi.newpipe.extractor; + +import org.schabi.newpipe.extractor.exceptions.ReCaptchaException; + +import java.io.IOException; +import java.util.Map; + +/** + * Created by Christian Schabesberger on 28.01.16. + * + * Copyright (C) Christian Schabesberger 2016 + * Downloader.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 . + */ + +public interface Downloader { + + /**Download the text file at the supplied URL as in download(String), + * but set the HTTP header field "Accept-Language" to the supplied string. + * @param siteUrl the URL of the text file to return the contents of + * @param language the language (usually a 2-character code) to set as the preferred language + * @return the contents of the specified text file + * @throws IOException*/ + String download(String siteUrl, String language) throws IOException, ReCaptchaException; + + /**Download the text file at the supplied URL as in download(String), + * but set the HTTP header field "Accept-Language" to the supplied string. + * @param siteUrl the URL of the text file to return the contents of + * @param customProperties set request header properties + * @return the contents of the specified text file + * @throws IOException*/ + String download(String siteUrl, Map customProperties) throws IOException, ReCaptchaException; + + /**Download (via HTTP) the text file located at the supplied URL, and return its contents. + * Primarily intended for downloading web pages. + * @param siteUrl the URL of the text file to download + * @return the contents of the specified text file + * @throws IOException*/ + String download(String siteUrl) throws IOException, ReCaptchaException; +} diff --git a/InfoItem.java b/InfoItem.java new file mode 100644 index 00000000..a3395e8e --- /dev/null +++ b/InfoItem.java @@ -0,0 +1,33 @@ +package org.schabi.newpipe.extractor; + +/** + * Created by the-scrabi on 11.02.17. + * + * Copyright (C) Christian Schabesberger 2017 + * InfoItem.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 . + */ + +public interface InfoItem { + public enum InfoType { + STREAM, + PLAYLIST, + CHANNEL + } + + InfoType infoType(); + String getTitle(); + String getLink(); +} diff --git a/InfoItemCollector.java b/InfoItemCollector.java new file mode 100644 index 00000000..d3b0927a --- /dev/null +++ b/InfoItemCollector.java @@ -0,0 +1,61 @@ +package org.schabi.newpipe.extractor; + +import org.schabi.newpipe.extractor.exceptions.ExtractionException; + +import java.util.List; +import java.util.Vector; + +/** + * Created by Christian Schabesberger on 12.02.17. + * + * Copyright (C) Christian Schabesberger 2017 + * InfoItemCollector.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 . + */ + +public class InfoItemCollector { + private List itemList = new Vector<>(); + private List errors = new Vector<>(); + private int serviceId = -1; + + public InfoItemCollector(int serviceId) { + this.serviceId = serviceId; + } + + public List getItemList() { + return itemList; + } + public List getErrors() { + return errors; + } + protected void addFromCollector(InfoItemCollector otherC) throws ExtractionException { + if(serviceId != otherC.serviceId) { + throw new ExtractionException("Service Id does not equal: " + + NewPipe.getNameOfService(serviceId) + + " and " + NewPipe.getNameOfService(otherC.serviceId)); + } + errors.addAll(otherC.errors); + itemList.addAll(otherC.itemList); + } + protected void addError(Exception e) { + errors.add(e); + } + protected void addItem(InfoItem item) { + itemList.add(item); + } + protected int getServiceId() { + return serviceId; + } +} diff --git a/LICENSE b/LICENSE new file mode 100644 index 00000000..94a9ed02 --- /dev/null +++ b/LICENSE @@ -0,0 +1,674 @@ + GNU GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The GNU General Public License is a free, copyleft license for +software and other kinds of works. + + The licenses for most software and other practical works are designed +to take away your freedom to share and change the works. By contrast, +the GNU General Public License is intended to guarantee your freedom to +share and change all versions of a program--to make sure it remains free +software for all its users. We, the Free Software Foundation, use the +GNU General Public License for most of our software; it applies also to +any other work released this way by its authors. You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +them if you wish), that you receive source code or can get it if you +want it, that you can change the software or use pieces of it in new +free programs, and that you know you can do these things. + + To protect your rights, we need to prevent others from denying you +these rights or asking you to surrender the rights. Therefore, you have +certain responsibilities if you distribute copies of the software, or if +you modify it: responsibilities to respect the freedom of others. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must pass on to the recipients the same +freedoms that you received. You must make sure that they, too, receive +or can get the source code. And you must show them these terms so they +know their rights. + + Developers that use the GNU GPL protect your rights with two steps: +(1) assert copyright on the software, and (2) offer you this License +giving you legal permission to copy, distribute and/or modify it. + + For the developers' and authors' protection, the GPL clearly explains +that there is no warranty for this free software. For both users' and +authors' sake, the GPL requires that modified versions be marked as +changed, so that their problems will not be attributed erroneously to +authors of previous versions. + + Some devices are designed to deny users access to install or run +modified versions of the software inside them, although the manufacturer +can do so. This is fundamentally incompatible with the aim of +protecting users' freedom to change the software. The systematic +pattern of such abuse occurs in the area of products for individuals to +use, which is precisely where it is most unacceptable. Therefore, we +have designed this version of the GPL to prohibit the practice for those +products. If such problems arise substantially in other domains, we +stand ready to extend this provision to those domains in future versions +of the GPL, as needed to protect the freedom of users. + + Finally, every program is threatened constantly by software patents. +States should not allow patents to restrict development and use of +software on general-purpose computers, but in those that do, we wish to +avoid the special danger that patents applied to a free program could +make it effectively proprietary. To prevent this, the GPL assures that +patents cannot be used to render the program non-free. + + The precise terms and conditions for copying, distribution and +modification follow. + + TERMS AND CONDITIONS + + 0. Definitions. + + "This License" refers to version 3 of the GNU General Public License. + + "Copyright" also means copyright-like laws that apply to other kinds of +works, such as semiconductor masks. + + "The Program" refers to any copyrightable work licensed under this +License. Each licensee is addressed as "you". "Licensees" and +"recipients" may be individuals or organizations. + + To "modify" a work means to copy from or adapt all or part of the work +in a fashion requiring copyright permission, other than the making of an +exact copy. The resulting work is called a "modified version" of the +earlier work or a work "based on" the earlier work. + + A "covered work" means either the unmodified Program or a work based +on the Program. + + To "propagate" a work means to do anything with it that, without +permission, would make you directly or secondarily liable for +infringement under applicable copyright law, except executing it on a +computer or modifying a private copy. Propagation includes copying, +distribution (with or without modification), making available to the +public, and in some countries other activities as well. + + To "convey" a work means any kind of propagation that enables other +parties to make or receive copies. Mere interaction with a user through +a computer network, with no transfer of a copy, is not conveying. + + An interactive user interface displays "Appropriate Legal Notices" +to the extent that it includes a convenient and prominently visible +feature that (1) displays an appropriate copyright notice, and (2) +tells the user that there is no warranty for the work (except to the +extent that warranties are provided), that licensees may convey the +work under this License, and how to view a copy of this License. If +the interface presents a list of user commands or options, such as a +menu, a prominent item in the list meets this criterion. + + 1. Source Code. + + The "source code" for a work means the preferred form of the work +for making modifications to it. "Object code" means any non-source +form of a work. + + A "Standard Interface" means an interface that either is an official +standard defined by a recognized standards body, or, in the case of +interfaces specified for a particular programming language, one that +is widely used among developers working in that language. + + The "System Libraries" of an executable work include anything, other +than the work as a whole, that (a) is included in the normal form of +packaging a Major Component, but which is not part of that Major +Component, and (b) serves only to enable use of the work with that +Major Component, or to implement a Standard Interface for which an +implementation is available to the public in source code form. A +"Major Component", in this context, means a major essential component +(kernel, window system, and so on) of the specific operating system +(if any) on which the executable work runs, or a compiler used to +produce the work, or an object code interpreter used to run it. + + The "Corresponding Source" for a work in object code form means all +the source code needed to generate, install, and (for an executable +work) run the object code and to modify the work, including scripts to +control those activities. However, it does not include the work's +System Libraries, or general-purpose tools or generally available free +programs which are used unmodified in performing those activities but +which are not part of the work. For example, Corresponding Source +includes interface definition files associated with source files for +the work, and the source code for shared libraries and dynamically +linked subprograms that the work is specifically designed to require, +such as by intimate data communication or control flow between those +subprograms and other parts of the work. + + The Corresponding Source need not include anything that users +can regenerate automatically from other parts of the Corresponding +Source. + + The Corresponding Source for a work in source code form is that +same work. + + 2. Basic Permissions. + + All rights granted under this License are granted for the term of +copyright on the Program, and are irrevocable provided the stated +conditions are met. This License explicitly affirms your unlimited +permission to run the unmodified Program. The output from running a +covered work is covered by this License only if the output, given its +content, constitutes a covered work. This License acknowledges your +rights of fair use or other equivalent, as provided by copyright law. + + You may make, run and propagate covered works that you do not +convey, without conditions so long as your license otherwise remains +in force. You may convey covered works to others for the sole purpose +of having them make modifications exclusively for you, or provide you +with facilities for running those works, provided that you comply with +the terms of this License in conveying all material for which you do +not control copyright. Those thus making or running the covered works +for you must do so exclusively on your behalf, under your direction +and control, on terms that prohibit them from making any copies of +your copyrighted material outside their relationship with you. + + Conveying under any other circumstances is permitted solely under +the conditions stated below. Sublicensing is not allowed; section 10 +makes it unnecessary. + + 3. Protecting Users' Legal Rights From Anti-Circumvention Law. + + No covered work shall be deemed part of an effective technological +measure under any applicable law fulfilling obligations under article +11 of the WIPO copyright treaty adopted on 20 December 1996, or +similar laws prohibiting or restricting circumvention of such +measures. + + When you convey a covered work, you waive any legal power to forbid +circumvention of technological measures to the extent such circumvention +is effected by exercising rights under this License with respect to +the covered work, and you disclaim any intention to limit operation or +modification of the work as a means of enforcing, against the work's +users, your or third parties' legal rights to forbid circumvention of +technological measures. + + 4. Conveying Verbatim Copies. + + You may convey verbatim copies of the Program's source code as you +receive it, in any medium, provided that you conspicuously and +appropriately publish on each copy an appropriate copyright notice; +keep intact all notices stating that this License and any +non-permissive terms added in accord with section 7 apply to the code; +keep intact all notices of the absence of any warranty; and give all +recipients a copy of this License along with the Program. + + You may charge any price or no price for each copy that you convey, +and you may offer support or warranty protection for a fee. + + 5. Conveying Modified Source Versions. + + You may convey a work based on the Program, or the modifications to +produce it from the Program, in the form of source code under the +terms of section 4, provided that you also meet all of these conditions: + + a) The work must carry prominent notices stating that you modified + it, and giving a relevant date. + + b) The work must carry prominent notices stating that it is + released under this License and any conditions added under section + 7. This requirement modifies the requirement in section 4 to + "keep intact all notices". + + c) You must license the entire work, as a whole, under this + License to anyone who comes into possession of a copy. This + License will therefore apply, along with any applicable section 7 + additional terms, to the whole of the work, and all its parts, + regardless of how they are packaged. This License gives no + permission to license the work in any other way, but it does not + invalidate such permission if you have separately received it. + + d) If the work has interactive user interfaces, each must display + Appropriate Legal Notices; however, if the Program has interactive + interfaces that do not display Appropriate Legal Notices, your + work need not make them do so. + + A compilation of a covered work with other separate and independent +works, which are not by their nature extensions of the covered work, +and which are not combined with it such as to form a larger program, +in or on a volume of a storage or distribution medium, is called an +"aggregate" if the compilation and its resulting copyright are not +used to limit the access or legal rights of the compilation's users +beyond what the individual works permit. Inclusion of a covered work +in an aggregate does not cause this License to apply to the other +parts of the aggregate. + + 6. Conveying Non-Source Forms. + + You may convey a covered work in object code form under the terms +of sections 4 and 5, provided that you also convey the +machine-readable Corresponding Source under the terms of this License, +in one of these ways: + + a) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by the + Corresponding Source fixed on a durable physical medium + customarily used for software interchange. + + b) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by a + written offer, valid for at least three years and valid for as + long as you offer spare parts or customer support for that product + model, to give anyone who possesses the object code either (1) a + copy of the Corresponding Source for all the software in the + product that is covered by this License, on a durable physical + medium customarily used for software interchange, for a price no + more than your reasonable cost of physically performing this + conveying of source, or (2) access to copy the + Corresponding Source from a network server at no charge. + + c) Convey individual copies of the object code with a copy of the + written offer to provide the Corresponding Source. This + alternative is allowed only occasionally and noncommercially, and + only if you received the object code with such an offer, in accord + with subsection 6b. + + d) Convey the object code by offering access from a designated + place (gratis or for a charge), and offer equivalent access to the + Corresponding Source in the same way through the same place at no + further charge. You need not require recipients to copy the + Corresponding Source along with the object code. If the place to + copy the object code is a network server, the Corresponding Source + may be on a different server (operated by you or a third party) + that supports equivalent copying facilities, provided you maintain + clear directions next to the object code saying where to find the + Corresponding Source. Regardless of what server hosts the + Corresponding Source, you remain obligated to ensure that it is + available for as long as needed to satisfy these requirements. + + e) Convey the object code using peer-to-peer transmission, provided + you inform other peers where the object code and Corresponding + Source of the work are being offered to the general public at no + charge under subsection 6d. + + A separable portion of the object code, whose source code is excluded +from the Corresponding Source as a System Library, need not be +included in conveying the object code work. + + A "User Product" is either (1) a "consumer product", which means any +tangible personal property which is normally used for personal, family, +or household purposes, or (2) anything designed or sold for incorporation +into a dwelling. In determining whether a product is a consumer product, +doubtful cases shall be resolved in favor of coverage. For a particular +product received by a particular user, "normally used" refers to a +typical or common use of that class of product, regardless of the status +of the particular user or of the way in which the particular user +actually uses, or expects or is expected to use, the product. A product +is a consumer product regardless of whether the product has substantial +commercial, industrial or non-consumer uses, unless such uses represent +the only significant mode of use of the product. + + "Installation Information" for a User Product means any methods, +procedures, authorization keys, or other information required to install +and execute modified versions of a covered work in that User Product from +a modified version of its Corresponding Source. The information must +suffice to ensure that the continued functioning of the modified object +code is in no case prevented or interfered with solely because +modification has been made. + + If you convey an object code work under this section in, or with, or +specifically for use in, a User Product, and the conveying occurs as +part of a transaction in which the right of possession and use of the +User Product is transferred to the recipient in perpetuity or for a +fixed term (regardless of how the transaction is characterized), the +Corresponding Source conveyed under this section must be accompanied +by the Installation Information. But this requirement does not apply +if neither you nor any third party retains the ability to install +modified object code on the User Product (for example, the work has +been installed in ROM). + + The requirement to provide Installation Information does not include a +requirement to continue to provide support service, warranty, or updates +for a work that has been modified or installed by the recipient, or for +the User Product in which it has been modified or installed. Access to a +network may be denied when the modification itself materially and +adversely affects the operation of the network or violates the rules and +protocols for communication across the network. + + Corresponding Source conveyed, and Installation Information provided, +in accord with this section must be in a format that is publicly +documented (and with an implementation available to the public in +source code form), and must require no special password or key for +unpacking, reading or copying. + + 7. Additional Terms. + + "Additional permissions" are terms that supplement the terms of this +License by making exceptions from one or more of its conditions. +Additional permissions that are applicable to the entire Program shall +be treated as though they were included in this License, to the extent +that they are valid under applicable law. If additional permissions +apply only to part of the Program, that part may be used separately +under those permissions, but the entire Program remains governed by +this License without regard to the additional permissions. + + When you convey a copy of a covered work, you may at your option +remove any additional permissions from that copy, or from any part of +it. (Additional permissions may be written to require their own +removal in certain cases when you modify the work.) You may place +additional permissions on material, added by you to a covered work, +for which you have or can give appropriate copyright permission. + + Notwithstanding any other provision of this License, for material you +add to a covered work, you may (if authorized by the copyright holders of +that material) supplement the terms of this License with terms: + + a) Disclaiming warranty or limiting liability differently from the + terms of sections 15 and 16 of this License; or + + b) Requiring preservation of specified reasonable legal notices or + author attributions in that material or in the Appropriate Legal + Notices displayed by works containing it; or + + c) Prohibiting misrepresentation of the origin of that material, or + requiring that modified versions of such material be marked in + reasonable ways as different from the original version; or + + d) Limiting the use for publicity purposes of names of licensors or + authors of the material; or + + e) Declining to grant rights under trademark law for use of some + trade names, trademarks, or service marks; or + + f) Requiring indemnification of licensors and authors of that + material by anyone who conveys the material (or modified versions of + it) with contractual assumptions of liability to the recipient, for + any liability that these contractual assumptions directly impose on + those licensors and authors. + + All other non-permissive additional terms are considered "further +restrictions" within the meaning of section 10. If the Program as you +received it, or any part of it, contains a notice stating that it is +governed by this License along with a term that is a further +restriction, you may remove that term. If a license document contains +a further restriction but permits relicensing or conveying under this +License, you may add to a covered work material governed by the terms +of that license document, provided that the further restriction does +not survive such relicensing or conveying. + + If you add terms to a covered work in accord with this section, you +must place, in the relevant source files, a statement of the +additional terms that apply to those files, or a notice indicating +where to find the applicable terms. + + Additional terms, permissive or non-permissive, may be stated in the +form of a separately written license, or stated as exceptions; +the above requirements apply either way. + + 8. Termination. + + You may not propagate or modify a covered work except as expressly +provided under this License. Any attempt otherwise to propagate or +modify it is void, and will automatically terminate your rights under +this License (including any patent licenses granted under the third +paragraph of section 11). + + However, if you cease all violation of this License, then your +license from a particular copyright holder is reinstated (a) +provisionally, unless and until the copyright holder explicitly and +finally terminates your license, and (b) permanently, if the copyright +holder fails to notify you of the violation by some reasonable means +prior to 60 days after the cessation. + + Moreover, your license from a particular copyright holder is +reinstated permanently if the copyright holder notifies you of the +violation by some reasonable means, this is the first time you have +received notice of violation of this License (for any work) from that +copyright holder, and you cure the violation prior to 30 days after +your receipt of the notice. + + Termination of your rights under this section does not terminate the +licenses of parties who have received copies or rights from you under +this License. If your rights have been terminated and not permanently +reinstated, you do not qualify to receive new licenses for the same +material under section 10. + + 9. Acceptance Not Required for Having Copies. + + You are not required to accept this License in order to receive or +run a copy of the Program. Ancillary propagation of a covered work +occurring solely as a consequence of using peer-to-peer transmission +to receive a copy likewise does not require acceptance. However, +nothing other than this License grants you permission to propagate or +modify any covered work. These actions infringe copyright if you do +not accept this License. Therefore, by modifying or propagating a +covered work, you indicate your acceptance of this License to do so. + + 10. Automatic Licensing of Downstream Recipients. + + Each time you convey a covered work, the recipient automatically +receives a license from the original licensors, to run, modify and +propagate that work, subject to this License. You are not responsible +for enforcing compliance by third parties with this License. + + An "entity transaction" is a transaction transferring control of an +organization, or substantially all assets of one, or subdividing an +organization, or merging organizations. If propagation of a covered +work results from an entity transaction, each party to that +transaction who receives a copy of the work also receives whatever +licenses to the work the party's predecessor in interest had or could +give under the previous paragraph, plus a right to possession of the +Corresponding Source of the work from the predecessor in interest, if +the predecessor has it or can get it with reasonable efforts. + + You may not impose any further restrictions on the exercise of the +rights granted or affirmed under this License. For example, you may +not impose a license fee, royalty, or other charge for exercise of +rights granted under this License, and you may not initiate litigation +(including a cross-claim or counterclaim in a lawsuit) alleging that +any patent claim is infringed by making, using, selling, offering for +sale, or importing the Program or any portion of it. + + 11. Patents. + + A "contributor" is a copyright holder who authorizes use under this +License of the Program or a work on which the Program is based. The +work thus licensed is called the contributor's "contributor version". + + A contributor's "essential patent claims" are all patent claims +owned or controlled by the contributor, whether already acquired or +hereafter acquired, that would be infringed by some manner, permitted +by this License, of making, using, or selling its contributor version, +but do not include claims that would be infringed only as a +consequence of further modification of the contributor version. For +purposes of this definition, "control" includes the right to grant +patent sublicenses in a manner consistent with the requirements of +this License. + + Each contributor grants you a non-exclusive, worldwide, royalty-free +patent license under the contributor's essential patent claims, to +make, use, sell, offer for sale, import and otherwise run, modify and +propagate the contents of its contributor version. + + In the following three paragraphs, a "patent license" is any express +agreement or commitment, however denominated, not to enforce a patent +(such as an express permission to practice a patent or covenant not to +sue for patent infringement). To "grant" such a patent license to a +party means to make such an agreement or commitment not to enforce a +patent against the party. + + If you convey a covered work, knowingly relying on a patent license, +and the Corresponding Source of the work is not available for anyone +to copy, free of charge and under the terms of this License, through a +publicly available network server or other readily accessible means, +then you must either (1) cause the Corresponding Source to be so +available, or (2) arrange to deprive yourself of the benefit of the +patent license for this particular work, or (3) arrange, in a manner +consistent with the requirements of this License, to extend the patent +license to downstream recipients. "Knowingly relying" means you have +actual knowledge that, but for the patent license, your conveying the +covered work in a country, or your recipient's use of the covered work +in a country, would infringe one or more identifiable patents in that +country that you have reason to believe are valid. + + If, pursuant to or in connection with a single transaction or +arrangement, you convey, or propagate by procuring conveyance of, a +covered work, and grant a patent license to some of the parties +receiving the covered work authorizing them to use, propagate, modify +or convey a specific copy of the covered work, then the patent license +you grant is automatically extended to all recipients of the covered +work and works based on it. + + A patent license is "discriminatory" if it does not include within +the scope of its coverage, prohibits the exercise of, or is +conditioned on the non-exercise of one or more of the rights that are +specifically granted under this License. You may not convey a covered +work if you are a party to an arrangement with a third party that is +in the business of distributing software, under which you make payment +to the third party based on the extent of your activity of conveying +the work, and under which the third party grants, to any of the +parties who would receive the covered work from you, a discriminatory +patent license (a) in connection with copies of the covered work +conveyed by you (or copies made from those copies), or (b) primarily +for and in connection with specific products or compilations that +contain the covered work, unless you entered into that arrangement, +or that patent license was granted, prior to 28 March 2007. + + Nothing in this License shall be construed as excluding or limiting +any implied license or other defenses to infringement that may +otherwise be available to you under applicable patent law. + + 12. No Surrender of Others' Freedom. + + If conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot convey a +covered work so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you may +not convey it at all. For example, if you agree to terms that obligate you +to collect a royalty for further conveying from those to whom you convey +the Program, the only way you could satisfy both those terms and this +License would be to refrain entirely from conveying the Program. + + 13. Use with the GNU Affero General Public License. + + Notwithstanding any other provision of this License, you have +permission to link or combine any covered work with a work licensed +under version 3 of the GNU Affero General Public License into a single +combined work, and to convey the resulting work. The terms of this +License will continue to apply to the part which is the covered work, +but the special requirements of the GNU Affero General Public License, +section 13, concerning interaction through a network will apply to the +combination as such. + + 14. Revised Versions of this License. + + The Free Software Foundation may publish revised and/or new versions of +the GNU General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + + Each version is given a distinguishing version number. If the +Program specifies that a certain numbered version of the GNU General +Public License "or any later version" applies to it, you have the +option of following the terms and conditions either of that numbered +version or of any later version published by the Free Software +Foundation. If the Program does not specify a version number of the +GNU General Public License, you may choose any version ever published +by the Free Software Foundation. + + If the Program specifies that a proxy can decide which future +versions of the GNU General Public License can be used, that proxy's +public statement of acceptance of a version permanently authorizes you +to choose that version for the Program. + + Later license versions may give you additional or different +permissions. However, no additional obligations are imposed on any +author or copyright holder as a result of your choosing to follow a +later version. + + 15. Disclaimer of Warranty. + + THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY +APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT +HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY +OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, +THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM +IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF +ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. Limitation of Liability. + + IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS +THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY +GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE +USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF +DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD +PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), +EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF +SUCH DAMAGES. + + 17. Interpretation of Sections 15 and 16. + + If the disclaimer of warranty and limitation of liability provided +above cannot be given local legal effect according to their terms, +reviewing courts shall apply local law that most closely approximates +an absolute waiver of all civil liability in connection with the +Program, unless a warranty or assumption of liability accompanies a +copy of the Program in return for a fee. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +state the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This program 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. + + This program 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 this program. If not, see . + +Also add information on how to contact you by electronic and paper mail. + + If the program does terminal interaction, make it output a short +notice like this when it starts in an interactive mode: + + Copyright (C) + This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, your program's commands +might be different; for a GUI interface, you would use an "about box". + + You should also get your employer (if you work as a programmer) or school, +if any, to sign a "copyright disclaimer" for the program, if necessary. +For more information on this, and how to apply and follow the GNU GPL, see +. + + The GNU General Public License does not permit incorporating your program +into proprietary programs. If your program is a subroutine library, you +may consider it more useful to permit linking proprietary applications with +the library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. But first, please read +. diff --git a/MediaFormat.java b/MediaFormat.java new file mode 100644 index 00000000..1455fe39 --- /dev/null +++ b/MediaFormat.java @@ -0,0 +1,83 @@ +package org.schabi.newpipe.extractor; + +/** + * Created by Adam Howard on 08/11/15. + * + * Copyright (c) Christian Schabesberger + * and Adam Howard 2015 + * + * MediaFormat.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 . + */ + +/**Static data about various media formats support by Newpipe, eg mime type, extension*/ + +public enum MediaFormat { + //video and audio combined formats + // id name suffix mime type + MPEG_4 (0x0, "MPEG-4", "mp4", "video/mp4"), + v3GPP (0x1, "3GPP", "3gp", "video/3gpp"), + WEBM (0x2, "WebM", "webm", "video/webm"), + // audio formats + M4A (0x3, "m4a", "m4a", "audio/mp4"), + WEBMA (0x4, "WebM", "webm", "audio/webm"); + + public final int id; + @SuppressWarnings("WeakerAccess") + public final String name; + @SuppressWarnings("WeakerAccess") + public final String suffix; + public final String mimeType; + + MediaFormat(int id, String name, String suffix, String mimeType) { + this.id = id; + this.name = name; + this.suffix = suffix; + this.mimeType = mimeType; + } + + /**Return the friendly name of the media format with the supplied id + * @param ident the id of the media format. Currently an arbitrary, NewPipe-specific number. + * @return the friendly name of the MediaFormat associated with this ids, + * or an empty String if none match it.*/ + public static String getNameById(int ident) { + for (MediaFormat vf : MediaFormat.values()) { + if(vf.id == ident) return vf.name; + } + return ""; + } + + /**Return the file extension of the media format with the supplied id + * @param ident the id of the media format. Currently an arbitrary, NewPipe-specific number. + * @return the file extension of the MediaFormat associated with this ids, + * or an empty String if none match it.*/ + public static String getSuffixById(int ident) { + for (MediaFormat vf : MediaFormat.values()) { + if(vf.id == ident) return vf.suffix; + } + return ""; + } + + /**Return the MIME type of the media format with the supplied id + * @param ident the id of the media format. Currently an arbitrary, NewPipe-specific number. + * @return the MIME type of the MediaFormat associated with this ids, + * or an empty String if none match it.*/ + public static String getMimeById(int ident) { + for (MediaFormat vf : MediaFormat.values()) { + if(vf.id == ident) return vf.mimeType; + } + return ""; + } +} diff --git a/NewPipe.java b/NewPipe.java new file mode 100644 index 00000000..4e8e4aa3 --- /dev/null +++ b/NewPipe.java @@ -0,0 +1,88 @@ +package org.schabi.newpipe.extractor; + +import org.schabi.newpipe.extractor.exceptions.ExtractionException; +import org.schabi.newpipe.extractor.services.youtube.YoutubeService; + +/** + * Created by Christian Schabesberger on 23.08.15. + * + * Copyright (C) Christian Schabesberger 2015 + * NewPipe.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 . + */ + +/**Provides access to the video streaming services supported by NewPipe. + * Currently only Youtube until the API becomes more stable.*/ + +@SuppressWarnings("ALL") +public class NewPipe { + + private NewPipe() { + } + + private static final String TAG = NewPipe.class.toString(); + + + private static Downloader downloader = null; + + public static StreamingService[] getServices() { + return ServiceList.serviceList; + } + public static StreamingService getService(int serviceId)throws ExtractionException { + for(StreamingService s : ServiceList.serviceList) { + if(s.getServiceId() == serviceId) { + return s; + } + } + return null; + } + public static StreamingService getService(String serviceName) throws ExtractionException { + return ServiceList.serviceList[getIdOfService(serviceName)]; + } + public static String getNameOfService(int id) { + try { + return getService(id).getServiceInfo().name; + } catch (Exception e) { + System.err.println("Service id not known"); + e.printStackTrace(); + return ""; + } + } + public static int getIdOfService(String serviceName) { + for(int i = 0; i < ServiceList.serviceList.length; i++) { + if(ServiceList.serviceList[i].getServiceInfo().name.equals(serviceName)) { + return i; + } + } + return -1; + } + + public static void init(Downloader d) { + downloader = d; + } + + public static Downloader getDownloader() { + return downloader; + } + + public static StreamingService getServiceByUrl(String url) { + for(StreamingService s : ServiceList.serviceList) { + if(s.getLinkTypeByUrl(url) != StreamingService.LinkType.NONE) { + return s; + } + } + return null; + } +} diff --git a/Parser.java b/Parser.java new file mode 100644 index 00000000..8e70f751 --- /dev/null +++ b/Parser.java @@ -0,0 +1,77 @@ +package org.schabi.newpipe.extractor; + +import org.schabi.newpipe.extractor.exceptions.ParsingException; + +import java.io.UnsupportedEncodingException; +import java.net.URLDecoder; +import java.util.HashMap; +import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * Created by Christian Schabesberger on 02.02.16. + * + * Copyright (C) Christian Schabesberger 2016 + * Parser.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 . + */ + +/** avoid using regex !!! */ +public class Parser { + + private Parser() { + } + + public static class RegexException extends ParsingException { + public RegexException(String message) { + super(message); + } + } + + public static String matchGroup1(String pattern, String input) throws RegexException { + return matchGroup(pattern, input, 1); + } + + public static String matchGroup(String pattern, String input, int group) throws RegexException { + Pattern pat = Pattern.compile(pattern); + Matcher mat = pat.matcher(input); + boolean foundMatch = mat.find(); + if (foundMatch) { + return mat.group(group); + } + else { + //Log.e(TAG, "failed to find pattern \""+pattern+"\" inside of \""+input+"\""); + if(input.length() > 1024) { + throw new RegexException("failed to find pattern \""+pattern); + } else { + throw new RegexException("failed to find pattern \"" + pattern + " inside of " + input + "\""); + } + } + } + + public static Map compatParseMap(final String input) throws UnsupportedEncodingException { + Map map = new HashMap<>(); + for(String arg : input.split("&")) { + String[] splitArg = arg.split("="); + if(splitArg.length > 1) { + map.put(splitArg[0], URLDecoder.decode(splitArg[1], "UTF-8")); + } else { + map.put(splitArg[0], ""); + } + } + return map; + } +} diff --git a/README.md b/README.md new file mode 100644 index 00000000..92ffdd54 --- /dev/null +++ b/README.md @@ -0,0 +1,5 @@ +NewPipe Extractor +================= + +This is the system independent core of [NewPipe](https://github.com/TeamNewPipe/NewPipe). +It can be used to create your own java based NewPipe client. diff --git a/ServiceList.java b/ServiceList.java new file mode 100644 index 00000000..89b350cb --- /dev/null +++ b/ServiceList.java @@ -0,0 +1,13 @@ +package org.schabi.newpipe.extractor; + +import org.schabi.newpipe.extractor.services.youtube.YoutubeService; + +/** + * Created by the-scrabi on 18.02.17. + */ + +class ServiceList { + public static final StreamingService[] serviceList = { + new YoutubeService(0) + }; +} diff --git a/StreamingService.java b/StreamingService.java new file mode 100644 index 00000000..ed3c17bf --- /dev/null +++ b/StreamingService.java @@ -0,0 +1,78 @@ +package org.schabi.newpipe.extractor; + +import org.schabi.newpipe.extractor.channel.ChannelExtractor; +import org.schabi.newpipe.extractor.exceptions.ExtractionException; +import org.schabi.newpipe.extractor.search.SearchEngine; +import org.schabi.newpipe.extractor.stream_info.StreamExtractor; + +import java.io.IOException; + +/** + * Created by Christian Schabesberger on 23.08.15. + * + * Copyright (C) Christian Schabesberger 2016 + * StreamingService.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 . + */ + +public abstract class StreamingService { + public class ServiceInfo { + public String name = ""; + } + + public enum LinkType { + NONE, + STREAM, + CHANNEL, + PLAYLIST + } + + private int serviceId; + + public StreamingService(int id) { + serviceId = id; + } + + public abstract ServiceInfo getServiceInfo(); + + public abstract StreamExtractor getExtractorInstance(String url) + throws IOException, ExtractionException; + public abstract SearchEngine getSearchEngineInstance(); + public abstract UrlIdHandler getStreamUrlIdHandlerInstance(); + public abstract UrlIdHandler getChannelUrlIdHandlerInstance(); + public abstract ChannelExtractor getChannelExtractorInstance(String url, int page) + throws ExtractionException, IOException; + public abstract SuggestionExtractor getSuggestionExtractorInstance(); + + public final int getServiceId() { + return serviceId; + } + + /** + * figure out where the link is pointing to (a channel, video, playlist, etc.) + */ + public final LinkType getLinkTypeByUrl(String url) { + UrlIdHandler sH = getStreamUrlIdHandlerInstance(); + UrlIdHandler cH = getChannelUrlIdHandlerInstance(); + + if(sH.acceptUrl(url)) { + return LinkType.STREAM; + } else if(cH.acceptUrl(url)) { + return LinkType.CHANNEL; + } else { + return LinkType.NONE; + } + } +} diff --git a/SuggestionExtractor.java b/SuggestionExtractor.java new file mode 100644 index 00000000..f198bc5e --- /dev/null +++ b/SuggestionExtractor.java @@ -0,0 +1,43 @@ +package org.schabi.newpipe.extractor; + +import org.schabi.newpipe.extractor.exceptions.ExtractionException; + +import java.io.IOException; +import java.util.List; + +/** + * Created by Christian Schabesberger on 28.09.16. + * + * Copyright (C) Christian Schabesberger 2016 + * SuggestionExtractor.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 . + */ + +public abstract class SuggestionExtractor { + + private int serviceId; + + public SuggestionExtractor(int serviceId) { + this.serviceId = serviceId; + } + + public abstract List suggestionList( + String query,String contentCountry) + throws ExtractionException, IOException; + + public int getServiceId() { + return serviceId; + } +} diff --git a/UrlIdHandler.java b/UrlIdHandler.java new file mode 100644 index 00000000..1218e7b8 --- /dev/null +++ b/UrlIdHandler.java @@ -0,0 +1,35 @@ +package org.schabi.newpipe.extractor; + +import org.schabi.newpipe.extractor.exceptions.ParsingException; + +/** + * Created by Christian Schabesberger on 26.07.16. + * + * Copyright (C) Christian Schabesberger 2016 + * UrlIdHandler.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 . + */ + +public interface UrlIdHandler { + + String getUrl(String videoId); + String getId(String siteUrl) throws ParsingException; + String cleanUrl(String siteUrl) throws ParsingException; + + /**When a VIEW_ACTION is caught this function will test if the url delivered within the calling + Intent was meant to be watched with this Service. + Return false if this service shall not allow to be called through ACTIONs.*/ + boolean acceptUrl(String videoUrl); +} diff --git a/channel/ChannelExtractor.java b/channel/ChannelExtractor.java new file mode 100644 index 00000000..fe7b9f7e --- /dev/null +++ b/channel/ChannelExtractor.java @@ -0,0 +1,62 @@ +package org.schabi.newpipe.extractor.channel; + +import org.schabi.newpipe.extractor.UrlIdHandler; +import org.schabi.newpipe.extractor.exceptions.ExtractionException; +import org.schabi.newpipe.extractor.exceptions.ParsingException; +import org.schabi.newpipe.extractor.stream_info.StreamInfoItemCollector; + +import java.io.IOException; + +/** + * Created by Christian Schabesberger on 25.07.16. + * + * Copyright (C) Christian Schabesberger 2016 + * ChannelExtractor.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 . + */ + +public abstract class ChannelExtractor { + private int serviceId; + private String url; + private UrlIdHandler urlIdHandler; + private StreamInfoItemCollector previewInfoCollector; + private int page = -1; + + public ChannelExtractor(UrlIdHandler urlIdHandler, String url, int page, int serviceId) + throws ExtractionException, IOException { + this.url = url; + this.page = page; + this.serviceId = serviceId; + this.urlIdHandler = urlIdHandler; + previewInfoCollector = new StreamInfoItemCollector(urlIdHandler, serviceId); + } + + public String getUrl() { return url; } + public UrlIdHandler getUrlIdHandler() { return urlIdHandler; } + public StreamInfoItemCollector getStreamPreviewInfoCollector() { + return previewInfoCollector; + } + + public abstract String getChannelName() throws ParsingException; + public abstract String getAvatarUrl() throws ParsingException; + public abstract String getBannerUrl() throws ParsingException; + public abstract String getFeedUrl() throws ParsingException; + public abstract StreamInfoItemCollector getStreams() throws ParsingException; + public abstract long getSubscriberCount() throws ParsingException; + public abstract boolean hasNextPage() throws ParsingException; + public int getServiceId() { + return serviceId; + } +} diff --git a/channel/ChannelInfo.java b/channel/ChannelInfo.java new file mode 100644 index 00000000..e1fd96ad --- /dev/null +++ b/channel/ChannelInfo.java @@ -0,0 +1,86 @@ +package org.schabi.newpipe.extractor.channel; + +import org.schabi.newpipe.extractor.InfoItem; +import org.schabi.newpipe.extractor.exceptions.ParsingException; +import org.schabi.newpipe.extractor.stream_info.StreamInfoItem; +import org.schabi.newpipe.extractor.stream_info.StreamInfoItemCollector; + +import java.util.List; +import java.util.Vector; + +/** + * Created by Christian Schabesberger on 31.07.16. + * + * Copyright (C) Christian Schabesberger 2016 + * ChannelInfo.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 . + */ + +public class ChannelInfo { + public void addException(Exception e) { + errors.add(e); + } + + public static ChannelInfo getInfo(ChannelExtractor extractor) + throws ParsingException { + ChannelInfo info = new ChannelInfo(); + + // importand data + info.service_id = extractor.getServiceId(); + info.channel_name = extractor.getChannelName(); + info.hasNextPage = extractor.hasNextPage(); + + try { + info.avatar_url = extractor.getAvatarUrl(); + } catch (Exception e) { + info.errors.add(e); + } + try { + info.banner_url = extractor.getBannerUrl(); + } catch (Exception e) { + info.errors.add(e); + } + try { + info.feed_url = extractor.getFeedUrl(); + } catch(Exception e) { + info.errors.add(e); + } + try { + StreamInfoItemCollector c = extractor.getStreams(); + info.related_streams = c.getItemList(); + info.errors.addAll(c.getErrors()); + } catch(Exception e) { + info.errors.add(e); + } + try { + info.subscriberCount = extractor.getSubscriberCount(); + } catch (Exception e) { + info.errors.add(e); + } + + return info; + } + + public int service_id = -1; + public String channel_name = ""; + public String avatar_url = ""; + public String banner_url = ""; + public String feed_url = ""; + public List related_streams = null; + public long subscriberCount = -1; + public boolean hasNextPage = false; + + public List errors = new Vector<>(); +} diff --git a/channel/ChannelInfoItem.java b/channel/ChannelInfoItem.java new file mode 100644 index 00000000..b834d28e --- /dev/null +++ b/channel/ChannelInfoItem.java @@ -0,0 +1,44 @@ +package org.schabi.newpipe.extractor.channel; + +import org.schabi.newpipe.extractor.InfoItem; + +/** + * Created by Christian Schabesberger on 11.02.17. + * + * Copyright (C) Christian Schabesberger 2017 + * ChannelInfoItem.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 . + */ + +public class ChannelInfoItem implements InfoItem { + + public int serviceId = -1; + public String channelName = ""; + public String thumbnailUrl = ""; + public String webPageUrl = ""; + public String description = ""; + public long subscriberCount = -1; + public int videoAmount = -1; + + public InfoType infoType() { + return InfoType.CHANNEL; + } + public String getTitle() { + return channelName; + } + public String getLink() { + return webPageUrl; + } +} diff --git a/channel/ChannelInfoItemCollector.java b/channel/ChannelInfoItemCollector.java new file mode 100644 index 00000000..525820d4 --- /dev/null +++ b/channel/ChannelInfoItemCollector.java @@ -0,0 +1,74 @@ +package org.schabi.newpipe.extractor.channel; + +import org.schabi.newpipe.extractor.InfoItemCollector; +import org.schabi.newpipe.extractor.NewPipe; +import org.schabi.newpipe.extractor.exceptions.FoundAdException; +import org.schabi.newpipe.extractor.exceptions.ParsingException; +import org.schabi.newpipe.extractor.stream_info.StreamInfoItem; +import org.schabi.newpipe.extractor.stream_info.StreamInfoItemExtractor; + +/** + * Created by Christian Schabesberger on 12.02.17. + * + * Copyright (C) Christian Schabesberger 2017 + * ChannelInfoItemCollector.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 . + */ + +public class ChannelInfoItemCollector extends InfoItemCollector { + public ChannelInfoItemCollector(int serviceId) { + super(serviceId); + } + + public ChannelInfoItem extract(ChannelInfoItemExtractor extractor) throws ParsingException { + ChannelInfoItem resultItem = new ChannelInfoItem(); + // importand information + resultItem.channelName = extractor.getChannelName(); + + resultItem.serviceId = getServiceId(); + resultItem.webPageUrl = extractor.getWebPageUrl(); + + // optional information + try { + resultItem.subscriberCount = extractor.getSubscriberCount(); + } catch (Exception e) { + addError(e); + } + try { + resultItem.videoAmount = extractor.getVideoAmount(); + } catch (Exception e) { + addError(e); + } + try { + resultItem.thumbnailUrl = extractor.getThumbnailUrl(); + } catch (Exception e) { + addError(e); + } + try { + resultItem.description = extractor.getDescription(); + } catch (Exception e) { + addError(e); + } + return resultItem; + } + + public void commit(ChannelInfoItemExtractor extractor) throws ParsingException { + try { + addItem(extract(extractor)); + } catch (Exception e) { + addError(e); + } + } +} diff --git a/channel/ChannelInfoItemExtractor.java b/channel/ChannelInfoItemExtractor.java new file mode 100644 index 00000000..864415cd --- /dev/null +++ b/channel/ChannelInfoItemExtractor.java @@ -0,0 +1,32 @@ +package org.schabi.newpipe.extractor.channel; + +import org.schabi.newpipe.extractor.exceptions.ParsingException; + +/** + * Created by Christian Schabesberger on 12.02.17. + * + * Copyright (C) Christian Schabesberger 2017 + * ChannelInfoItemExtractor.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 . + */ + +public interface ChannelInfoItemExtractor { + String getThumbnailUrl() throws ParsingException; + String getChannelName() throws ParsingException; + String getWebPageUrl() throws ParsingException; + String getDescription() throws ParsingException; + long getSubscriberCount() throws ParsingException; + int getVideoAmount() throws ParsingException; +} diff --git a/copyright b/copyright new file mode 100644 index 00000000..42108656 --- /dev/null +++ b/copyright @@ -0,0 +1,15 @@ +Copyright: 2017 Christian Schabesberger + +License: GPL-3.0+ + This program 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. + + This program 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 this program. If not, see . diff --git a/exceptions/ExtractionException.java b/exceptions/ExtractionException.java new file mode 100644 index 00000000..31185018 --- /dev/null +++ b/exceptions/ExtractionException.java @@ -0,0 +1,33 @@ +package org.schabi.newpipe.extractor.exceptions; + +/** + * Created by Christian Schabesberger on 30.01.16. + * + * Copyright (C) Christian Schabesberger 2016 + * ExtractionException.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 . + */ + +public class ExtractionException extends Exception { + public ExtractionException(String message) { + super(message); + } + public ExtractionException(Throwable cause) { + super(cause); + } + public ExtractionException(String message, Throwable cause) { + super(message, cause); + } +} \ No newline at end of file diff --git a/exceptions/FoundAdException.java b/exceptions/FoundAdException.java new file mode 100644 index 00000000..4d6d2982 --- /dev/null +++ b/exceptions/FoundAdException.java @@ -0,0 +1,30 @@ +package org.schabi.newpipe.extractor.exceptions; + +/** + * Created by Christian Schabesberger on 12.09.16. + * + * Copyright (C) Christian Schabesberger 2016 + * FoundAdException.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 . + */ + +public class FoundAdException extends ParsingException { + public FoundAdException(String message) { + super(message); + } + public FoundAdException(String message, Throwable cause) { + super(message, cause); + } +} diff --git a/exceptions/ParsingException.java b/exceptions/ParsingException.java new file mode 100644 index 00000000..41a7acce --- /dev/null +++ b/exceptions/ParsingException.java @@ -0,0 +1,31 @@ +package org.schabi.newpipe.extractor.exceptions; + +/** + * Created by Christian Schabesberger on 31.01.16. + * + * Copyright (C) Christian Schabesberger 2016 + * ParsingException.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 . + */ + + +public class ParsingException extends ExtractionException { + public ParsingException(String message) { + super(message); + } + public ParsingException(String message, Throwable cause) { + super(message, cause); + } +} \ No newline at end of file diff --git a/exceptions/ReCaptchaException.java b/exceptions/ReCaptchaException.java new file mode 100644 index 00000000..a28ec99f --- /dev/null +++ b/exceptions/ReCaptchaException.java @@ -0,0 +1,27 @@ +package org.schabi.newpipe.extractor.exceptions; + +/** + * Created by beneth on 07.12.16. + * + * Copyright (C) Christian Schabesberger 2016 + * ReCaptchaException.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 . + */ + +public class ReCaptchaException extends ExtractionException { + public ReCaptchaException(String message) { + super(message); + } +} diff --git a/search/InfoItemSearchCollector.java b/search/InfoItemSearchCollector.java new file mode 100644 index 00000000..b7ec0e3c --- /dev/null +++ b/search/InfoItemSearchCollector.java @@ -0,0 +1,79 @@ +package org.schabi.newpipe.extractor.search; + +import org.schabi.newpipe.extractor.InfoItemCollector; +import org.schabi.newpipe.extractor.UrlIdHandler; +import org.schabi.newpipe.extractor.channel.ChannelInfoItemCollector; +import org.schabi.newpipe.extractor.channel.ChannelInfoItemExtractor; +import org.schabi.newpipe.extractor.exceptions.ExtractionException; +import org.schabi.newpipe.extractor.exceptions.FoundAdException; +import org.schabi.newpipe.extractor.exceptions.ParsingException; +import org.schabi.newpipe.extractor.stream_info.StreamInfoItemCollector; +import org.schabi.newpipe.extractor.stream_info.StreamInfoItemExtractor; + +/** + * Created by Christian Schabesberger on 12.02.17. + * + * Copyright (C) Christian Schabesberger 2017 + * InfoItemSearchCollector.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 . + */ + +public class InfoItemSearchCollector extends InfoItemCollector { + private String suggestion = ""; + private StreamInfoItemCollector streamCollector; + private ChannelInfoItemCollector channelCollector; + + SearchResult result = new SearchResult(); + + InfoItemSearchCollector(UrlIdHandler handler, int serviceId) { + super(serviceId); + streamCollector = new StreamInfoItemCollector(handler, serviceId); + channelCollector = new ChannelInfoItemCollector(serviceId); + } + + public void setSuggestion(String suggestion) { + this.suggestion = suggestion; + } + + public SearchResult getSearchResult() throws ExtractionException { + + addFromCollector(channelCollector); + addFromCollector(streamCollector); + + result.suggestion = suggestion; + result.errors = getErrors(); + return result; + } + + public void commit(StreamInfoItemExtractor extractor) { + try { + result.resultList.add(streamCollector.extract(extractor)); + } catch(FoundAdException ae) { + System.err.println("Found add"); + } catch (Exception e) { + addError(e); + } + } + + public void commit(ChannelInfoItemExtractor extractor) { + try { + result.resultList.add(channelCollector.extract(extractor)); + } catch(FoundAdException ae) { + System.err.println("Found add"); + } catch (Exception e) { + addError(e); + } + } +} diff --git a/search/SearchEngine.java b/search/SearchEngine.java new file mode 100644 index 00000000..9cef050e --- /dev/null +++ b/search/SearchEngine.java @@ -0,0 +1,53 @@ +package org.schabi.newpipe.extractor.search; + +import org.schabi.newpipe.extractor.UrlIdHandler; +import org.schabi.newpipe.extractor.exceptions.ExtractionException; +import org.schabi.newpipe.extractor.stream_info.StreamInfoItemCollector; + +import java.io.IOException; +import java.util.EnumSet; + +/** + * Created by Christian Schabesberger on 10.08.15. + * + * Copyright (C) Christian Schabesberger 2015 + * SearchEngine.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 . + */ + +public abstract class SearchEngine { + public enum Filter { + STREAM, CHANNEL, PLAY_LIST + } + + public static class NothingFoundException extends ExtractionException { + public NothingFoundException(String message) { + super(message); + } + } + private InfoItemSearchCollector collector; + + public SearchEngine(UrlIdHandler urlIdHandler, int serviceId) { + collector = new InfoItemSearchCollector(urlIdHandler, serviceId); + } + + protected InfoItemSearchCollector getInfoItemSearchCollector() { + return collector; + } + //Result search(String query, int page); + public abstract InfoItemSearchCollector search( + String query, int page, String contentCountry, EnumSet filter) + throws ExtractionException, IOException; +} diff --git a/search/SearchResult.java b/search/SearchResult.java new file mode 100644 index 00000000..155b4374 --- /dev/null +++ b/search/SearchResult.java @@ -0,0 +1,56 @@ +package org.schabi.newpipe.extractor.search; + +import org.schabi.newpipe.extractor.InfoItem; +import org.schabi.newpipe.extractor.exceptions.ExtractionException; +import org.schabi.newpipe.extractor.stream_info.StreamInfoItem; + +import java.io.IOException; +import java.util.EnumSet; +import java.util.List; +import java.util.Vector; + +/** + * Created by Christian Schabesberger on 29.02.16. + * + * Copyright (C) Christian Schabesberger 2016 + * SearchResult.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 . + */ + +public class SearchResult { + public static SearchResult getSearchResult(SearchEngine engine, String query, + int page, String languageCode, EnumSet filter) + throws ExtractionException, IOException { + + SearchResult result = engine + .search(query, page, languageCode, filter) + .getSearchResult(); + if(result.resultList.isEmpty()) { + if(result.suggestion.isEmpty()) { + if(result.errors.isEmpty()) { + throw new ExtractionException("Empty result despite no error"); + } + } else { + // This is used as a fallback. Do not relay on it !!! + throw new SearchEngine.NothingFoundException(result.suggestion); + } + } + return result; + } + + public String suggestion = ""; + public List resultList = new Vector<>(); + public List errors = new Vector<>(); +} diff --git a/services/youtube/YoutubeChannelExtractor.java b/services/youtube/YoutubeChannelExtractor.java new file mode 100644 index 00000000..0b8fda71 --- /dev/null +++ b/services/youtube/YoutubeChannelExtractor.java @@ -0,0 +1,361 @@ +package org.schabi.newpipe.extractor.services.youtube; + + + +import org.json.JSONException; +import org.json.JSONObject; +import org.jsoup.Jsoup; +import org.jsoup.nodes.Document; +import org.jsoup.nodes.Element; +import org.schabi.newpipe.extractor.AbstractStreamInfo; +import org.schabi.newpipe.extractor.Downloader; +import org.schabi.newpipe.extractor.NewPipe; +import org.schabi.newpipe.extractor.Parser; +import org.schabi.newpipe.extractor.UrlIdHandler; +import org.schabi.newpipe.extractor.channel.ChannelExtractor; +import org.schabi.newpipe.extractor.exceptions.ExtractionException; +import org.schabi.newpipe.extractor.exceptions.ParsingException; +import org.schabi.newpipe.extractor.stream_info.StreamInfoItemCollector; +import org.schabi.newpipe.extractor.stream_info.StreamInfoItemExtractor; + + +import java.io.IOException; + +/** + * Created by Christian Schabesberger on 25.07.16. + * + * Copyright (C) Christian Schabesberger 2016 + * YoutubeChannelExtractor.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 . + */ + +public class YoutubeChannelExtractor extends ChannelExtractor { + + private static final String TAG = YoutubeChannelExtractor.class.toString(); + + // private CSSOMParser cssParser = new CSSOMParser(new SACParserCSS3()); + + private Document doc = null; + + private boolean isAjaxPage = false; + private static String userUrl = ""; + private static String channelName = ""; + private static String avatarUrl = ""; + private static String bannerUrl = ""; + private static String feedUrl = ""; + private static long subscriberCount = -1; + // the fist page is html all other pages are ajax. Every new page can be requested by sending + // this request url. + private static String nextPageUrl = ""; + + public YoutubeChannelExtractor(UrlIdHandler urlIdHandler, String url, int page, int serviceId) + throws ExtractionException, IOException { + super(urlIdHandler, url, page, serviceId); + + Downloader downloader = NewPipe.getDownloader(); + + url = urlIdHandler.cleanUrl(url) ; //+ "/video?veiw=0&flow=list&sort=dd"; + + if(page == 0) { + if (isUserUrl(url)) { + userUrl = url; + } else { + // we first need to get the user url. Otherwise we can't find videos + String channelPageContent = downloader.download(url); + Document channelDoc = Jsoup.parse(channelPageContent, url); + userUrl = getUserUrl(channelDoc); + } + + userUrl = userUrl + "/videos?veiw=0&flow=list&sort=dd&live_view=10000"; + String pageContent = downloader.download(userUrl); + doc = Jsoup.parse(pageContent, userUrl); + nextPageUrl = getNextPageUrl(doc); + isAjaxPage = false; + } else { + String ajaxDataRaw = downloader.download(nextPageUrl); + JSONObject ajaxData; + try { + ajaxData = new JSONObject(ajaxDataRaw); + String htmlDataRaw = ajaxData.getString("content_html"); + doc = Jsoup.parse(htmlDataRaw, nextPageUrl); + + String nextPageHtmlDataRaw = ajaxData.getString("load_more_widget_html"); + if(!nextPageHtmlDataRaw.isEmpty()) { + Document nextPageData = Jsoup.parse(nextPageHtmlDataRaw, nextPageUrl); + nextPageUrl = getNextPageUrl(nextPageData); + } else { + nextPageUrl = ""; + } + } catch (JSONException e) { + throw new ParsingException("Could not parse json data for next page", e); + } + isAjaxPage = true; + } + } + + @Override + public String getChannelName() throws ParsingException { + try { + if(!isAjaxPage) { + channelName = doc.select("span[class=\"qualified-channel-title-text\"]").first() + .select("a").first().text(); + } + return channelName; + } catch(Exception e) { + throw new ParsingException("Could not get channel name"); + } + } + + @Override + public String getAvatarUrl() throws ParsingException { + try { + if(!isAjaxPage) { + avatarUrl = doc.select("img[class=\"channel-header-profile-image\"]") + .first().attr("abs:src"); + } + return avatarUrl; + } catch(Exception e) { + throw new ParsingException("Could not get avatar", e); + } + } + + @Override + public String getBannerUrl() throws ParsingException { + try { + if(!isAjaxPage) { + Element el = doc.select("div[id=\"gh-banner\"]").first().select("style").first(); + String cssContent = el.html(); + String url = "https:" + Parser.matchGroup1("url\\(([^)]+)\\)", cssContent); + + if (url.contains("s.ytimg.com") || url.contains("default_banner")) { + bannerUrl = null; + } else { + bannerUrl = url; + } + } + return bannerUrl; + } catch(Exception e) { + throw new ParsingException("Could not get Banner", e); + } + } + + @Override + public StreamInfoItemCollector getStreams() throws ParsingException { + StreamInfoItemCollector collector = getStreamPreviewInfoCollector(); + Element ul; + if(isAjaxPage) { + ul = doc.select("body").first(); + } else { + ul = doc.select("ul[id=\"browse-items-primary\"]").first(); + } + + for(final Element li : ul.children()) { + if (li.select("div[class=\"feed-item-dismissable\"]").first() != null) { + collector.commit(new StreamInfoItemExtractor() { + @Override + public AbstractStreamInfo.StreamType getStreamType() throws ParsingException { + return AbstractStreamInfo.StreamType.VIDEO_STREAM; + } + + @Override + public boolean isAd() throws ParsingException { + if(!li.select("span[class*=\"icon-not-available\"]").isEmpty()) { + return true; + } else { + return false; + } + } + + @Override + public String getWebPageUrl() throws ParsingException { + try { + Element el = li.select("div[class=\"feed-item-dismissable\"]").first(); + Element dl = el.select("h3").first().select("a").first(); + return dl.attr("abs:href"); + } catch (Exception e) { + throw new ParsingException("Could not get web page url for the video", e); + } + } + + @Override + public String getTitle() throws ParsingException { + try { + Element el = li.select("div[class=\"feed-item-dismissable\"]").first(); + Element dl = el.select("h3").first().select("a").first(); + return dl.text(); + } catch (Exception e) { + throw new ParsingException("Could not get title", e); + } + } + + @Override + public int getDuration() throws ParsingException { + try { + return YoutubeParsingHelper.parseDurationString( + li.select("span[class=\"video-time\"]").first().text()); + } catch(Exception e) { + if(isLiveStream(li)) { + // -1 for no duration + return -1; + } else { + throw new ParsingException("Could not get Duration: " + getTitle(), e); + } + } + } + + @Override + public String getUploader() throws ParsingException { + return getChannelName(); + } + + @Override + public String getUploadDate() throws ParsingException { + try { + Element meta = li.select("div[class=\"yt-lockup-meta\"]").first(); + Element li = meta.select("li").first(); + if (li == null && meta != null) { + //this means we have a youtube red video + return ""; + }else { + return li.text(); + } + } catch(Exception e) { + throw new ParsingException("Could not get uplaod date", e); + } + } + + @Override + public long getViewCount() throws ParsingException { + String output; + String input; + try { + input = li.select("div[class=\"yt-lockup-meta\"]").first() + .select("li").get(1) + .text(); + } catch (IndexOutOfBoundsException e) { + return -1; + } + + output = Parser.matchGroup1("([0-9,\\. ]*)", input) + .replace(" ", "") + .replace(".", "") + .replace(",", ""); + + 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); + } + } + } + + @Override + public String getThumbnailUrl() throws ParsingException { + try { + String url; + Element te = li.select("span[class=\"yt-thumb-clip\"]").first() + .select("img").first(); + url = te.attr("abs:src"); + // Sometimes youtube sends links to gif files which somehow seem to not exist + // anymore. Items with such gif also offer a secondary image source. So we are going + // to use that if we've caught such an item. + if (url.contains(".gif")) { + url = te.attr("abs:data-thumb"); + } + return url; + } catch (Exception e) { + throw new ParsingException("Could not get thumbnail url", e); + } + } + + private boolean isLiveStream(Element item) { + Element bla = item.select("span[class*=\"yt-badge-live\"]").first(); + + if(bla == null) { + // sometimes livestreams dont have badges but sill are live streams + // if video time is not available we most likly have an offline livestream + if(item.select("span[class*=\"video-time\"]").first() == null) { + return true; + } + } + return bla != null; + } + }); + } + } + + return collector; + } + + @Override + public long getSubscriberCount() throws ParsingException { + Element el = doc.select("span[class*=\"yt-subscription-button-subscriber-count\"]") + .first(); + if(el != null) { + subscriberCount = Long.parseLong(el.text().replaceAll("\\D+","")); + } else if(el == null && subscriberCount == -1) { + throw new ParsingException("Could not get subscriber count"); + } + return subscriberCount; + } + + @Override + public String getFeedUrl() throws ParsingException { + try { + if(userUrl.contains("channel")) { + //channels don't have feeds in youtube, only user can provide such + return ""; + } + if(!isAjaxPage) { + feedUrl = doc.select("link[title=\"RSS\"]").first().attr("abs:href"); + } + return feedUrl; + } catch(Exception e) { + throw new ParsingException("Could not get feed url", e); + } + } + + @Override + public boolean hasNextPage() throws ParsingException { + return !nextPageUrl.isEmpty(); + } + + private String getUserUrl(Document d) throws ParsingException { + return d.select("span[class=\"qualified-channel-title-text\"]").first() + .select("a").first().attr("abs:href"); + } + + private boolean isUserUrl(String url) throws ParsingException { + return url.contains("/user/"); + } + + private String getNextPageUrl(Document d) throws ParsingException { + try { + Element button = d.select("button[class*=\"yt-uix-load-more\"]").first(); + if(button != null) { + return button.attr("abs:data-uix-load-more-href"); + } else { + // sometimes channels are simply so small, they don't have a second/next4q page + return ""; + } + } catch(Exception e) { + throw new ParsingException("could not load next page url", e); + } + } +} diff --git a/services/youtube/YoutubeChannelInfoItemExtractor.java b/services/youtube/YoutubeChannelInfoItemExtractor.java new file mode 100644 index 00000000..7dafb63e --- /dev/null +++ b/services/youtube/YoutubeChannelInfoItemExtractor.java @@ -0,0 +1,83 @@ +package org.schabi.newpipe.extractor.services.youtube; + +import org.schabi.newpipe.extractor.Parser; +import org.schabi.newpipe.extractor.channel.ChannelInfoItemExtractor; +import org.schabi.newpipe.extractor.exceptions.ParsingException; +import org.jsoup.nodes.Element; + +/** + * Created by Christian Schabesberger on 12.02.17. + * + * Copyright (C) Christian Schabesberger 2017 + * YoutubeChannelInfoItemExtractor.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 . + */ + +public class YoutubeChannelInfoItemExtractor implements ChannelInfoItemExtractor { + private Element el; + + public YoutubeChannelInfoItemExtractor(Element el) { + this.el = el; + } + + public String getThumbnailUrl() throws ParsingException { + Element img = el.select("span[class*=\"yt-thumb-simple\"]").first() + .select("img").first(); + + String url = img.attr("abs:src"); + + if(url.contains("gif")) { + url = img.attr("abs:data-thumb"); + } + return url; + } + + public String getChannelName() throws ParsingException { + return el.select("a[class*=\"yt-uix-tile-link\"]").first() + .text(); + } + + public String getWebPageUrl() throws ParsingException { + return el.select("a[class*=\"yt-uix-tile-link\"]").first() + .attr("abs:href"); + } + + public long getSubscriberCount() throws ParsingException { + Element subsEl = el.select("span[class*=\"yt-subscriber-count\"]").first(); + if(subsEl == null) { + return 0; + } else { + return Integer.parseInt(subsEl.text().replaceAll("\\D+","")); + } + } + + public int getVideoAmount() throws ParsingException { + Element metaEl = el.select("ul[class*=\"yt-lockup-meta-info\"]").first(); + if(metaEl == null) { + return 0; + } else { + return Integer.parseInt(metaEl.text().replaceAll("\\D+","")); + } + } + + public String getDescription() throws ParsingException { + Element desEl = el.select("div[class*=\"yt-lockup-description\"]").first(); + if(desEl == null) { + return ""; + } else { + return desEl.text(); + } + } +} diff --git a/services/youtube/YoutubeChannelUrlIdHandler.java b/services/youtube/YoutubeChannelUrlIdHandler.java new file mode 100644 index 00000000..23960f60 --- /dev/null +++ b/services/youtube/YoutubeChannelUrlIdHandler.java @@ -0,0 +1,47 @@ +package org.schabi.newpipe.extractor.services.youtube; + +import org.schabi.newpipe.extractor.Parser; +import org.schabi.newpipe.extractor.UrlIdHandler; +import org.schabi.newpipe.extractor.exceptions.ParsingException; + +/** + * Created by Christian Schabesberger on 25.07.16. + * + * Copyright (C) Christian Schabesberger 2016 + * YoutubeChannelUrlIdHandler.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 . + */ + +public class YoutubeChannelUrlIdHandler implements UrlIdHandler { + + public String getUrl(String channelId) { + return "https://www.youtube.com/" + channelId; + } + + public String getId(String siteUrl) throws ParsingException { + return Parser.matchGroup1("/(user/[A-Za-z0-9_-]*|channel/[A-Za-z0-9_-]*)", siteUrl); + } + + public String cleanUrl(String siteUrl) throws ParsingException { + return getUrl(getId(siteUrl)); + } + + public boolean acceptUrl(String videoUrl) { + return (videoUrl.contains("youtube") || + videoUrl.contains("youtu.be")) && + ( videoUrl.contains("/user/") || + videoUrl.contains("/channel/")); + } +} diff --git a/services/youtube/YoutubeParsingHelper.java b/services/youtube/YoutubeParsingHelper.java new file mode 100644 index 00000000..3b4ad42e --- /dev/null +++ b/services/youtube/YoutubeParsingHelper.java @@ -0,0 +1,66 @@ +package org.schabi.newpipe.extractor.services.youtube; + + +import org.schabi.newpipe.extractor.exceptions.ParsingException; + +/** + * Created by Christian Schabesberger on 02.03.16. + * + * Copyright (C) Christian Schabesberger 2016 + * 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 . + */ + +public class YoutubeParsingHelper { + + private 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; + + 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); + } +} diff --git a/services/youtube/YoutubeSearchEngine.java b/services/youtube/YoutubeSearchEngine.java new file mode 100644 index 00000000..d650535f --- /dev/null +++ b/services/youtube/YoutubeSearchEngine.java @@ -0,0 +1,119 @@ +package org.schabi.newpipe.extractor.services.youtube; + +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.NewPipe; +import org.schabi.newpipe.extractor.UrlIdHandler; +import org.schabi.newpipe.extractor.exceptions.ExtractionException; +import org.schabi.newpipe.extractor.search.InfoItemSearchCollector; +import org.schabi.newpipe.extractor.search.SearchEngine; + +import java.net.URLEncoder; +import java.io.IOException; +import java.util.EnumSet; + + +/** + * Created by Christian Schabesberger on 09.08.15. + * + * Copyright (C) Christian Schabesberger 2015 + * YoutubeSearchEngine.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 . + */ + +public class YoutubeSearchEngine extends SearchEngine { + + private static final String TAG = YoutubeSearchEngine.class.toString(); + public static final String CHARSET_UTF_8 = "UTF-8"; + + public YoutubeSearchEngine(UrlIdHandler urlIdHandler, int serviceId) { + super(urlIdHandler, serviceId); + } + + @Override + public InfoItemSearchCollector search(String query, + int page, + String languageCode, + EnumSet filter) + throws IOException, ExtractionException { + InfoItemSearchCollector collector = getInfoItemSearchCollector(); + + + Downloader downloader = NewPipe.getDownloader(); + + String url = "https://www.youtube.com/results" + + "?q=" + URLEncoder.encode(query, CHARSET_UTF_8) + + "&page=" + Integer.toString(page + 1); + if(filter.contains(Filter.STREAM) && !filter.contains(Filter.CHANNEL)) { + url += "&sp=EgIQAQ%253D%253D"; + } else if(!filter.contains(Filter.STREAM) && filter.contains(Filter.CHANNEL)) { + url += "&sp=EgIQAg%253D%253D"; + } + + String site; + //String url = builder.build().toString(); + //if we've been passed a valid language code, append it to the URL + if(!languageCode.isEmpty()) { + //assert Pattern.matches("[a-z]{2}(-([A-Z]{2}|[0-9]{1,3}))?", languageCode); + site = downloader.download(url, languageCode); + } + else { + site = downloader.download(url); + } + + Document doc = Jsoup.parse(site, url); + Element list = doc.select("ol[class=\"item-section\"]").first(); + + for (Element item : list.children()) { + /* First we need to determine which kind of item we are working with. + Youtube depicts five different kinds of items on its search result page. These are + regular videos, playlists, channels, two types of video suggestions, and a "no video + found" item. Since we only want videos, we need to filter out all the others. + An example for this can be seen here: + https://www.youtube.com/results?search_query=asdf&page=1 + + We already applied a filter to the url, so we don't need to care about channels and + playlists now. + */ + + Element el; + + // both types of spell correction item + if ((el = item.select("div[class*=\"spell-correction\"]").first()) != null) { + collector.setSuggestion(el.select("a").first().text()); + if(list.children().size() == 1) { + throw new NothingFoundException("Did you mean: " + el.select("a").first().text()); + } + // search message item + } else if ((el = item.select("div[class*=\"search-message\"]").first()) != null) { + throw new NothingFoundException(el.text()); + + // video item type + } else if ((el = item.select("div[class*=\"yt-lockup-video\"]").first()) != null) { + collector.commit(new YoutubeStreamInfoItemExtractor(el)); + } else if((el = item.select("div[class*=\"yt-lockup-channel\"]").first()) != null) { + collector.commit(new YoutubeChannelInfoItemExtractor(el)); + } else { + // noinspection ConstantConditions + // simply ignore not known items + // throw new ExtractionException("unexpected element found: \"" + item + "\""); + } + } + + return collector; + } +} diff --git a/services/youtube/YoutubeService.java b/services/youtube/YoutubeService.java new file mode 100644 index 00000000..c8cc68fd --- /dev/null +++ b/services/youtube/YoutubeService.java @@ -0,0 +1,82 @@ +package org.schabi.newpipe.extractor.services.youtube; + +import org.schabi.newpipe.extractor.StreamingService; +import org.schabi.newpipe.extractor.UrlIdHandler; +import org.schabi.newpipe.extractor.channel.ChannelExtractor; +import org.schabi.newpipe.extractor.exceptions.ExtractionException; +import org.schabi.newpipe.extractor.search.SearchEngine; +import org.schabi.newpipe.extractor.SuggestionExtractor; +import org.schabi.newpipe.extractor.stream_info.StreamExtractor; + +import java.io.IOException; + + +/** + * Created by Christian Schabesberger on 23.08.15. + * + * Copyright (C) Christian Schabesberger 2015 + * YoutubeService.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 . + */ + +public class YoutubeService extends StreamingService { + + public YoutubeService(int id) { + super(id); + } + + @Override + public ServiceInfo getServiceInfo() { + ServiceInfo serviceInfo = new ServiceInfo(); + serviceInfo.name = "Youtube"; + return serviceInfo; + } + @Override + public StreamExtractor getExtractorInstance(String url) + throws ExtractionException, IOException { + UrlIdHandler urlIdHandler = YoutubeStreamUrlIdHandler.getInstance(); + if(urlIdHandler.acceptUrl(url)) { + return new YoutubeStreamExtractor(urlIdHandler, url, getServiceId()); + } + else { + throw new IllegalArgumentException("supplied String is not a valid Youtube URL"); + } + } + @Override + public SearchEngine getSearchEngineInstance() { + return new YoutubeSearchEngine(getStreamUrlIdHandlerInstance(), getServiceId()); + } + + @Override + public UrlIdHandler getStreamUrlIdHandlerInstance() { + return YoutubeStreamUrlIdHandler.getInstance(); + } + + @Override + public UrlIdHandler getChannelUrlIdHandlerInstance() { + return new YoutubeChannelUrlIdHandler(); + } + + @Override + public ChannelExtractor getChannelExtractorInstance(String url, int page) + throws ExtractionException, IOException { + return new YoutubeChannelExtractor(getChannelUrlIdHandlerInstance(), url, page, getServiceId()); + } + + @Override + public SuggestionExtractor getSuggestionExtractorInstance() { + return new YoutubeSuggestionExtractor(getServiceId()); + } +} diff --git a/services/youtube/YoutubeStreamExtractor.java b/services/youtube/YoutubeStreamExtractor.java new file mode 100644 index 00000000..2df36164 --- /dev/null +++ b/services/youtube/YoutubeStreamExtractor.java @@ -0,0 +1,878 @@ +package org.schabi.newpipe.extractor.services.youtube; + +import org.json.JSONException; +import org.json.JSONObject; +import org.jsoup.Jsoup; +import org.jsoup.nodes.Document; +import org.jsoup.nodes.Element; +import org.mozilla.javascript.Context; +import org.mozilla.javascript.Function; +import org.mozilla.javascript.ScriptableObject; +import org.schabi.newpipe.extractor.AbstractStreamInfo; +import org.schabi.newpipe.extractor.exceptions.ExtractionException; +import org.schabi.newpipe.extractor.exceptions.ParsingException; +import org.schabi.newpipe.extractor.exceptions.ReCaptchaException; +import org.schabi.newpipe.extractor.stream_info.AudioStream; +import org.schabi.newpipe.extractor.Downloader; +import org.schabi.newpipe.extractor.NewPipe; +import org.schabi.newpipe.extractor.Parser; +import org.schabi.newpipe.extractor.UrlIdHandler; +import org.schabi.newpipe.extractor.MediaFormat; +import org.schabi.newpipe.extractor.stream_info.StreamExtractor; +import org.schabi.newpipe.extractor.stream_info.StreamInfo; +import org.schabi.newpipe.extractor.stream_info.StreamInfoItemCollector; +import org.schabi.newpipe.extractor.stream_info.StreamInfoItemExtractor; +import org.schabi.newpipe.extractor.stream_info.VideoStream; + +import java.io.IOException; +import java.util.List; +import java.util.Map; +import java.util.Vector; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * Created by Christian Schabesberger on 06.08.15. + * + * Copyright (C) Christian Schabesberger 2015 + * YoutubeStreamExtractor.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 . + */ + +public class YoutubeStreamExtractor extends StreamExtractor { + public static final String URL_ENCODED_FMT_STREAM_MAP = "url_encoded_fmt_stream_map"; + public static final String HTTPS = "https:"; + public static final String CONTENT = "content"; + public static final String REGEX_INT = "[^\\d]"; + + // exceptions + + public class DecryptException extends ParsingException { + DecryptException(String message, Throwable cause) { + super(message, cause); + } + } + + // special content not available exceptions + + public class GemaException extends ContentNotAvailableException { + GemaException(String message) { + super(message); + } + } + + public class LiveStreamException extends ContentNotAvailableException { + LiveStreamException(String message) { + super(message); + } + } + + // ---------------- + + // Sometimes if the html page of youtube is already downloaded, youtube web page will internally + // download the /get_video_info page. Since a certain date dashmpd url is only available over + // this /get_video_info page, so we always need to download this one to. + // %%video_id%% will be replaced by the actual video id + // $$el_type$$ will be replaced by the actual el_type (se the declarations below) + private static final String GET_VIDEO_INFO_URL = + "https://www.youtube.com/get_video_info?video_id=%%video_id%%$$el_type$$&ps=default&eurl=&gl=US&hl=en"; + // eltype is necessary for the url above + private static final String EL_INFO = "el=info"; + + public enum ItagType { + AUDIO, + VIDEO, + VIDEO_ONLY + } + + private static class ItagItem { + public ItagItem(int id, ItagType type, MediaFormat format, String res, int fps) { + this.id = id; + this.itagType = type; + this.mediaFormatId = format.id; + this.resolutionString = res; + this.fps = fps; + } + public ItagItem(int id, ItagType type, MediaFormat format, int samplingRate, int bandWidth) { + this.id = id; + this.itagType = type; + this.mediaFormatId = format.id; + this.samplingRate = samplingRate; + this.bandWidth = bandWidth; + } + public int id; + public ItagType itagType; + public int mediaFormatId; + public String resolutionString; + public int fps = -1; + public int samplingRate = -1; + public int bandWidth = -1; + } + + private static final ItagItem[] itagList = { + // video streams + // id, ItagType, MediaFormat, Resolution, fps + new ItagItem(17, ItagType.VIDEO, MediaFormat.v3GPP, "144p", 12), + new ItagItem(18, ItagType.VIDEO, MediaFormat.MPEG_4, "360p", 24), + new ItagItem(22, ItagType.VIDEO, MediaFormat.MPEG_4, "720p", 24), + new ItagItem(36, ItagType.VIDEO, MediaFormat.v3GPP, "240p", 24), + new ItagItem(37, ItagType.VIDEO, MediaFormat.MPEG_4, "1080p", 24), + new ItagItem(38, ItagType.VIDEO, MediaFormat.MPEG_4, "1080p", 24), + new ItagItem(43, ItagType.VIDEO, MediaFormat.WEBM, "360p", 24), + new ItagItem(44, ItagType.VIDEO, MediaFormat.WEBM, "480p", 24), + new ItagItem(45, ItagType.VIDEO, MediaFormat.WEBM, "720p", 24), + new ItagItem(46, ItagType.VIDEO, MediaFormat.WEBM, "1080p", 24), + // audio streams + // id, ItagType, MediaFormat, samplingR, bandwidth + new ItagItem(249, ItagType.AUDIO, MediaFormat.WEBMA, 0, 0), // bandwith/samplingR 0 because not known + new ItagItem(250, ItagType.AUDIO, MediaFormat.WEBMA, 0, 0), + new ItagItem(171, ItagType.AUDIO, MediaFormat.WEBMA, 0, 0), + new ItagItem(140, ItagType.AUDIO, MediaFormat.M4A, 0, 0), + new ItagItem(251, ItagType.AUDIO, MediaFormat.WEBMA, 0, 0), + // video only streams + new ItagItem(160, ItagType.VIDEO_ONLY, MediaFormat.MPEG_4, "144p", 24), + new ItagItem(133, ItagType.VIDEO_ONLY, MediaFormat.MPEG_4, "240p", 24), + new ItagItem(134, ItagType.VIDEO_ONLY, MediaFormat.MPEG_4, "360p", 24), + new ItagItem(135, ItagType.VIDEO_ONLY, MediaFormat.MPEG_4, "480p", 24), + new ItagItem(136, ItagType.VIDEO_ONLY, MediaFormat.MPEG_4, "720p", 24), + new ItagItem(137, ItagType.VIDEO_ONLY, MediaFormat.MPEG_4, "1080p", 24), + }; + + /**These lists only contain itag formats that are supported by the common Android Video player. + However if you are looking for a list showing all itag formats, look at + https://github.com/rg3/youtube-dl/issues/1687 */ + + public static boolean itagIsSupported(int itag) { + for(ItagItem item : itagList) { + if(itag == item.id) { + return true; + } + } + return false; + } + + public static ItagItem getItagItem(int itag) throws ParsingException { + for(ItagItem item : itagList) { + if(itag == item.id) { + return item; + } + } + throw new ParsingException("itag=" + Integer.toString(itag) + " not supported"); + } + + private static final String TAG = YoutubeStreamExtractor.class.toString(); + private final Document doc; + private JSONObject playerArgs; + private boolean isAgeRestricted; + private Map videoInfoPage; + + // static values + private static final String DECRYPTION_FUNC_NAME="decrypt"; + + // cached values + private static volatile String decryptionCode = ""; + + UrlIdHandler urlidhandler = YoutubeStreamUrlIdHandler.getInstance(); + String pageUrl = ""; + + public YoutubeStreamExtractor(UrlIdHandler urlIdHandler, String pageUrl, int serviceId) + throws ExtractionException, IOException { + super(urlIdHandler, pageUrl, serviceId); + //most common videoInfo fields are now set in our superclass, for all services + this.pageUrl = pageUrl; + Downloader downloader = NewPipe.getDownloader(); + String pageContent = downloader.download(urlidhandler.cleanUrl(pageUrl)); + doc = Jsoup.parse(pageContent, pageUrl); + JSONObject ytPlayerConfig; + String playerUrl; + + // Check if the video is age restricted + if (pageContent.contains(" method + je.printStackTrace(); + System.err.println("failed to load title from JSON args; trying to extract it from HTML"); + try { // fall through to fall-back + return doc.select("meta[name=title]").attr(CONTENT); + } catch (Exception e) { + throw new ParsingException("failed permanently to load title.", e); + } + } + } + + @Override + public String getDescription() throws ParsingException { + try { + return doc.select("p[id=\"eow-description\"]").first().html(); + } catch (Exception e) {//todo: add fallback method <-- there is no ... as long as i know + throw new ParsingException("failed to load description.", e); + } + } + + @Override + public String getUploader() throws ParsingException { + try { + if (playerArgs == null) { + return videoInfoPage.get("author"); + } + //json player args method + return playerArgs.getString("author"); + } catch(JSONException je) { + je.printStackTrace(); + System.err.println( + "failed to load uploader name from JSON args; trying to extract it from HTML"); + } try {//fall through to fallback HTML method + return doc.select("div.yt-user-info").first().text(); + } catch (Exception e) { + throw new ParsingException("failed permanently to load uploader name.", e); + } + } + + @Override + public int getLength() throws ParsingException { + try { + if (playerArgs == null) { + return Integer.valueOf(videoInfoPage.get("length_seconds")); + } + return playerArgs.getInt("length_seconds"); + } catch (JSONException e) {//todo: find fallback method + throw new ParsingException("failed to load video duration from JSON args", e); + } + } + + @Override + public long getViewCount() throws ParsingException { + try { + String viewCountString = doc.select("meta[itemprop=interactionCount]").attr(CONTENT); + return Long.parseLong(viewCountString); + } catch (Exception e) {//todo: find fallback method + throw new ParsingException("failed to get number of views", e); + } + } + + @Override + public String getUploadDate() throws ParsingException { + try { + return doc.select("meta[itemprop=datePublished]").attr(CONTENT); + } catch (Exception e) {//todo: add fallback method + throw new ParsingException("failed to get upload date.", e); + } + } + + @Override + public String getThumbnailUrl() throws ParsingException { + //first attempt getting a small image version + //in the html extracting part we try to get a thumbnail with a higher resolution + // Try to get high resolution thumbnail if it fails use low res from the player instead + try { + return doc.select("link[itemprop=\"thumbnailUrl\"]").first().attr("abs:href"); + } catch(Exception e) { + System.err.println("Could not find high res Thumbnail. Using low res instead"); + } + try { //fall through to fallback + return playerArgs.getString("thumbnail_url"); + } catch (JSONException je) { + throw new ParsingException( + "failed to extract thumbnail URL from JSON args; trying to extract it from HTML", je); + } catch (NullPointerException ne) { + // Get from the video info page instead + return videoInfoPage.get("thumbnail_url"); + } + } + + @Override + public String getUploaderThumbnailUrl() throws ParsingException { + try { + return doc.select("a[class*=\"yt-user-photo\"]").first() + .select("img").first() + .attr("abs:data-thumb"); + } catch (Exception e) {//todo: add fallback method + throw new ParsingException("failed to get uploader thumbnail URL.", e); + } + } + + @Override + public String getDashMpdUrl() throws ParsingException { + try { + String dashManifestUrl = ""; + if(videoInfoPage != null && videoInfoPage.containsKey("dashmpd")) { + dashManifestUrl = videoInfoPage.get("dashmpd"); + } else if (playerArgs.has("dashmpd")) { + dashManifestUrl = playerArgs.getString("dashmpd"); + } else { + return ""; + } + if(!dashManifestUrl.contains("/signature/")) { + String encryptedSig = Parser.matchGroup1("/s/([a-fA-F0-9\\.]+)", dashManifestUrl); + String decryptedSig; + + decryptedSig = decryptSignature(encryptedSig, decryptionCode); + dashManifestUrl = dashManifestUrl.replace("/s/" + encryptedSig, "/signature/" + decryptedSig); + } + return dashManifestUrl; + } catch (Exception e) { + throw new ParsingException( + "Could not get \"dashmpd\" maybe VideoInfoPage is broken.", e); + } + } + + + @Override + public List getAudioStreams() throws ParsingException { + Vector audioStreams = new Vector<>(); + try{ + String encodedUrlMap; + // playerArgs could be null if the video is age restricted + if (playerArgs == null) { + if(videoInfoPage.containsKey("adaptive_fmts")) { + encodedUrlMap = videoInfoPage.get("adaptive_fmts"); + } else { + return null; + } + } else { + if(playerArgs.has("adaptive_fmts")) { + encodedUrlMap = playerArgs.getString("adaptive_fmts"); + } else { + return null; + } + } + for(String url_data_str : encodedUrlMap.split(",")) { + // This loop iterates through multiple streams, therefor tags + // is related to one and the same stream at a time. + Map tags = Parser.compatParseMap( + org.jsoup.parser.Parser.unescapeEntities(url_data_str, true)); + + int itag = Integer.parseInt(tags.get("itag")); + + if (itagIsSupported(itag)) { + ItagItem itagItem = getItagItem(itag); + if (itagItem.itagType == ItagType.AUDIO) { + String streamUrl = tags.get("url"); + // if video has a signature: decrypt it and add it to the url + if (tags.get("s") != null) { + streamUrl = streamUrl + "&signature=" + + decryptSignature(tags.get("s"), decryptionCode); + } + + audioStreams.add(new AudioStream(streamUrl, + itagItem.mediaFormatId, + itagItem.bandWidth, + itagItem.samplingRate)); + } + } + } + } catch (Exception e) { + throw new ParsingException("Could not get audiostreams", e); + } + return audioStreams; + } + + @Override + public List getVideoStreams() throws ParsingException { + Vector videoStreams = new Vector<>(); + + try{ + String encodedUrlMap; + // playerArgs could be null if the video is age restricted + if (playerArgs == null) { + encodedUrlMap = videoInfoPage.get(URL_ENCODED_FMT_STREAM_MAP); + } else { + encodedUrlMap = playerArgs.getString(URL_ENCODED_FMT_STREAM_MAP); + } + for(String url_data_str : encodedUrlMap.split(",")) { + try { + // This loop iterates through multiple streams, therefor tags + // is related to one and the same stream at a time. + Map tags = Parser.compatParseMap( + org.jsoup.parser.Parser.unescapeEntities(url_data_str, true)); + + int itag = Integer.parseInt(tags.get("itag")); + + if (itagIsSupported(itag)) { + ItagItem itagItem = getItagItem(itag); + if(itagItem.itagType == ItagType.VIDEO) { + String streamUrl = tags.get("url"); + // if video has a signature: decrypt it and add it to the url + if (tags.get("s") != null) { + streamUrl = streamUrl + "&signature=" + + decryptSignature(tags.get("s"), decryptionCode); + } + videoStreams.add(new VideoStream( + streamUrl, + itagItem.mediaFormatId, + itagItem.resolutionString)); + } + } + } catch (Exception e) { + //todo: dont log throw an error + System.err.println("Could not get Video stream."); + e.printStackTrace(); + } + } + + } catch (Exception e) { + throw new ParsingException("Failed to get video streams", e); + } + + if(videoStreams.isEmpty()) { + throw new ParsingException("Failed to get any video stream"); + } + return videoStreams; + } + + @Override + public List getVideoOnlyStreams() throws ParsingException { + return null; + } + + /**Attempts to parse (and return) the offset to start playing the video from. + * @return the offset (in seconds), or 0 if no timestamp is found.*/ + @Override + public int getTimeStamp() throws ParsingException { + String timeStamp; + try { + timeStamp = Parser.matchGroup1("((#|&|\\?)t=\\d{0,3}h?\\d{0,3}m?\\d{1,3}s?)", pageUrl); + } catch (Parser.RegexException e) { + // catch this instantly since an url does not necessarily have to have a time stamp + + // -2 because well the testing system will then know its the regex that failed :/ + // not good i know + return -2; + } + + if(!timeStamp.isEmpty()) { + try { + String secondsString = ""; + String minutesString = ""; + String hoursString = ""; + try { + secondsString = Parser.matchGroup1("(\\d{1,3})s", timeStamp); + minutesString = Parser.matchGroup1("(\\d{1,3})m", timeStamp); + hoursString = Parser.matchGroup1("(\\d{1,3})h", timeStamp); + } catch (Exception e) { + //it could be that time is given in another method + if (secondsString.isEmpty() //if nothing was got, + && minutesString.isEmpty()//treat as unlabelled seconds + && hoursString.isEmpty()) { + secondsString = Parser.matchGroup1("t=(\\d+)", timeStamp); + } + } + + int seconds = secondsString.isEmpty() ? 0 : Integer.parseInt(secondsString); + int minutes = minutesString.isEmpty() ? 0 : Integer.parseInt(minutesString); + int hours = hoursString.isEmpty() ? 0 : Integer.parseInt(hoursString); + + //don't trust BODMAS! + return seconds + (60 * minutes) + (3600 * hours); + //Log.d(TAG, "derived timestamp value:"+ret); + //the ordering varies internationally + } catch (ParsingException e) { + throw new ParsingException("Could not get timestamp.", e); + } + } else { + return 0; + } + } + + @Override + public int getAgeLimit() throws ParsingException { + if (!isAgeRestricted) { + return 0; + } + try { + return Integer.valueOf(doc.head() + .getElementsByAttributeValue("property", "og:restrictions:age") + .attr(CONTENT).replace("+", "")); + } catch (Exception e) { + throw new ParsingException("Could not get age restriction"); + } + } + + @Override + public String getAverageRating() throws ParsingException { + try { + if (playerArgs == null) { + return videoInfoPage.get("avg_rating"); + } + return playerArgs.getString("avg_rating"); + } catch (JSONException e) { + throw new ParsingException("Could not get Average rating", e); + } + } + + @Override + public int getLikeCount() throws ParsingException { + String likesString = ""; + try { + + Element button = doc.select("button.like-button-renderer-like-button").first(); + try { + likesString = button.select("span.yt-uix-button-content").first().text(); + } catch (NullPointerException e) { + //if this ckicks in our button has no content and thefore likes/dislikes are disabled + return -1; + } + return Integer.parseInt(likesString.replaceAll(REGEX_INT, "")); + } catch (NumberFormatException nfe) { + throw new ParsingException( + "failed to parse likesString \"" + likesString + "\" as integers", nfe); + } catch (Exception e) { + throw new ParsingException("Could not get like count", e); + } + } + + @Override + public int getDislikeCount() throws ParsingException { + String dislikesString = ""; + try { + Element button = doc.select("button.like-button-renderer-dislike-button").first(); + try { + dislikesString = button.select("span.yt-uix-button-content").first().text(); + } catch (NullPointerException e) { + //if this kicks in our button has no content and therefore likes/dislikes are disabled + return -1; + } + return Integer.parseInt(dislikesString.replaceAll(REGEX_INT, "")); + } catch(NumberFormatException nfe) { + throw new ParsingException( + "failed to parse dislikesString \"" + dislikesString + "\" as integers", nfe); + } catch(Exception e) { + throw new ParsingException("Could not get dislike count", e); + } + } + + @Override + public StreamInfoItemExtractor getNextVideo() throws ParsingException { + try { + return extractVideoPreviewInfo(doc.select("div[class=\"watch-sidebar-section\"]").first() + .select("li").first()); + } catch(Exception e) { + throw new ParsingException("Could not get next video", e); + } + } + + @Override + public StreamInfoItemCollector getRelatedVideos() throws ParsingException { + try { + StreamInfoItemCollector collector = getStreamPreviewInfoCollector(); + Element ul = doc.select("ul[id=\"watch-related\"]").first(); + if(ul != null) { + for (Element li : ul.children()) { + // first check if we have a playlist. If so leave them out + if (li.select("a[class*=\"content-link\"]").first() != null) { + collector.commit(extractVideoPreviewInfo(li)); + } + } + } + return collector; + } catch(Exception e) { + throw new ParsingException("Could not get related videos", e); + } + } + + @Override + public String getPageUrl() { + return pageUrl; + } + + @Override + public String getChannelUrl() throws ParsingException { + try { + return doc.select("div[class=\"yt-user-info\"]").first().children() + .select("a").first().attr("abs:href"); + } catch(Exception e) { + throw new ParsingException("Could not get channel link", e); + } + } + + @Override + public StreamInfo.StreamType getStreamType() throws ParsingException { + //todo: if implementing livestream support this value should be generated dynamically + return StreamInfo.StreamType.VIDEO_STREAM; + } + + /**Provides information about links to other videos on the video page, such as related videos. + * This is encapsulated in a StreamInfoItem object, + * which is a subset of the fields in a full StreamInfo.*/ + private StreamInfoItemExtractor extractVideoPreviewInfo(final Element li) { + return new StreamInfoItemExtractor() { + @Override + public AbstractStreamInfo.StreamType getStreamType() throws ParsingException { + return AbstractStreamInfo.StreamType.VIDEO_STREAM; + } + + @Override + public boolean isAd() throws ParsingException { + if(!li.select("span[class*=\"icon-not-available\"]").isEmpty()) { + return true; + } else { + return false; + } + } + + @Override + public String getWebPageUrl() throws ParsingException { + return li.select("a.content-link").first().attr("abs:href"); + } + + @Override + public String getTitle() throws ParsingException { + //todo: check NullPointerException causing + return li.select("span.title").first().text(); + //this page causes the NullPointerException, after finding it by searching for "tjvg": + //https://www.youtube.com/watch?v=Uqg0aEhLFAg + } + + @Override + public int getDuration() throws ParsingException { + return YoutubeParsingHelper.parseDurationString( + li.select("span.video-time").first().text()); + } + + @Override + public String getUploader() throws ParsingException { + return li.select("span.g-hovercard").first().text(); + } + + @Override + public String getUploadDate() throws ParsingException { + return null; + } + + @Override + public long getViewCount() throws ParsingException { + //this line is unused + //String views = li.select("span.view-count").first().text(); + + //Log.i(TAG, "title:"+info.title); + //Log.i(TAG, "view count:"+views); + + try { + return Long.parseLong(li.select("span.view-count") + .first().text().replaceAll(REGEX_INT, "")); + } catch (Exception e) { + //related videos sometimes have no view count + return 0; + } + } + + @Override + public String getThumbnailUrl() throws ParsingException { + Element img = li.select("img").first(); + String thumbnailUrl = img.attr("abs:src"); + // Sometimes youtube sends links to gif files which somehow seem to not exist + // anymore. Items with such gif also offer a secondary image source. So we are going + // to use that if we caught such an item. + if (thumbnailUrl.contains(".gif")) { + thumbnailUrl = img.attr("data-thumb"); + } + if (thumbnailUrl.startsWith("//")) { + thumbnailUrl = HTTPS + thumbnailUrl; + } + return thumbnailUrl; + } + }; + } + + + private String loadDecryptionCode(String playerUrl) throws DecryptException { + String decryptionFuncName; + String decryptionFunc; + String helperObjectName; + String helperObject; + String callerFunc = "function " + DECRYPTION_FUNC_NAME + "(a){return %%(a);}"; + String decryptionCode; + + try { + Downloader downloader = NewPipe.getDownloader(); + if(!playerUrl.contains("https://youtube.com")) { + //sometimes the https://youtube.com part does not get send with + //than we have to add it by hand + playerUrl = "https://youtube.com" + playerUrl; + } + String playerCode = downloader.download(playerUrl); + + decryptionFuncName = + Parser.matchGroup("([\"\\'])signature\\1\\s*,\\s*([a-zA-Z0-9$]+)\\(", playerCode, 2); + + String functionPattern = "(" + + decryptionFuncName.replace("$", "\\$") + + "=function\\([a-zA-Z0-9_]+\\)\\{.+?\\})"; + decryptionFunc = "var " + Parser.matchGroup1(functionPattern, playerCode) + ";"; + + helperObjectName = Parser + .matchGroup1(";([A-Za-z0-9_\\$]{2})\\...\\(", decryptionFunc); + + String helperPattern = "(var " + + helperObjectName.replace("$", "\\$") + "=\\{.+?\\}\\};)"; + helperObject = Parser.matchGroup1(helperPattern, playerCode); + + + callerFunc = callerFunc.replace("%%", decryptionFuncName); + decryptionCode = helperObject + decryptionFunc + callerFunc; + } catch(IOException ioe) { + throw new DecryptException("Could not load decrypt function", ioe); + } catch(Exception e) { + throw new DecryptException("Could not parse decrypt function ", e); + } + + return decryptionCode; + } + + private String decryptSignature(String encryptedSig, String decryptionCode) + throws DecryptException{ + Context context = Context.enter(); + context.setOptimizationLevel(-1); + Object result = null; + try { + ScriptableObject scope = context.initStandardObjects(); + context.evaluateString(scope, decryptionCode, "decryptionCode", 1, null); + Function decryptionFunc = (Function) scope.get("decrypt", scope); + result = decryptionFunc.call(context, scope, scope, new Object[]{encryptedSig}); + } catch (Exception e) { + throw new DecryptException("could not get decrypt signature", e); + } finally { + Context.exit(); + } + return result == null ? "" : result.toString(); + } + + private String findErrorReason(Document doc) { + String errorMessage = doc.select("h1[id=\"unavailable-message\"]").first().text(); + if(errorMessage.contains("GEMA")) { + // Gema sometimes blocks youtube music content in germany: + // https://www.gema.de/en/ + // Detailed description: + // https://en.wikipedia.org/wiki/GEMA_%28German_organization%29 + return "GEMA"; + } + return ""; + } +} diff --git a/services/youtube/YoutubeStreamInfoItemExtractor.java b/services/youtube/YoutubeStreamInfoItemExtractor.java new file mode 100644 index 00000000..44d606e2 --- /dev/null +++ b/services/youtube/YoutubeStreamInfoItemExtractor.java @@ -0,0 +1,186 @@ +package org.schabi.newpipe.extractor.services.youtube; + +import org.jsoup.nodes.Element; +import org.schabi.newpipe.extractor.AbstractStreamInfo; +import org.schabi.newpipe.extractor.Parser; +import org.schabi.newpipe.extractor.exceptions.FoundAdException; +import org.schabi.newpipe.extractor.exceptions.ParsingException; +import org.schabi.newpipe.extractor.stream_info.StreamInfoItemExtractor; + +/** + * Copyright (C) Christian Schabesberger 2016 + * YoutubeStreamInfoItemExtractor.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 . + */ + +public class YoutubeStreamInfoItemExtractor implements StreamInfoItemExtractor { + + private final Element item; + + public YoutubeStreamInfoItemExtractor(Element item) throws FoundAdException { + this.item = item; + } + + @Override + public String getWebPageUrl() throws ParsingException { + try { + Element el = item.select("div[class*=\"yt-lockup-video\"").first(); + Element dl = el.select("h3").first().select("a").first(); + return dl.attr("abs:href"); + } catch (Exception e) { + throw new ParsingException("Could not get web page url for the video", e); + } + } + + @Override + public String getTitle() throws ParsingException { + try { + Element el = item.select("div[class*=\"yt-lockup-video\"").first(); + Element dl = el.select("h3").first().select("a").first(); + return dl.text(); + } catch (Exception e) { + throw new ParsingException("Could not get title", e); + } + } + + @Override + public int getDuration() throws ParsingException { + try { + return YoutubeParsingHelper.parseDurationString( + item.select("span[class=\"video-time\"]").first().text()); + } catch(Exception e) { + if(isLiveStream(item)) { + // -1 for no duration + return -1; + } else { + throw new ParsingException("Could not get Duration: " + getTitle(), e); + } + } + } + + @Override + public String getUploader() throws ParsingException { + try { + return item.select("div[class=\"yt-lockup-byline\"]").first() + .select("a").first() + .text(); + } catch (Exception e) { + throw new ParsingException("Could not get uploader", e); + } + } + + @Override + public String getUploadDate() throws ParsingException { + try { + Element div = item.select("div[class=\"yt-lockup-meta\"]").first(); + if(div == null) { + return null; + } else { + return div.select("li").first().text(); + } + } catch(Exception e) { + throw new ParsingException("Could not get uplaod date", e); + } + } + + @Override + public long getViewCount() throws ParsingException { + String output; + String input; + try { + Element div = item.select("div[class=\"yt-lockup-meta\"]").first(); + if(div == null) { + return -1; + } else { + input = div.select("li").get(1) + .text(); + } + } catch (IndexOutOfBoundsException e) { + if(isLiveStream(item)) { + // -1 for no view count + return -1; + } else { + throw new ParsingException( + "Could not parse yt-lockup-meta although available: " + getTitle(), e); + } + } + + output = Parser.matchGroup1("([0-9,\\. ]*)", input) + .replace(" ", "") + .replace(".", "") + .replace(",", ""); + + 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); + } + } + } + + @Override + public String getThumbnailUrl() throws ParsingException { + try { + String url; + Element te = item.select("div[class=\"yt-thumb video-thumb\"]").first() + .select("img").first(); + url = te.attr("abs:src"); + // Sometimes youtube sends links to gif files which somehow seem to not exist + // anymore. Items with such gif also offer a secondary image source. So we are going + // to use that if we've caught such an item. + if (url.contains(".gif")) { + url = te.attr("abs:data-thumb"); + } + return url; + } catch (Exception e) { + throw new ParsingException("Could not get thumbnail url", e); + } + } + + @Override + public AbstractStreamInfo.StreamType getStreamType() { + if(isLiveStream(item)) { + return AbstractStreamInfo.StreamType.LIVE_STREAM; + } else { + return AbstractStreamInfo.StreamType.VIDEO_STREAM; + } + } + + @Override + public boolean isAd() throws ParsingException { + if(!item.select("span[class*=\"icon-not-available\"]").isEmpty()) { + return true; + } else { + return false; + } + } + + private boolean isLiveStream(Element item) { + Element bla = item.select("span[class*=\"yt-badge-live\"]").first(); + + if(bla == null) { + // sometimes livestreams dont have badges but sill are live streams + // if video time is not available we most likly have an offline livestream + if(item.select("span[class*=\"video-time\"]").first() == null) { + return true; + } + } + return bla != null; + } +} diff --git a/services/youtube/YoutubeStreamUrlIdHandler.java b/services/youtube/YoutubeStreamUrlIdHandler.java new file mode 100644 index 00000000..8c08a610 --- /dev/null +++ b/services/youtube/YoutubeStreamUrlIdHandler.java @@ -0,0 +1,163 @@ +package org.schabi.newpipe.extractor.services.youtube; + +import org.schabi.newpipe.extractor.Downloader; +import org.schabi.newpipe.extractor.NewPipe; +import org.schabi.newpipe.extractor.Parser; +import org.schabi.newpipe.extractor.UrlIdHandler; +import org.schabi.newpipe.extractor.exceptions.FoundAdException; +import org.schabi.newpipe.extractor.exceptions.ParsingException; +import org.schabi.newpipe.extractor.exceptions.ReCaptchaException; + +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.URLDecoder; + +/** + * Created by Christian Schabesberger on 02.02.16. + * + * Copyright (C) Christian Schabesberger 2016 + * YoutubeStreamUrlIdHandler.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 . + */ + +public class YoutubeStreamUrlIdHandler implements UrlIdHandler { + + private static final YoutubeStreamUrlIdHandler instance = new YoutubeStreamUrlIdHandler(); + private static final String ID_PATTERN = "([\\-a-zA-Z0-9_]{11})"; + + private YoutubeStreamUrlIdHandler() {} + + public static YoutubeStreamUrlIdHandler getInstance() { + return instance; + } + + @Override + public String getUrl(String videoId) { + return "https://www.youtube.com/watch?v=" + videoId; + } + + @Override + public String getId(String url) throws ParsingException, IllegalArgumentException { + if(url.isEmpty()) { + throw new IllegalArgumentException("The url parameter should not be empty"); + } + + String id; + String lowercaseUrl = url.toLowerCase(); + if(lowercaseUrl.contains("youtube")) { + if (url.contains("attribution_link")) { + try { + String escapedQuery = Parser.matchGroup1("u=(.[^&|$]*)", url); + String query = URLDecoder.decode(escapedQuery, "UTF-8"); + id = Parser.matchGroup1("v=" + ID_PATTERN, query); + } catch (UnsupportedEncodingException uee) { + throw new ParsingException("Could not parse attribution_link", uee); + } + } else if(lowercaseUrl.contains("youtube.com/shared?ci=")) { + return getRealIdFromSharedLink(url); + } else if (url.contains("vnd.youtube")) { + id = Parser.matchGroup1(ID_PATTERN, url); + } else if (url.contains("embed")) { + id = Parser.matchGroup1("embed/" + ID_PATTERN, url); + } else if(url.contains("googleads")) { + throw new FoundAdException("Error found add: " + url); + } else { + id = Parser.matchGroup1("[?&]v=" + ID_PATTERN, url); + } + } + else if(lowercaseUrl.contains("youtu.be")) { + if(url.contains("v=")) { + id = Parser.matchGroup1("v=" + ID_PATTERN, url); + } else { + id = Parser.matchGroup1("[Yy][Oo][Uu][Tt][Uu]\\.[Bb][Ee]/" + ID_PATTERN, url); + } + } + else { + throw new ParsingException("Error no suitable url: " + url); + } + + + if(!id.isEmpty()){ + return id; + } else { + throw new ParsingException("Error could not parse url: " + url); + } + } + + /** + * Get the real url from a shared uri. + * + * Shared URI's look like this: + *
+     *     * https://www.youtube.com/shared?ci=PJICrTByb3E
+     *     * vnd.youtube://www.youtube.com/shared?ci=PJICrTByb3E&feature=twitter-deep-link
+     * 
+ * @param url The shared url + * @return the id of the stream + * @throws ParsingException + */ + private String getRealIdFromSharedLink(String url) throws ParsingException { + URI uri; + try { + uri = new URI(url); + } catch (URISyntaxException e) { + throw new ParsingException("Invalid shared link", e); + } + String sharedId = getSharedId(uri); + Downloader downloader = NewPipe.getDownloader(); + String content; + try { + content = downloader.download("https://www.youtube.com/shared?ci=" + sharedId); + } catch (IOException | ReCaptchaException e) { + throw new ParsingException("Unable to resolve shared link", e); + } + // is this bad? is this fragile?: + String realId = Parser.matchGroup1("rel=\"shortlink\" href=\"https://youtu.be/" + ID_PATTERN, content); + if(sharedId.equals(realId)) { + throw new ParsingException("Got same id for as shared id: " + sharedId); + } + return realId; + } + + private String getSharedId(URI uri) throws ParsingException { + if (!"/shared".equals(uri.getPath())) { + throw new ParsingException("Not a shared link: " + uri.toString() + " (path != " + uri.getPath() + ")"); + } + return Parser.matchGroup1("ci=" + ID_PATTERN, uri.getQuery()); + } + + public String cleanUrl(String complexUrl) throws ParsingException { + return getUrl(getId(complexUrl)); + } + + @Override + public boolean acceptUrl(String videoUrl) { + String lowercaseUrl = videoUrl.toLowerCase(); + if(lowercaseUrl.contains("youtube") || + lowercaseUrl.contains("youtu.be")) { + // bad programming I know + try { + getId(videoUrl); + return true; + } catch (Exception e) { + return false; + } + } else { + return false; + } + } +} diff --git a/services/youtube/YoutubeSuggestionExtractor.java b/services/youtube/YoutubeSuggestionExtractor.java new file mode 100644 index 00000000..0535961e --- /dev/null +++ b/services/youtube/YoutubeSuggestionExtractor.java @@ -0,0 +1,99 @@ +package org.schabi.newpipe.extractor.services.youtube; + +import org.schabi.newpipe.extractor.Downloader; +import org.schabi.newpipe.extractor.NewPipe; +import org.schabi.newpipe.extractor.exceptions.ExtractionException; +import org.schabi.newpipe.extractor.exceptions.ParsingException; +import org.schabi.newpipe.extractor.SuggestionExtractor; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.net.URLEncoder; +import java.util.ArrayList; +import java.util.List; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; + +/** + * Created by Christian Schabesberger on 28.09.16. + * + * Copyright (C) Christian Schabesberger 2015 + * YoutubeSuggestionExtractor.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 . + */ + +public class YoutubeSuggestionExtractor extends SuggestionExtractor { + + public static final String CHARSET_UTF_8 = "UTF-8"; + + public YoutubeSuggestionExtractor(int serviceId) { + super(serviceId); + } + + @Override + public List suggestionList( + String query, String contentCountry) + throws ExtractionException, IOException { + List suggestions = new ArrayList<>(); + + Downloader dl = NewPipe.getDownloader(); + + String url = "https://suggestqueries.google.com/complete/search" + + "?client=" + "" + + "&output=" + "toolbar" + + "&ds=" + "yt" + + "&hl=" + URLEncoder.encode(contentCountry, CHARSET_UTF_8) + + "&q=" + URLEncoder.encode(query, CHARSET_UTF_8); + + + String response = dl.download(url); + + //TODO: Parse xml data using Jsoup not done + DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); + DocumentBuilder dBuilder; + org.w3c.dom.Document doc = null; + + try { + dBuilder = dbFactory.newDocumentBuilder(); + doc = dBuilder.parse(new InputSource( + new ByteArrayInputStream(response.getBytes(CHARSET_UTF_8)))); + doc.getDocumentElement().normalize(); + } catch (ParserConfigurationException | SAXException | IOException e) { + throw new ParsingException("Could not parse document."); + } + + try { + NodeList nList = doc.getElementsByTagName("CompleteSuggestion"); + for (int temp = 0; temp < nList.getLength(); temp++) { + + NodeList nList1 = doc.getElementsByTagName("suggestion"); + Node nNode1 = nList1.item(temp); + if (nNode1.getNodeType() == Node.ELEMENT_NODE) { + org.w3c.dom.Element eElement = (org.w3c.dom.Element) nNode1; + suggestions.add(eElement.getAttribute("data")); + } + } + return suggestions; + } catch(Exception e) { + throw new ParsingException("Could not get suggestions form document.", e); + } + } +} diff --git a/stream_info/AudioStream.java b/stream_info/AudioStream.java new file mode 100644 index 00000000..98eb1762 --- /dev/null +++ b/stream_info/AudioStream.java @@ -0,0 +1,46 @@ +package org.schabi.newpipe.extractor.stream_info; + +/** + * Created by Christian Schabesberger on 04.03.16. + * + * Copyright (C) Christian Schabesberger 2016 + * AudioStream.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 . + */ + +public class AudioStream { + public String url = ""; + public int format = -1; + public int bandwidth = -1; + public int sampling_rate = -1; + + public AudioStream(String url, int format, int bandwidth, int samplingRate) { + this.url = url; this.format = format; + this.bandwidth = bandwidth; this.sampling_rate = samplingRate; + } + + // reveals whether two streams are the same, but have different urls + public boolean equalStats(AudioStream cmp) { + return format == cmp.format + && bandwidth == cmp.bandwidth + && sampling_rate == cmp.sampling_rate; + } + + // reveals whether two streams are equal + public boolean equals(AudioStream cmp) { + return cmp != null && equalStats(cmp) + && url == cmp.url; + } +} diff --git a/stream_info/StreamExtractor.java b/stream_info/StreamExtractor.java new file mode 100644 index 00000000..1b7c1b5a --- /dev/null +++ b/stream_info/StreamExtractor.java @@ -0,0 +1,104 @@ +package org.schabi.newpipe.extractor.stream_info; + +/** + * Created by Christian Schabesberger on 10.08.15. + * + * Copyright (C) Christian Schabesberger 2016 + * StreamExtractor.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 . + */ + +import org.schabi.newpipe.extractor.UrlIdHandler; +import org.schabi.newpipe.extractor.exceptions.ExtractionException; +import org.schabi.newpipe.extractor.exceptions.ParsingException; + +import java.util.List; + +/**Scrapes information from a video streaming service (eg, YouTube).*/ + + +@SuppressWarnings("ALL") +public abstract class StreamExtractor { + + private int serviceId; + private String url; + private UrlIdHandler urlIdHandler; + private StreamInfoItemCollector previewInfoCollector; + + public class ExtractorInitException extends ExtractionException { + public ExtractorInitException(String message) { + super(message); + } + public ExtractorInitException(Throwable cause) { + super(cause); + } + public ExtractorInitException(String message, Throwable cause) { + super(message, cause); + } + } + + public class ContentNotAvailableException extends ParsingException { + public ContentNotAvailableException(String message) { + super(message); + } + public ContentNotAvailableException(String message, Throwable cause) { + super(message, cause); + } + } + + public StreamExtractor(UrlIdHandler urlIdHandler, String url, int serviceId) { + this.serviceId = serviceId; + this.urlIdHandler = urlIdHandler; + previewInfoCollector = new StreamInfoItemCollector(urlIdHandler, serviceId); + } + + protected StreamInfoItemCollector getStreamPreviewInfoCollector() { + return previewInfoCollector; + } + + public String getUrl() { + return url; + } + + public UrlIdHandler getUrlIdHandler() { + return urlIdHandler; + } + + public abstract int getTimeStamp() throws ParsingException; + public abstract String getTitle() throws ParsingException; + public abstract String getDescription() throws ParsingException; + public abstract String getUploader() throws ParsingException; + public abstract String getChannelUrl() throws ParsingException; + public abstract int getLength() throws ParsingException; + public abstract long getViewCount() throws ParsingException; + public abstract String getUploadDate() throws ParsingException; + public abstract String getThumbnailUrl() throws ParsingException; + public abstract String getUploaderThumbnailUrl() throws ParsingException; + public abstract List getAudioStreams() throws ParsingException; + public abstract List getVideoStreams() throws ParsingException; + public abstract List getVideoOnlyStreams() throws ParsingException; + public abstract String getDashMpdUrl() throws ParsingException; + public abstract int getAgeLimit() throws ParsingException; + public abstract String getAverageRating() throws ParsingException; + public abstract int getLikeCount() throws ParsingException; + public abstract int getDislikeCount() throws ParsingException; + public abstract StreamInfoItemExtractor getNextVideo() throws ParsingException; + public abstract StreamInfoItemCollector getRelatedVideos() throws ParsingException; + public abstract String getPageUrl(); + public abstract StreamInfo.StreamType getStreamType() throws ParsingException; + public int getServiceId() { + return serviceId; + } +} diff --git a/stream_info/StreamInfo.java b/stream_info/StreamInfo.java new file mode 100644 index 00000000..cf3df49d --- /dev/null +++ b/stream_info/StreamInfo.java @@ -0,0 +1,294 @@ +package org.schabi.newpipe.extractor.stream_info; + +import org.schabi.newpipe.extractor.AbstractStreamInfo; +import org.schabi.newpipe.extractor.DashMpdParser; +import org.schabi.newpipe.extractor.InfoItem; +import org.schabi.newpipe.extractor.UrlIdHandler; +import org.schabi.newpipe.extractor.exceptions.ExtractionException; + +import java.io.IOException; +import java.util.List; +import java.util.Vector; + +/** + * Created by Christian Schabesberger on 26.08.15. + * + * Copyright (C) Christian Schabesberger 2016 + * StreamInfo.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 . + */ + +/**Info object for opened videos, ie the video ready to play.*/ +@SuppressWarnings("ALL") +public class StreamInfo extends AbstractStreamInfo { + + public static class StreamExctractException extends ExtractionException { + StreamExctractException(String message) { + super(message); + } + } + + public StreamInfo() {} + + /**Creates a new StreamInfo object from an existing AbstractVideoInfo. + * All the shared properties are copied to the new StreamInfo.*/ + @SuppressWarnings("WeakerAccess") + public StreamInfo(AbstractStreamInfo avi) { + this.id = avi.id; + this.title = avi.title; + this.uploader = avi.uploader; + this.thumbnail_url = avi.thumbnail_url; + this.webpage_url = avi.webpage_url; + this.upload_date = avi.upload_date; + this.upload_date = avi.upload_date; + this.view_count = avi.view_count; + + //todo: better than this + if(avi instanceof StreamInfoItem) { + //shitty String to convert code + /* + String dur = ((StreamInfoItem)avi).duration; + int minutes = Integer.parseInt(dur.substring(0, dur.indexOf(":"))); + int seconds = Integer.parseInt(dur.substring(dur.indexOf(":")+1, dur.length())); + */ + this.duration = ((StreamInfoItem)avi).duration; + } + } + + public void addException(Exception e) { + errors.add(e); + } + + /**Fills out the video info fields which are common to all services. + * Probably needs to be overridden by subclasses*/ + public static StreamInfo getVideoInfo(StreamExtractor extractor) + throws ExtractionException, IOException { + StreamInfo streamInfo = new StreamInfo(); + + streamInfo = extractImportantData(streamInfo, extractor); + streamInfo = extractStreams(streamInfo, extractor); + streamInfo = extractOptionalData(streamInfo, extractor); + + return streamInfo; + } + + private static StreamInfo extractImportantData( + StreamInfo streamInfo, StreamExtractor extractor) + throws ExtractionException, IOException { + /* ---- important data, withoug the video can't be displayed goes here: ---- */ + // if one of these is not available an exception is meant to be thrown directly into the frontend. + + UrlIdHandler uiconv = extractor.getUrlIdHandler(); + + streamInfo.service_id = extractor.getServiceId(); + streamInfo.webpage_url = extractor.getPageUrl(); + streamInfo.stream_type = extractor.getStreamType(); + streamInfo.id = uiconv.getId(extractor.getPageUrl()); + streamInfo.title = extractor.getTitle(); + streamInfo.age_limit = extractor.getAgeLimit(); + + if((streamInfo.stream_type == StreamType.NONE) + || (streamInfo.webpage_url == null || streamInfo.webpage_url.isEmpty()) + || (streamInfo.id == null || streamInfo.id.isEmpty()) + || (streamInfo.title == null /* streamInfo.title can be empty of course */) + || (streamInfo.age_limit == -1)) { + throw new ExtractionException("Some importand stream information was not given."); + } + + return streamInfo; + } + + private static StreamInfo extractStreams( + StreamInfo streamInfo, StreamExtractor extractor) + throws ExtractionException, IOException { + /* ---- stream extraction goes here ---- */ + // At least one type of stream has to be available, + // otherwise an exception will be thrown directly into the frontend. + + try { + streamInfo.dashMpdUrl = extractor.getDashMpdUrl(); + } catch(Exception e) { + streamInfo.addException(new ExtractionException("Couldn't get Dash manifest", e)); + } + + /* Load and extract audio */ + try { + streamInfo.audio_streams = extractor.getAudioStreams(); + } catch(Exception e) { + streamInfo.addException(new ExtractionException("Couldn't get audio streams", e)); + } + // also try to get streams from the dashMpd + if(streamInfo.dashMpdUrl != null && !streamInfo.dashMpdUrl.isEmpty()) { + if(streamInfo.audio_streams == null) { + streamInfo.audio_streams = new Vector<>(); + } + //todo: make this quick and dirty solution a real fallback + // same as the quick and dirty above + try { + streamInfo.audio_streams.addAll( + DashMpdParser.getAudioStreams(streamInfo.dashMpdUrl)); + } catch(Exception e) { + streamInfo.addException( + new ExtractionException("Couldn't get audio streams from dash mpd", e)); + } + } + /* Extract video stream url*/ + try { + streamInfo.video_streams = extractor.getVideoStreams(); + } catch (Exception e) { + streamInfo.addException( + new ExtractionException("Couldn't get video streams", e)); + } + /* Extract video only stream url*/ + try { + streamInfo.video_only_streams = extractor.getVideoOnlyStreams(); + } catch(Exception e) { + streamInfo.addException( + new ExtractionException("Couldn't get video only streams", e)); + } + + // either dash_mpd audio_only or video has to be available, otherwise we didn't get a stream, + // and therefore failed. (Since video_only_streams are just optional they don't caunt). + if((streamInfo.video_streams == null || streamInfo.video_streams.isEmpty()) + && (streamInfo.audio_streams == null || streamInfo.audio_streams.isEmpty()) + && (streamInfo.dashMpdUrl == null || streamInfo.dashMpdUrl.isEmpty())) { + throw new StreamExctractException( + "Could not get any stream. See error variable to get further details."); + } + + return streamInfo; + } + + private static StreamInfo extractOptionalData( + StreamInfo streamInfo, StreamExtractor extractor) { + /* ---- optional data goes here: ---- */ + // If one of these fails, the frontend needs to handle that they are not available. + // Exceptions are therefore not thrown into the frontend, but stored into the error List, + // so the frontend can afterwards check where errors happened. + + try { + streamInfo.thumbnail_url = extractor.getThumbnailUrl(); + } catch(Exception e) { + streamInfo.addException(e); + } + try { + streamInfo.duration = extractor.getLength(); + } catch(Exception e) { + streamInfo.addException(e); + } + try { + streamInfo.uploader = extractor.getUploader(); + } catch(Exception e) { + streamInfo.addException(e); + } + try { + streamInfo.channel_url = extractor.getChannelUrl(); + } catch(Exception e) { + streamInfo.addException(e); + } + try { + streamInfo.description = extractor.getDescription(); + } catch(Exception e) { + streamInfo.addException(e); + } + try { + streamInfo.view_count = extractor.getViewCount(); + } catch(Exception e) { + streamInfo.addException(e); + } + try { + streamInfo.upload_date = extractor.getUploadDate(); + } catch(Exception e) { + streamInfo.addException(e); + } + try { + streamInfo.uploader_thumbnail_url = extractor.getUploaderThumbnailUrl(); + } catch(Exception e) { + streamInfo.addException(e); + } + try { + streamInfo.start_position = extractor.getTimeStamp(); + } catch(Exception e) { + streamInfo.addException(e); + } + try { + streamInfo.average_rating = extractor.getAverageRating(); + } catch(Exception e) { + streamInfo.addException(e); + } + try { + streamInfo.like_count = extractor.getLikeCount(); + } catch(Exception e) { + streamInfo.addException(e); + } + try { + streamInfo.dislike_count = extractor.getDislikeCount(); + } catch(Exception e) { + streamInfo.addException(e); + } + try { + // get next video + if(streamInfo.next_video != null) + { + StreamInfoItemCollector c = new StreamInfoItemCollector( + extractor.getUrlIdHandler(), extractor.getServiceId()); + StreamInfoItemExtractor nextVideo = extractor.getNextVideo(); + c.commit(nextVideo); + if(c.getItemList().size() != 0) { + streamInfo.next_video = (StreamInfoItem) c.getItemList().get(0); + } + streamInfo.errors.addAll(c.getErrors()); + } + } + catch(Exception e) { + streamInfo.addException(e); + } + try { + // get related videos + StreamInfoItemCollector c = extractor.getRelatedVideos(); + streamInfo.related_streams = c.getItemList(); + streamInfo.errors.addAll(c.getErrors()); + } catch(Exception e) { + streamInfo.addException(e); + } + + return streamInfo; + } + + public String uploader_thumbnail_url = ""; + public String channel_url = ""; + public String description = ""; + + public List video_streams = null; + public List audio_streams = null; + public List video_only_streams = null; + // video streams provided by the dash mpd do not need to be provided as VideoStream. + // Later on this will also aplly to audio streams. Since dash mpd is standarized, + // crawling such a file is not service dependent. Therefore getting audio only streams by yust + // providing the dash mpd fille will be possible in the future. + public String dashMpdUrl = ""; + public int duration = -1; + + public int age_limit = -1; + public int like_count = -1; + public int dislike_count = -1; + public String average_rating = ""; + public StreamInfoItem next_video = null; + public List related_streams = null; + //in seconds. some metadata is not passed using a StreamInfo object! + public int start_position = 0; + + public List errors = new Vector<>(); +} diff --git a/stream_info/StreamInfoItem.java b/stream_info/StreamInfoItem.java new file mode 100644 index 00000000..8a6db5bc --- /dev/null +++ b/stream_info/StreamInfoItem.java @@ -0,0 +1,41 @@ +package org.schabi.newpipe.extractor.stream_info; + +/** + * Created by Christian Schabesberger on 26.08.15. + * + * Copyright (C) Christian Schabesberger 2016 + * StreamInfoItem.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 . + */ + +import org.schabi.newpipe.extractor.AbstractStreamInfo; +import org.schabi.newpipe.extractor.InfoItem; + +/**Info object for previews of unopened videos, eg search results, related videos*/ +public class StreamInfoItem extends AbstractStreamInfo implements InfoItem { + public int duration; + + public InfoType infoType() { + return InfoType.STREAM; + } + + public String getTitle() { + return title; + } + + public String getLink() { + return webpage_url; + } +} \ No newline at end of file diff --git a/stream_info/StreamInfoItemCollector.java b/stream_info/StreamInfoItemCollector.java new file mode 100644 index 00000000..03ff7bd8 --- /dev/null +++ b/stream_info/StreamInfoItemCollector.java @@ -0,0 +1,102 @@ +package org.schabi.newpipe.extractor.stream_info; + +import org.schabi.newpipe.extractor.InfoItemCollector; +import org.schabi.newpipe.extractor.NewPipe; +import org.schabi.newpipe.extractor.UrlIdHandler; +import org.schabi.newpipe.extractor.exceptions.FoundAdException; +import org.schabi.newpipe.extractor.exceptions.ParsingException; + +import java.util.List; +import java.util.Vector; + +/** + * Created by Christian Schabesberger on 28.02.16. + * + * Copyright (C) Christian Schabesberger 2016 + * StreamInfoItemCollector.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 . + */ + +public class StreamInfoItemCollector extends InfoItemCollector { + + private UrlIdHandler urlIdHandler; + + public StreamInfoItemCollector(UrlIdHandler handler, int serviceId) { + super(serviceId); + urlIdHandler = handler; + } + + private UrlIdHandler getUrlIdHandler() { + return urlIdHandler; + } + + public StreamInfoItem extract(StreamInfoItemExtractor extractor) throws Exception { + if(extractor.isAd()) { + throw new FoundAdException("Found ad"); + } + + StreamInfoItem resultItem = new StreamInfoItem(); + // importand information + resultItem.service_id = getServiceId(); + resultItem.webpage_url = extractor.getWebPageUrl(); + if (getUrlIdHandler() == null) { + throw new ParsingException("Error: UrlIdHandler not set"); + } else if (!resultItem.webpage_url.isEmpty()) { + resultItem.id = NewPipe.getService(getServiceId()) + .getStreamUrlIdHandlerInstance() + .getId(resultItem.webpage_url); + } + resultItem.title = extractor.getTitle(); + resultItem.stream_type = extractor.getStreamType(); + + // optional information + try { + resultItem.duration = extractor.getDuration(); + } catch (Exception e) { + addError(e); + } + try { + resultItem.uploader = extractor.getUploader(); + } catch (Exception e) { + addError(e); + } + try { + resultItem.upload_date = extractor.getUploadDate(); + } catch (Exception e) { + addError(e); + } + try { + resultItem.view_count = extractor.getViewCount(); + } catch (Exception e) { + addError(e); + } + try { + resultItem.thumbnail_url = extractor.getThumbnailUrl(); + } catch (Exception e) { + addError(e); + } + return resultItem; + } + + public void commit(StreamInfoItemExtractor extractor) throws ParsingException { + try { + addItem(extract(extractor)); + } catch(FoundAdException ae) { + //System.out.println("AD_WARNING: " + ae.getMessage()); + } catch (Exception e) { + addError(e); + } + } +} diff --git a/stream_info/StreamInfoItemExtractor.java b/stream_info/StreamInfoItemExtractor.java new file mode 100644 index 00000000..b5432b42 --- /dev/null +++ b/stream_info/StreamInfoItemExtractor.java @@ -0,0 +1,36 @@ +package org.schabi.newpipe.extractor.stream_info; + +import org.schabi.newpipe.extractor.AbstractStreamInfo; +import org.schabi.newpipe.extractor.exceptions.ParsingException; + +/** + * Created by Christian Schabesberger on 28.02.16. + * + * Copyright (C) Christian Schabesberger 2016 + * StreamInfoItemExtractor.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 . + */ + +public interface StreamInfoItemExtractor { + AbstractStreamInfo.StreamType getStreamType() throws ParsingException; + String getWebPageUrl() throws ParsingException; + String getTitle() throws ParsingException; + int getDuration() throws ParsingException; + String getUploader() throws ParsingException; + String getUploadDate() throws ParsingException; + long getViewCount() throws ParsingException; + String getThumbnailUrl() throws ParsingException; + boolean isAd() throws ParsingException; +} diff --git a/stream_info/VideoStream.java b/stream_info/VideoStream.java new file mode 100644 index 00000000..c3e12fdb --- /dev/null +++ b/stream_info/VideoStream.java @@ -0,0 +1,44 @@ +package org.schabi.newpipe.extractor.stream_info; + +/** + * Created by Christian Schabesberger on 04.03.16. + * + * Copyright (C) Christian Schabesberger 2016 + * VideoStream.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 . + */ + +public class VideoStream { + //url of the stream + public String url = ""; + public int format = -1; + public String resolution = ""; + + public VideoStream(String url, int format, String res) { + this.url = url; this.format = format; resolution = res; + } + + // reveals wether two streams are the same, but have diferent urls + public boolean equalStats(VideoStream cmp) { + return format == cmp.format + && resolution == cmp.resolution; + } + + // revelas wether two streams are equal + public boolean equals(VideoStream cmp) { + return cmp != null && equalStats(cmp) + && url == cmp.url; + } +}