def _generate_timecode(self, sequence, timecode): match = re.match(self._TIMECODE, timecode, flags=re.U) if match: start, end = ( self._fix_timecode( timecode=re.sub(r"[\.,]", ",", match.group("appeartime")) ), self._fix_timecode( timecode=re.sub(r"[\.,]", ",", match.group("disappertime")) ), ) return "{seq}\r\n{appeartime} --> {disappertime}\r\n".format( seq=sequence, appeartime=start, disappertime=end ) return ""
def fix_kv(m): v = m.group(0) if v in ("true", "false", "null"): return v elif v.startswith("/*") or v.startswith("//") or v == ",": return "" if v[0] in ("'", '"'): v = re.sub( r'(?s)\\.|"', lambda m: { '"': '\\"', "\\'": "'", "\\\n": "", "\\x": "\\u00", }.get(m.group(0), m.group(0)), v[1:-1], ) for regex, base in INTEGER_TABLE: im = re.match(regex, v) if im: i = int(im.group(1), base) return '"%d":' % i if v.endswith(":") else "%d" % i return '"%s"' % v
def to_configs(username="", password="", cookies="", quality="", output="", language=""): configs = load_configs() fname = ".udemy-dl.conf" fmode = "w" if configs: cfu = configs.get("username") cfp = configs.get("password") cfc = configs.get("cookies") cfq = configs.get("quality") cfl = configs.get("language") cfo = configs.get("output") if cfo: cfo = re.sub(r'"', "", cfo.strip()) if username and cfu != username: configs.update({"username": username}) if password and cfp != password: configs.update({"password": password}) if cookies and cfc != cookies: configs.update({"cookies": cookies}) if quality and cfq != quality: configs.update({"quality": quality}) if language and cfl != language: configs.update({"language": language}) if output and cfo != output: output = re.sub(r'"', "", output.strip()) configs.update({"output": output}) with open(fname, fmode) as fd: json.dump(configs, fd, indent=4) if not configs: if output: output = re.sub(r'"', "", output.strip()) creds = { "username": username, "password": password, "quality": quality, "output": output, "language": language, "cookies": cookies, } with open(fname, fmode) as fd: json.dump(creds, fd, indent=4) return "cached"
def to_filepath(base, name): base = re.sub(r'"', "", base.strip()) filepath = os.path.join(base, name) try: os.makedirs(filepath) except: pass return filepath
def set_log_filepath(self, course_path): course_path = re.sub(r'"', "", course_path.strip()) if os.path.exists(course_path): self._log_filepath = os.path.join(course_path, "udemy-dl.log") file_handler = logging.FileHandler(self._log_filepath) logging.basicConfig( format="[%(asctime)s][%(name)s] %(levelname)-5.5s %(message)s", level=logging.INFO, handlers=[file_handler], )
def hidden_inputs(html): html = re.sub(r"<!--(?:(?!<!--).)*-->", "", html) hidden_inputs = {} # pylint: disable=W for entry in re.findall(r"(?i)(<input[^>]+>)", html): attrs = extract_attributes(entry) if not entry: continue if attrs.get("type") not in ("hidden", "submit"): continue name = attrs.get("name") or attrs.get("id") value = attrs.get("value") if name and value is not None: hidden_inputs[name] = value return hidden_inputs
def js_to_json(code): COMMENT_RE = r"/\*(?:(?!\*/).)*?\*/|//[^\n]*" SKIP_RE = r"\s*(?:{comment})?\s*".format(comment=COMMENT_RE) INTEGER_TABLE = ( (r"(?s)^(0[xX][0-9a-fA-F]+){skip}:?$".format(skip=SKIP_RE), 16), (r"(?s)^(0+[0-7]+){skip}:?$".format(skip=SKIP_RE), 8), ) def fix_kv(m): v = m.group(0) if v in ("true", "false", "null"): return v elif v.startswith("/*") or v.startswith("//") or v == ",": return "" if v[0] in ("'", '"'): v = re.sub( r'(?s)\\.|"', lambda m: { '"': '\\"', "\\'": "'", "\\\n": "", "\\x": "\\u00", }.get(m.group(0), m.group(0)), v[1:-1], ) for regex, base in INTEGER_TABLE: im = re.match(regex, v) if im: i = int(im.group(1), base) return '"%d":' % i if v.endswith(":") else "%d" % i return '"%s"' % v return re.sub( r"""(?sx) "(?:[^"\\]*(?:\\\\|\\['"nurtbfx/\n]))*[^"\\]*"| '(?:[^'\\]*(?:\\\\|\\['"nurtbfx/\n]))*[^'\\]*'| {comment}|,(?={skip}[\]}}])| [a-zA-Z_][.a-zA-Z_0-9]*| \b(?:0[xX][0-9a-fA-F]+|0+[0-7]+)(?:{skip}:)?| [0-9]+(?={skip}:) """.format(comment=COMMENT_RE, skip=SKIP_RE), fix_kv, code, )
def _clean(self, text): ok = re.compile(r'[^\\/:*?"<>|]') text = "".join(x if ok.match(x) else "_" for x in text) text = re.sub(r"\.+$", "", text.strip()) return text