Exemple #1
0
        private static void LoadList(List <List <object> > datalist)
        {
            SortedDictionary <int, List <ProblemInfo> > catData
                = new SortedDictionary <int, List <ProblemInfo> >();

            //Load problem from list
            foreach (List <object> lst in datalist)
            {
                ProblemInfo plist = new ProblemInfo(lst);
                problemList.Add(plist);

                //set the file size
                string file = LocalDirectory.GetProblemHtml(plist.pnum);
                if (File.Exists(file))
                {
                    plist.FileSize = (new System.IO.FileInfo(file)).Length;
                }

                SetProblem(plist.pnum, plist);
                SetNumber(plist.pid, plist.pnum);

                //Categorize
                if (!catData.ContainsKey(plist.Volume))
                {
                    catData.Add(plist.Volume, new List <ProblemInfo>());
                }
                catData[plist.Volume].Add(plist);
            }

            //add volume category
            var volCat = new CategoryNode("Volumes", "Problem list by volumes");

            categoryRoot.branches.Add(volCat);
            foreach (var data in catData.Values)
            {
                string vol = string.Format("Volume {0:000}", data[0].Volume);
                var    nod = new CategoryNode(vol, "", volCat);
                volCat.branches.Add(nod);
                foreach (var p in data)
                {
                    nod.problems.Add(new CategoryProblem(p.pnum));
                }
            }
            volCat.ProcessData();
        }
Exemple #2
0
        /// <summary>
        /// Parse HTML file and find all local contents to download.
        /// </summary>
        /// <param name="pnum">Problem number</param>
        /// <param name="replace">True, if you want to replace old files.</param>
        /// <returns>List of files to download</returns>
        public static List <DownloadTask> ProcessHtmlContent(long pnum, bool replace)
        {
            try
            {
                string external = string.Format("http://uva.onlinejudge.org/external/{0}/", pnum / 100);

                string filepath = LocalDirectory.GetProblemHtml(pnum);
                if (!File.Exists(filepath))
                {
                    return(new List <DownloadTask>());
                }

                List <string>       urls  = new List <string>();
                List <DownloadTask> tasks = new List <DownloadTask>();

                HtmlAgilityPack.HtmlDocument htdoc = new HtmlAgilityPack.HtmlDocument();
                htdoc.Load(filepath);
                DFS(htdoc.DocumentNode, urls);
                htdoc.Save(filepath);

                foreach (string str in urls)
                {
                    string url = str.StartsWith("./") ? str.Remove(0, 2) : str;
                    while (url.StartsWith("/"))
                    {
                        url = url.Remove(0, 1);
                    }
                    string file = url.Replace('/', Path.DirectorySeparatorChar);
                    file = LocalDirectory.GetProblemContent(pnum, file);
                    if (replace || LocalDirectory.GetFileSize(file) < 10)
                    {
                        tasks.Add(new DownloadTask(external + url, file, pnum));
                    }
                }

                urls.Clear();
                return(tasks);
            }
            catch (Exception ex)
            {
                Logger.Add(ex.Message, "Internet");
                return(new List <DownloadTask>());
            }
        }
        private void DownloadProblem()
        {
            //initial data
            int      total    = 2;
            int      finished = 0;
            long     vol      = current / 100;
            string   status   = "";
            FileInfo pdffile  = new FileInfo(LocalDirectory.GetProblemPdf(current));
            FileInfo htmlfile = new FileInfo(LocalDirectory.GetProblemHtml(current));
            string   pdf      = string.Format("http://uva.onlinejudge.org/external/{0}/{1}.pdf", vol, current);
            string   html     = string.Format("http://uva.onlinejudge.org/external/{0}/{1}.html", vol, current);

            //download HTML file
            if (ReplaceOldFiles == 0 || ReplaceOldFiles == 1 ||
                !htmlfile.Exists || htmlfile.Length < 100)
            {
                status = "Downloading " + htmlfile.Name + "... ";
                backgroundWorker1.ReportProgress(100 * finished / total, status);
                status = DownloadFile(html, htmlfile.FullName, 100);
                ++finished;
                backgroundWorker1.ReportProgress(100 * finished / total, status);
            }

            if (!Internet.Downloader.IsInternetConnected())
            {
                status = "Not connected to the Internet";
                backgroundWorker1.ReportProgress(0, status);
                CurrentState = State.Cancelling;
            }
            if (CurrentState != State.Running)
            {
                return;
            }

            //download PDF file
            if (ReplaceOldFiles == 0 || ReplaceOldFiles == 2 ||
                !pdffile.Exists || pdffile.Length < 200)
            {
                status = "Downloading " + pdffile.Name + "... ";
                backgroundWorker1.ReportProgress(100 * finished / total, status);
                status = DownloadFile(pdf, pdffile.FullName, 200);
                ++finished;
                backgroundWorker1.ReportProgress(100 * finished / total, status);
            }

            if (!Internet.Downloader.IsInternetConnected())
            {
                status = "Not connected to the Internet";
                backgroundWorker1.ReportProgress(0, status);
                CurrentState = State.Cancelling;
            }
            if (CurrentState != State.Running)
            {
                return;
            }

            //download HTML contents
            var list = Functions.ProcessHtmlContent(current,
                                                    ReplaceOldFiles == 0 || ReplaceOldFiles == 3);

            finished = 0;
            total    = list.Count;
            foreach (var itm in list)
            {
                if (CurrentState != State.Running)
                {
                    return;
                }

                if (Internet.Downloader.IsInternetConnected())
                {
                    status = "Downloading " + Path.GetFileName(itm.FileName) + "... ";
                    backgroundWorker1.ReportProgress(100 * finished / total, status);
                    status = DownloadFile(itm.Url.ToString(), itm.FileName, 10);
                    ++finished;
                    backgroundWorker1.ReportProgress(100 * finished / total, status);
                }
                else
                {
                    status = "Not connected to the Internet";
                    backgroundWorker1.ReportProgress(0, status);
                    CurrentState = State.Cancelling;
                }
            }
        }