mirror of
https://github.com/zoriya/Opus.git
synced 2025-12-06 06:26:15 +00:00
youtube playlist downloader
This commit is contained in:
@@ -4,18 +4,22 @@ using Android.OS;
|
||||
using Android.Provider;
|
||||
using Android.Support.V4.App;
|
||||
using System.IO;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using YoutubeExplode;
|
||||
using YoutubeExplode.Models;
|
||||
using YoutubeExplode.Models.MediaStreams;
|
||||
using Console = System.Console;
|
||||
using File = System.IO.File;
|
||||
using MusicApp.Resources.values;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MusicApp.Resources.Portable_Class
|
||||
{
|
||||
[Service]
|
||||
public class Downloader : Service
|
||||
{
|
||||
private static bool isDownloading = false;
|
||||
private NotificationCompat.Builder notification;
|
||||
private int notificationID = 1001;
|
||||
|
||||
@@ -42,6 +46,10 @@ namespace MusicApp.Resources.Portable_Class
|
||||
|
||||
private async void DownloadAudio(string videoID, string path, string name)
|
||||
{
|
||||
while (isDownloading)
|
||||
await Task.Delay(1000);
|
||||
|
||||
isDownloading = true;
|
||||
CreateNotification("Downloading: ", name);
|
||||
|
||||
YoutubeClient client = new YoutubeClient();
|
||||
@@ -58,6 +66,7 @@ namespace MusicApp.Resources.Portable_Class
|
||||
FileStream output = File.Create(filePath);
|
||||
await input.CopyToAsync(output);
|
||||
output.Dispose();
|
||||
isDownloading = false;
|
||||
|
||||
SetMetaData(filePath, videoInfo.Title, videoInfo.Author.Name, videoInfo.Thumbnails.HighResUrl);
|
||||
}
|
||||
|
||||
@@ -236,6 +236,33 @@ namespace MusicApp.Resources.Portable_Class
|
||||
}
|
||||
}
|
||||
|
||||
public static async void DownloadFiles(string[] names, string[] videoIDs)
|
||||
{
|
||||
ISharedPreferences prefManager = PreferenceManager.GetDefaultSharedPreferences(Android.App.Application.Context);
|
||||
if (prefManager.GetString("downloadPath", null) != null)
|
||||
{
|
||||
Toast.MakeText(Android.App.Application.Context, "Downloading...", ToastLength.Short).Show();
|
||||
Context context = Android.App.Application.Context;
|
||||
for(int i = 0; i < names.Length; i++)
|
||||
{
|
||||
Intent intent = new Intent(context, typeof(Downloader));
|
||||
intent.PutExtra("videoID", videoIDs[i]);
|
||||
intent.PutExtra("path", prefManager.GetString("downloadPath", null));
|
||||
intent.PutExtra("name", names[i]);
|
||||
context.StartService(intent);
|
||||
await Task.Delay(10000);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Snackbar.Make(MainActivity.instance.FindViewById(Resource.Id.contentView), "Download Path Not Set.", Snackbar.LengthShort).SetAction("Set Path", (v) =>
|
||||
{
|
||||
Intent intent = new Intent(Android.App.Application.Context, typeof(Preferences));
|
||||
MainActivity.instance.StartActivity(intent);
|
||||
}).Show();
|
||||
}
|
||||
}
|
||||
|
||||
public static void RemoveFromPlaylist(string videoID)
|
||||
{
|
||||
youtubeService.PlaylistItems.Delete(videoID).Execute();
|
||||
|
||||
@@ -11,6 +11,7 @@ using Java.Util;
|
||||
using MusicApp.Resources.values;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using System;
|
||||
|
||||
namespace MusicApp.Resources.Portable_Class
|
||||
{
|
||||
@@ -23,7 +24,7 @@ namespace MusicApp.Resources.Portable_Class
|
||||
|
||||
private List<Song> playlists = new List<Song>();
|
||||
private List<Google.Apis.YouTube.v3.Data.Playlist> YtPlaylists = new List<Google.Apis.YouTube.v3.Data.Playlist>();
|
||||
private string[] actions = new string[] { "Random play", "Rename", "Delete" };
|
||||
private string[] actions = new string[] { "Random play", "Rename", "Delete", "Download" };
|
||||
private bool isEmpty = false;
|
||||
|
||||
|
||||
@@ -158,6 +159,9 @@ namespace MusicApp.Resources.Portable_Class
|
||||
case 2:
|
||||
RemovePlaylist(e.Position, playlists[e.Position].GetPath());
|
||||
break;
|
||||
case 3:
|
||||
DownloadPlaylist(playlists[e.Position].GetPath());
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
@@ -180,14 +184,12 @@ namespace MusicApp.Resources.Portable_Class
|
||||
|
||||
foreach (var item in ytPlaylist.Items)
|
||||
{
|
||||
System.Console.WriteLine(item.Snippet.Title);
|
||||
Song song = new Song(item.Snippet.Title, "", item.Snippet.Thumbnails.Default__.Url, -1, -1, item.ContentDetails.VideoId, true, false);
|
||||
tracks.Add(song);
|
||||
}
|
||||
|
||||
nextPageToken = ytPlaylist.NextPageToken;
|
||||
}
|
||||
System.Console.WriteLine("All tracks are retrived");
|
||||
YoutubeEngine.PlayFiles(tracks.ToArray());
|
||||
}
|
||||
|
||||
@@ -249,5 +251,30 @@ namespace MusicApp.Resources.Portable_Class
|
||||
Activity.AddContentView(emptyView, View.LayoutParameters);
|
||||
}
|
||||
}
|
||||
|
||||
private async void DownloadPlaylist(string playlistID)
|
||||
{
|
||||
List<string> names = new List<string>();
|
||||
List<string> videoIDs = new List<string>();
|
||||
string nextPageToken = "";
|
||||
while (nextPageToken != null)
|
||||
{
|
||||
var ytPlaylistRequest = YoutubeEngine.youtubeService.PlaylistItems.List("snippet, contentDetails");
|
||||
ytPlaylistRequest.PlaylistId = playlistID;
|
||||
ytPlaylistRequest.MaxResults = 50;
|
||||
ytPlaylistRequest.PageToken = nextPageToken;
|
||||
|
||||
var ytPlaylist = await ytPlaylistRequest.ExecuteAsync();
|
||||
|
||||
foreach (var item in ytPlaylist.Items)
|
||||
{
|
||||
names.Add(item.Snippet.Title);
|
||||
videoIDs.Add(item.ContentDetails.VideoId);
|
||||
}
|
||||
|
||||
nextPageToken = ytPlaylist.NextPageToken;
|
||||
}
|
||||
YoutubeEngine.DownloadFiles(names.ToArray(), videoIDs.ToArray());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user