Ejemplo n.º 1
0
        public static void DownloadImages(string[] urls, string username, ImageQuality quality)
        {
            if (urls.Length == 0)
            {
                return;
            }

            string firstImageLink = GetMediaLink(urls[0], quality);
            int    qualityIndex   = firstImageLink.IndexOf(':', firstImageLink.LastIndexOf('/'));

            string file = GetImageFileName(firstImageLink);
            string ext  = Path.GetExtension(file); // includes dot

            string[] fileNameParts = qualityIndex == -1 ? new string[] {
                Path.ChangeExtension(file, null)
            } : new string[] {
                username,
                Path.ChangeExtension(file, null),
                firstImageLink.Substring(qualityIndex + 1)
            };

            using (SaveFileDialog dialog = new SaveFileDialog {
                AutoUpgradeEnabled = true,
                OverwritePrompt = urls.Length == 1,
                Title = "Save image",
                FileName = $"{string.Join(" ", fileNameParts.Where(part => part.Length > 0))}{ext}",
                Filter = (urls.Length == 1 ? "Image" : "Images") + (string.IsNullOrEmpty(ext) ? " (unknown)|*.*" : $" (*{ext})|*{ext}")
            }){
                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    void OnFailure(Exception ex)
                    {
                        FormMessage.Error("Image Download", "An error occurred while downloading the image: " + ex.Message, FormMessage.OK);
                    }

                    if (urls.Length == 1)
                    {
                        BrowserUtils.DownloadFileAsync(firstImageLink, dialog.FileName, null, OnFailure);
                    }
                    else
                    {
                        string pathBase = Path.ChangeExtension(dialog.FileName, null);
                        string pathExt  = Path.GetExtension(dialog.FileName);

                        for (int index = 0; index < urls.Length; index++)
                        {
                            BrowserUtils.DownloadFileAsync(GetMediaLink(urls[index], quality), $"{pathBase} {index+1}{pathExt}", null, OnFailure);
                        }
                    }
                }
            }
        }
Ejemplo n.º 2
0
        public static void DownloadVideo(string url, string username)
        {
            string filename = BrowserUtils.GetFileNameFromUrl(url);
            string ext      = Path.GetExtension(filename);

            using (SaveFileDialog dialog = new SaveFileDialog {
                AutoUpgradeEnabled = true,
                OverwritePrompt = true,
                Title = "Save Video",
                FileName = string.IsNullOrEmpty(username) ? filename : $"{username} {filename}".TrimStart(),
                Filter = "Video" + (string.IsNullOrEmpty(ext) ? " (unknown)|*.*" : $" (*{ext})|*{ext}")
            }){
                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    DownloadFileAuth(url, dialog.FileName, null, ex => {
                        FormMessage.Error("Video Download", "An error occurred while downloading the video: " + ex.Message, FormMessage.OK);
                    });
                }
            }
        }
Ejemplo n.º 3
0
        private static void DownloadFileAuth(string url, string target, Action onSuccess, Action <Exception> onFailure)
        {
            const string AuthCookieName = "auth_token";

            TaskScheduler scheduler = TaskScheduler.FromCurrentSynchronizationContext();

            using (ICookieManager cookies = Cef.GetGlobalCookieManager()){
                cookies.VisitUrlCookiesAsync(url, true).ContinueWith(task => {
                    string cookieStr = null;

                    if (task.Status == TaskStatus.RanToCompletion)
                    {
                        Cookie found = task.Result?.Find(cookie => cookie.Name == AuthCookieName); // the list may be null

                        if (found != null)
                        {
                            cookieStr = $"{found.Name}={found.Value}";
                        }
                    }

                    BrowserUtils.DownloadFileAsync(url, target, cookieStr, onSuccess, onFailure);
                }, scheduler);
            }
        }
Ejemplo n.º 4
0
 public static string GetImageFileName(string url)
 {
     return(BrowserUtils.GetFileNameFromUrl(ExtractMediaBaseLink(url)));
 }