|
1 | 1 | import renpy
|
2 |
| -from installer import _, download, remove, exists, move, processing, run, mkdir, unpack, error, info |
| 2 | +from installer import _, remove, exists, move, processing, run, mkdir, unpack, error, info |
| 3 | + |
| 4 | +def download(url, filename, hash=None): |
| 5 | + """ |
| 6 | + Downloads `url` to `filename`, a tempfile. |
| 7 | + """ |
| 8 | + |
| 9 | + import installer |
| 10 | + import requests |
| 11 | + import time |
| 12 | + import renpy |
| 13 | + from renpy.store import interface, _ |
| 14 | + |
| 15 | + download_url = url |
| 16 | + download_file = installer._friendly(filename) |
| 17 | + |
| 18 | + filename = installer._path(filename) |
| 19 | + |
| 20 | + if hash is not None: |
| 21 | + if installer._check_hash(filename, hash): |
| 22 | + return |
| 23 | + |
| 24 | + progress_time = time.time() |
| 25 | + |
| 26 | + try: |
| 27 | + |
| 28 | + response = requests.get(url, stream=True, proxies=renpy.exports.proxies, timeout=15, headers={"Referer" : "https://code.visualstudio.com/download", "User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36 renpy/8'}) |
| 29 | + response.raise_for_status() |
| 30 | + |
| 31 | + total_size = int(response.headers.get('content-length', 1)) |
| 32 | + |
| 33 | + downloaded = 0 |
| 34 | + |
| 35 | + with open(filename, "wb") as f: |
| 36 | + |
| 37 | + for i in response.iter_content(65536): |
| 38 | + |
| 39 | + f.write(i) |
| 40 | + downloaded += len(i) |
| 41 | + |
| 42 | + if time.time() - progress_time > 0.1: |
| 43 | + progress_time = time.time() |
| 44 | + if not installer.quiet: |
| 45 | + interface.processing( |
| 46 | + _("Downloading [installer.download_file]..."), |
| 47 | + complete=downloaded, total=total_size) |
| 48 | + |
| 49 | + except requests.HTTPError as e: |
| 50 | + if not installer.quiet: |
| 51 | + raise |
| 52 | + |
| 53 | + interface.error(_("Could not download [installer.download_file] from [installer.download_url]:\n{b}[installer.download_error]")) |
| 54 | + |
| 55 | + if hash is not None: |
| 56 | + if not installer.quiet: |
| 57 | + raise Exception("Hash check failed.") |
| 58 | + if not installer._check_hash(filename, hash): |
| 59 | + interface.error(_("The downloaded file [installer.download_file] from [installer.download_url] is not correct.")) |
| 60 | + |
| 61 | + |
3 | 62 |
|
4 | 63 | info(_("Visual Studio Code is licensed under {a=https://code.visualstudio.com/license}Microsoft Software License Terms{/a}, and may collect some information about you and your use.\n\nBy installing Visual Studio Code, you agree to thse terms."))
|
5 | 64 |
|
|
0 commit comments