Skip to content

Commit

Permalink
Merge pull request yausername#224 from kadwanev/update-channel-select
Browse files Browse the repository at this point in the history
Allow update from nightly or stable channel
  • Loading branch information
xibr authored Apr 11, 2023
2 parents 18b4985 + b56d8e6 commit 6d4dcf8
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 25 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,9 @@ try {

* yt-dlp supports myriad different options which be seen [here](https://github.com/yt-dlp/yt-dlp)

* yt-dlp binary can be updated from within the library
* yt-dlp binary can be updated from within the library (A example can be found in the [sample app](app/src/main/java/com/yausername/youtubedl_android_example/MainActivity.java))
```java
YoutubeDL.getInstance().updateYoutubeDL(this);
YoutubeDL.getInstance().updateYoutubeDL(this, updateChannel); // UpdateChannel.NIGHTLY or UpdateChannel.STABLE
```

## FFmpeg
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.yausername.youtubedl_android_example;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
Expand Down Expand Up @@ -28,7 +29,7 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
private ProgressBar progressBar;

private boolean updating = false;
private CompositeDisposable compositeDisposable = new CompositeDisposable();
private final CompositeDisposable compositeDisposable = new CompositeDisposable();

private static final String TAG = "MainActivity";

Expand Down Expand Up @@ -81,39 +82,45 @@ public void onClick(View v) {
break;
}
case R.id.btn_update: {
updateYoutubeDL();
AlertDialog dialog = new AlertDialog.Builder(this)
.setTitle("Update Channel")
.setItems(new String[]{"Stable Releases", "Nightly Releases"},
(dialogInterface, which) -> updateYoutubeDL(which == 0
? YoutubeDL.UpdateChannel.STABLE : YoutubeDL.UpdateChannel.NIGHTLY))
.create();
dialog.show();
break;
}
}
}

private void updateYoutubeDL() {
private void updateYoutubeDL(YoutubeDL.UpdateChannel updateChannel) {
if (updating) {
Toast.makeText(MainActivity.this, "update is already in progress", Toast.LENGTH_LONG).show();
Toast.makeText(MainActivity.this, "Update is already in progress!", Toast.LENGTH_LONG).show();
return;
}

updating = true;
progressBar.setVisibility(View.VISIBLE);
Disposable disposable = Observable.fromCallable(() -> YoutubeDL.getInstance().updateYoutubeDL(getApplication()))
Disposable disposable = Observable.fromCallable(() -> YoutubeDL.getInstance().updateYoutubeDL(this, updateChannel))
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(status -> {
progressBar.setVisibility(View.GONE);
switch (status) {
case DONE:
Toast.makeText(MainActivity.this, "update successful", Toast.LENGTH_LONG).show();
Toast.makeText(MainActivity.this, "Update successful " + YoutubeDL.getInstance().versionName(this), Toast.LENGTH_LONG).show();
break;
case ALREADY_UP_TO_DATE:
Toast.makeText(MainActivity.this, "already up to date", Toast.LENGTH_LONG).show();
Toast.makeText(MainActivity.this, "Already up to date " + YoutubeDL.getInstance().versionName(this), Toast.LENGTH_LONG).show();
break;
default:
Toast.makeText(MainActivity.this, status.toString(), Toast.LENGTH_LONG).show();
break;
}
updating = false;
}, e -> {
if(BuildConfig.DEBUG) Log.e(TAG, "failed to update", e);
if (BuildConfig.DEBUG) Log.e(TAG, "failed to update", e);
progressBar.setVisibility(View.GONE);
Toast.makeText(MainActivity.this, "update failed", Toast.LENGTH_LONG).show();
updating = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,10 +266,10 @@ public YoutubeDLResponse execute(YoutubeDLRequest request, @Nullable String proc
return youtubeDLResponse;
}

synchronized public UpdateStatus updateYoutubeDL(Context appContext) throws YoutubeDLException {
synchronized public UpdateStatus updateYoutubeDL(Context appContext, UpdateChannel updateChannel) throws YoutubeDLException {
assertInit();
try {
return YoutubeDLUpdater.update(appContext);
return YoutubeDLUpdater.update(appContext, updateChannel);
} catch (IOException e) {
throw new YoutubeDLException("failed to update youtube-dl", e);
}
Expand All @@ -280,7 +280,16 @@ public String version(Context appContext) {
return YoutubeDLUpdater.version(appContext);
}

@Nullable
public String versionName(Context appContext) {
return YoutubeDLUpdater.versionName(appContext);
}

public enum UpdateStatus {
DONE, ALREADY_UP_TO_DATE
}

public enum UpdateChannel {
STABLE, NIGHTLY
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.yausername.youtubedl_android.YoutubeDL.UpdateStatus;
import com.yausername.youtubedl_android.YoutubeDL.UpdateChannel;
import com.yausername.youtubedl_common.SharedPrefsHelper;

import org.apache.commons.io.FileUtils;
Expand All @@ -21,17 +22,20 @@ class YoutubeDLUpdater {
private YoutubeDLUpdater() {
}

private static final String releasesUrl = "https://api.github.com/repos/yt-dlp/yt-dlp/releases/latest";
private static final String youtubeDLVersionKey = "youtubeDLVersion";
private static final String youtubeDLStableChannelUrl = "https://api.github.com/repos/yt-dlp/yt-dlp/releases/latest";
private static final String youtubeDLNightlyChannelUrl = "https://api.github.com/repos/yt-dlp/yt-dlp-nightly-builds/releases/latest";
private static final String dlpBinaryName = "yt-dlp";
private static final String dlpVersionKey = "dlpVersion";
private static final String dlpVersionNameKey = "dlpVersionName";

static UpdateStatus update(Context appContext) throws IOException, YoutubeDLException {
JsonNode json = checkForUpdate(appContext);
static UpdateStatus update(Context appContext, UpdateChannel youtubeDLChannel) throws IOException, YoutubeDLException {
JsonNode json = checkForUpdate(appContext, youtubeDLChannel);
if (null == json) return UpdateStatus.ALREADY_UP_TO_DATE;

String downloadUrl = getDownloadUrl(json);
File file = download(appContext, downloadUrl);
File ytdlpDir = getYoutubeDLDir(appContext);
File binary = new File(ytdlpDir, "yt-dlp");
File binary = new File(ytdlpDir, dlpBinaryName);

try {
/* purge older version */
Expand All @@ -49,19 +53,20 @@ static UpdateStatus update(Context appContext) throws IOException, YoutubeDLExce
file.delete();
}

updateSharedPrefs(appContext, getTag(json));
updateSharedPrefs(appContext, getTag(json), getName(json));
return UpdateStatus.DONE;
}

private static void updateSharedPrefs(Context appContext, String tag) {
SharedPrefsHelper.update(appContext, youtubeDLVersionKey, tag);
private static void updateSharedPrefs(Context appContext, String tag, String name) {
SharedPrefsHelper.update(appContext, dlpVersionKey, tag);
SharedPrefsHelper.update(appContext, dlpVersionNameKey, name);
}

private static JsonNode checkForUpdate(Context appContext) throws IOException {
URL url = new URL(releasesUrl);
private static JsonNode checkForUpdate(Context appContext, UpdateChannel youtubeDLChannel) throws IOException {
URL url = new URL((youtubeDLChannel == UpdateChannel.NIGHTLY) ? youtubeDLNightlyChannelUrl : youtubeDLStableChannelUrl);
JsonNode json = YoutubeDL.objectMapper.readTree(url);
String newVersion = getTag(json);
String oldVersion = SharedPrefsHelper.get(appContext, youtubeDLVersionKey);
String oldVersion = SharedPrefsHelper.get(appContext, dlpVersionKey);
if (newVersion.equals(oldVersion)) {
return null;
}
Expand All @@ -72,6 +77,10 @@ private static String getTag(JsonNode json) {
return json.get("tag_name").asText();
}

private static String getName(JsonNode json) {
return json.get("name").asText();
}

@NonNull
private static String getDownloadUrl(@NonNull JsonNode json) throws YoutubeDLException {
ArrayNode assets = (ArrayNode) json.get("assets");
Expand All @@ -89,7 +98,7 @@ private static String getDownloadUrl(@NonNull JsonNode json) throws YoutubeDLExc
@NonNull
private static File download(Context appContext, String url) throws IOException {
URL downloadUrl = new URL(url);
File file = File.createTempFile("yt-dlp", null, appContext.getCacheDir());
File file = File.createTempFile(dlpBinaryName, null, appContext.getCacheDir());
FileUtils.copyURLToFile(downloadUrl, file, 5000, 10000);
return file;
}
Expand All @@ -102,7 +111,12 @@ private static File getYoutubeDLDir(Context appContext) {

@Nullable
static String version(Context appContext) {
return SharedPrefsHelper.get(appContext, youtubeDLVersionKey);
return SharedPrefsHelper.get(appContext, dlpVersionKey);
}

@Nullable
static String versionName(Context appContext) {
return SharedPrefsHelper.get(appContext, dlpVersionNameKey);
}

}

0 comments on commit 6d4dcf8

Please sign in to comment.