Esempio n. 1
0
        public static string GetJSON(string url)
        {
            if (!Properties.Settings.Default.jsonSupport)
            {
                return(null);
            }

            try {
                using (WebClient wc = new WebClient()) {
                    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
                    wc.Headers.Add("User-Agent: " + Program.UserAgent);
                    string json  = wc.DownloadString(url);
                    byte[] bytes = Encoding.ASCII.GetBytes(json);
                    using (var stream = new MemoryStream(bytes)) {
                        var quotas     = new XmlDictionaryReaderQuotas();
                        var jsonReader = JsonReaderWriterFactory.CreateJsonReader(stream, quotas);
                        var xml        = XDocument.Load(jsonReader);
                        stream.Flush();
                        stream.Close();
                        return(xml.ToString());
                    }
                }
            }
            catch (WebException WebE) {
                ErrorLog.ReportWebException(WebE, url);
                return(null);
            }
            catch (Exception ex) {
                ErrorLog.ReportException(ex);
                return(null);
            }
        }
Esempio n. 2
0
        public static string GetJSON(string url)
        {
            if (!Properties.Settings.Default.jsonSupport)
            {
                return(null);
            }

            try {
                using (WebClient wc = new WebClient()) {
                    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
                    wc.Headers.Add("User-Agent: " + Program.UserAgent);
                    string json  = wc.DownloadString(url);
                    byte[] bytes = Encoding.ASCII.GetBytes(json);
                    using (var stream = new MemoryStream(bytes)) {
                        var quotas     = new XmlDictionaryReaderQuotas();
                        var jsonReader = JsonReaderWriterFactory.CreateJsonReader(stream, quotas);
                        var xml        = XDocument.Load(jsonReader);
                        stream.Flush();
                        stream.Close();
                        return(xml.ToString());
                    }
                }
                // maybe having a task in a new thread is wrong, but for the sake of testing nothing is.
                //ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
                //using (HttpClient ApiClient = new HttpClient()) {
                //    ApiClient.DefaultRequestHeaders.Add("User-Agent", Program.UserAgent);
                //    string ReturnedString = null;
                //    if (AcquireHeaders) {
                //        var ReturnedContent = await ApiClient.SendAsync(new HttpRequestMessage(HttpMethod.Head, url));
                //        ReturnedString = ReturnedContent.ToString();
                //    }
                //    else {
                //        ApiClient.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
                //        ReturnedString = await ApiClient.GetStringAsync(url);
                //    }
                //    return Task.FromResult(ReturnedString);
                //}
            }
            catch (WebException WebE) {
                ErrorLog.ReportWebException(WebE, url);
                return(null);
            }
            catch (Exception ex) {
                ErrorLog.ReportException(ex);
                return(null);
            }
        }
Esempio n. 3
0
        public static void UpdateYoutubeDl()
        {
            GetGitVersionString(1);

            if (Downloads.Default.useYtdlUpdater && General.Default.UseStaticYtdl && !string.IsNullOrEmpty(General.Default.ytdlPath) && File.Exists(General.Default.ytdlPath) || File.Exists(Environment.CurrentDirectory + "\\youtube-dl.exe"))
            {
                Process UpdateYoutubeDl = new Process();
                UpdateYoutubeDl.StartInfo.Arguments = "-U";

                if (!General.Default.UseStaticYtdl || string.IsNullOrEmpty(General.Default.ytdlPath))
                {
                    UpdateYoutubeDl.StartInfo.FileName = Environment.CurrentDirectory + "\\youtube-dl.exe";
                }
                else
                {
                    UpdateYoutubeDl.StartInfo.FileName = General.Default.ytdlPath;
                }

                UpdateYoutubeDl.Start();
                UpdateYoutubeDl.WaitForExit();
            }
            else
            {
                if (!General.Default.UseStaticYtdl || string.IsNullOrEmpty(General.Default.ytdlPath))
                {
                    Thread DownloadYoutubeDl = new Thread(() => {
                        using (WebClient wc = new WebClient()) {
                            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
                            wc.Headers.Add("User-Agent: " + Program.UserAgent);
                            try {
                                string ytdlDownloadPath = null;
                                if (General.Default.UseStaticYtdl && !string.IsNullOrEmpty(General.Default.ytdlPath))
                                {
                                    ytdlDownloadPath = General.Default.ytdlPath;
                                }
                                else
                                {
                                    ytdlDownloadPath = Environment.CurrentDirectory + "\\youtube-dl.exe";
                                }
                                wc.DownloadFile(string.Format(GitData.GitLinks.ApplicationDownloadUrl, GitData.GitLinks.Users[1], GitData.GitLinks.ApplciationNames[1], GitData.YoutubeDlVersion), ytdlDownloadPath);
                                if (GitData.YoutubeDlVersion != Properties.Settings.Default.YoutubeDlVersion)
                                {
                                    Properties.Settings.Default.YoutubeDlVersion = GitData.YoutubeDlVersion;
                                    Properties.Settings.Default.Save();
                                }
                                MessageBox.Show("Youtube-dl has been updated.");
                            }
                            catch (WebException webex) {
                                ErrorLog.ReportWebException(webex, string.Format(GitData.GitLinks.ApplicationDownloadUrl, GitData.GitLinks.Users[1], GitData.GitLinks.ApplciationNames[1], GitData.YoutubeDlVersion));
                            }
                            catch (Exception ex) {
                                ErrorLog.ReportException(ex);
                            }
                        }
                    });
                    DownloadYoutubeDl.Start();
                }
                else
                {
                    Thread DownloadYoutubeDl = new Thread(() => {
                        using (WebClient wc = new WebClient()) {
                            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
                            wc.Headers.Add("User-Agent: " + Program.UserAgent);
                            try {
                                wc.DownloadFile(string.Format(GitData.GitLinks.ApplicationDownloadUrl, GitData.GitLinks.Users[1], GitData.GitLinks.ApplciationNames[1], GitData.YoutubeDlVersion), General.Default.ytdlPath);
                                if (GitData.YoutubeDlVersion != Properties.Settings.Default.YoutubeDlVersion)
                                {
                                    Properties.Settings.Default.YoutubeDlVersion = GitData.YoutubeDlVersion;
                                    Properties.Settings.Default.Save();
                                }
                                MessageBox.Show("Youtube-dl has been updated.");
                            }
                            catch (WebException webex) {
                                ErrorLog.ReportWebException(webex, string.Format(GitData.GitLinks.ApplicationDownloadUrl, GitData.GitLinks.Users[1], GitData.GitLinks.ApplciationNames[1], GitData.YoutubeDlVersion));
                            }
                            catch (Exception ex) {
                                ErrorLog.ReportException(ex);
                            }
                        }
                    });
                    DownloadYoutubeDl.Start();
                }
            }
        }