Skip to content

Commit 1b17c43

Browse files
Add files via upload
1 parent 3880ba1 commit 1b17c43

36 files changed

+1027
-0
lines changed

app/build.gradle

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
plugins {
2+
id 'com.android.application'
3+
}
4+
5+
android {
6+
namespace 'com.example.youtubedownloader'
7+
compileSdk 33
8+
9+
defaultConfig {
10+
applicationId "com.example.youtubedownloader"
11+
minSdk 24
12+
targetSdk 33
13+
versionCode 1
14+
versionName "1.0"
15+
16+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
17+
}
18+
19+
buildTypes {
20+
release {
21+
minifyEnabled false
22+
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
23+
}
24+
}
25+
compileOptions {
26+
sourceCompatibility JavaVersion.VERSION_1_8
27+
targetCompatibility JavaVersion.VERSION_1_8
28+
}
29+
}
30+
31+
dependencies {
32+
implementation 'androidx.appcompat:appcompat:1.6.1'
33+
implementation 'com.google.android.material:material:1.5.0'
34+
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
35+
implementation 'com.github.HaarigerHarald:android-youtubeExtractor:master-SNAPSHOT'
36+
implementation 'com.github.bumptech.glide:glide:4.15.1'
37+
testImplementation 'junit:junit:4.13.2'
38+
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
39+
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
40+
}

app/proguard-rules.pro

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.example.youtubedownloader;
2+
3+
import android.content.Context;
4+
5+
import androidx.test.platform.app.InstrumentationRegistry;
6+
import androidx.test.ext.junit.runners.AndroidJUnit4;
7+
8+
import org.junit.Test;
9+
import org.junit.runner.RunWith;
10+
11+
import static org.junit.Assert.*;
12+
13+
/**
14+
* Instrumented test, which will execute on an Android device.
15+
*
16+
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
17+
*/
18+
@RunWith(AndroidJUnit4.class)
19+
public class ExampleInstrumentedTest {
20+
@Test
21+
public void useAppContext() {
22+
// Context of the app under test.
23+
Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
24+
assertEquals("com.example.youtubedownloader", appContext.getPackageName());
25+
}
26+
}

app/src/main/AndroidManifest.xml

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:tools="http://schemas.android.com/tools">
4+
5+
<uses-permission android:name="android.permission.INTERNET"/>
6+
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
7+
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
8+
9+
<application
10+
android:allowBackup="true"
11+
android:dataExtractionRules="@xml/data_extraction_rules"
12+
android:fullBackupContent="@xml/backup_rules"
13+
android:icon="@mipmap/ic_launcher"
14+
android:label="@string/app_name"
15+
android:roundIcon="@mipmap/ic_launcher_round"
16+
android:supportsRtl="true"
17+
android:theme="@style/Theme.YouTubeDownloader"
18+
tools:targetApi="31">
19+
<activity
20+
android:name=".MainActivity"
21+
android:exported="true">
22+
<intent-filter>
23+
<action android:name="android.intent.action.MAIN" />
24+
25+
<category android:name="android.intent.category.LAUNCHER" />
26+
</intent-filter>
27+
</activity>
28+
</application>
29+
30+
</manifest>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
package com.example.youtubedownloader;
2+
3+
import android.annotation.SuppressLint;
4+
import android.app.DownloadManager;
5+
import android.content.Context;
6+
import android.net.Uri;
7+
import android.os.Bundle;
8+
import android.os.Environment;
9+
import android.util.SparseArray;
10+
import android.view.View;
11+
import android.widget.ImageView;
12+
import android.widget.TextView;
13+
import android.widget.Toast;
14+
15+
import androidx.appcompat.app.AppCompatActivity;
16+
import androidx.recyclerview.widget.RecyclerView;
17+
18+
import com.bumptech.glide.Glide;
19+
import com.google.android.material.appbar.MaterialToolbar;
20+
import com.google.android.material.button.MaterialButton;
21+
import com.google.android.material.textfield.TextInputEditText;
22+
23+
import java.text.MessageFormat;
24+
import java.util.Objects;
25+
26+
import at.huber.youtubeExtractor.VideoMeta;
27+
import at.huber.youtubeExtractor.YouTubeExtractor;
28+
import at.huber.youtubeExtractor.YtFile;
29+
30+
public class MainActivity extends AppCompatActivity {
31+
32+
@Override
33+
protected void onCreate(Bundle savedInstanceState) {
34+
super.onCreate(savedInstanceState);
35+
setContentView(R.layout.activity_main);
36+
37+
MaterialToolbar toolbar = findViewById(R.id.toolbar);
38+
setSupportActionBar(toolbar);
39+
40+
MaterialButton download = findViewById(R.id.download);
41+
TextInputEditText editText = findViewById(R.id.urlET);
42+
ImageView imageView = findViewById(R.id.thumbnail);
43+
TextView title = findViewById(R.id.titleTV);
44+
TextView channelName = findViewById(R.id.channelName);
45+
TextView videoLength = findViewById(R.id.videoLength);
46+
TextView viewCount = findViewById(R.id.viewCount);
47+
RecyclerView recyclerView = findViewById(R.id.recycler);
48+
49+
download.setOnClickListener(new View.OnClickListener() {
50+
@Override
51+
@SuppressLint("StaticFieldLeak")
52+
public void onClick(View view) {
53+
new YouTubeExtractor(MainActivity.this) {
54+
@Override
55+
public void onExtractionComplete(SparseArray<YtFile> files, VideoMeta videoMeta) {
56+
if (files == null) {
57+
Toast.makeText(MainActivity.this, "There was an error while extracting video", Toast.LENGTH_SHORT).show();
58+
return;
59+
}
60+
title.setText(MessageFormat.format("Title: {0}", videoMeta.getTitle()));
61+
channelName.setText(MessageFormat.format("Channel Name: {0}", videoMeta.getAuthor()));
62+
videoLength.setText(MessageFormat.format("Video Duration: {0}", getDuration(videoMeta.getVideoLength())));
63+
viewCount.setText(MessageFormat.format("Views Count: {0}", videoMeta.getViewCount()));
64+
Glide.with(getApplicationContext()).load(videoMeta.getMaxResImageUrl().replace("http","https")).fitCenter().into(imageView);
65+
66+
SparseArray<YtFile> array = new SparseArray<>();
67+
68+
for (int i = 0; i < files.size(); i++) {
69+
YtFile ytFile = files.get(files.keyAt(i));
70+
71+
if (ytFile.getFormat().getHeight() == -1 || ytFile.getFormat().getHeight() >= 360) {
72+
array.put(files.keyAt(i), ytFile);
73+
}
74+
}
75+
76+
ResultAdapter adapter = new ResultAdapter(MainActivity.this, array);
77+
recyclerView.setAdapter(adapter);
78+
79+
adapter.setOnItemClickListener(new ResultAdapter.OnItemClickListener() {
80+
@Override
81+
public void onClick(YtFile ytFile) {
82+
String filename = videoMeta.getTitle() + "." + ytFile.getFormat().getExt();
83+
filename = filename.replaceAll("[\\\\><\"|*?%:#/]", "");
84+
download(ytFile.getUrl(), videoMeta.getTitle(), filename);
85+
}
86+
});
87+
}
88+
}.extract(Objects.requireNonNull(editText.getText()).toString());
89+
}
90+
});
91+
}
92+
93+
public String getDuration(long duration) {
94+
final int MINUTES_IN_AN_HOUR = 60;
95+
final int SECONDS_IN_A_MINUTE = 60;
96+
97+
int seconds = (int) (duration % SECONDS_IN_A_MINUTE);
98+
int totalMinutes = (int) (duration / SECONDS_IN_A_MINUTE);
99+
int minutes = totalMinutes % MINUTES_IN_AN_HOUR;
100+
int hours = totalMinutes / MINUTES_IN_AN_HOUR;
101+
102+
return hours + ":" + minutes + ":" + seconds;
103+
}
104+
105+
private void download(String youtubeDlUrl, String downloadTitle, String fileName) {
106+
Uri uri = Uri.parse(youtubeDlUrl);
107+
DownloadManager.Request request = new DownloadManager.Request(uri);
108+
request.setTitle(downloadTitle);
109+
110+
request.allowScanningByMediaScanner();
111+
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
112+
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName);
113+
114+
DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
115+
manager.enqueue(request);
116+
Toast.makeText(this, "Downloading...", Toast.LENGTH_SHORT).show();
117+
}
118+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.example.youtubedownloader;
2+
3+
import android.content.Context;
4+
import android.util.SparseArray;
5+
import android.view.LayoutInflater;
6+
import android.view.View;
7+
import android.view.ViewGroup;
8+
import android.widget.TextView;
9+
10+
import androidx.annotation.NonNull;
11+
import androidx.recyclerview.widget.RecyclerView;
12+
13+
import com.google.android.material.button.MaterialButton;
14+
15+
import at.huber.youtubeExtractor.YtFile;
16+
17+
public class ResultAdapter extends RecyclerView.Adapter<ResultAdapter.ViewHolder> {
18+
Context context;
19+
SparseArray<YtFile> files;
20+
OnItemClickListener onItemClickListener;
21+
22+
public ResultAdapter(Context context, SparseArray<YtFile> files) {
23+
this.context = context;
24+
this.files = files;
25+
}
26+
27+
@NonNull
28+
@Override
29+
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
30+
View view = LayoutInflater.from(context).inflate(R.layout.result_list_item, parent, false);
31+
return new ViewHolder(view);
32+
}
33+
34+
@Override
35+
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
36+
holder.res.setText(String.valueOf(files.get(files.keyAt(position)).getFormat().getHeight()));
37+
holder.ext.setText(String.valueOf(files.get(files.keyAt(position)).getFormat().getExt()));
38+
holder.download.setOnClickListener(view -> holder.itemView.performClick());
39+
holder.itemView.setOnClickListener(view -> onItemClickListener.onClick(files.get(files.keyAt(position))));
40+
}
41+
42+
@Override
43+
public int getItemCount() {
44+
return files.size();
45+
}
46+
47+
public static class ViewHolder extends RecyclerView.ViewHolder {
48+
TextView res, ext;
49+
MaterialButton download;
50+
public ViewHolder(@NonNull View itemView) {
51+
super(itemView);
52+
res = itemView.findViewById(R.id.list_item_res);
53+
ext = itemView.findViewById(R.id.list_item_ext);
54+
download = itemView.findViewById(R.id.list_item_download);
55+
}
56+
}
57+
58+
public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
59+
this.onItemClickListener = onItemClickListener;
60+
}
61+
62+
public interface OnItemClickListener {
63+
void onClick(YtFile ytFile);
64+
}
65+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
2+
xmlns:aapt="http://schemas.android.com/aapt"
3+
android:width="108dp"
4+
android:height="108dp"
5+
android:viewportWidth="108"
6+
android:viewportHeight="108">
7+
<path android:pathData="M31,63.928c0,0 6.4,-11 12.1,-13.1c7.2,-2.6 26,-1.4 26,-1.4l38.1,38.1L107,108.928l-32,-1L31,63.928z">
8+
<aapt:attr name="android:fillColor">
9+
<gradient
10+
android:endX="85.84757"
11+
android:endY="92.4963"
12+
android:startX="42.9492"
13+
android:startY="49.59793"
14+
android:type="linear">
15+
<item
16+
android:color="#44000000"
17+
android:offset="0.0" />
18+
<item
19+
android:color="#00000000"
20+
android:offset="1.0" />
21+
</gradient>
22+
</aapt:attr>
23+
</path>
24+
<path
25+
android:fillColor="#FFFFFF"
26+
android:fillType="nonZero"
27+
android:pathData="M65.3,45.828l3.8,-6.6c0.2,-0.4 0.1,-0.9 -0.3,-1.1c-0.4,-0.2 -0.9,-0.1 -1.1,0.3l-3.9,6.7c-6.3,-2.8 -13.4,-2.8 -19.7,0l-3.9,-6.7c-0.2,-0.4 -0.7,-0.5 -1.1,-0.3C38.8,38.328 38.7,38.828 38.9,39.228l3.8,6.6C36.2,49.428 31.7,56.028 31,63.928h46C76.3,56.028 71.8,49.428 65.3,45.828zM43.4,57.328c-0.8,0 -1.5,-0.5 -1.8,-1.2c-0.3,-0.7 -0.1,-1.5 0.4,-2.1c0.5,-0.5 1.4,-0.7 2.1,-0.4c0.7,0.3 1.2,1 1.2,1.8C45.3,56.528 44.5,57.328 43.4,57.328L43.4,57.328zM64.6,57.328c-0.8,0 -1.5,-0.5 -1.8,-1.2s-0.1,-1.5 0.4,-2.1c0.5,-0.5 1.4,-0.7 2.1,-0.4c0.7,0.3 1.2,1 1.2,1.8C66.5,56.528 65.6,57.328 64.6,57.328L64.6,57.328z"
28+
android:strokeWidth="1"
29+
android:strokeColor="#00000000" />
30+
</vector>

0 commit comments

Comments
 (0)