Package: pymegatools
PyMegatools - A Simple Python Wrapper formegatoolsThis is a Simple Python Library for themegatoolscommand line utilityAs of right now, you can use this library to download a file or get it's name from mega.nzInstallationYou can either install it from PyPipipinstallpymegatoolsor traditionally withsetup.pypython3setup.pyinstallA quick exampleThis example shows how to use this library to download any file from mega.nzfrompymegatoolsimportMegatools# Initialization# By default the linux x86_64 or windows64 binary is loaded depending on your platformmega=Megatools()# Or you can get the official megatools static binaries for your platform at https://megatools.megous.com/builds/experimental/# And load it like this:mega=Megatools(executable='path/to/megatools')# Get version of the currrent mega binaryprint("Version:",mega.version)url="https://mega.nz/file/yuZ0QJ6J#jFc2HL6rIoDVU9kECBpMEIAbcv2WQcz6le9kS_bb2gc"# Get a file name from urlprint(mega.filename(url))# Downloading a file from urlmega.download(url)The output should look something like:Version:1.11.0
10MB.bin
10MB.bin:0.00%-0bytesof9.5MiB
10MB.bin:0.13%-12.7KiB(13000bytes)of9.5MiB(12.4KiB/s)10MB.bin:4.28%-416.4KiB(426400bytes)of9.5MiB(401.3KiB/s)10MB.bin:21.35%-2.0MiB(2126800bytes)of9.5MiB(1.6MiB/s)10MB.bin:36.62%-3.5MiB(3647800bytes)of9.5MiB(1.4MiB/s)10MB.bin:53.97%-5.1MiB(5376800bytes)of9.5MiB(1.6MiB/s)10MB.bin:69.16%-6.6MiB(6890000bytes)of9.5MiB(1.4MiB/s)10MB.bin:88.32%-8.4MiB(8798400bytes)of9.5MiB(1.8MiB/s)Downloaded10MB.binPassing in a progress callback to modify and redirect the output of downloadsfrompymegatoolsimportMegatools# We define a callback function that accepts# - The output stream as `stream`# - The popen Process as `process`# - A custom argument `prefix`defprogress_callback(stream,process,prefix):# A stream is just a list of lines of the output# We read the last line in the output streamlatest_line=stream[-1]# And then we append it to a file instead of printing it to the consolewithopen('output.txt','a+')asf:f.write(prefix+latest_line)# Initializing megatoolsmega=Megatools()url="https://mega.nz/file/yuZ0QJ6J#jFc2HL6rIoDVU9kECBpMEIAbcv2WQcz6le9kS_bb2gc"# Downloading the file and passing in our progress callback# We also pass in our prefix (the custom argument)prefix='This is the special prefix: 'mega.download(url,progress=progress_callback,progress_arguments=(prefix,))Now the output is written to output.txtcatoutput.txtThisisthespecialprefix:10MB.bin:0.00%-0bytesof9.5MiB
Thisisthespecialprefix:10MB.bin:0.14%-14.0KiB(14300bytes)of9.5MiB(10.9KiB/s)Thisisthespecialprefix:10MB.bin:3.99%-389.7KiB(399100bytes)of9.5MiB(374.1KiB/s)Thisisthespecialprefix:10MB.bin:22.58%-2.2MiB(2258100bytes)of9.5MiB(1.7MiB/s)Thisisthespecialprefix:10MB.bin:44.21%-4.2MiB(4421300bytes)of9.5MiB(2.1MiB/s)Thisisthespecialprefix:10MB.bin:63.60%-6.1MiB(6359600bytes)of9.5MiB(1.8MiB/s)Thisisthespecialprefix:10MB.bin:83.10%-7.9MiB(8309600bytes)of9.5MiB(1.9MiB/s)Thisisthespecialprefix:10MB.bin:98.72%-9.4MiB(9872200bytes)of9.5MiB(1.5MiB/s)Thisisthespecialprefix:Downloaded10MB.binUsing Megatools with async progress callbacksimportasynciofrompymegatoolsimportMegatoolsasyncdefmain():mega=Megatools()url="https://mega.nz/file/yuZ0QJ6J#jFc2HL6rIoDVU9kECBpMEIAbcv2WQcz6le9kS_bb2gc"# To use megatools with the default async callback, simply set assume_async to True and await the resultawaitmega.download(url,assume_async=True)# OR# Use `Megatools.async_download`awaitmega.async_download(url)# To use megatools with a custom async progess callback, simply await the download methodasyncdefasync_progress(stream,process):# Do async stuffprint(end=stream[-1])awaitmega.download(url,progress=async_progress)# OR# Use `Megatools.async_download`awaitmega.async_download(url,progress=async_progress)asyncio.run(main())Error Handling# Pymegatools raises a MegaError if anything goes wrong,# for example you try to download a file that already exists.frompymegatoolsimportMegatools,MegaErrormega=Megatools()url="https://mega.nz/file/yuZ0QJ6J#jFc2HL6rIoDVU9kECBpMEIAbcv2WQcz6le9kS_bb2gc"# Download file for the first tinemega.download(url)# Attempt to download same file again# Should throw error, so let's catch it.try:mega.download(url)exceptMegaErrorasexception:print(f"Error caught{exception}")The output should look something like:10MB.bin:0.00%-0bytesof9.5MiB
10MB.bin:0.34%-33.0KiB(33800bytes)of9.5MiB(32.9KiB/s)10MB.bin:3.21%-313.4KiB(320872bytes)of9.5MiB(278.7KiB/s)10MB.bin:15.03%-1.4MiB(1502800bytes)of9.5MiB(1.1MiB/s)10MB.bin:24.80%-2.4MiB(2480400bytes)of9.5MiB(949.2KiB/s)10MB.bin:31.93%-3.0MiB(3192800bytes)of9.5MiB(693.9KiB/s)10MB.bin:40.20%-3.8MiB(4019600bytes)of9.5MiB(807.0KiB/s)10MB.bin:43.73%-4.2MiB(4373200bytes)of9.5MiB(211.9KiB/s)10MB.bin:43.73%-4.2MiB(4373200bytes)of9.5MiB(0bytes/s)10MB.bin:61.62%-5.9MiB(6162000bytes)of9.5MiB(1.7MiB/s)10MB.bin:76.00%-7.2MiB(7599800bytes)of9.5MiB(1.4MiB/s)10MB.bin:91.66%-8.7MiB(9166300bytes)of9.5MiB(1.5MiB/s)10MB.bin:98.68%-9.4MiB(9868300bytes)of9.5MiB(496.6KiB/s)10MB.bin:98.68%-9.4MiB(9868300bytes)of9.5MiB(0bytes/s)10MB.bin:98.70%-9.4MiB(9869600bytes)of9.5MiB(1.3KiB/s)Downloaded10MB.bin
ERROR:Downloadfailedfor'https://mega.nz/file/yuZ0QJ6J#jFc2HL6rIoDVU9kECBpMEIAbcv2WQcz6le9kS_bb2gc':Localfilealreadyexists:./10MB.bin
Errorcaught[returnCode1]Downloadfailedfor'https://mega.nz/file/yuZ0QJ6J#jFc2HL6rIoDVU9kECBpMEIAbcv2WQcz6le9kS_bb2gc':Localfilealreadyexists:./10MB.binCredits@megousfor making the amazing megatools cmdline utility