Пример #1
0
        public static string CreatePDFFileFromTxtFile(string textfilefullpath, string playerName)
        {
            Document doc     = new Document();
            Section  section = doc.AddSection();

            section.PageSetup.TopMargin    = 25;
            section.PageSetup.LeftMargin   = 30;
            section.PageSetup.RightMargin  = 30;
            section.PageSetup.BottomMargin = 25;

            MigraDoc.DocumentObjectModel.Font font = new MigraDoc.DocumentObjectModel.Font("Adobe Garamond Pro", 22);
            font.Bold      = true;
            font.Italic    = false;
            font.Underline = Underline.None;
            Paragraph paragraph = section.AddParagraph();

            paragraph.AddFormattedText(playerName + "'s Stats up to Season " + ConfigFile.SEASON_LAST, font);
            paragraph.Format.Shading.Color = Colors.LightSteelBlue;
            paragraph.Format.Alignment     = ParagraphAlignment.Center;
            Paragraph paragraph2 = section.AddParagraph();

            paragraph2.AddFormattedText("  ", new MigraDoc.DocumentObjectModel.Font("Courier New", 24));

            if (new FileInfo(Path.Combine(ConfigFile.DIRECTORY_OUTPUT.FullName, "chart.png")).Exists)
            {
                MigraDoc.DocumentObjectModel.Shapes.Image image = section.AddImage(Path.Combine(ConfigFile.DIRECTORY_OUTPUT.FullName, "chart.png"));
                image.Width              = "18cm";
                image.LockAspectRatio    = true;
                image.Left               = MigraDoc.DocumentObjectModel.Shapes.ShapePosition.Center;
                image.LineFormat.Visible = true;
                image.LineFormat.Width   = 1;
                image.LineFormat.Color   = Colors.LightGreen;
            }
            Paragraph paragraph3 = section.AddParagraph();

            paragraph3.AddFormattedText("  ", new MigraDoc.DocumentObjectModel.Font("Courier New", 10));

            if (new FileInfo(Path.Combine(ConfigFile.DIRECTORY_OUTPUT.FullName, "chart2.png")).Exists)
            {
                MigraDoc.DocumentObjectModel.Shapes.Image image = section.AddImage(Path.Combine(ConfigFile.DIRECTORY_OUTPUT.FullName, "chart2.png"));
                image.Width              = "18cm";
                image.LockAspectRatio    = true;
                image.Left               = MigraDoc.DocumentObjectModel.Shapes.ShapePosition.Center;
                image.LineFormat.Visible = true;
                image.LineFormat.Width   = 1;
                image.LineFormat.Color   = Colors.LightCoral;
            }
            Paragraph paragraph4 = section.AddParagraph();

            paragraph4.AddFormattedText("  ", new MigraDoc.DocumentObjectModel.Font("Courier New", 10));

            if (new FileInfo(Path.Combine(ConfigFile.DIRECTORY_OUTPUT.FullName, "chart3.png")).Exists)
            {
                MigraDoc.DocumentObjectModel.Shapes.Image image = section.AddImage(Path.Combine(ConfigFile.DIRECTORY_OUTPUT.FullName, "chart3.png"));
                image.Width              = "18cm";
                image.LockAspectRatio    = true;
                image.Left               = MigraDoc.DocumentObjectModel.Shapes.ShapePosition.Center;
                image.LineFormat.Visible = true;
                image.LineFormat.Width   = 1;
                image.LineFormat.Color   = Colors.LightBlue;
            }

            section.AddPageBreak();


            //just font arrangements as you wish

            section = DataOps.PrintDataSeason(section, textfilefullpath);
            section.AddPageBreak();
            section = DataOps.PrintDataByCategory(section, textfilefullpath);
            //add each line to pdf
            //foreach (string line in textFileLines)
            //{
            //    MigraDoc.DocumentObjectModel.Font font = new MigraDoc.DocumentObjectModel.Font("Courier New", 10);
            //    font.Bold = true;

            //    Paragraph paragraph = section.AddParagraph();
            //    paragraph.AddFormattedText(line, font);

            //}


            //save pdf document
            PdfDocumentRenderer renderer = new PdfDocumentRenderer();

            renderer.Document = doc;
            renderer.RenderDocument();
            FileInfo fileName = new FileInfo(Path.Combine(ConfigFile.DIRECTORY_OUTPUT.FullName, playerName + "_" + ConfigFile.SEASON_LAST.ToString() + ".pdf"));

            FileOps.CheckFile(fileName, true);
            renderer.Save(fileName.FullName);
            //if(fileName.Exists)
            //    Process.Start(fileName.FullName);
            return(fileName.FullName);
        }
Пример #2
0
        /// <summary>
        /// Downloads all data inside a range determined by
        /// parameters.
        /// </summary>
        /// <param name="season"></param>
        /// <param name="type"></param>
        /// <param name="email"></param>
        /// <param name="password"></param>
        /// <param name="startPage"></param>
        /// <param name="finishPage"></param>
        /// <param name="taskNumber"></param>
        /// <param name="dData"></param>
        /// <param name="bar"></param>
        /// <param name="writeDB"></param>
        public static async void LoginAndDownload(int?season      = null, int?type           = null, string email    = null,
                                                  string password = null, int?startPage      = null, int?finishPage  = null,
                                                  int?taskNumber  = null, DownloadData dData = null, ProgressBar bar = null,
                                                  bool writeDB    = false)
        {
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls
                                                   | SecurityProtocolType.Tls11
                                                   | SecurityProtocolType.Tls12
                                                   | SecurityProtocolType.Ssl3;
            ConfigFile.IncrementTaskStarted();
            if (dData != null)
            {
                if (dData.Season != null && season == null)
                {
                    season = dData.Season;
                }
                if (dData.Type != null && type == null)
                {
                    type = dData.Type;
                }
                if (dData.User != null && email == null)
                {
                    email = dData.User;
                }
                if (dData.Password != null && password == null)
                {
                    password = dData.Password;
                }
                if (dData.StartPage != null && startPage == null)
                {
                    startPage = dData.StartPage;
                }
                if (dData.EndPage != null && finishPage == null)
                {
                    finishPage = dData.EndPage;
                }
                if (dData.TaskNumber != null && taskNumber == null)
                {
                    taskNumber = dData.TaskNumber;
                }
            }
            if (string.IsNullOrWhiteSpace(email))
            {
                email = ConfigFile.DEFAULT_USER;
            }
            if (string.IsNullOrWhiteSpace(password))
            {
                password = ConfigFile.DEFAULT_PASS;
            }
            if (season == null)
            {
                season = 1;
            }
            if (type == null)
            {
                type = 0;
            }
            if (startPage == null)
            {
                startPage = ConfigFile.MIN_PAGES;
            }
            if (finishPage == null)
            {
                finishPage = ConfigFile.MAX_PAGES;
            }
            if (string.IsNullOrWhiteSpace(email) || string.IsNullOrWhiteSpace(password))
            {
                if (string.IsNullOrWhiteSpace(email))
                {
                    Mex.AddMessage("Task " + (taskNumber != null ? taskNumber : 0) + " is missing the user email. Skipping", Mex.ERROR);
                }
                if (string.IsNullOrWhiteSpace(password))
                {
                    Mex.AddMessage("Task " + (taskNumber != null ? taskNumber : 0) + " is missing the user password. Skipping", Mex.ERROR);
                }
                Mex.PrintErrorMessagesInForm();
                Mex.RemoveAll();
                return;
            }
            // END VALIDATION SECTION

            string typeStr = null;

            switch (type)
            {
            case 0:
                typeStr = "GENERAL";
                break;

            case 1:
                typeStr = "LIGHT";
                break;

            case 2:
                typeStr = "MEDIUM";
                break;

            case 3:
                typeStr = "HEAVY";
                break;

            case 4:
                typeStr = "ASSAULT";
                break;
            }
            string fileName;

            if (season < 10)
            {
                fileName = "S0" + season + "_" + typeStr + ".txt";
            }
            else
            {
                fileName = "S" + season + "_" + typeStr + ".txt";
            }

            //string DirDestination = @"C:\TEST\Output\";
            string DirDestination = ConfigFile.DIRECTORY_OUTPUT.FullName;

            FileInfo fileOutput = new FileInfo(Path.Combine(DirDestination, fileName));

            FileOps.CheckFile(fileOutput);

            season      = season - 1; //adjust to 'base 0' web request
            finishPage += 1;          //adjust to include last page
            string BaseAddress     = "https://mwomercs.com/do/login";
            var    cookieContainer = new CookieContainer();
            Uri    uri             = new Uri("https://mwomercs.com/profile/leaderboards");
            var    handler         = new HttpClientHandler();

            handler.CookieContainer = cookieContainer;
            handler.CookieContainer.Add(uri, new System.Net.Cookie("leaderboard_season", season.ToString()));
            using (var client = new HttpClient(handler)
            {
                BaseAddress = new Uri(BaseAddress)
            })
            {
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/xml"));
                HttpResponseMessage risposta = await client.PostAsync(BaseAddress, new FormUrlEncodedContent(
                                                                          new[]
                {
                    new KeyValuePair <string, string> ("email", email),
                    new KeyValuePair <string, string> ("password", password)
                })
                                                                      );

                string responseBodyAsText = await risposta.Content.ReadAsStringAsync();

                Logger.PrintF(new FileInfo(Path.Combine(DirDestination, fileName)).FullName, "** STARTING DOWNLOAD", true);
                string ownRank        = "<tr class=\"yourRankRow\">";
                string endPages       = "<td colspan='10'>No results found";
                string resp           = null;
                int    lastPage       = 0;
                int    startPageReal  = (startPage == null ? 0 : (int)startPage);
                int    finishPageReal = (finishPage == null ? 10000 : (int)finishPage);
                bar.Maximum = finishPageReal - startPageReal;
                bar.Value   = 0;
                for (int page = startPageReal; page < finishPageReal; page++)
                {
                    risposta = await client.GetAsync("https://mwomercs.com/profile/leaderboards?page=" + page.ToString() + "&type=" + type.ToString());

                    responseBodyAsText = await risposta.Content.ReadAsStringAsync();

                    resp = DataOps.ParseHTML(responseBodyAsText);
                    if (resp.Contains(ownRank))
                    {
                        resp = resp.Replace(ownRank, string.Empty);
                    }

                    if (resp.Contains(endPages))
                    {
                        lastPage = page;
                        resp     = resp.Replace(endPages, string.Empty);
                        Logger.PrintF(Path.Combine(DirDestination, fileName), resp, false);
                        if (bar != null)
                        {
                            bar.Value = bar.Maximum;
                        }
                        break;
                    }
                    Logger.PrintF(Path.Combine(DirDestination, fileName), resp, false);
                    if (bar != null)
                    {
                        bar.Value++;
                    }
                }
                Logger.PrintF(Path.Combine(DirDestination, fileName), "** FINISH DOWNLOADING", true);
            }
            if (writeDB)
            {
                //DataOps.WriteToDB(new FileInfo(Path.Combine(DirDestination, fileName).ToString()), (int)season + 1, (int)type);
            }
            ConfigFile.IncrementTaskFinished();
        }