Skip to content

Instantly share code, notes, and snippets.

@Sorrow446
Created April 5, 2020 03:02
Show Gist options
  • Save Sorrow446/97f187d16d76b1cb375a13ac6c3164e1 to your computer and use it in GitHub Desktop.
Save Sorrow446/97f187d16d76b1cb375a13ac6c3164e1 to your computer and use it in GitHub Desktop.
Mega.nz old to new format converter.
#!/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