Skip to content

Commit

Permalink
Added history; changed colors;
Browse files Browse the repository at this point in the history
  • Loading branch information
tastelessjolt committed Aug 28, 2018
1 parent c0f0c40 commit 6f6f63a
Show file tree
Hide file tree
Showing 31 changed files with 315 additions and 43 deletions.
Binary file added app/src/main/ic_launcher-web.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 22 additions & 7 deletions app/src/main/java/me/harshithgoka/youtubedl/Extractor.java
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,9 @@ public VideoInfo getFormats(String you_url) {
String response;
String video_id = "";
String thumbnail_url = "";
String length = "";
String view_count = "";
String author = "";
JSONObject ret = new JSONObject();

try {
Expand All @@ -209,11 +212,17 @@ public VideoInfo getFormats(String you_url) {
Pattern ytcond_end = Pattern.compile(";[ ]*ytplayer\\.load");
String json = ytcond_end.split(ytconf.split(response)[1])[0];
JSONObject ytconfig = new JSONObject(json);
ret.put ("data", ytconfig);
ret.put("data", ytconfig);
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");
JSONObject args = ytconfig.getJSONObject("args");
String fmts = args.getString("url_encoded_fmt_stream_map") + "," + ytconfig.getJSONObject("args").getString("adaptive_fmts");
String title = args.optString("title", "videoplayback");
video_id = args.getString("video_id");
length = args.getString("length_seconds");
view_count = args.getString("view_count");
author = args.getString("author");

String[] fmts_enc = fmts.split(",");
List<Format> formats = new ArrayList<>();

Expand Down Expand Up @@ -242,7 +251,7 @@ else if (params.contains("s")) {
Pattern playerType = Pattern.compile("(html5player-([^/]+?)(?:/html5player(?:-new)?)?\\.js)|((?:www|player)-([^/]+)(?:/[a-z]{2}_[A-Z]{2})?/base\\.js)");
m = playerType.matcher(player_url);
if (!m.find()) {
Log.d("ERR","Couldn't find Player URL");
Log.d("ERR", "Couldn't find Player URL");
Log.d("ERR", response);
}

Expand All @@ -262,7 +271,7 @@ else if (params.contains("s")) {

f.url = url;

for (String param: params) {
for (String param : params) {
if (param.equals("itag")) {
f.setItag(Integer.parseInt(query_pairs.get(param)));
}
Expand All @@ -279,10 +288,16 @@ else if (params.contains("s")) {
formats.add(f);
}

VideoInfo videoInfo = new VideoInfo(video_id, thumbnail_url, formats);
VideoInfo videoInfo = new VideoInfo(video_id, title, length, view_count, author, thumbnail_url, formats);
return videoInfo;


} catch (IllegalStateException e) {
try {
ret.put("message", e.toString());
} catch (JSONException e1) {
e1.printStackTrace();
}
Log.d("Err", e.toString());
} catch (IOException e) {
try {
ret.put("message", e.toString());
Expand Down
73 changes: 51 additions & 22 deletions app/src/main/java/me/harshithgoka/youtubedl/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
Expand Down Expand Up @@ -54,16 +54,23 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe

Pattern youtubeUrlPattern;

RecyclerView recyclerView;
FormatAdapter adapter;
LinearLayoutManager linearLayoutManager;
RecyclerView formatsRecyclerView;
FormatAdapter formatAdapter;
LinearLayoutManager formatLinearLayoutManager;

RecyclerView viRecyclerView;
VideoInfoAdapter viAdapter;
LinearLayoutManager viLinearLayoutManager;

BottomSheetBehavior<View> bottomSheetBehavior;
List<ProgressBar> progressBars;

SharedPreferences mPrefs;
ArrayList<VideoInfo> history;

TextView videoTitle;


@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
Expand Down Expand Up @@ -115,24 +122,19 @@ protected void onCreate(Bundle savedInstanceState) {

youtubeUrlPattern = Pattern.compile(extractor._VALID_URL);

// Formats Holder Bottom Sheet
bottomSheetBehavior = BottomSheetBehavior.from(findViewById(R.id.bottom_sheet));
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);

recyclerView = findViewById(R.id.recycler_view);
adapter = new FormatAdapter(getApplicationContext(), formats);
recyclerView.setAdapter(adapter);
linearLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(linearLayoutManager);

BottomAppBar bar = (BottomAppBar) findViewById(R.id.bar);
bar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Handle the navigation click by showing a BottomDrawer etc.
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
}
});
videoTitle = findViewById(R.id.video_title);
// Formats
formatsRecyclerView = findViewById(R.id.recycler_view);
formatAdapter = new FormatAdapter(getApplicationContext(), formats);
formatsRecyclerView.setAdapter(formatAdapter);
formatLinearLayoutManager = new LinearLayoutManager(getApplicationContext());
formatsRecyclerView.setLayoutManager(formatLinearLayoutManager);

// History Get Content
mPrefs = getPreferences(MODE_PRIVATE);
Gson gson = new Gson();
String json = mPrefs.getString(HISTORY, "");
Expand All @@ -143,6 +145,23 @@ public void onClick(View v) {
history = new ArrayList<>();
}

// History
viRecyclerView = findViewById(R.id.historyRecyclerView);
viAdapter = new VideoInfoAdapter(this, history);
viRecyclerView.setAdapter(viAdapter);
viLinearLayoutManager = new LinearLayoutManager(getApplicationContext());
viRecyclerView.setLayoutManager(viLinearLayoutManager);


BottomAppBar bar = (BottomAppBar) findViewById(R.id.bar);
bar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Handle the navigation click by showing a BottomDrawer etc.
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
}
});

// ATTENTION: This was auto-generated to handle app links.
Intent appLinkIntent = getIntent();
String appLinkAction = appLinkIntent.getAction();
Expand Down Expand Up @@ -241,6 +260,16 @@ public void hideLoading() {
}
}

void loadVideoInfo(VideoInfo videoInfo) {
Log.d("II", "Loading videoInfo");
formats.clear();
formatAdapter.notifyItemRangeRemoved(0, videoInfo.formats.size());
formats.addAll(videoInfo.formats);
formatAdapter.notifyItemRangeInserted(0, videoInfo.formats.size());
videoTitle.setText(videoInfo.title);
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
}

@Override
public void onClick(View v) {
switch (v.getId()){
Expand Down Expand Up @@ -279,12 +308,12 @@ protected VideoInfo doInBackground(String... strings) {
protected void onPostExecute(VideoInfo videoInfo) {
hideLoading();
if (videoInfo != null) {

List<Format> formats = videoInfo.formats;
history.add(0, videoInfo);
if (formats.size() > 0) {
MainActivity.this.formats.clear();
MainActivity.this.formats.addAll(formats);
MainActivity.this.adapter.notifyDataSetChanged();
history.add(0, videoInfo);
viAdapter.notifyItemInserted(0);
loadVideoInfo(videoInfo);

String finalurl = formats.get(0).url;
println(finalurl);
Expand Down
30 changes: 29 additions & 1 deletion app/src/main/java/me/harshithgoka/youtubedl/VideoInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,44 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

public class VideoInfo {
public String video_id;
public String thumbnail_url;
public String title;
public String author;
public int length;
public int view_count;
public List<Format> formats;

VideoInfo(String id, String thumbnail_url, List<Format> formats) {

VideoInfo(String id, String title, String length, String view_count, String author, String thumbnail_url, List<Format> formats) {
this.video_id = id;
this.title = title;
this.author = author;
try {
this.view_count = Integer.parseInt(view_count);
}
catch (Exception e) {
// do nothing
this.view_count = -1;
}

try {
this.length = Integer.parseInt(length);
}
catch (Exception e) {
// do nothing
this.length = -1;
}

this.thumbnail_url = thumbnail_url;
this.formats = new ArrayList<>();
this.formats.addAll(formats);
}

public String getUrl() {
return String.format(Locale.UK, "https://www.youtube.com/watch?v=%s", video_id);
}
}
93 changes: 93 additions & 0 deletions app/src/main/java/me/harshithgoka/youtubedl/VideoInfoAdapter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package me.harshithgoka.youtubedl;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;
import java.util.Locale;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public class VideoInfoAdapter extends RecyclerView.Adapter<VideoInfoAdapter.VideoInfoViewHolder> {

List<VideoInfo> videoInfos;
Context context;

VideoInfoAdapter(Context context, List<VideoInfo> videoInfos) {
this.context = context;
this.videoInfos = videoInfos;
}

@NonNull
@Override
public VideoInfoViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.video_info_item, parent, false);

return new VideoInfoViewHolder(itemView);
}

@Override
public void onBindViewHolder(@NonNull VideoInfoViewHolder holder, int position) {
VideoInfo videoInfo = videoInfos.get(position);
holder.title.setText(videoInfo.title);
holder.videoId.setText(videoInfo.video_id);
holder.author.setText(videoInfo.author);
}

@Override
public int getItemCount() {
if (videoInfos != null)
return videoInfos.size();
else
return 0;
}

class VideoInfoViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView title, videoId, author;
View copyLink;
public VideoInfoViewHolder(@NonNull View itemView) {
super(itemView);
title = itemView.findViewById(R.id.video_title);
videoId = itemView.findViewById(R.id.videoId);
copyLink = itemView.findViewById(R.id.copyLink);
author = itemView.findViewById(R.id.author);
copyLink.setOnClickListener(this);
itemView.setOnClickListener(this);
}

void copyUrl (VideoInfo videoInfo) {
String url = videoInfo.getUrl();

ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
assert clipboard != null;
ClipData clip = ClipData.newRawUri("DownloadURL", Uri.parse(url));
clipboard.setPrimaryClip(clip);

Toast.makeText(context, String.format(Locale.UK, "Copied Link: %s", url), Toast.LENGTH_SHORT).show();
}

@Override
public void onClick(View v) {
int pos = getAdapterPosition();
VideoInfo videoInfo = videoInfos.get(pos);

switch (v.getId()) {
case R.id.copyLink:
copyUrl(videoInfo);
break;
case R.id.videoInfo:
((MainActivity) context).loadVideoInfo(videoInfo);
break;
}
}
}
}
22 changes: 18 additions & 4 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
android:layout_height="match_parent">

<TextView
android:id="@+id/textView"
Expand Down Expand Up @@ -49,7 +48,7 @@
android:id="@+id/url"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ems="10"
android:textSize="20sp"
android:hint="@string/url_hint"
android:inputType="textUri"
android:text="@string/example_url" />
Expand All @@ -68,14 +67,28 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textInputLayout" />

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/historyRecyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />

</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>

<com.google.android.material.bottomappbar.BottomAppBar
android:id="@+id/bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_height="48dp"
android:layout_gravity="bottom"
android:elevation="10dp"
android:translationZ="10dp"
app:backgroundTint="@color/colorPrimary"
app:navigationIcon="@drawable/ic_menu_24" />

Expand Down Expand Up @@ -111,4 +124,5 @@


<include layout="@layout/formats_layout"/>

</androidx.coordinatorlayout.widget.CoordinatorLayout>
Loading

0 comments on commit 6f6f63a

Please sign in to comment.