Skip to content

Commit

Permalink
bug fixes; code style improvements;
Browse files Browse the repository at this point in the history
  • Loading branch information
sealedtx committed Mar 22, 2019
1 parent 806d034 commit 5db11ee
Show file tree
Hide file tree
Showing 13 changed files with 267 additions and 143 deletions.
44 changes: 0 additions & 44 deletions src/main/java/com/github/kiulian/downloader/Constants.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.github.kiulian.downloader;

import java.io.File;

public interface YoutubeDownloadCallback {

void onDownloading(int progress);

void onFinished(File file);

void onError(Throwable throwable);
}
34 changes: 26 additions & 8 deletions src/main/java/com/github/kiulian/downloader/YoutubeDownloader.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand All @@ -33,13 +33,16 @@
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

public class YoutubeDownloader {

public static final char[] ILLEGAL_FILENAME_CHARACTERS = {'/', '\n', '\r', '\t', '\0', '\f', '`', '?', '*', '\\', '<', '>', '|', '\"', ':'};

public interface DownloadCallback {

void onDownloading(int progress);
Expand All @@ -49,8 +52,11 @@ public interface DownloadCallback {
void onError(Throwable throwable);
}

private static final String AUDIO = "audio";
private static final String VIDEO = "video";
private static final String CONFIG_START = "ytplayer.config = ";
private static final String CONFIG_END = ";ytplayer.load";
private static final String ERROR = "\"status\":\"ERROR\",\"reason\":\"";


public static YoutubeVideo getVideo(String videoId) throws YoutubeException, IOException {
Expand All @@ -61,6 +67,15 @@ public static YoutubeVideo getVideo(String videoId) throws YoutubeException, IOE
int start = page.indexOf(CONFIG_START);
int end = page.indexOf(CONFIG_END);

if (start == -1 || end == -1) {
int errorIndex = page.indexOf(ERROR);
if (errorIndex != -1) {
String reason = page.substring(errorIndex + ERROR.length(), page.indexOf("\"", errorIndex + ERROR.length() + 1));
throw new YoutubeException.VideoUnavailableException(reason);
} else {
throw new YoutubeException.BadPageException("Could not parse web page");
}
}
String cfg = page.substring(start + CONFIG_START.length(), end);

JSONObject object;
Expand All @@ -74,8 +89,8 @@ public static YoutubeVideo getVideo(String videoId) throws YoutubeException, IOE
throw new YoutubeException.BadPageException("Could not parse web page");
}

JSONArray jsonFormats = object.getJSONArray("formats");
JSONArray jsonAdaptiveFormats = object.getJSONArray("adaptiveFormats");
JSONArray jsonFormats = object.containsKey("formats") ? object.getJSONArray("formats") : new JSONArray();
JSONArray jsonAdaptiveFormats = object.containsKey("adaptiveFormats") ? object.getJSONArray("adaptiveFormats") : new JSONArray();

List<Format> formats = new ArrayList<>(jsonAdaptiveFormats.size() + jsonFormats.size());
int i;
Expand All @@ -91,11 +106,11 @@ public static YoutubeVideo getVideo(String videoId) throws YoutubeException, IOE
try {
JSONObject json = jsonAdaptiveFormats.getJSONObject(i);
String mimeType = json.getString("mimeType");
if (mimeType.contains(Constants.AUDIO))
if (mimeType.contains(AUDIO))
formats.add(new AudioFormat(json));
else if (mimeType.contains(Constants.VIDEO))
else if (mimeType.contains(VIDEO))
formats.add(new VideoFormat(json));
} catch (NullPointerException e) {
} catch (Exception e) {
e.printStackTrace();
}
}
Expand All @@ -104,7 +119,10 @@ else if (mimeType.contains(Constants.VIDEO))
}

private static String loadPage(String url) throws IOException {
URLConnection connection = new URL(url).openConnection();
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36");
connection.setRequestProperty("Accept-Language", "en-US,en;");

BufferedReader in = new BufferedReader(new InputStreamReader(
connection.getInputStream()));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand All @@ -21,7 +21,7 @@
*/

public class YoutubeException extends Exception {
public YoutubeException(String message) {
private YoutubeException(String message) {
super(message);
}

Expand All @@ -47,5 +47,11 @@ public FormatNotFoundException(String message) {

}

public static class LiveVideoException extends YoutubeException {

public LiveVideoException(String message) {
super(message);
}

}
}
22 changes: 22 additions & 0 deletions src/main/java/com/github/kiulian/downloader/model/Extension.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.github.kiulian.downloader.model;

public class Extension {

public static final Extension MP4 = new Extension("mp4");
public static final Extension WEBM = new Extension("webm");
public static final Extension THREEGP = new Extension("3gp");
public static final Extension FLV = new Extension("flv");
public static final Extension HLS = new Extension("hls");
public static final Extension M4A = new Extension("m4a");
public static final Extension UNKNOWN = new Extension("unknown");

private final String value;

private Extension(String value) {
this.value = value;
}

public String value() {
return value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand All @@ -24,6 +24,7 @@
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class VideoDetails {
Expand All @@ -47,7 +48,7 @@ public VideoDetails(String videoId) {
public void setDetails(JSONObject json) {
title = json.getString("title");
lengthSeconds = json.getInteger("lengthSeconds");
keywords = json.getJSONArray("keywords").toJavaList(String.class);
keywords = json.containsKey("keywords") ? json.getJSONArray("keywords").toJavaList(String.class) : Collections.emptyList();
shortDescription = json.getString("shortDescription");
JSONArray jsonThumbnails = json.getJSONObject("thumbnail").getJSONArray("thumbnails");
thumbnails = new ArrayList<>(jsonThumbnails.size());
Expand Down Expand Up @@ -78,7 +79,7 @@ public List<String> keywords() {
return keywords;
}

public String shortDescription() {
public String description() {
return shortDescription;
}

Expand All @@ -94,11 +95,11 @@ public int viewCount() {
return viewCount;
}

public int getAverageRating() {
public int averageRating() {
return averageRating;
}

public boolean liveContent() {
public boolean isLive() {
return isLiveContent;
}
}
Loading

0 comments on commit 5db11ee

Please sign in to comment.