-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Port away from urlgrabber to requests
- Loading branch information
Martin Briza
committed
Apr 20, 2016
1 parent
11115b3
commit 71ab535
Showing
11 changed files
with
126 additions
and
3,703 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
from liveusb import LiveUSBError | ||
|
||
import subprocess | ||
import os | ||
import sys | ||
import requests | ||
import tempfile | ||
|
||
from PyQt5.QtCore import QStandardPaths | ||
|
||
|
||
def find_downloads(): | ||
# todo look into SUDO_UID and PKEXEC_UID for the original user | ||
if sys.platform.startswith("linux"): | ||
import pwd | ||
uid = 0 | ||
if 'SUDO_UID' in os.environ: | ||
uid = int(os.environ['SUDO_UID']) | ||
elif 'PKEXEC_UID' in os.environ: | ||
uid = int(os.environ['PKEXEC_UID']) | ||
|
||
pw = pwd.getpwuid(uid) | ||
p = subprocess.Popen(['sudo', '-u', pw[0], 'xdg-user-dir', 'DOWNLOAD'], stdout=subprocess.PIPE) | ||
p.wait() | ||
path = p.stdout.readline().strip() | ||
else: | ||
path = QStandardPaths.writableLocation(QStandardPaths.DownloadLocation) | ||
|
||
return path | ||
|
||
def chown_file(path): | ||
if sys.platform.startswith("linux"): | ||
import pwd | ||
uid = 0 | ||
gid = 0 | ||
if 'SUDO_UID' in os.environ: | ||
uid = int(os.environ['SUDO_UID']) | ||
gid = int(os.environ['SUDO_GID']) | ||
elif 'PKEXEC_UID' in os.environ: | ||
uid = int(os.environ['PKEXEC_UID']) | ||
pw = pwd.getpwuid(uid) | ||
gid = pw[3] | ||
else: | ||
pass | ||
|
||
os.chown(path, uid, gid) | ||
else: | ||
pass | ||
|
||
|
||
def download(url, target_folder=find_downloads(), update_maximum = None, update_current = None): | ||
CHUNK_SIZE = 1024 * 1024 | ||
current_size = 0 | ||
|
||
file_name = os.path.basename(url) | ||
full_path = os.path.join(target_folder, file_name) | ||
if os.path.exists(full_path): | ||
current_size = os.path.getsize(full_path) | ||
bytes_read = current_size | ||
|
||
if current_size > 0: | ||
resume_header = {'Range': 'bytes=%d-' % current_size} | ||
else: | ||
resume_header = {} | ||
|
||
try: | ||
r = requests.get(url, headers=resume_header, stream=True, allow_redirects=True, timeout=(30.0, 30.0)) | ||
|
||
if r.status_code == 200: | ||
mode = "ab" | ||
elif r.status_code == 206: | ||
mode = "wb" | ||
elif r.status_code == 416: | ||
return full_path | ||
else: | ||
raise LiveUSBError("Couldn't download the file: %s (%d)" % (r.reason, r.status_code)) | ||
|
||
if update_maximum: | ||
update_maximum(current_size + int(r.headers['Content-Length'])) | ||
|
||
with open(full_path, mode) as f: | ||
chown_file(full_path) | ||
|
||
for chunk in r.iter_content(CHUNK_SIZE): | ||
f.write(chunk) | ||
bytes_read += len(chunk) | ||
if update_current: | ||
update_current(bytes_read) | ||
|
||
except requests.exceptions.ReadTimeout as e: | ||
raise LiveUSBError("Your internet connection seems to be broken") | ||
except requests.exceptions.ConnectTimeout as e: | ||
raise LiveUSBError("Your internet connection seems to be broken") | ||
except Exception as e: | ||
raise LiveUSBError("Couldn't download the file: %s (%d): %s" % (r.reason, r.status_code, e.message)) | ||
|
||
return full_path | ||
|
||
|
||
|
||
def __print(val): | ||
print(val) | ||
|
||
if __name__ == '__main__': | ||
import pprint | ||
#download("https://download.fedoraproject.org/pub/fedora/linux/releases/23/Workstation/x86_64/iso/Fedora-Live-Workstation-x86_64-23-10.iso", update_maximum=__print, update_current=__print) | ||
print find_downloads() |
This file contains 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
This file contains 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
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.