Skip to content

Commit

Permalink
Closes #28: Handle errors when reading video duration
Browse files Browse the repository at this point in the history
  • Loading branch information
Alkisum committed Jul 31, 2019
1 parent f5353dd commit 52fd9aa
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
34 changes: 24 additions & 10 deletions src/main/java/controller/tasks/RssReader.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package controller.tasks;

import database.Database;
import javafx.application.Platform;
import javafx.concurrent.Task;
import model.Channel;
import model.Video;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import view.dialog.ErrorDialog;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
Expand All @@ -21,7 +23,7 @@
* Class to retrieve and read RSS Feeds.
*
* @author Alkisum
* @version 2.4
* @version 2.9
* @since 1.0
*/
public class RssReader extends Task<Void> {
Expand Down Expand Up @@ -55,6 +57,7 @@ public RssReader(final List<Channel> channels) {
protected final Void call() throws Exception {

List<Video> videos = new ArrayList<>();
boolean durationError = false;

updateMessage("Initializing...");

Expand Down Expand Up @@ -95,13 +98,16 @@ protected final Void call() throws Exception {

Element eltEntry = (Element) nodeEntry;

// Title
String title = eltEntry.getElementsByTagName("title")
.item(0).getTextContent();

// YT ID
String ytId = eltEntry.getElementsByTagName("yt:videoId")
.item(0).getTextContent();
if (ytId == null) {
continue;
}

// Title
String title = eltEntry.getElementsByTagName("title")
.item(0).getTextContent();

// URL
Node nodeUrl = eltEntry.getElementsByTagName("link")
Expand Down Expand Up @@ -131,12 +137,16 @@ protected final Void call() throws Exception {
}
}

if (ytId != null && !Database.videoExists(ytId)) {
if (!Database.videoExists(ytId)) {

// Duration
long duration = 0;
if (url != null) {
duration = Video.retrieveDuration(url);
try {
duration = Video.retrieveDuration(url);
} catch (Exception e) {
durationError = true;
}
}

videos.add(new Video(
Expand All @@ -148,9 +158,7 @@ protected final Void call() throws Exception {
ytId));
}

if (ytId != null) {
ytIds.add(ytId);
}
ytIds.add(ytId);
}
}
channel.clean(ytIds);
Expand All @@ -160,6 +168,12 @@ protected final Void call() throws Exception {
updateMessage("Downloading thumbnails...");
Database.insertVideos(videos);
}

// Errors occurred when reading durations
if (durationError) {
Platform.runLater(() -> ErrorDialog.show("Duration error",
"An error occurred when reading the duration from videos."));
}
return null;
}

Expand Down
7 changes: 4 additions & 3 deletions src/main/java/model/Video.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* Class defining video.
*
* @author Alkisum
* @version 2.6
* @version 2.9
* @since 1.0
*/
public class Video {
Expand Down Expand Up @@ -262,9 +262,10 @@ public final String getFormatDuration() {
*
* @param url URL of YouTube page
* @return Duration of the video
* @throws IOException The YouTube page has not been found
* @throws Exception The YouTube page has not been found
* or the duration of the video cannot be read from the page
*/
public static long retrieveDuration(final String url) throws IOException {
public static long retrieveDuration(final String url) throws Exception {
String duration = "";
int i = 0;
while (duration.equals("") && i <= 3) {
Expand Down

0 comments on commit 52fd9aa

Please # to comment.