Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- from pathlib import Path
- from urllib import parse, request
- import gzip
- import json
- import sys
- BASE_URL = "https://cloud-api.yandex.net/v1/disk/public/resources/download?public_key="
- def fetch_json(url: str) -> dict[str, str]:
- with request.urlopen(url) as res:
- response_info = response.info()
- content_encoding = response_info["Content-Encoding"]
- if content_encoding == "gzip" or content_encoding == "deflate":
- buffer = gzip.GzipFile(fileobj=BytesIO(response.read()), mode="rb")
- else:
- buffer = response
- content_charset = response_info.get_content_charset("utf-8")
- return json.load(buffer, encoding=content_charset)
- def dl_stuff(u: str):
- data = fetch_json(BASE_URL + parse.quote_plus(u))
- dl_url = data["href"]
- file_name = parse.parse_qs(parse.urlparse(dl_url).query).get("filename")[0]
- request.urlretrieve(dl_url, Path.cwd().joinpath(file_name))
- if __name__ == "__main__":
- for u in sys.argv[1:]:
- dl_stuff(u)
Advertisement
Add Comment
Please, Sign In to add comment