Skip to content
This repository has been archived by the owner on Jan 19, 2022. It is now read-only.

Commit

Permalink
Removes need for Google API
Browse files Browse the repository at this point in the history
  • Loading branch information
gfrn committed Jun 13, 2021
1 parent 26c1f1a commit 360c3c6
Show file tree
Hide file tree
Showing 27 changed files with 152 additions and 78,950 deletions.
12 changes: 10 additions & 2 deletions WPFMETRO/App.config
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8" ?>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<section name="WPFMETRO.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
</configSections>
Expand Down Expand Up @@ -51,4 +51,12 @@
</setting>
</WPFMETRO.Properties.Settings>
</userSettings>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
2 changes: 1 addition & 1 deletion WPFMETRO/Converter.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public partial class Converter : MetroWindow

private Process ffMpegProc = new Process();

private readonly string videoFilter = "Video (*.m4a;*.mp4;*.3gp;*.m4v;*.mov;*.webm)|*.m4a;*.mp4;*.3gp;*.m4v;*.mov;*.webm";
private readonly string videoFilter = "Video (*.m4a;*.mp4;*.3gp;*.m4v;*.mov;*.webm;*.mkv)|*.m4a;*.mp4;*.3gp;*.m4v;*.mov;*.webm;*.mkv";
private readonly string audioFilter = "Audio (*.ogg;*.mp3;*.m4a;*.flac; *.mpeg) |*.ogg;*.mp3;*.m4a;*.flac;*.mpeg";

private string inputFile = "";
Expand Down
22 changes: 10 additions & 12 deletions WPFMETRO/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ private void CheckForUpdates()
WorkingDirectory = Environment.CurrentDirectory,
FileName = Assembly.GetExecutingAssembly().Location,
Verb = "runas"
};
};

MessageBoxResult userDialogResult = MessageBox.Show(Localization.Strings.AskToUpdateYoutubeDL + "\n" + Localization.Strings.CurrentVersion +
MessageBoxResult userDialogResult = MessageBox.Show(Localization.Strings.AskToUpdateYoutubeDL + "\n" + Localization.Strings.CurrentVersion +
Properties.Settings.Default.YoutubeDLVersion + "\n" + Localization.Strings.NewVersion + lastVersionYoutubeDL, "", MessageBoxButton.YesNo,
MessageBoxImage.Question); //Asks user if he wants to update the y-dl binary

Expand All @@ -103,7 +103,7 @@ private void CheckForUpdates()
else
{
if (lastVersionYoutubeDL != Properties.Settings.Default.YoutubeDLVersion)
{
{
if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + @"\youtube-dl.exe"))
{
File.Delete(AppDomain.CurrentDomain.BaseDirectory + @"\youtube-dl.exe");
Expand Down Expand Up @@ -138,23 +138,21 @@ private async void SelectButton_Click(object sender, RoutedEventArgs e)
UrlBox.Background = Brushes.Transparent;
formats.Clear();
string Url = UrlBox.Text;
if (Properties.Settings.Default.PrioritizePlaylists && UrlBox.Text.Contains(@"playlist?list="))
if (Properties.Settings.Default.PrioritizePlaylists && (UrlBox.Text.Contains(@"?list=") || UrlBox.Text.Contains(@"&list=") || UrlBox.Text.Contains(@"channel")))
{
formats.Add("default", "default (mp4)");
formats.Add("mp3", "mp3");
formats.Add("flac", "flac");
FiletypeBox.ItemsSource = formats;
FiletypeBox.IsEnabled = AddToQueueButton.IsEnabled = true;
UrlBox.Background = Brushes.DarkGreen;


}
else
{
UrlBox.Text = UrlBox.Text.Contains(@"&list=") ? UrlBox.Text.Substring(0, UrlBox.Text.IndexOf(@"&list=")) : UrlBox.Text;

await Task.Run(() => formats=queue.GetFormats(Url));
await Task.Run(() => videoInfo=queue.GetInfo(Url));
await Task.Run(() => formats = queue.GetFormats(Url));
await Task.Run(() => videoInfo = queue.GetInfo(Url));

if (formats.Count > 0)
{
Expand All @@ -167,7 +165,7 @@ private async void SelectButton_Click(object sender, RoutedEventArgs e)
UrlBox.Clear();
MessageBox.Show(Localization.Strings.InvalidURL, Localization.Strings.Error);
}

DownloadStatus.Text = Localization.Strings.NoDownload;
}

Expand Down Expand Up @@ -257,7 +255,7 @@ private async void AddFromTextButton_Click(object sender, RoutedEventArgs e)
{
await Task.Run(() => formats = queue.GetFormats(video));
await Task.Run(() => videoInfo = queue.GetInfo(video));
queue.ModifyQueue(title, thumbURL,video, filename, path, filetype, formats);
queue.ModifyQueue(title, thumbURL, video, filename, path, filetype, formats);
}
}
public void UpdateCard()
Expand All @@ -271,9 +269,9 @@ public void UpdateCard()

TitleLabel.Text = video.Title;
UrlLabel.Text = video.ID;
foreach(var kvp in video.AvailableFormats)
foreach (var kvp in video.AvailableFormats)
{
if(kvp.Key == video.SelectedFormat)
if (kvp.Key == video.SelectedFormat)
{
FiletypeLabel.Text = kvp.Value;
break;
Expand Down
123 changes: 75 additions & 48 deletions WPFMETRO/Queue.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Google.Apis.Services;
using Google.Apis.YouTube.v3;
using System.Text.Json;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
Expand All @@ -8,6 +7,16 @@
using System.Text;
using System.Text.RegularExpressions;

class VideoJSON
{
public string id { get; set; }
public string title { get; set; }
}
class Playlist
{
public List<VideoJSON> entries { get; set; }
}

namespace WPFMETRO
{
public class QueueException : Exception
Expand All @@ -21,7 +30,6 @@ class Queue
{
public ObservableCollection<Video> Videos = new ObservableCollection<Video>();

YouTubeService yt = new YouTubeService(new BaseClientService.Initializer() { ApiKey = "" });
private Process ytbDL = new Process();

private ProcessStartInfo ytbDLInfo = new ProcessStartInfo
Expand Down Expand Up @@ -54,9 +62,9 @@ public List<string> GetInfo(string url)
string output = f.Data ?? "null";
if (isThumb)
{
if(isNext && info == null)
if (isNext && info == null)
{
info = output == "null" || output.Contains("No thumbnail") ? info : output.Substring(output.LastIndexOf(' ')+1);
info = output == "null" || output.Contains("No thumbnail") ? info : output.Substring(output.LastIndexOf(' ') + 1);
}
else
{
Expand Down Expand Up @@ -135,7 +143,7 @@ public Dictionary<string, string> GetFormats(string url)
ytbDL.WaitForExit();
ytbDL.CancelOutputRead();

if(formats.Count > 1)
if (formats.Count > 1)
{
formats.Add("default", "default");
if (formats.Count > 1 && hasAudio)
Expand All @@ -144,7 +152,7 @@ public Dictionary<string, string> GetFormats(string url)
formats.Add("flac", "flac");
}
}

}

return formats;
Expand All @@ -153,48 +161,63 @@ public Dictionary<string, string> GetFormats(string url)

public void DownloadPlaylist(string ID, string filename, string path, string filetype, Dictionary<string, string> formats)
{
var nextPageToken = "";
while (nextPageToken != null)
{
var playlistItemsListRequest = yt.PlaylistItems.List("snippet");
playlistItemsListRequest.PlaylistId = ID.Substring(ID.IndexOf("=") + 1);
playlistItemsListRequest.MaxResults = 50;
playlistItemsListRequest.PageToken = nextPageToken;

var playlistItemsListResponse = playlistItemsListRequest.Execute();
ytbDLInfo.Arguments = String.Format("-J --flat-playlist \"{0}\"", ID);
ytbDL = Process.Start(ytbDLInfo);
Playlist playlist = null;

foreach (var playlistItem in playlistItemsListResponse.Items)
ytbDL.OutputDataReceived += new DataReceivedEventHandler(
(s, f) =>
{
if (f.Data != null)
{
ModifyQueue(playlistItem.Snippet.Title, playlistItem.Snippet.Thumbnails.Default__.Url, "https://www.youtube.com/watch?v=" + playlistItem.Snippet.ResourceId.VideoId, filename, path, filetype, formats);
playlist = JsonSerializer.Deserialize<Playlist>(f.Data);
}
});

ytbDL.Start();
ytbDL.BeginOutputReadLine();
ytbDL.WaitForExit();
ytbDL.CancelOutputRead();

nextPageToken = playlistItemsListResponse.NextPageToken;
foreach (VideoJSON v in playlist.entries)
{
string url = "https://www.youtube.com/watch?v=" + v.id;
ModifyQueue(v.title, null, url, filename, path, filetype, formats);
}
}

public void DownloadChannel(string ID, string filename, string path, string filetype, Dictionary<string, string> formats)
{
var channelItemsRequest = yt.Channels.List("contentDetails");
channelItemsRequest.Id = ID.Substring(ID.IndexOf("channel/") + 8);
channelItemsRequest.MaxResults = 50;

var channelsListResponse = channelItemsRequest.Execute();
ytbDLInfo.Arguments = String.Format("-J --flat-playlist \"{0}\"/videos", ID);
ytbDL = Process.Start(ytbDLInfo);
Playlist playlist = null;

foreach (var item in channelsListResponse.Items)
ytbDL.OutputDataReceived += new DataReceivedEventHandler(
(s, f) =>
{
string uploadsListID = item.ContentDetails.RelatedPlaylists.Uploads;

DownloadPlaylist(uploadsListID, filename, path, filetype, formats);
}
if (f.Data != null)
{
playlist = JsonSerializer.Deserialize<Playlist>(f.Data);
}
});

ytbDL.Start();
ytbDL.BeginOutputReadLine();
ytbDL.WaitForExit();
ytbDL.CancelOutputRead();

foreach (VideoJSON v in playlist.entries)
{
string url = "https://www.youtube.com/watch?v=" + v.id;
ModifyQueue(v.title, null, url, filename, path, filetype, formats);
}
}

public void ModifyQueue(string title, string thumbURL, string ID, string filename, string path, string filetype, Dictionary<string, string> formats)
{
if (ID.Contains("youtu.be/") || ID.Contains("youtube.com/"))
{
if (ID.Contains("playlist"))
if (ID.Contains("list="))
{
DownloadPlaylist(ID, filename, path, filetype, formats);
}
Expand All @@ -204,16 +227,18 @@ public void ModifyQueue(string title, string thumbURL, string ID, string filenam
}
else
{
Video video = new Video();
video.ID = ID;
video.Name = filename;
video.Path = path;
video.SelectedFormat = filetype;
video.ThumbURL = thumbURL;
video.Title = title;
video.AvailableFormats = formats.ToList();

Videos.Add(video);
Video video = new Video
{
ID = ID,
Name = filename,
Path = path,
SelectedFormat = filetype,
ThumbURL = thumbURL,
Title = title,
AvailableFormats = formats.ToList()
};

Videos.Add(video);
}
}
else
Expand All @@ -223,14 +248,16 @@ public void ModifyQueue(string title, string thumbURL, string ID, string filenam
case 0:
throw new QueueException(Localization.Strings.InvalidURL);
default:
Video videoNonYoutube = new Video();
videoNonYoutube.ID = ID;
videoNonYoutube.Name = filename;
videoNonYoutube.Path = path;
videoNonYoutube.SelectedFormat = filetype;
videoNonYoutube.ThumbURL = thumbURL;
videoNonYoutube.Title = title;
videoNonYoutube.AvailableFormats = formats.ToList();
Video videoNonYoutube = new Video
{
ID = ID,
Name = filename,
Path = path,
SelectedFormat = filetype,
ThumbURL = thumbURL,
Title = title,
AvailableFormats = formats.ToList()
};

Videos.Add(videoNonYoutube);
break;
Expand Down
Loading

0 comments on commit 360c3c6

Please sign in to comment.