Skip to content

Instantly share code, notes, and snippets.

@MirrorAce
Created June 28, 2021 07:51
Show Gist options
  • Save MirrorAce/a651da1a6da72f3f97f7c30d718498b2 to your computer and use it in GitHub Desktop.
Save MirrorAce/a651da1a6da72f3f97f7c30d718498b2 to your computer and use it in GitHub Desktop.
MirrorAce: Example Code that helps to upload files to MirrorAce.com via API with Python
"""
Example script to upload files to MirrorAce via api. */
Detailed information on API available at https://mirrorace.org/api. */
Author: MirrorAce */
URL: https://mirrorace.org */
"""
from requests import Session
import mimetypes
from pathlib import Path
import math
def get_mime(f):
return mimetypes.guess_type(f)[0]
def read_in_chunks(file_object, offset=0, chunk_size=65536):
while True:
file_object.seek(offset)
data = file_object.read(chunk_size)
if not data:
break
return data
s = Session()
api_key = "0123456789abcdefabcdefabcdefabcd"
api_token = "0123456789abcdefabcdefabcdefabcd"
#absolute file path
f = "/home/file.rar"
#filename = 'file.zip'; #set manually
filename = Path(f).name #set automatically #file.rar
mime_type = get_mime(f)
file_size = Path(f).stat().st_size
"""
STEP 1: get upload_key
"""
url = "https://mirrorace.org/api/v1/file/upload"
data = {"api_key": api_key, "api_token": api_token}
req = s.post(url, data=data).json()
if req['status'] != 'success':
print("Error: ", req)
exit()
"""
STEP 2: Upload file
"""
url = req['result']['server_file']
cTracker = req['result']['cTracker']
upload_key = req['result']['upload_key']
default_mirrors = req['result']['default_mirrors']
max_chunk_size = int(req['result']['max_chunk_size'])
max_file_size = int(req['result']['max_file_size'])
max_mirrors = int(req['result']['max_mirrors'])
max_chunk_size = 10485760
#check file size limit
if (file_size >= max_file_size):
print("Error: File exceeds maximum file size allowed: ", max_file_size)
exit()
#setup
mirrors = default_mirrors
chunk_size = max_chunk_size
chunks = file_size / chunk_size
chunks = math.ceil(chunks)
last_range = False
response = False
fp = open(f,'rb')
i = 0
while i < chunks:
range_start = 0
range_end = min(chunk_size, file_size - 1)
if (last_range != False) :
range_start = last_range + 1
range_end = min(range_start + chunk_size, file_size - 1)
last_range = range_end
chunk = read_in_chunks(fp, range_start, (range_end - range_start + 1))
files = {'files':(filename, chunk)}
data = {"api_key": api_key, "api_token": api_token, "cTracker": cTracker, "upload_key": upload_key, "mirrors[]": mirrors}
headers = {
'Content-Length': str((range_end - range_start + 1)),
'Content-Range': 'bytes' + str(range_start) + '-' + str(range_end) + '/' + str(file_size),
}
req = s.post(url, files=files, data=data, headers=headers).json()
if req['status'] != 'success':
fp.close()
print("Error: ", req)
exit()
print('uploaded chunk ', i)
i += 1
download_link = req['result']['url']
print(download_link)
@niskala5570
Copy link

Thx mate!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment