示例#1
0
        public bool DownloadFile(GitHubFile file, string DestinationDir)
        {
            // Check if the file is new/updated first
            if (!DownloadNeeded(file, DestinationDir))
            {
                return(true);
            }

            // download.....
            try
            {
                if (logger != null)
                {
                    logger("Download github file " + file.Name);
                }

                WriteLog("Download github file " + file.Name, "");
                string destFile = Path.Combine(DestinationDir, file.Name);

                using (WebClient client = new WebClient())
                {
                    client.DownloadFile(file.DownloadURL, destFile);
                }

                return(true);
            }
            catch (WebException ex)
            {
                using (WebResponse response = ex.Response)
                {
                    HttpWebResponse httpResponse = (HttpWebResponse)response;
                    System.Diagnostics.Trace.WriteLine(ex.StackTrace);
                    System.Diagnostics.Trace.WriteLine("WebException : " + ex.Message);
                    System.Diagnostics.Trace.WriteLine("Response code : " + httpResponse.StatusCode);
                    System.Diagnostics.Trace.WriteLine(ex.StackTrace);
                    WriteLog("WebException" + ex.Message, "");
                    WriteLog($"HTTP Error code: {httpResponse.StatusCode}", "");

                    return(false);
                }
            }
            catch (Exception ex)
            {
                if (logger != null)
                {
                    logger("GitHub DownloadFile Exception" + ex.Message);
                }

                WriteLog("GitHub DownloadFile Exception" + ex.Message, "");
                return(false);
            }
        }
示例#2
0
        public bool DownloadNeeded(GitHubFile file, string DestinationDir)
        {
            string destFile = Path.Combine(DestinationDir, file.Name);

            if (!File.Exists(destFile))
            {
                return(true);
            }
            else
            {
                // Calculate sha
                string sha = CalcSha1(destFile).ToLower();

                if (sha.Equals(file.sha))
                {
                    return(false);
                }
            }

            return(true);
        }
示例#3
0
        public List <GitHubFile> GetDataFiles(string gitdir)
        {
            List <GitHubFile> files = new List <GitHubFile>();

            try
            {
                HttpWebRequest request = WebRequest.Create("https://api.github.com/repos/EDDiscovery/EDDiscoveryData/contents/" + gitdir) as HttpWebRequest;
                request.UserAgent = "TestApp";
                using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
                {
                    StreamReader reader   = new StreamReader(response.GetResponseStream());
                    string       content1 = reader.ReadToEnd();
                    JArray       ja       = JArray.Parse(content1);

                    if (ja != null)
                    {
                        foreach (JObject jo in ja)
                        {
                            GitHubFile file = new GitHubFile(jo);
                            files.Add(file);
                        }
                        return(files);
                    }
                    else
                    {
                        return(null);
                    };
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine($"Exception: {ex.Message}");
                Trace.WriteLine($"ETrace: {ex.StackTrace}");
                return(null);
            }
        }