|
| 1 | +import io as _io |
| 2 | +import typing as _typing |
| 3 | + |
| 4 | +import requests as _requests |
| 5 | +from gimme_cached_property import cached_property |
| 6 | + |
| 7 | +from stream.io import ( |
| 8 | + BinaryFile as _BinaryFile, |
| 9 | +) |
| 10 | + |
| 11 | + |
| 12 | +class BaseHttpFile(_BinaryFile): |
| 13 | + def __init__(self, url: str): |
| 14 | + self.__url = url |
| 15 | + |
| 16 | + @property |
| 17 | + def url(self) -> str: |
| 18 | + return self.__url |
| 19 | + |
| 20 | + def __eq__(self, other): |
| 21 | + if not isinstance(other, BaseHttpFile): |
| 22 | + return False |
| 23 | + if self.url != other.url: |
| 24 | + return False |
| 25 | + return True |
| 26 | + |
| 27 | + # --- os --- |
| 28 | + |
| 29 | + @property |
| 30 | + def name(self) -> str: |
| 31 | + return self.url |
| 32 | + |
| 33 | + def fileno(self) -> int: |
| 34 | + raise self.NotSupported |
| 35 | + |
| 36 | + @property |
| 37 | + def isatty(self) -> bool: |
| 38 | + return False |
| 39 | + |
| 40 | + # --- seek --- |
| 41 | + |
| 42 | + def seekable(self) -> bool: |
| 43 | + return False |
| 44 | + |
| 45 | + def tell(self) -> int: |
| 46 | + raise self.NotSupported |
| 47 | + |
| 48 | + def seek(self, offset: int, whence: int = _io.SEEK_SET) -> int: |
| 49 | + raise self.NotSupported |
| 50 | + |
| 51 | + def truncate(self, size: _typing.Optional[int] = None) -> int: |
| 52 | + raise self.NotSupported |
| 53 | + |
| 54 | + |
| 55 | +class HttpDownloadFile(BaseHttpFile): |
| 56 | + def __init__( |
| 57 | + self, |
| 58 | + url: str, |
| 59 | + *, |
| 60 | + chuck_size: int = 8192, |
| 61 | + ): |
| 62 | + super().__init__(url) |
| 63 | + self.__chuck_size = chuck_size |
| 64 | + |
| 65 | + self.__request = _requests.get(self.url, stream=True) |
| 66 | + |
| 67 | + @property |
| 68 | + def _chuck_size(self) -> int: |
| 69 | + return self.__chuck_size |
| 70 | + |
| 71 | + @property |
| 72 | + def _request(self): |
| 73 | + return self.__request |
| 74 | + |
| 75 | + # --- os --- |
| 76 | + |
| 77 | + def __enter__(self): |
| 78 | + self.__request.__enter__() |
| 79 | + return self |
| 80 | + |
| 81 | + def close(self) -> None: |
| 82 | + self.__request.close() |
| 83 | + |
| 84 | + # --- read --- |
| 85 | + |
| 86 | + def readable(self) -> bool: |
| 87 | + return True |
| 88 | + |
| 89 | + @cached_property |
| 90 | + def _iter(self) -> _typing.Iterator[int]: |
| 91 | + for chunk in self._request.iter_content(chunk_size=self._chuck_size): |
| 92 | + if not chunk: |
| 93 | + continue # filter out keep-alive new chunks |
| 94 | + yield from chunk |
| 95 | + |
| 96 | + def _read_character(self) -> int: |
| 97 | + try: |
| 98 | + return next(self._iter) |
| 99 | + except StopIteration: |
| 100 | + raise self.EOF from None |
| 101 | + |
| 102 | + # --- not writable --- |
| 103 | + |
| 104 | + def writable(self) -> bool: |
| 105 | + return False |
| 106 | + |
| 107 | + def write(self, s: bytes) -> None: |
| 108 | + raise self.NotSupported |
| 109 | + |
| 110 | + def flush(self) -> None: |
| 111 | + raise self.NotSupported |
| 112 | + |
| 113 | + |
| 114 | +def download_cmd(): |
| 115 | + import argparse |
| 116 | + from stream.io.local import LocalFile |
| 117 | + parser = argparse.ArgumentParser() |
| 118 | + parser.add_argument('url', type=str) |
| 119 | + parser.add_argument('file', type=str) |
| 120 | + args = parser.parse_args() |
| 121 | + |
| 122 | + with HttpDownloadFile(args.url) as f1: |
| 123 | + with LocalFile(args.file, 'wb') as f2: |
| 124 | + f1.copy_to(f2) |
| 125 | + |
| 126 | + |
| 127 | +def get_cmd(): |
| 128 | + import argparse |
| 129 | + from stream.io.std import StdOut |
| 130 | + parser = argparse.ArgumentParser() |
| 131 | + parser.add_argument('url', type=str) |
| 132 | + args = parser.parse_args() |
| 133 | + |
| 134 | + with HttpDownloadFile(args.url) as f: |
| 135 | + with StdOut() as stdout: |
| 136 | + f.copy_to(stdout.buffer) |
0 commit comments