76 lines
1.7 KiB
Python
76 lines
1.7 KiB
Python
import cloudscraper
|
|
import json
|
|
import sys
|
|
import time
|
|
|
|
|
|
# Usage: dlp.py <URL> <OUTPUT FILE>
|
|
# If output file is "-", then file is saved to stdout
|
|
|
|
|
|
def status(data):
|
|
sys.stderr.write(json.dumps(data)+"\n")
|
|
|
|
|
|
try:
|
|
if len(sys.argv) != 3:
|
|
status({
|
|
"status": "failure",
|
|
"message": "Invalid command options provided",
|
|
})
|
|
exit(1)
|
|
|
|
url = sys.argv[1]
|
|
filepath = sys.argv[2]
|
|
|
|
scraper = cloudscraper.create_scraper()
|
|
response = scraper.get(url, stream=True)
|
|
|
|
if response.status_code != 200:
|
|
status({
|
|
"status": "failure",
|
|
"message": "Server returned: %d %s" % (
|
|
response.status_code,
|
|
response.reason,
|
|
)
|
|
})
|
|
exit(2)
|
|
|
|
total_size = int(response.headers.get("content-length", 0))
|
|
block_size = 4096
|
|
received_size = 0
|
|
start = time.process_time()
|
|
|
|
if filepath == "-":
|
|
file = sys.stdout.buffer
|
|
else:
|
|
file = open(filepath, "wb")
|
|
|
|
for data in response.iter_content(block_size):
|
|
file.write(data)
|
|
received_size += len(data)
|
|
status({
|
|
"status": "progress",
|
|
"total": total_size,
|
|
"received": received_size,
|
|
"speed": len(data) / (time.process_time() - start),
|
|
})
|
|
|
|
file.flush()
|
|
file.close()
|
|
|
|
if total_size != 0 and total_size != received_size:
|
|
status({
|
|
"status": "failure",
|
|
"message": "Could not download whole file",
|
|
})
|
|
exit(2)
|
|
else:
|
|
status({"status": "success"})
|
|
|
|
except Exception as ex:
|
|
status({
|
|
"status": "failure",
|
|
"message": str(ex),
|
|
})
|