示例#1
0
        private void ShowQueue()
        {
            State = ActionState.FoundAndHandled;

            string    linksPage;
            var       page     = 0;
            const int pageSize = 20;
            string    message;

            do
            {
                Console.Clear();

                Console.WriteLine("Queue:");

                var totalPages = (int)Math.Ceiling(SharedVars.DownloadQueue.Count / 20.0);

                linksPage = SharedVars.DownloadQueue.Skip(page * pageSize).Take(pageSize)
                            .Select((link, i) => $"{i + 1}. {link.Name}").Join();

                ConsoleUtils.WriteLine(linksPage, ConsoleColor.White);

                message = FormatMessage(page, totalPages, pageSize, linksPage);

                page++;
                if (page * pageSize > SharedVars.DownloadQueue.Count)
                {
                    page = 0;
                }
            } while (MenuChooseItem.AskYesNoQuestion(message,
                                                     onOther: fullInputString =>
            {
                page = OnInQueueActionWords(fullInputString, linksPage, page, pageSize);
            }));
        }
        protected void ConfirmAction(string outcome, Action onIsYes)
        {
            var confirmMessage = string.Join("\n",
                                             $"I noticed that you entered {Type.ToLower()}, which will {outcome}",
                                             "Is that the action you wanted to perform (answer no if it was entered by mistake)? [Y/N] "
                                             );

            MenuChooseItem.AskYesNoQuestion(confirmMessage, onIsYes);
        }
示例#3
0
        private static void HandleQueueModificationBaseAction(QueueModificationBaseAction action)
        {
            List <IDownloadableLink> matchingLinks;

            switch (action.MatchingItemType)
            {
            case ItemTypeToAddRemove.Course:
                matchingLinks = SharedVars.Courses
                                .Where((course, j) => action.MatchingItems.Contains(j))
                                .SelectMany(course => SectionExtractor.ExtractSectionsForCourse(course).Result
                                            .SelectMany(section => section.Links))
                                .ToList();
                break;

            case ItemTypeToAddRemove.Section:
                matchingLinks = SharedVars.Sections
                                .Where((section, j) => action.MatchingItems.Contains(j))
                                .SelectMany(section => section.Links)
                                .ToList();
                break;

            case ItemTypeToAddRemove.Link:
                matchingLinks = SharedVars.SelectedSection.Links
                                .Where((link, j) => action.MatchingItems.Contains(j))
                                .ToList();
                break;

            default:
                matchingLinks = Enumerable.Empty <IDownloadableLink>().ToList();
                break;
            }

            if (!matchingLinks.Any())
            {
                ConsoleUtils.WriteLine("Unfortunately downloading files using multiple naming methods at once is not possible", ConsoleIOType.Error);
                return;
            }

            do
            {
                string message;
                if (action is AddAction)
                {
                    var count = matchingLinks.Except(SharedVars.DownloadQueue).Count();
                    SharedVars.DownloadQueue.AddUnique(matchingLinks);

                    message = $"Added {count} items (to revert, " +
                              "simply call Remove like you did with Add in the same way and location";
                }
                else
                {
                    var count = SharedVars.DownloadQueue.Intersect(matchingLinks).Count();
                    SharedVars.DownloadQueue.RemoveAll(link => matchingLinks.Contains(link));

                    message = $"Removed {count} items (to revert, " +
                              "simply call Add like you did with Remove in the same way and location";
                }

                ConsoleUtils.WriteLine(message, ConsoleColor.Yellow);

                if (action is AddAction)
                {
                    action = new RemoveAction();
                }
                else
                {
                    action = new AddAction();
                }
            } while (MenuChooseItem.AskYesNoQuestion("Do you want to revert now? [Y/N] "));
        }
示例#4
0
        public static async Task Run()
        {
            SharedVars.CurrentRunningActionType = RunningActionType.AskForCourse;
            while (true)
            {
                try
                {
                    switch (SharedVars.CurrentRunningActionType)
                    {
                    case RunningActionType.AskForCourse:
                        await AskForCourse();

                        break;

                    case RunningActionType.AskForSection:
                        await AskForSection();

                        break;

                    case RunningActionType.AskForMultipleLinks:
                        AskForMultipleLinks();
                        break;

                    case RunningActionType.AskForNamingMethod:
                        await AskForNamingMethod();

                        break;

                    case RunningActionType.DownloadSelectedLinks:
                        await DownloadSelectedLinks();

                        break;
                    }

                    SharedVars.CurrentRunningActionType++;
                }
                catch (BaseAction action)
                {
                    await ActionHandler.HandleAction(action);
                }

                if (SharedVars.CurrentRunningActionType == RunningActionType.Repeat)
                {
                    var startAgain = MenuChooseItem.AskYesNoQuestion("Do you want to start again? [Y/N] ",
                                                                     () =>
                    {
                        SharedVars.ChosenItemsTillNow.Clear();
                        SharedVars.CurrentRunningActionType = RunningActionType.AskForCourse;
                    },
                                                                     Dispose);

                    if (!startAgain) // onNo
                    {
                        break;
                    }
                }

                if (SharedVars.CurrentRunningActionType == RunningActionType.End)
                {
                    Dispose();
                    break;
                }
            }
        }
示例#5
0
        private static async Task CreateSession()
        {
            var httpClientHandler = new HttpClientHandler
            {
                AllowAutoRedirect = false
            };

            _downloadProgressTrackingHandler = new ProgressMessageHandler(httpClientHandler);

            SessionClient = new HttpClient(_downloadProgressTrackingHandler)
            {
                BaseAddress = new Uri("http://courses.finki.ukim.mk/"),
            };
            SessionClient.DefaultRequestHeaders.UserAgent.ParseAdd("CoursesDownloader-C# Console App");

            HttpResponseMessage login = null;

            while (true)
            {
                Console.WriteLine("Establishing connection with courses");

                try
                {
                    login = await SessionClient.GetAsyncHttp("http://courses.finki.ukim.mk/login/index.php");

                    if (!login.IsSuccessStatusCode)
                    {
                        throw new HttpRequestException();
                    }
                }
                catch (HttpRequestException)
                {
                    Console.WriteLine("Connection cannot be established");
                    var shouldRetry = MenuChooseItem.AskYesNoQuestion("Do you want to try again? [Y/N] ",
                                                                      Console.Clear,
                                                                      () => { Environment.Exit(0); });

                    if (shouldRetry) // onYes
                    {
                        continue;
                    }
                }

                break;
            }

            Console.WriteLine("Preparing CAS login");

            var text = await login.Content.ReadAsStringAsync();

            login.Dispose();

            var doc = new HtmlDocument();

            doc.LoadHtml(text);

            var hiddenInput = doc.DocumentNode.SelectNodes("//form//input[@type=\"hidden\"]");

            var loginData = new Dictionary <string, string>();

            foreach (var x in hiddenInput)
            {
                loginData[x.Attributes.First(t => t.Name == "name").Value] = x.Attributes.First(t => t.Name == "value").Value;
            }

EnterCredentialsAgain:

            var(username, password) = CredentialUtil.GetCredential(CASTarget);

            while (username.IsNullOrEmpty() || password.IsNullOrEmpty())
            {
                username = ConsoleUtils.ReadLine("Please enter your CAS username >>> ", ConsoleIOType.Question);
                password = ConsoleUtils.ReadLine("Please enter your CAS password >>> ", ConsoleIOType.Question, true);

                CredentialUtil.SetCredentials(CASTarget, username, password);
            }

            loginData["username"] = username;
            loginData["password"] = password;

            using (var loginDataContent = new FormUrlEncodedContent(loginData.ToArray()))
            {
                Console.WriteLine("Logging into CAS");

                using (var response = await SessionClient.PostAsyncHttp(login.RequestMessage.RequestUri, loginDataContent))
                {
                    // if redirected to CAS, wrong password or username
                    if (response.RequestMessage.RequestUri.Host == new Uri(CASTarget).Host)
                    {
                        ConsoleUtils.WriteLine("The username or password you entered is incorrect. Please try again");
                        CredentialUtil.RemoveCredentials(CASTarget); // remove incorrect credentials
                        goto EnterCredentialsAgain;
                    }

                    FindSessKey(await response.Content.ReadAsStringAsync());
                }
            }

            LoginTime = DateTime.Now;
        }