示例#1
0
        static void Main(string[] args)
        {
            SaveDirectory  = args[0];
            ScheduleURL    = args[1];
            CompressSeries = args[2] == "compressseries";
            ConsoleExt.Start();
            HTMLRender.Initialize();
            var downloaded = File.ReadAllLines("downloaded.txt");

            Mega = new Mega();
            foreach (var line in downloaded)
            {
                MatchList.Add(int.Parse(line));
                DemoList.Add(int.Parse(line), null);
            }
            ConsoleExt.Log("Starting.");
            if (CompressSeries)
            {
                var account = File.ReadAllLines("megaaccount.txt");
                ConsoleExt.Log("Logging into Mega.");
                Mega2.Init(new MegaUser(account[0], account[1]), (a =>
                {
                    ConsoleExt.Log("Logged into Mega.");
                    Mega2 = a;
                    Mega.sid2 = a.User.Sid;
                }), (a =>
                {
                    ConsoleExt.Log(
                        "Failed to log into Mega. Error code: {0}",
                        a);
                    Mega2 = null;
                }));
            }
            if (!File.Exists("matchiddownload.txt"))
            {
                ConsoleExt.Log("Starting spider on {0}.", ScheduleURL);
                CheckLoop();
            }
            else
            {
                ConsoleExt.Log("Found matchiddownload.txt, forcing regular downloads.");
                var data   = File.ReadAllLines("matchiddownload.txt");
                var first  = true;
                var series = "";
                foreach (string t in data)
                {
                    if (first)
                    {
                        series = t;
                        first  = false;
                    }
                    else if (string.IsNullOrWhiteSpace(t))
                    {
                        first = true;
                    }
                    else
                    {
                        if (!MatchIDList.ContainsKey(series))
                        {
                            MatchIDList.Add(series, new List <string>());
                        }
                        MatchIDList[series].Add(t);
                    }
                }
                foreach (var pair in MatchIDList)
                {
                    for (int i = 0; i < pair.Value.Count; i++)
                    {
                        var game = pair.Value[i];
                        DownloadDemoId(game, pair.Key, ((i + 1) == pair.Value.Count));
                    }
                }
            }
            ConsoleExt.Log("Done.");
            Console.Read();
        }
示例#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);
         }
     }
 }