예제 #1
0
        public static void InitStreams()
        {
            while (true)
            {
                try
                {
                    MountainProjectDataSearch.InitMountainProjectData(xmlPath);
                    break;
                }
                catch
                {
                    ConsoleHelper.Write("MountainProjectAreas.xml is in use. Waiting 5s before trying again...");
                    Thread.Sleep(TimeSpan.FromSeconds(5));
                }
            }

            BotFunctions.RedditHelper = new RedditHelper();
            BotFunctions.RedditHelper.Auth(credentialsPath).Wait();
            requestForApprovalURL = GetCredentialValue(credentialsPath, "requestForApprovalURL");
            WebServerURL          = GetCredentialValue(credentialsPath, "webServerURL");
            spreadsheetHistoryURL = GetCredentialValue(credentialsPath, "spreadsheetURL");

            //Start approval server
            ApprovalServer = new Server(9999)
            {
                HandleRequest = ApprovalServerRequestHandler.HandleRequest
            };
            ApprovalServer.Start();
        }
예제 #2
0
        public void TestSearch()
        {
            MountainProjectDataSearch.InitMountainProjectData(@"..\..\MountainProjectDBBuilder\bin\MountainProjectAreas.xml");

            for (int i = 0; i < testCriteria_search.GetLength(0); i++)
            {
                string query       = testCriteria_search[i, 0];
                string expectedUrl = testCriteria_search[i, 1];

                _ = ResultParameters.ParseParameters(ref query); //This is here just to filter out any query items (not to be used)
                SearchParameters searchParameters = SearchParameters.ParseParameters(ref query);

                SearchResult searchResult = MountainProjectDataSearch.Search(query, searchParameters);

                if (string.IsNullOrEmpty(expectedUrl))
                {
                    Assert.IsNull(searchResult.FilteredResult, "Failed for " + testCriteria_search[i, 0]);
                }
                else
                {
                    Assert.AreEqual(Utilities.MPBASEURL + expectedUrl, searchResult.FilteredResult.URL, "Failed for " + testCriteria_search[i, 0]);
                }

                Assert.IsTrue(searchResult.TimeTaken.TotalSeconds < 5, $"{query} took too long ({searchResult.TimeTaken.TotalMilliseconds} ms)");
            }
        }
예제 #3
0
        static void Main(string[] args)
        {
            Console.OutputEncoding = System.Text.Encoding.UTF8;

            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;

            if (args.FirstOrDefault(p => p.Contains("xmlpath=")) != null)
            {
                xmlPath = args.FirstOrDefault(p => p.Contains("xmlpath=")).Split('=')[1];
            }

            if (args.FirstOrDefault(p => p.Contains("credentials=")) != null)
            {
                credentialsPath = args.FirstOrDefault(p => p.Contains("credentials=")).Split('=')[1];
            }

            if (args.FirstOrDefault(p => p.Contains("repliedto=")) != null)
            {
                repliedToPath = args.FirstOrDefault(p => p.Contains("repliedto=")).Split('=')[1];
            }

            if (args.FirstOrDefault(p => p.Contains("blacklisted=")) != null)
            {
                blacklistedPath = args.FirstOrDefault(p => p.Contains("blacklisted=")).Split('=')[1];
            }

            CheckRequiredFiles();
            MountainProjectDataSearch.InitMountainProjectData(xmlPath);
            redditHelper.Auth(credentialsPath).Wait();
            DoBotLoop().Wait();
        }
예제 #4
0
 private void InitMPData()
 {
     //The MountainProject data will actually persist between unit tests if multiple unit tests are run at once. Doing it this way
     //ensures that the delay time to init the MountainProject data is only part of the first test
     if (MountainProjectDataSearch.DestAreas.Count == 0)
     {
         MountainProjectDataSearch.InitMountainProjectData(@"..\..\MountainProjectDBBuilder\bin\MountainProjectAreas.xml");
     }
 }
예제 #5
0
        public void TestCommentBodyParse()
        {
            MountainProjectDataSearch.InitMountainProjectData(@"..\..\MountainProjectDBBuilder\bin\MountainProjectAreas.xml");

            for (int i = 0; i < testCriteria_keyword.GetLength(0); i++)
            {
                string commentBody = testCriteria_keyword[i, 0];
                string expectedUrl = testCriteria_keyword[i, 1];
                string resultReply = BotReply.GetReplyForCommentBody(commentBody);

                Assert.IsTrue(resultReply.Contains(expectedUrl));
            }
        }
예제 #6
0
        public void TestSearch()
        {
            MountainProjectDataSearch.InitMountainProjectData(@"..\..\MountainProjectDBBuilder\bin\MountainProjectAreas.xml");

            for (int i = 0; i < testCriteria_search.GetLength(0); i++)
            {
                string   query       = testCriteria_search[i, 0];
                string   expectedUrl = testCriteria_search[i, 1];
                MPObject result      = MountainProjectDataSearch.SearchMountainProject(query);

                Assert.AreEqual(Utilities.MPBASEURL + expectedUrl, result.URL);
            }
        }
예제 #7
0
        private static void ParseInputString()
        {
            MountainProjectDataSearch.InitMountainProjectData(serializationPath);
            if (MountainProjectDataSearch.DestAreas.Count() == 0)
            {
                Console.WriteLine("The xml either doesn't exist or is empty");
                Environment.Exit(0);
            }

            Console.WriteLine("File read.");

            bool keepSearching = true;

            while (keepSearching)
            {
                Console.WriteLine("\n\nPlease input the string you would like to parse: ");
                string input = Console.ReadLine();

                Stopwatch stopwatch = Stopwatch.StartNew();
                MPObject  result    = MountainProjectDataSearch.SearchMountainProject(input);
                stopwatch.Stop();

                if (result == null)
                {
                    Console.WriteLine("Nothing found matching \"" + input + "\"");
                }
                else
                {
                    string resultStr = "";
                    if (result is Area)
                    {
                        resultStr = (result as Area).ToString();
                    }
                    else if (result is Route)
                    {
                        resultStr = (result as Route).ToString();
                    }

                    Console.WriteLine("The following was found: " + resultStr + " (Found in " + stopwatch.ElapsedMilliseconds + " ms)");
                    Console.WriteLine($"Parent: {MountainProjectDataSearch.GetParent(result, -1).Name}");
                    Console.WriteLine("\nOpen result? (y/n) ");
                    if (Console.ReadKey().Key == ConsoleKey.Y)
                    {
                        Process.Start(result.URL);
                    }
                }

                Console.WriteLine("\nSearch something else? (y/n) ");
                keepSearching = Console.ReadKey().Key == ConsoleKey.Y;
            }
        }
예제 #8
0
        public void TestLocationString()
        {
            MountainProjectDataSearch.InitMountainProjectData(@"..\..\MountainProjectDBBuilder\bin\MountainProjectAreas.xml");

            for (int i = 0; i < testCriteria_location.GetLength(0); i++)
            {
                string query            = testCriteria_location[i, 0];
                string expectedLocation = testCriteria_location[i, 1];
                string resultLocation   = BotReply.GetLocationString(MountainProjectDataSearch.SearchMountainProject(query));
                resultLocation = resultLocation.Replace("\n\n", "");        //Remove markdown newline
                resultLocation = resultLocation.Replace("Located in ", ""); //Simplify results for unit test

                Assert.AreEqual(expectedLocation, resultLocation);
            }
        }
예제 #9
0
        public void TestLocationString()
        {
            MountainProjectDataSearch.InitMountainProjectData(@"..\..\MountainProjectDBBuilder\bin\MountainProjectAreas.xml");

            for (int i = 0; i < testCriteria_location.GetLength(0); i++)
            {
                string       query            = testCriteria_location[i, 0];
                string       expectedLocation = testCriteria_location[i, 1];
                SearchResult searchResult     = MountainProjectDataSearch.Search(query);
                string       resultLocation   = BotReply.GetLocationString(searchResult.FilteredResult);
                resultLocation = resultLocation.Replace(Markdown.NewLine, "");        //Remove markdown newline
                resultLocation = resultLocation.Replace("Located in ", "");           //Simplify results for unit test
                resultLocation = Regex.Replace(resultLocation, @"\[|\]|\(.*?\)", ""); //Remove markdown link formatting

                Assert.AreEqual(expectedLocation, resultLocation, "Failed for " + testCriteria_location[i, 0]);
                Assert.IsTrue(searchResult.TimeTaken.TotalSeconds < 5, $"{query} took too long ({searchResult.TimeTaken.Milliseconds} ms)");
            }
        }
예제 #10
0
        static void Main(string[] args)
        {
            if (args.FirstOrDefault(p => p.Contains("xmlpath=")) != null)
            {
                xmlPath = args.FirstOrDefault(p => p.Contains("xmlpath=")).Split('=')[1];
            }

            if (args.FirstOrDefault(p => p.Contains("credentials=")) != null)
            {
                credentialsPath = args.FirstOrDefault(p => p.Contains("credentials=")).Split('=')[1];
            }

            if (args.FirstOrDefault(p => p.Contains("repliedto=")) != null)
            {
                repliedToPath = args.FirstOrDefault(p => p.Contains("repliedto=")).Split('=')[1];
            }

            CheckRequiredFiles();
            MountainProjectDataSearch.InitMountainProjectData(xmlPath);
            AuthReddit().Wait();
            DoBotLoop().Wait();
        }
예제 #11
0
        private static void AddNewItems()
        {
            Parsers.TotalTimer = totalTimer;
            List <Area> destAreas = Parsers.GetDestAreas();

            DateTime        lastBuild = File.GetLastWriteTime(serializationPath);
            string          rssUrl    = $"https://www.mountainproject.com/rss/new?selectedIds={string.Join(",", destAreas.Select(p => p.ID))}&routes=on&areas=on";
            SyndicationFeed feed      = null;

            using (XmlReader reader = XmlReader.Create(rssUrl))
            {
                feed = SyndicationFeed.Load(reader);
            }

            IEnumerable <string> newlyAddedItemUrls = feed.Items.Where(p => p.PublishDate > lastBuild).OrderBy(p => p.PublishDate).Select(p => p.Links[0].Uri.ToString());

            MountainProjectDataSearch.InitMountainProjectData(serializationPath);

            foreach (string newItemUrl in newlyAddedItemUrls)
            {
                string newId = Utilities.GetID(newItemUrl);

                if (MountainProjectDataSearch.GetItemWithMatchingID(newId) != null) //Item has already been added (probably via a recursive area add)
                {
                    continue;
                }

                MPObject newItem;
                if (newItemUrl.Contains(Utilities.MPAREAURL))
                {
                    newItem = new Area {
                        ID = newId
                    };
                    Parsers.ParseAreaAsync(newItem as Area).Wait();
                }
                else
                {
                    newItem = new Route {
                        ID = newId
                    };
                    Parsers.ParseRouteAsync(newItem as Route).Wait();
                }

                Area currentParent = null;
                bool itemAddedViaRecursiveParse = false;
                foreach (string parentId in newItem.ParentIDs) //Make sure all parents are populated
                {
                    MPObject matchingItem = MountainProjectDataSearch.GetItemWithMatchingID(parentId);
                    if (matchingItem == null)
                    {
                        Area newArea = new Area {
                            ID = parentId
                        };
                        Parsers.ParseAreaAsync(newArea).Wait();
                        currentParent.SubAreas.Add(newArea);
                        itemAddedViaRecursiveParse = true;
                        break;
                    }
                    else
                    {
                        currentParent = matchingItem as Area;
                    }
                }

                if (!itemAddedViaRecursiveParse)
                {
                    if (newItem is Area)
                    {
                        (MountainProjectDataSearch.GetItemWithMatchingID(newItem.ParentIDs.Last()) as Area).SubAreas.Add(newItem as Area);
                    }
                    else
                    {
                        (MountainProjectDataSearch.GetItemWithMatchingID(newItem.ParentIDs.Last()) as Area).Routes.Add(newItem as Route);
                    }
                }
            }

            destAreas = MountainProjectDataSearch.DestAreas;

            totalTimer.Stop();
            Console.WriteLine($"------PROGRAM FINISHED------ ({totalTimer.Elapsed})");
            Console.WriteLine();
            Console.WriteLine($"Total # of areas: {Parsers.TotalAreas}, total # of routes: {Parsers.TotalRoutes}");
            SerializeResults(destAreas);

            SendReport($"MountainProjectDBBuilder database updated SUCCESSFULLY in {totalTimer.Elapsed}", $"{newlyAddedItemUrls.Count()} new items:\n\n{string.Join("\n", newlyAddedItemUrls)}");
        }
예제 #12
0
        private static void ParseInputString()
        {
            MountainProjectDataSearch.InitMountainProjectData(serializationPath);
            if (MountainProjectDataSearch.DestAreas.Count() == 0)
            {
                Console.WriteLine("The xml either doesn't exist or is empty");
                Environment.Exit(0);
            }

            Console.WriteLine("File read.");

            bool keepSearching = true;

            while (keepSearching)
            {
                Console.WriteLine("\n\nPlease input the string you would like to parse: ");
                string input = Console.ReadLine();

                SearchParameters searchParameters = SearchParameters.ParseParameters(ref input);
                ResultParameters resultParameters = ResultParameters.ParseParameters(ref input);

                bool allResults = input.Contains("-all");
                if (allResults)
                {
                    input = input.Replace("-all", "").Trim();
                }

                Stopwatch    stopwatch    = Stopwatch.StartNew();
                SearchResult searchResult = MountainProjectDataSearch.Search(input, searchParameters);
                stopwatch.Stop();

                if (searchResult.IsEmpty())
                {
                    Console.WriteLine("Nothing found matching \"" + input + "\"");
                }
                else if (allResults)
                {
                    List <MPObject> matchedObjectsByPopularity = searchResult.AllResults.OrderByDescending(p => p.Popularity).ToList();
                    Console.WriteLine($"Found {matchedObjectsByPopularity.Count} items match that search query (found in {stopwatch.ElapsedMilliseconds} ms):");
                    foreach (MPObject result in matchedObjectsByPopularity)
                    {
                        string url = result.URL.Replace(Utilities.MPBASEURL, "");
                        if (result is Route)
                        {
                            Console.WriteLine($"    Route: {result.Name} (Pop: {result.Popularity}) | Location: {GetLocationString(result)} | {url}");
                        }
                        else if (result is Area)
                        {
                            Console.WriteLine($"    Area: {result.Name} (Pop: {result.Popularity}) | Location: {GetLocationString(result)} | {url}");
                        }
                    }
                }
                else
                {
                    string   resultStr = "";
                    MPObject result    = searchResult.FilteredResult;
                    if (result is Area)
                    {
                        resultStr = (result as Area).ToString();
                    }
                    else if (result is Route)
                    {
                        resultStr = (result as Route).ToString(resultParameters);
                    }

                    Console.WriteLine($"The following was found (found in {stopwatch.ElapsedMilliseconds} ms):");
                    Console.WriteLine("    " + resultStr);
                    Console.WriteLine($"    Location: {GetLocationString(result, searchResult.RelatedLocation)}");
                    Console.WriteLine("\nOpen result? (y/n) ");
                    if (Console.ReadLine().ToLower() == "y")
                    {
                        Process.Start(result.URL);
                    }
                }

                Console.WriteLine("\nSearch something else? (y/n) ");
                keepSearching = Console.ReadLine().ToLower() == "y";
            }
        }