Skip to content

Commit

Permalink
add DownloadManager to download directly with the actual title
Browse files Browse the repository at this point in the history
  • Loading branch information
tastelessjolt committed Jul 23, 2018
1 parent 58100b7 commit 56b4a4a
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 46 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 13 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,25 @@ android {
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

// ndk {
// abiFilters 'x86', 'armeabi-v7a'
// }
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
buildToolsVersion '27.0.1'
buildToolsVersion '27.0.3'
// externalNativeBuild {
// cmake {
// path '../cpp/CMakeLists.txt'
// }
// }


}

dependencies {
Expand All @@ -31,7 +42,7 @@ dependencies {
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
}
repositories {
mavenCentral()
Expand Down
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package="me.harshithgoka.youtubedl">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<application
android:allowBackup="true"
Expand Down
5 changes: 3 additions & 2 deletions app/src/main/java/me/harshithgoka/youtubedl/Format.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@
*/

public class Format implements Parcelable {
public String title;
public int itag;
public String url;
public String quality;
public String type;

public Format () {

public Format (String title) {
this.title = title;
}

protected Format(Parcel in) {
Expand Down
68 changes: 30 additions & 38 deletions app/src/main/java/me/harshithgoka/youtubedl/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package me.harshithgoka.youtubedl;

import android.annotation.TargetApi;
import android.app.DownloadManager;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
Expand Down Expand Up @@ -30,6 +31,8 @@
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
Expand Down Expand Up @@ -133,10 +136,22 @@ private void println (String s) {
log.append(s + "\n");
}

public String preprocess (String s) {
int index = s.lastIndexOf("#");
if (index > 0) {
s = s.substring(0, index);
}

s = s.replaceFirst("m.youtube.com", "www.youtube.com");

return s;
}

public void startPoint(View button) {
String url = urlEdit.getText().toString();

url = preprocess(url);

println("Url: " + url);

AsyncTask<String, Void, List<Format>> asyncTask = new GetInfoAsyncTask(getApplicationContext(), webView);
Expand Down Expand Up @@ -203,44 +218,8 @@ public JSInterpreter parseSigJs(String response) {
// (r'(["\'])signature\1\s*,\s*(?P<sig>[a-zA-Z0-9$]+)\(',
// r'\.sig\|\|(?P<sig>[a-zA-Z0-9$]+)\('),


// TODO: extract actual js function from script using some JS interpretor library
// webView.loadUrl("javascript:" + response);
// webView.evaluateJavascript(func_name, new ValueCallback<String>() {
// @Override
// public void onReceiveValue(String s) {
// Log.d("Result", s);
// }
// });


JSInterpreter jsInterpreter = new JSInterpreter(response);
return jsInterpreter;

// org.mozilla.javascript.Context rhino = org.mozilla.javascript.Context.enter();
// //disabling the optimizer to better support Android.
// rhino.setOptimizationLevel(-1);
//
// try {
//
// Scriptable scope = rhino.initStandardObjects();
//
// /**
// * evaluateString(Scriptable scope, java.lang.String source, java.lang.String sourceName,
// * int lineno, java.lang.Object securityDomain)
// *
// */
// rhino.evaluateString(scope, response, "JavaScript", 1, null);
//
//
// Function function = (Function) scope.get("evaluate", scope);
//
// }
// catch (Exception e) {
//
// e.printStackTrace();
// }

}

String signatureCacheId (String sig) {
Expand Down Expand Up @@ -352,12 +331,12 @@ protected List<Format> doInBackground(String... strings) {
ret.put("status", true);

String fmts = ytconfig.getJSONObject("args").getString("url_encoded_fmt_stream_map") + "," + ytconfig.getJSONObject("args").getString("adaptive_fmts");

String title = ytconfig.getJSONObject("args").optString("title", "videoplayback");
String[] fmts_enc = fmts.split(",");
List<Format> formats = new ArrayList<>();

for (String fmt : fmts_enc) {
Format f = new Format();
Format f = new Format(title);

Map<String, String> query_pairs = new LinkedHashMap<String, String>();
String[] pairs = fmt.split("&");
Expand Down Expand Up @@ -438,6 +417,16 @@ else if (params.contains("s")) {
return null;
}

public void download (String url, String name) {
DownloadManager.Request req = new DownloadManager.Request(Uri.parse(url));
req.setTitle(name);
req.allowScanningByMediaScanner();
req.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);

DownloadManager dm = context.getSystemService(DownloadManager.class);
dm.enqueue(req);
}

@Override
protected void onPostExecute(List<Format> formats) {
if (formats != null) {
Expand All @@ -454,6 +443,9 @@ protected void onPostExecute(List<Format> formats) {
clipboard.setPrimaryClip(clip);

Toast.makeText(getApplicationContext(), String.format("Best quality link (%s) copied to Clipboard", formats.get(0).quality), Toast.LENGTH_SHORT).show();

download(finalurl, formats.get(0).title);

}
else {
println("No. of formats: 0");
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
classpath 'com.android.tools.build:gradle:3.1.3'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

// NOTE: Do not place your application dependencies here; they belong
Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Tue Dec 05 21:04:56 IST 2017
#Tue Jul 17 18:10:43 IST 2018
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip

0 comments on commit 56b4a4a

Please sign in to comment.