Ejemplo n.º 1
0
 private void TryAdd(string url, string id, string ext)
 {
     if (!ValidExtensions.Contains(ext))
     {
         Logger.WriteLine($"Unknown extension found: {ext}");
         return;
     }
     if (table.ContainsKey(id))
     {
         KattisProblem problem = table[id];
         string        lang    = ValidExtensions.Get(ext);
         if (!problem.Contains(lang))
         {
             Logger.WriteLine($"Added {lang} to problem {id}");
             problem.Add(lang, url);
         }
         else
         {
             Logger.WriteLine($"{lang} already found in problem {id}");
         }
     }
     else
     {
         string name = string.Empty;
         if (Mapping.ContainsKey(id))
         {
             name = Mapping.Get(id);
         }
         else
         {
             HtmlDocument doc  = web.Load($"https://open.kattis.com/problems/{id}");
             HtmlNode     node = doc.DocumentNode.SelectSingleNode("//head/title");
             if (node != null)
             {
                 Match match = Regex.Match(node.InnerHtml, @"(.+) – Kattis, Kattis");
                 name = WebUtility.HtmlDecode(match.Groups[1].ToString());
             }
             else
             {
                 Logger.WriteLine($"Could not find title: https://open.kattis.com/problems/{id}");
             }
         }
         if (!string.IsNullOrEmpty(name) && !name.Equals("404: Not Found"))
         {
             table.Add(id, new KattisProblem(name, id));
             KattisProblem problem = table[id];
             string        lang    = ValidExtensions.Get(ext);
             if (!problem.Contains(lang))
             {
                 Logger.WriteLine($"Added {lang} to problem {id}");
                 problem.Add(lang, url);
             }
             else
             {
                 Logger.WriteLine($"{lang} already found in problem {id}");
             }
         }
         else
         {
             Logger.WriteLine($"Invalid name found with: {url} {id} {ext}");
         }
     }
 }
Ejemplo n.º 2
0
 private static void check(string githubProblemURL, string githubProblemID, string githubProblemExt, HashSet <string> ignore, HashSet <string> IDsAddedToTable, SortedDictionary <string, SortedSet <KattisProblem> > table, Dictionary <string, string> idToNameMap)
 {
     log.WriteLine("Checking: " + githubProblemID + githubProblemExt);
     // check if valid extension
     if (!Extensions.names.ContainsKey(githubProblemExt))
     {
         log.WriteLine("Found invalid extention '" + githubProblemExt + "'");
         return;
     }
     // check if need to ignore the problem
     if (ignore.Contains(githubProblemID) || ignore.Contains(githubProblemExt) || ignore.Contains(githubProblemID + githubProblemExt))
     {
         log.WriteLine("Ignored " + githubProblemID + githubProblemExt);
         return;
     }
     // check if proper formatting
     if (!Regex.IsMatch(githubProblemID + githubProblemExt, @"^[a-zA-Z\d]+\.[a-zA-Z\d]+$"))
     {
         log.WriteLine("Incorrect format found '" + githubProblemID + githubProblemExt + "'");
         return;
     }
     // check if this ID is already in the table
     if (IDsAddedToTable.Contains(githubProblemID))
     {
         // use foreach loop since the key value of table is a SortedSet, and you cannot access indexes.
         // could be improved but most problems dont have the same name, so doesn't impact performance enough
         foreach (KattisProblem p in table[idToNameMap[githubProblemID]])
         {
             if (p.id == githubProblemID)
             {
                 string actualLang = Extensions.names[githubProblemExt];
                 // check if the actual language is already recorded - if not, add it; otherwise check more stuff
                 if (p.langs.ContainsKey(actualLang))
                 {
                     // check if the extension is already in - if it is, dont re-add it; otherwise add it
                     if (!p.langs[actualLang].ContainsKey(githubProblemExt))
                     {
                         p.langs[actualLang].Add(githubProblemExt, githubProblemURL);
                     }
                 }
                 else
                 {
                     p.langs.Add(actualLang, new SortedDictionary <string, string> {
                         { githubProblemExt, githubProblemURL }
                     });
                 }
                 break;
             }
         }
     }
     else
     {
         string kattisName = string.Empty;
         // this is true if user hasn't updated their KattisIDNameMapping.txt
         if (!idToNameMap.ContainsKey(githubProblemID))
         {
             Stream kattisStream = getURLStream("https://open.kattis.com/problems/" + githubProblemID);
             // this is true if the ID is a nonexistent kattis problem
             if (kattisStream == null)
             {
                 log.WriteLine("Invalid link found 'https://open.kattis.com/problems/" + githubProblemID + "'");
                 return;
             }
             log.WriteLine("Recommendation: You should update KattisIDNameMapping.txt to ensure fast speeds");
             CustomReader kattisReader = new CustomReader(kattisStream);
             string       line;
             while ((line = kattisReader.NextLine()) != null)
             {
                 // a line with "headline-wrapper" has the name of the problem in it
                 if (!line.Contains("headline-wrapper", StringComparison.Ordinal))
                 {
                     continue;
                 }
                 assignKattisName(line, out kattisName);
                 break;
             }
             kattisStream.Close();
             kattisReader.Close();
             // fix weird html that causes apostrophes to be &#039;
             if (kattisName.Contains("&#039;", StringComparison.Ordinal))
             {
                 kattisName = kattisName.Replace("&#039;", "'");
             }
         }
         else
         {
             kattisName = idToNameMap[githubProblemID];
         }
         IDsAddedToTable.Add(githubProblemID);
         SortedDictionary <string, SortedDictionary <string, string> > langs = new SortedDictionary <string, SortedDictionary <string, string> > {
             { Extensions.names[githubProblemExt], new SortedDictionary <string, string> {
                   { githubProblemExt, githubProblemURL }
               } }
         };
         KattisProblem prob = new KattisProblem(githubProblemID, kattisName, langs);
         // check if the name is already in the table since some problems can have the same names but different IDs
         if (table.ContainsKey(kattisName))
         {
             table[kattisName].Add(prob);
         }
         else
         {
             table.Add(kattisName, new SortedSet <KattisProblem> {
                 prob
             });
         }
     }
 }