Skip to content

Commit

Permalink
add thumbnail_url,img; fix formats size bug causing random crash; uni…
Browse files Browse the repository at this point in the history
…que history
  • Loading branch information
tastelessjolt committed Sep 16, 2018
1 parent 6c9ce02 commit 14c047d
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 9 deletions.
3 changes: 3 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ dependencies {

implementation 'com.github.tony19:named-regexp:0.2.5'
implementation 'com.google.code.gson:gson:2.8.5'

implementation 'com.github.bumptech.glide:glide:4.8.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.8.0'
}
repositories {
mavenCentral()
Expand Down
5 changes: 4 additions & 1 deletion app/src/main/java/me/harshithgoka/youtubedl/Extractor.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@

import java.io.IOException;
import java.net.URLDecoder;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
Expand Down Expand Up @@ -242,6 +244,7 @@ public VideoInfo getFormats(String you_url) {
length = args.getString("length_seconds");
view_count = args.getString("view_count");
author = args.getString("author");
thumbnail_url = args.optString("thumbnail_url");

String[] fmts_enc = fmts.split(",");
List<Format> formats = new ArrayList<>();
Expand Down Expand Up @@ -313,7 +316,7 @@ else if (params.contains("s")) {
formats.add(f);
}

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

} catch (IllegalStateException e) {
Expand Down
15 changes: 12 additions & 3 deletions app/src/main/java/me/harshithgoka/youtubedl/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -313,8 +313,9 @@ public void hideLoading() {

void loadVideoInfo(VideoInfo videoInfo) {
Log.d("II", "Loading videoInfo");
int numRemoved = formats.size();
formats.clear();
formatAdapter.notifyItemRangeRemoved(0, videoInfo.formats.size());
formatAdapter.notifyItemRangeRemoved(0, numRemoved);
formats.addAll(videoInfo.formats);
formatAdapter.notifyItemRangeInserted(0, videoInfo.formats.size());
videoTitle.setText(videoInfo.title);
Expand Down Expand Up @@ -358,8 +359,16 @@ protected void onPostExecute(VideoInfo videoInfo) {

List<Format> formats = videoInfo.formats;
if (formats.size() > 0) {
history.add(0, videoInfo);
viAdapter.notifyItemInserted(0);
int index;
if ((index = history.indexOf(videoInfo)) != -1) {
history.remove(index);
history.add(0, videoInfo);
viAdapter.notifyItemMoved(index, 0);
}
else {
history.add(0, videoInfo);
viAdapter.notifyItemInserted(0);
}
loadVideoInfo(videoInfo);

String finalurl = formats.get(0).url;
Expand Down
20 changes: 19 additions & 1 deletion app/src/main/java/me/harshithgoka/youtubedl/VideoInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
import android.os.Parcel;
import android.os.Parcelable;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Locale;

Expand All @@ -15,9 +18,10 @@ public class VideoInfo {
public int length;
public int view_count;
public List<Format> formats;
public Timestamp last_updated;


VideoInfo(String id, String title, String length, String view_count, String author, String thumbnail_url, List<Format> formats) {
VideoInfo(String id, String title, String length, String view_count, String author, String thumbnail_url, Timestamp last_updated, List<Format> formats) {
this.video_id = id;
this.title = title;
this.author = author;
Expand All @@ -40,9 +44,23 @@ public class VideoInfo {
this.thumbnail_url = thumbnail_url;
this.formats = new ArrayList<>();
this.formats.addAll(formats);

this.last_updated = last_updated;
}

public String getUrl() {
return String.format(Locale.UK, "https://www.youtube.com/watch?v=%s", video_id);
}

public boolean isEquivalent(VideoInfo v1) {
return video_id.equals(v1.video_id);
}

@Override
public boolean equals(Object obj) {
if (obj instanceof VideoInfo) {
return this.isEquivalent((VideoInfo) obj);
}
return false;
}
}
10 changes: 10 additions & 0 deletions app/src/main/java/me/harshithgoka/youtubedl/VideoInfoAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,20 @@
import android.content.ClipboardManager;
import android.content.Context;
import android.net.Uri;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;

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

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

public class VideoInfoAdapter extends RecyclerView.Adapter<VideoInfoAdapter.VideoInfoViewHolder> {
Expand Down Expand Up @@ -41,6 +45,10 @@ public void onBindViewHolder(@NonNull VideoInfoViewHolder holder, int position)
holder.title.setText(videoInfo.title);
holder.videoId.setText(videoInfo.video_id);
holder.author.setText(videoInfo.author);

Glide.with(context)
.load(videoInfo.thumbnail_url)
.into(holder.thumbnail);
}

@Override
Expand All @@ -54,12 +62,14 @@ public int getItemCount() {
class VideoInfoViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView title, videoId, author;
View copyLink;
AppCompatImageView thumbnail;
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);
thumbnail = itemView.findViewById(R.id.thumb);
copyLink.setOnClickListener(this);
itemView.setOnClickListener(this);
}
Expand Down
19 changes: 16 additions & 3 deletions app/src/main/res/layout/video_info_item.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:text="Title"
android:textAppearance="@style/TextAppearance.AppCompat.Headline"
android:textAppearance="@style/TextAppearance.AppCompat.SearchResult.Title"
app:layout_constraintEnd_toStartOf="@+id/copyLink"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintStart_toEndOf="@+id/thumb"
app:layout_constraintTop_toTopOf="parent" />

<View
Expand Down Expand Up @@ -66,11 +66,24 @@
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="Author"
android:textAppearance="@style/TextAppearance.AppCompat.SearchResult.Subtitle"
android:textColor="@color/colorPrimaryDark"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/videoId"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintStart_toEndOf="@+id/thumb"
app:layout_constraintTop_toBottomOf="@+id/video_title" />

<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/thumb"
android:layout_width="90dp"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

</com.google.android.material.card.MaterialCardView>
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.2.0-rc02'
classpath 'com.android.tools.build:gradle:3.2.0-rc03'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down

0 comments on commit 14c047d

Please sign in to comment.