def _redeem_courses_ui(
    driver,
    settings: Settings,
    scrapers: ScraperManager,
) -> None:
    """
    Method to scrape courses from the supported sites and enroll in them on udemy.

    :param WebDriver driver: WebDriver to use to complete enrolment
    :param Settings settings: Core settings used for Udemy
    :param ScraperManager scrapers:
    :return:
    """
    udemy_actions = UdemyActionsUI(driver, settings)
    udemy_actions.login()
    loop = asyncio.get_event_loop()

    while True:
        udemy_course_links = loop.run_until_complete(scrapers.run())

        if udemy_course_links:
            for course_link in set(
                udemy_course_links
            ):  # Cast to set to remove duplicate links
                try:
                    status = udemy_actions.enroll(course_link)
                    if status == UdemyStatus.ENROLLED.value:
                        # Try to avoid udemy throttling by sleeping for 1-5 seconds
                        sleep_time = random.choice(range(1, 5))
                        logger.debug(
                            f"Sleeping for {sleep_time} seconds between enrolments"
                        )
                        time.sleep(sleep_time)
                except NoSuchElementException as e:
                    logger.error(f"No such element: {e}")
                except TimeoutException:
                    logger.error(f"Timeout on link: {course_link}")
                except WebDriverException:
                    logger.error(f"Webdriver exception on link: {course_link}")
                except KeyboardInterrupt:
                    udemy_actions.stats.table()
                    logger.warning("Exiting the script")
                    return
                except exceptions.RobotException as e:
                    logger.error(e)
                    return
                except Exception as e:
                    logger.error(f"Unexpected exception: {e}")
                finally:
                    if settings.is_ci_build:
                        logger.info("We have attempted to subscribe to 1 udemy course")
                        logger.info("Ending test")
                        return
        else:
            udemy_actions.stats.table()
            logger.info("All scrapers complete")
            return
示例#2
0
def _redeem_courses(
    settings: Settings,
    scrapers: ScraperManager,
) -> None:
    """
    Method to scrape courses from tutorialbar.com and enroll in them on udemy

    :param Settings settings: Core settings used for Udemy
    :param ScraperManager scrapers:
    :return:
    """
    udemy_actions = UdemyActions(settings)
    udemy_actions.login()
    loop = asyncio.get_event_loop()

    while True:
        udemy_course_links = loop.run_until_complete(scrapers.run())

        if udemy_course_links:
            for course_link in udemy_course_links:
                try:
                    status = udemy_actions.enroll(course_link)
                    if status == UdemyStatus.ENROLLED.value:
                        # Try to avoid udemy throttling by sleeping for 1-5 seconds
                        sleep_time = random.choice(range(1, 5))
                        logger.debug(
                            f"Sleeping for {sleep_time} seconds between enrolments"
                        )
                        time.sleep(sleep_time)
                except KeyboardInterrupt:
                    logger.error("Exiting the script")
                    return
                except Exception as e:
                    logger.error(f"Unexpected exception: {e}")
                finally:
                    if settings.is_ci_build:
                        logger.info(
                            "We have attempted to subscribe to 1 udemy course")
                        logger.info("Ending test")
                        return
        else:
            logger.info("All scrapers complete")
            return
示例#3
0
def _redeem_courses(
    settings: Settings,
    scrapers: ScraperManager,
) -> None:
    """
    Method to scrape courses from tutorialbar.com and enroll in them on udemy

    :param Settings settings: Core settings used for Udemy
    :param ScraperManager scrapers:
    :return:
    """
    cache = CourseCache()
    udemy_actions = UdemyActions(settings)
    udemy_actions.login()
    loop = asyncio.get_event_loop()

    while True:
        udemy_course_links = loop.run_until_complete(scrapers.run())

        if udemy_course_links:
            for course_link in udemy_course_links:
                try:
                    if course_link not in cache:
                        status = udemy_actions.enroll(course_link)
                        cache.add(course_link, status)
                        time.sleep(2)  # Try to avoid udemy throttling
                    else:
                        logger.debug(f"In cache: {course_link}")
                except KeyboardInterrupt:
                    logger.error("Exiting the script")
                    return
                except Exception as e:
                    logger.error(f"Unexpected exception: {e}")
                finally:
                    if settings.is_ci_build:
                        logger.info(
                            "We have attempted to subscribe to 1 udemy course")
                        logger.info("Ending test")
                        return
        else:
            logger.info("All scrapers complete")
            return
def _redeem_courses(settings: Settings,
                    scrapers: ScraperManager,
                    counter_enroled: int = 0,
                    counter_already_enroled: int = 0,
                    counter_expired: int = 0,
                    counter_other_languages: int = 0,
                    counter_other_categories: int = 0,
                    counter_total: int = 0) -> None:
    """
    Method to scrape courses from tutorialbar.com and enroll in them on udemy

    :param Settings settings: Core settings used for Udemy
    :param ScraperManager scrapers:
    :return:
    """
    udemy_actions = UdemyActions(settings)
    udemy_actions.login()
    loop = asyncio.get_event_loop()

    while True:
        udemy_course_links = loop.run_until_complete(scrapers.run())
        logger.info(f"Total courses this time: {len(udemy_course_links)}")
        if udemy_course_links:
            for course_link in udemy_course_links:
                counter_total += 1
                try:
                    status = udemy_actions.enroll(course_link)

                    if status == UdemyStatus.ENROLLED.value:
                        counter_enroled += 1
                        # Try to avoid udemy throttling by sleeping for 1-5 seconds
                        sleep_time = random.choice(range(1, 5))
                        logger.debug(
                            f"Sleeping for {sleep_time} seconds between enrolments"
                        )
                        time.sleep(sleep_time)
                    elif status == UdemyStatus.ALREADY_ENROLLED.value:
                        counter_already_enroled += 1
                    elif status == UdemyStatus.EXPIRED.value:
                        counter_expired += 1
                    elif status == UdemyStatus.UNWANTED_LANGUAGE.value:
                        counter_other_languages += 1
                    elif status == UdemyStatus.UNWANTED_CATEGORY.value:
                        counter_other_categories += 1

                except KeyboardInterrupt:
                    logger.error("Exiting the script")
                    return
                except Exception as e:
                    logger.error(f"Unexpected exception: {e}")
                finally:
                    if settings.is_ci_build:
                        logger.info(
                            "We have attempted to subscribe to 1 udemy course")
                        logger.info("Ending test")
                        return

        else:
            logger.info("All scrapers complete\n\n")
            logger.info("\t####################################")
            logger.info("\t#             RESULTS              #")
            logger.info("\t####################################")
            logger.info(
                f"\t#  New Enrolled Courses:     {counter_enroled:04}  #")
            logger.info(
                f"\t#  Already Enrolled Courses: {counter_already_enroled:04}  #"
            )
            logger.info(
                f"\t#  Expired:                  {counter_expired:04}  #")
            logger.info(
                f"\t#  Other Languages:          {counter_other_languages:04}  #"
            )
            logger.info(
                f"\t#  Other Categories:         {counter_other_categories:04}  #"
            )
            logger.info("\t####################################")
            logger.info(
                f"\t#  Total Courses Scraped:    {counter_total:04}  #")
            logger.info("\t####################################")
            return