-
Notifications
You must be signed in to change notification settings - Fork 422
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Base Implementation: Parse the upload date of StreamInfoItems
In the format '2 days ago' (in English) on a YouTube channel page. (Parser extensible to other pages.)
- Loading branch information
1 parent
514ed7b
commit 180836c
Showing
16 changed files
with
316 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,17 @@ | ||
package org.schabi.newpipe.extractor.services.youtube.extractors; | ||
|
||
import org.jsoup.nodes.Element; | ||
import org.jsoup.select.Elements; | ||
import org.schabi.newpipe.extractor.exceptions.ParsingException; | ||
import org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeParsingHelper; | ||
import org.schabi.newpipe.extractor.stream.StreamInfoItemExtractor; | ||
import org.schabi.newpipe.extractor.stream.StreamType; | ||
import org.schabi.newpipe.extractor.stream.TimeAgoParser; | ||
import org.schabi.newpipe.extractor.utils.Utils; | ||
|
||
import javax.annotation.Nullable; | ||
import java.util.Calendar; | ||
|
||
/* | ||
* Copyright (C) Christian Schabesberger 2016 <[email protected]> | ||
* YoutubeStreamInfoItemExtractor.java is part of NewPipe. | ||
|
@@ -28,9 +33,18 @@ | |
public class YoutubeStreamInfoItemExtractor implements StreamInfoItemExtractor { | ||
|
||
private final Element item; | ||
private final TimeAgoParser timeAgoParser; | ||
|
||
private String cachedUploadDate; | ||
|
||
public YoutubeStreamInfoItemExtractor(Element item) { | ||
/** | ||
* Creates an extractor of StreamInfoItems from a YouTube page. | ||
* @param item The page element | ||
* @param timeAgoParser A parser of the textual dates or {@code null}. | ||
*/ | ||
public YoutubeStreamInfoItemExtractor(Element item, @Nullable TimeAgoParser timeAgoParser) { | ||
this.item = item; | ||
this.timeAgoParser = timeAgoParser; | ||
} | ||
|
||
@Override | ||
|
@@ -126,20 +140,35 @@ public String getUploaderUrl() throws ParsingException { | |
} | ||
|
||
@Override | ||
public String getUploadDate() throws ParsingException { | ||
public String getTextualUploadDate() throws ParsingException { | ||
if (cachedUploadDate != null) { | ||
return cachedUploadDate; | ||
} | ||
|
||
try { | ||
Element meta = item.select("div[class=\"yt-lockup-meta\"]").first(); | ||
if (meta == null) return ""; | ||
|
||
Element li = meta.select("li").first(); | ||
if(li == null) return ""; | ||
final Elements li = meta.select("li"); | ||
if (li.isEmpty()) return ""; | ||
|
||
return meta.select("li").first().text(); | ||
return cachedUploadDate = li.first().text(); | ||
} catch (Exception e) { | ||
throw new ParsingException("Could not get upload date", e); | ||
} | ||
} | ||
|
||
@Override | ||
public Calendar getUploadDate() throws ParsingException { | ||
String textualUploadDate = getTextualUploadDate(); | ||
if (timeAgoParser != null | ||
&& textualUploadDate != null && !"".equals(textualUploadDate)) { | ||
return timeAgoParser.parse(textualUploadDate); | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
@Override | ||
public long getViewCount() throws ParsingException { | ||
String input; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.