예제 #1
0
def search_regex(
    pattern, string, name, default=NO_DEFAULT, fatal=True, flags=0, group=None
):
    """
    Perform a regex search on the given string, using a single or a list of
    patterns returning the first matching group.
    In case of failure return a default value or raise a WARNING or a
    RegexNotFoundError, depending on fatal, specifying the field name.
    """
    if isinstance(pattern, str):
        mobj = re.search(pattern, string, flags)
    else:
        for p in pattern:
            mobj = re.search(p, string, flags)
            if mobj:
                break

    _name = name

    if mobj:
        if group is None:
            # return the first matching group
            return next(g for g in mobj.groups() if g is not None)
        else:
            return mobj.group(group)
    elif default is not NO_DEFAULT:
        return default
    elif fatal:
        print("[-] Unable to extract %s" % _name)
        exit(0)
    else:
        print("[-] unable to extract %s" % _name)
        exit(0)
예제 #2
0
 def _course_name(self, url):
     mobj = re.search(
         r"(?i)(?://(?P<portal_name>.+?).udemy.com/(?:course(/draft)*/)?(?P<name_or_id>[a-zA-Z0-9_-]+))",
         url,
     )
     if mobj:
         return mobj.group("portal_name"), mobj.group("name_or_id")
예제 #3
0
def extract_cookie_string(raw_cookies):
    cookies = {}
    try:
        access_token = re.search(
            r"(?i)(?:access_token=(?P<access_token>\w+))", raw_cookies
        )
    except Exception as error:
        logger.error(
            msg=f"Cookies error, {error}, unable to extract access_token from cookies."
        )
        sys.exit(0)
    if not access_token:
        logger.error(msg="Unable to find access_token, proper cookies required")
        logger.info(
            msg="follow: https://github.com/r0oth3x49/udemy-dl#how-to-login-with-cookie",
            new_line=True,
        )
        sys.stdout.flush()
        sys.exit(0)
    access_token = access_token.group("access_token")
    cookies.update({"access_token": access_token})
    return cookies