diff --git a/.vs/youtube-downloader/v16/.suo b/.vs/youtube-downloader/v16/.suo index 01b74db..94bba03 100644 Binary files a/.vs/youtube-downloader/v16/.suo and b/.vs/youtube-downloader/v16/.suo differ diff --git a/Program.cs b/Program.cs index a28710c..1b5442c 100644 --- a/Program.cs +++ b/Program.cs @@ -34,6 +34,13 @@ namespace youtube_downloader { rp.AsFile(rq, Path.Combine(webSitePath, "index.html")); }); + Route.Add("/api/Search/{text}", (rq, rp, args) => + { + string search=System.Web.HttpUtility.UrlDecode(args["text"]); + string json = JsonConvert.SerializeObject(Server.Functions.Downloader.Search(search)); + rp.AsText(json, "application/json"); + + }); Route.Add("/api/Storage/GetDirectories/{Path}", (rq, rp, args) => { string path = Server.Functions.Downloader.DL.GetPath(true, args["Path"]); @@ -62,6 +69,7 @@ namespace youtube_downloader rp.AsText("[]", "application/json"); } }); + Route.Add("/api/Storage/FileExists/{Path}", (rq, rp, args) => { string path = Server.Functions.Downloader.DL.GetPath(true, args["Path"]); diff --git a/Server/Functions/Downloader.cs b/Server/Functions/Downloader.cs index 071d470..69493e5 100644 --- a/Server/Functions/Downloader.cs +++ b/Server/Functions/Downloader.cs @@ -283,70 +283,197 @@ namespace youtube_downloader.Server.Functions if (canDownload) { - switch (v.Resolution) + try { - case Resolution.Convert: + switch (v.Resolution) + { + case Resolution.Convert: - string mypath = GetPath(true, "Converted", v.Video.Id + "-vidonly.bkp"); - string mypathaudio = GetPath(true, "Audio", v.Video.Id + "incomplete.mp4"); - string mypathCompleteAudio = GetPath(true, "Audio", v.Video.Id + ".mp4"); - string mypathComplete = GetPath(true, "Converted", v.Video.Id + ".mp4"); - string mypathIncompleteConverting = GetPath(true, "Converted", "conv.mkv"); + string mypath = GetPath(true, "Converted", v.Video.Id + "-vidonly.bkp"); + string mypathaudio = GetPath(true, "Audio", v.Video.Id + "incomplete.mp4"); + string mypathCompleteAudio = GetPath(true, "Audio", v.Video.Id + ".mp4"); + string mypathComplete = GetPath(true, "Converted", v.Video.Id + ".mp4"); + string mypathIncompleteConverting = GetPath(true, "Converted", "conv.mkv"); - if (Continue(mypathComplete)) - { - var s3 = await ytc.Videos.Streams.GetManifestAsync(v.Video.Id); - var best2 = s3.GetAudioOnlyStreams().GetWithHighestBitrate(); - - - - var best = s3.GetVideoOnlyStreams().GetWithHighestVideoQuality(); - P.Length = best.Size.Bytes + best2.Size.Bytes; - ProgressTwo p = new ProgressTwo(best.Size.Bytes, best2.Size.Bytes, DownloadP); - await ytc.Videos.Streams.DownloadAsync(best,mypath, p.Video); - IProgress pv = p.Video; - pv.Report(1); - if (Continue(mypathCompleteAudio)) + if (Continue(mypathComplete)) { - await ytc.Videos.Streams.DownloadAsync(best2, mypathaudio,p.Audio); - File.Move(mypathaudio, mypathCompleteAudio); + var s3 = await ytc.Videos.Streams.GetManifestAsync(v.Video.Id); + var best2 = s3.GetAudioOnlyStreams().GetWithHighestBitrate(); + + + + var best = s3.GetVideoOnlyStreams().GetWithHighestVideoQuality(); + P.Length = best.Size.Bytes + best2.Size.Bytes; + ProgressTwo p = new ProgressTwo(best.Size.Bytes, best2.Size.Bytes, DownloadP); + + using (var destStrm = System.IO.File.Open(mypath, FileMode.OpenOrCreate, FileAccess.ReadWrite)) + { + long pos = 0; + long len = 0; + using (var srcStrm = await ytc.Videos.Streams.GetAsync(best)) + { + len = srcStrm.Length; + pos = destStrm.Length; + IProgress myProgress = p.Video; + if (pos >= len) + { + + myProgress.Report(1); + + } + srcStrm.Seek(destStrm.Length, SeekOrigin.Begin); + byte[] buffer = new byte[4096]; + int read = 0; + do + { + read = srcStrm.Read(buffer, 0, buffer.Length); + destStrm.Write(buffer, 0, read); + + pos += read; + double myP = (double)pos / (double)len; + myProgress.Report(myP); + } + while (read > 0); + } + + } + + IProgress pv = p.Video; + pv.Report(1); + if (Continue(mypathCompleteAudio)) + { + long pos = 0; + long len = 0; + using (var destStrm = System.IO.File.Open(mypathaudio, FileMode.OpenOrCreate, FileAccess.ReadWrite)) + { + using (var srcStrm = await ytc.Videos.Streams.GetAsync(best2)) + { + len = srcStrm.Length; + pos = destStrm.Length; + IProgress myProgress = p.Audio; + if (pos >= len) + { + + myProgress.Report(1); + + } + srcStrm.Seek(destStrm.Length, SeekOrigin.Begin); + byte[] buffer = new byte[4096]; + int read = 0; + do + { + read = srcStrm.Read(buffer, 0, buffer.Length); + destStrm.Write(buffer, 0, read); + + pos += read; + double myP = (double)pos / (double)len; + myProgress.Report(myP); + } + while (read > 0); + } + + } + File.Move(mypathaudio, mypathCompleteAudio); + } + IProgress pa = p.Video; + pa.Report(1); + ffmpeg.mux(mypath, mypathCompleteAudio, mypathIncompleteConverting); + + + File.Move(mypathIncompleteConverting, mypathComplete); + } + break; + case Resolution.NoConvert: + string mypath2 = GetPath(true, "NotConverted", v.Video.Id + "incomplete.mp4"); + string mypath2Complete = GetPath(true, "NotConverted", v.Video.Id + ".mp4"); + + if (Continue(mypath2Complete)) + { + var s = await ytc.Videos.Streams.GetManifestAsync(v.Video.Id); + var best = s.GetMuxedStreams().GetWithHighestVideoQuality(); + P.Length = best.Size.Bytes; + long pos = 0; + long len = 0; + using (var destStrm = System.IO.File.Open(mypath2, FileMode.OpenOrCreate, FileAccess.ReadWrite)) + { + using (var srcStrm = await ytc.Videos.Streams.GetAsync(best)) + { + len = srcStrm.Length; + pos = destStrm.Length; + IProgress myProgress = DownloadP; + if (pos >= len) + { + + myProgress.Report(1); + + } + srcStrm.Seek(destStrm.Length, SeekOrigin.Begin); + byte[] buffer = new byte[4096]; + int read = 0; + do + { + read = srcStrm.Read(buffer, 0, buffer.Length); + destStrm.Write(buffer, 0, read); + + pos += read; + double myP = (double)pos / (double)len; + myProgress.Report(myP); + } + while (read > 0); + } + + } + File.Move(mypath2, mypath2Complete); } - IProgress pa = p.Video; - pa.Report(1); - ffmpeg.mux(mypath,mypathCompleteAudio,mypathIncompleteConverting); - File.Move(mypathIncompleteConverting, mypathComplete); - } - break; - case Resolution.NoConvert: - string mypath2 = GetPath(true, "NotConverted", v.Video.Id + "incomplete.mp4"); - string mypath2Complete = GetPath(true, "NotConverted", v.Video.Id + ".mp4"); + break; + case Resolution.Audio: + string mypath3 = GetPath(true, "Audio", v.Video.Id + "incomplete.mp4"); + string mypath3Complete = GetPath(true, "Audio", v.Video.Id + ".mp4"); + if (Continue(mypath3Complete)) + { + var s2 = await ytc.Videos.Streams.GetManifestAsync(v.Video.Id); + var best2 = s2.GetAudioOnlyStreams().GetWithHighestBitrate(); + P.Length = best2.Size.Bytes; + long pos = 0; + long len = 0; + using (var destStrm = System.IO.File.Open(mypath3, FileMode.OpenOrCreate, FileAccess.ReadWrite)) + { + using (var srcStrm = await ytc.Videos.Streams.GetAsync(best2)) + { + len = srcStrm.Length; + pos = destStrm.Length; + IProgress myProgress = DownloadP; + if (pos >= len) + { - if (Continue(mypath2Complete)) - { - var s = await ytc.Videos.Streams.GetManifestAsync(v.Video.Id); - var best = s.GetMuxedStreams().GetWithHighestVideoQuality(); - P.Length = best.Size.Bytes; + myProgress.Report(1); - await ytc.Videos.Streams.DownloadAsync(best, mypath2, DownloadP); - File.Move(mypath2, mypath2Complete); - } - + } + srcStrm.Seek(destStrm.Length, SeekOrigin.Begin); + byte[] buffer = new byte[4096]; + int read = 0; + do + { + read = srcStrm.Read(buffer, 0, buffer.Length); + destStrm.Write(buffer, 0, read); - break; - case Resolution.Audio: - string mypath3 = GetPath(true, "Audio", v.Video.Id + "incomplete.mp4"); - string mypath3Complete = GetPath(true, "Audio", v.Video.Id + ".mp4"); - if (Continue(mypath3Complete)) - { - var s2 = await ytc.Videos.Streams.GetManifestAsync(v.Video.Id); - var best2 = s2.GetAudioOnlyStreams().GetWithHighestBitrate(); - P.Length = best2.Size.Bytes; - await ytc.Videos.Streams.DownloadAsync(best2, mypath3, DownloadP); - File.Move(mypath3, mypath3Complete); - } - break; + pos += read; + double myP = (double)pos / (double)len; + myProgress.Report(myP); + } + while (read > 0); + } + + } + File.Move(mypath3, mypath3Complete); + } + break; + } + }catch(Exception ex) + { + Console.WriteLine(ex.Message); } } System.Threading.Thread.Sleep(1); @@ -370,7 +497,20 @@ namespace youtube_downloader.Server.Functions _ = ex; } } - + public static async Task> Search(string text) + { + List media = new List(); + try + { + await DL.ytc.Search.GetVideosAsync(text).ForEachAsync((e) => { media.Add(new SavedMedia() { Title = e.Title, Id = e.Id, Kind = InfoType.Video }); }); + await DL.ytc.Search.GetPlaylistsAsync(text).ForEachAsync((e) => { media.Add(new SavedMedia() { Title = e.Title, Id = e.Id, Kind = InfoType.Playlist }); }); + await DL.ytc.Search.GetChannelsAsync(text).ForEachAsync((e) => { media.Add(new SavedMedia() { Title = e.Title, Id = e.Id, Kind = InfoType.Channel }); }); + }catch (Exception ex) + { + _ = ex; + } + return media; + } internal static List GetQueueItems() { return DL.Queue; diff --git a/Server/Functions/SavedMedia.cs b/Server/Functions/SavedMedia.cs new file mode 100644 index 0000000..edfbdb9 --- /dev/null +++ b/Server/Functions/SavedMedia.cs @@ -0,0 +1,15 @@ +using TessesYoutubeDownloader.Server.Models; +using youtube_downloader.Server.Models; + +namespace youtube_downloader.Server.Functions +{ + + public class SavedMedia + { + public InfoType Kind { get; set; } + public string Id { get; set; } + + public string Title { get; set; } + + } +} \ No newline at end of file diff --git a/Server/Models/SavedChannel.cs b/Server/Models/SavedChannel.cs index 1ea2caa..69d8979 100644 --- a/Server/Models/SavedChannel.cs +++ b/Server/Models/SavedChannel.cs @@ -3,10 +3,11 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using youtube_downloader.Server.Functions; namespace TessesYoutubeDownloader.Server.Models { - class SavedChannel + public class SavedChannel { public static SavedChannel FromChannel(YoutubeExplode.Channels.Channel c, Action downloadThumbnail) { diff --git a/Server/Models/SavedPlaylist.cs b/Server/Models/SavedPlaylist.cs index 95907b4..6f7672e 100644 --- a/Server/Models/SavedPlaylist.cs +++ b/Server/Models/SavedPlaylist.cs @@ -3,10 +3,11 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using youtube_downloader.Server.Functions; namespace TessesYoutubeDownloader.Server.Models { - class SavedPlaylist + public class SavedPlaylist { public static async Task FromPlaylistId(Resolution res,YoutubeExplode.YoutubeClient ytc,YoutubeExplode.Playlists.PlaylistId id,Action addToQueue, Action downloadThumbnail) { diff --git a/Server/Models/SavedVideo.cs b/Server/Models/SavedVideo.cs index c56fdc4..b26b9d6 100644 --- a/Server/Models/SavedVideo.cs +++ b/Server/Models/SavedVideo.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using youtube_downloader.Server.Functions; namespace TessesYoutubeDownloader.Server.Models { diff --git a/bin/Debug/youtube-downloader.exe b/bin/Debug/youtube-downloader.exe index 9df455d..9312185 100644 Binary files a/bin/Debug/youtube-downloader.exe and b/bin/Debug/youtube-downloader.exe differ diff --git a/bin/Debug/youtube-downloader.pdb b/bin/Debug/youtube-downloader.pdb index 8b4d726..34a2370 100644 Binary files a/bin/Debug/youtube-downloader.pdb and b/bin/Debug/youtube-downloader.pdb differ diff --git a/obj/Debug/youtube-downloader.csproj.AssemblyReference.cache b/obj/Debug/youtube-downloader.csproj.AssemblyReference.cache index f6bcfe2..277f15f 100644 Binary files a/obj/Debug/youtube-downloader.csproj.AssemblyReference.cache and b/obj/Debug/youtube-downloader.csproj.AssemblyReference.cache differ diff --git a/obj/Debug/youtube-downloader.csproj.CoreCompileInputs.cache b/obj/Debug/youtube-downloader.csproj.CoreCompileInputs.cache index 348d625..c13eb93 100644 --- a/obj/Debug/youtube-downloader.csproj.CoreCompileInputs.cache +++ b/obj/Debug/youtube-downloader.csproj.CoreCompileInputs.cache @@ -1 +1 @@ -d24c4380a60b7d95eb6c0794042e9b1bb52b78fb +05a58042d13f08ad611e38c9c233d47d4e6ac009 diff --git a/obj/Debug/youtube-downloader.exe b/obj/Debug/youtube-downloader.exe index 9df455d..9312185 100644 Binary files a/obj/Debug/youtube-downloader.exe and b/obj/Debug/youtube-downloader.exe differ diff --git a/obj/Debug/youtube-downloader.pdb b/obj/Debug/youtube-downloader.pdb index 8b4d726..34a2370 100644 Binary files a/obj/Debug/youtube-downloader.pdb and b/obj/Debug/youtube-downloader.pdb differ diff --git a/youtube-downloader.csproj b/youtube-downloader.csproj index 3b0b564..a51c6ff 100644 --- a/youtube-downloader.csproj +++ b/youtube-downloader.csproj @@ -133,6 +133,7 @@ +