示例#1
0
 static Demo DownloadDemoId(string matchdata, string series, bool compress = false)
 {
     using (var wc = new WebClient {
         Proxy = null
     })
     {
         var matchid = matchdata.Split(new[] { ' ' }, 2)[0];
         var game    = matchdata.Split(new[] { ' ' }, 2)[1];
         ConsoleExt.Log("Starting download of {0} - {1}.", matchid, game);
         var demo = new Demo
         {
             Description = series + " " + game,
             Game        = game,
             MatchID     = matchid,
             MatchNumber = -1,
             Series      = series
         };
         var demoresponse = wc.DownloadString("https://rjackson.me/tools/matchurls?matchid=" + demo.MatchID);
         var demourl      = DemoURLRegex.Match(demoresponse).Groups[1].Captures[0].Value;
         var demoname     = DemoNameRegex.Match(demoresponse).Groups[1].Captures[0].Value.Split('_')[0] + ".dem.bz2";
         ConsoleExt.Log("Downloading {0}.", demourl);
         var progressbar = new ProgressBar();
         wc.DownloadProgressChanged += (sender, args) =>
         {
             progressbar.Progress = args.ProgressPercentage;
             var padded = demo.Description.PadRight(Console.WindowWidth);
             var size   = string.Format("{0:#,0}/{1:#,0}KB, {2}%",
                                        args.BytesReceived / 1000,
                                        args.TotalBytesToReceive / 1000,
                                        args.ProgressPercentage);
             progressbar.Message = padded.Insert(padded.Length - size.Length,
                                                 size);
         };
         wc.DownloadFileCompleted += (sender, args) =>
         {
             Downloads--;
             if (args.Cancelled || args.Error != null)
             {
                 if (args.Error.Message.Contains("404"))
                 {
                     ConsoleExt.Log("{0}'s demo file is pending upload.");
                 }
                 else
                 {
                     ConsoleExt.Log("Downloading of {0} failed. Reason: {1}",
                                    demo.MatchID,
                                    args.Cancelled
                                                                        ? "Cancelled"
                                                                        : args.Error.Message);
                 }
                 return;
             }
             try
             {
                 Decompress(Path.Combine(SaveDirectory, demo.Series,
                                         demoname), progressbar);
                 var path = Path.Combine(SaveDirectory, demo.Series,
                                         "details.txt");
                 File.AppendAllText(path,
                                    string.Format(
                                        "{0} - {1} - {2} | {3}\r\n{4}\r\n\r\n",
                                        demo.MatchID, demo.Series,
                                        demo.Game,
                                        "dota2://matchid=" + demo.MatchID,
                                        "playdemo \"" + @"replays\" +
                                        demo.Series + @"\" +
                                        Path.GetFileNameWithoutExtension(demoname) +
                                        "\""));
                 if (compress)
                 {
                     Compress(demo.Series);
                 }
             }
             catch (Exception ex)
             {
                 ConsoleExt.Log("Exception occured finalizing file: {0}", ex.Message);
             }
         };
         Directory.CreateDirectory(Path.Combine(SaveDirectory, demo.Series));
         Downloads++;
         ConsoleExt.AddProgressBar(progressbar);
         wc.DownloadFileAsync(new Uri(demourl), Path.Combine(SaveDirectory, demo.Series, demoname));
         demo.Path = Path.Combine(SaveDirectory, demo.Series, demoname);
         return(demo);
     }
 }
示例#2
0
 static Demo DownloadDemoWeb(int matchnum)
 {
     ConsoleExt.Log("Starting download of {0}", matchnum);
     using (var wc = new WebClient {
         Proxy = null
     })
     {
         try
         {
             var response = wc.DownloadString("http://www.dota2.com/international/game/" + matchnum);
             if (response.Contains("This game was not played."))
             {
                 if (ScheduleURL.Contains("mainevent"))
                 {
                     var dem = new Demo
                     {
                         Description = DescriptionRegex.Match(response).Groups[1].Captures[0].Value
                     };
                     if (dem.Description.Contains("3/3") || dem.Description.Contains("4/5") || dem.Description.Contains("5/5"))
                     {
                         //File.AppendAllText("uploadlist.txt", dem.Series + "\r\n");
                         if (!File.Exists(Path.Combine(SaveDirectory, "TI3 - " + dem.Series + ".zip")))
                         {
                             File.AppendAllText("downloaded.txt", matchnum + "\r\n");
                             ConsoleExt.Log("{0} was not played, compressing series.", matchnum);
                             Compress(dem.Series);
                         }
                         return(new Demo());
                     }
                     ConsoleExt.Log("{0} is not uploaded yet.", matchnum);
                     return(null);
                 }
                 ConsoleExt.Log("{0} is not uploaded yet.", matchnum);
                 return(null);
             }
             var demo = new Demo
             {
                 Description = DescriptionRegex.Match(response).Groups[1].Captures[0].Value,
                 TeamA       = TeamARegex.Match(response).Groups[1].Captures[0].Value,
                 TeamB       = TeamBRegex.Match(response).Groups[1].Captures[0].Value,
                 MatchID     = MatchIDRegex.Match(response).Groups[1].Captures[0].Value,
                 MatchNumber = matchnum
             };
             if (demo.Series.StartsWith("Series"))
             {
                 demo.NumberOfGames = 4;
             }
             else if (demo.Series.StartsWith("UB"))
             {
                 demo.NumberOfGames = 3;
             }
             else if (demo.Series.StartsWith("LB"))
             {
                 demo.NumberOfGames = demo.Game.Contains("/3") ? 3 : 1;
             }
             else if (demo.Series.StartsWith("Grand"))
             {
                 demo.NumberOfGames = 5;
             }
             var demoresponse = wc.DownloadString("https://rjackson.me/tools/matchurls?matchid=" + demo.MatchID);
             var demourl      = DemoURLRegex.Match(demoresponse).Groups[1].Captures[0].Value;
             var demoname     = DemoNameRegex.Match(demoresponse).Groups[1].Captures[0].Value.Split('_')[0] + ".dem.bz2";
             if (File.Exists(Path.Combine(SaveDirectory, "TI3 - " + demo.Series,
                                          Path.GetFileNameWithoutExtension(demoname))))
             {
                 File.AppendAllText("downloaded.txt", matchnum + "\r\n");
                 File.AppendAllText(Path.Combine(SaveDirectory, "TI3 - " + demo.Series, "details.txt"),
                                    string.Format(
                                        "{0} - {1} - {2} - {3} vs {4} | {5}\r\n{6}\r\n\r\n",
                                        demo.MatchNumber, demo.MatchID, demo.Description,
                                        demo.TeamA, demo.TeamB, "dota2://matchid=" + demo.MatchID,
                                        "playdemo \"" + @"replays\" + "TI3 - " +
                                        demo.Series + @"\" + Path.GetFileNameWithoutExtension(demoname) + "\""));
                 AddHTML(demo);
                 HTMLRender.Render();
                 return(demo);
             }
             ConsoleExt.Log("Downloading {0}.", demourl);
             var progressbar = new ProgressBar()
             {
                 Message = "Downloading " + demourl
             };
             wc.DownloadProgressChanged += (sender, args) =>
             {
                 progressbar.Progress = args.ProgressPercentage;
                 var padded = demo.Description.PadRight(Console.WindowWidth);
                 var size   = string.Format("{0:#,0}/{1:#,0}KB, {2}%",
                                            args.BytesReceived / 1000,
                                            args.TotalBytesToReceive / 1000,
                                            args.ProgressPercentage);
                 progressbar.Message =
                     padded.Insert(padded.Length - size.Length,
                                   size);
             };
             wc.DownloadFileCompleted += (sender, args) =>
             {
                 Downloads--;
                 if (args.Cancelled || args.Error != null)
                 {
                     progressbar.Destroy = true;
                     if (args.Error.Message.Contains("404"))
                     {
                         progressbar.Message = string.Format("{0}'s demo file is pending upload.", matchnum);
                     }
                     else
                     {
                         progressbar.Message = string.Format(
                             "Downloading of {0} failed. Reason: {1}", matchnum,
                             args.Cancelled ? "Cancelled" : args.Error.ToString());
                     }
                     DemoList.Remove(matchnum);
                     return;
                 }
                 try
                 {
                     Decompress(Path.Combine(SaveDirectory, "TI3 - " + demo.Series,
                                             demoname), progressbar);
                     File.AppendAllText("downloaded.txt", matchnum + "\r\n");
                     var path = Path.Combine(SaveDirectory, "TI3 - " + demo.Series, "details.txt");
                     File.AppendAllText(path,
                                        string.Format(
                                            "{0} - {1} - {2} - {3} vs {4} | {5}\r\n{6}\r\n\r\n",
                                            demo.MatchNumber, demo.MatchID, demo.Description,
                                            demo.TeamA, demo.TeamB, "dota2://matchid=" + demo.MatchID,
                                            "playdemo \"" + @"replays\" + "TI3 - " +
                                            demo.Series + @"\" + Path.GetFileNameWithoutExtension(demoname) + "\""));
                     AddHTML(demo);
                     HTMLRender.Render();
                     ConsoleExt.Log("DEBUG: {0} has {1} files ,{2} .dem files, {3} number of games.",
                                    demo.Description, Directory.GetFiles(Path.Combine(SaveDirectory, "TI3 - " + demo.Series)).Count(),
                                    Directory.GetFiles(Path.Combine(SaveDirectory, "TI3 - " + demo.Series)).Count(s => Path.GetExtension(s) == ".dem"),
                                    demo.NumberOfGames);
                     if (Directory.GetFiles(Path.Combine(SaveDirectory, "TI3 - " + demo.Series)).Count(s => Path.GetExtension(s) == ".dem") >= demo.NumberOfGames &&
                         !File.Exists(Path.Combine(SaveDirectory, "TI3 - " + demo.Series + ".zip")))
                     {
                         ConsoleExt.Log(
                             "Starting compression and upload of {0}.", matchnum);
                         Compress(demo.Series);
                     }
                     else
                     {
                         progressbar.Destroy = true;
                     }
                 }
                 catch (Exception ex)
                 {
                     progressbar.Destroy = true;
                     ConsoleExt.Log("Exception occured finalizing file: {0}", ex.Message);
                 }
             };
             Directory.CreateDirectory(Path.Combine(SaveDirectory, "TI3 - " + demo.Series));
             Downloads++;
             wc.DownloadFileAsync(new Uri(demourl), Path.Combine(SaveDirectory, "TI3 - " + demo.Series, demoname));
             ConsoleExt.AddProgressBar(progressbar);
             demo.Path = Path.Combine(SaveDirectory, "TI3 - " + demo.Series, demoname);
             return(demo);
         }
         catch (Exception ex)
         {
             ConsoleExt.Log("Download of {0} failed. Exception: {1}", matchnum, ex);
             return(null);
         }
     }
 }