Created
April 5, 2020 03:02
-
-
Save Sorrow446/97f187d16d76b1cb375a13ac6c3164e1 to your computer and use it in GitHub Desktop.
Mega.nz old to new format converter.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import os | |
import re | |
import sys | |
import argparse | |
parser = argparse.ArgumentParser() | |
parser.add_argument("-u", "--url", nargs="+", required=True, help="URL or text filename.") | |
parser.add_argument("-o", "--output", help="Output text filename.") | |
args = parser.parse_args() | |
def exist_check(f, delete): | |
if os.path.isfile(f): | |
if delete: | |
os.remove(f) | |
elif not delete: | |
print("Source text file doesn't exist.") | |
sys.exit(1) | |
def read_txt(): | |
exist_check(args.url[0], False) | |
with open(args.url[0]) as f: | |
return f.readlines() | |
def write_txt(url): | |
with open(args.output, "a") as f: | |
f.write(url + "\n") | |
def main(url): | |
regex = ( | |
"https://mega\.nz/(#!|#F!|folder|file)(?:/)?" | |
"([a-zA-Z\d]{8})(?:!|#)([\w-]{22}|[\w-]{43})(?:/)?$" | |
) | |
m = re.match(regex, url) | |
if not m: | |
print("Invalid URL.") | |
return | |
print(url) | |
try: | |
media_type = {"#!": "file", "#F!": "folder"}[m.group(1)] | |
except KeyError: | |
print("Already in new format.") | |
return | |
new = "https://mega.nz/{}/{}#{}".format(media_type, m.group(2), | |
m.group(3) | |
) | |
print("->", new) | |
if args.output: | |
write_txt(new) | |
if __name__ == "__main__": | |
if args.output: | |
exist_check(args.output, True) | |
if args.url[0].endswith(".txt"): | |
args.url = read_txt() | |
for url in args.url: | |
main(url.strip()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment