Skip to content

file_handler

RemoteFileHandler

Download file from url, might be GDrive shareable link

Source code in common/ayon_common/distribution/file_handler.py
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
class RemoteFileHandler:
    """Download file from url, might be GDrive shareable link"""

    @staticmethod
    def download_url(
        url: str,
        root: str,
        filename: Optional[str] = None,
        max_redirect_hops: int = 3,
        headers: Optional[dict[str, str]] = None,
    ):
        """Download a file from url and place it in root.

        Args:
            url (str): URL to download file from
            root (str): Directory to place downloaded file in
            filename (str, optional): Name to save the file under.
                If None, use the basename of the URL
            max_redirect_hops (Optional[int]): Maximum number of redirect
                hops allowed
            headers (Optional[dict[str, str]]): Additional required headers
                - Authentication etc..

        """
        root = os.path.expanduser(root)
        if not filename:
            filename = os.path.basename(url)
        fpath = os.path.join(root, filename)

        if not os.path.exists(root):
            os.makedirs(root, exist_ok=True)

        # expand redirect chain if needed
        url = RemoteFileHandler._get_redirect_url(
            url, max_hops=max_redirect_hops, headers=headers)

        # check if file is located on Google Drive
        file_id = RemoteFileHandler._get_google_drive_file_id(url)
        if file_id is not None:
            return RemoteFileHandler.download_file_from_google_drive(
                file_id, root, filename)

        # download the file
        try:
            print(f"Downloading {url} to {fpath}")
            RemoteFileHandler._urlretrieve(url, fpath, headers=headers)
        except (urllib.error.URLError, IOError) as exc:
            if url[:5] != "https":
                raise exc

            url = url.replace("https:", "http:")
            print((
                "Failed download. Trying https -> http instead."
                f" Downloading {url} to {fpath}"
            ))
            RemoteFileHandler._urlretrieve(url, fpath, headers=headers)

    @staticmethod
    def download_file_from_google_drive(
        file_id: str,
        root: str,
        filename: Optional[str] = None,
    ):
        """Download a Google Drive file from  and place it in root.
        Args:
            file_id (str): id of file to be downloaded
            root (str): Directory to place downloaded file in
            filename (Optional[str]): Name to save the file under.
                If None, use the id of the file.

        """
        # Based on https://stackoverflow.com/questions/38511444/python-download-files-from-google-drive-using-url # noqa

        url = "https://docs.google.com/uc?export=download"

        root = os.path.expanduser(root)
        if not filename:
            filename = file_id
        fpath = os.path.join(root, filename)

        if not os.path.exists(root):
            os.makedirs(root, exist_ok=True)

        # TODO validate checksum of existing file and download
        #   only if incomplete.
        if os.path.isfile(fpath):
            os.remove(fpath)

        session = requests.Session()

        response = session.get(url, params={"id": file_id}, stream=True)
        token = RemoteFileHandler._get_confirm_token(response)

        if token:
            params = {"id": file_id, "confirm": token}
            response = session.get(url, params=params, stream=True)

        response_content_generator = response.iter_content(32768)
        first_chunk = None
        while not first_chunk:  # filter out keep-alive new chunks
            first_chunk = next(response_content_generator)

        if RemoteFileHandler._quota_exceeded(first_chunk):
            msg = (
                f"The daily quota of the file {filename} is exceeded and "
                f"it can't be downloaded. This is a limitation of "
                f"Google Drive and can only be overcome by trying "
                f"again later."
            )
            raise RuntimeError(msg)

        RemoteFileHandler._save_response_content(
            itertools.chain(
                (first_chunk, ), response_content_generator
            ),
            fpath
        )
        response.close()

    @staticmethod
    def _urlretrieve(url, filename, chunk_size=None, headers=None):
        final_headers = {"User-Agent": USER_AGENT}
        if headers:
            final_headers.update(headers)

        chunk_size = chunk_size or 8192
        with open(filename, "wb") as fh:
            with urllib.request.urlopen(
                urllib.request.Request(url, headers=final_headers)
            ) as response:
                for chunk in iter(lambda: response.read(chunk_size), ""):
                    if not chunk:
                        break
                    fh.write(chunk)

    @staticmethod
    def _get_redirect_url(url, max_hops, headers=None):
        initial_url = url
        final_headers = {"Method": "HEAD", "User-Agent": USER_AGENT}
        if headers:
            final_headers.update(headers)
        for _ in range(max_hops + 1):
            with urllib.request.urlopen(
                urllib.request.Request(url, headers=final_headers)
            ) as response:
                if response.url == url or response.url is None:
                    return url

                return response.url
        else:
            raise RecursionError(
                f"Request to {initial_url} exceeded {max_hops} redirects. "
                f"The last redirect points to {url}."
            )

    @staticmethod
    def _get_confirm_token(response):
        for key, value in response.cookies.items():
            if key.startswith("download_warning"):
                return value

        # handle antivirus warning for big zips
        found = re.search("(confirm=)([^&.+])", response.text)
        if found:
            return found.groups()[1]

        return None

    @staticmethod
    def _save_response_content(
        response_gen, destination,
    ):
        with open(destination, "wb") as f:
            for chunk in response_gen:
                if chunk:  # filter out keep-alive new chunks
                    f.write(chunk)

    @staticmethod
    def _quota_exceeded(first_chunk):
        try:
            return "Google Drive - Quota exceeded" in first_chunk.decode()
        except UnicodeDecodeError:
            return False

    @staticmethod
    def _get_google_drive_file_id(url):
        parts = urlparse(url)

        if re.match(r"(drive|docs)[.]google[.]com", parts.netloc) is None:
            return None

        match = re.match(r"/file/d/(?P<id>[^/]*)", parts.path)
        if match is None:
            return None

        return match.group("id")

download_file_from_google_drive(file_id, root, filename=None) staticmethod

Download a Google Drive file from and place it in root. Args: file_id (str): id of file to be downloaded root (str): Directory to place downloaded file in filename (Optional[str]): Name to save the file under. If None, use the id of the file.

Source code in common/ayon_common/distribution/file_handler.py
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
@staticmethod
def download_file_from_google_drive(
    file_id: str,
    root: str,
    filename: Optional[str] = None,
):
    """Download a Google Drive file from  and place it in root.
    Args:
        file_id (str): id of file to be downloaded
        root (str): Directory to place downloaded file in
        filename (Optional[str]): Name to save the file under.
            If None, use the id of the file.

    """
    # Based on https://stackoverflow.com/questions/38511444/python-download-files-from-google-drive-using-url # noqa

    url = "https://docs.google.com/uc?export=download"

    root = os.path.expanduser(root)
    if not filename:
        filename = file_id
    fpath = os.path.join(root, filename)

    if not os.path.exists(root):
        os.makedirs(root, exist_ok=True)

    # TODO validate checksum of existing file and download
    #   only if incomplete.
    if os.path.isfile(fpath):
        os.remove(fpath)

    session = requests.Session()

    response = session.get(url, params={"id": file_id}, stream=True)
    token = RemoteFileHandler._get_confirm_token(response)

    if token:
        params = {"id": file_id, "confirm": token}
        response = session.get(url, params=params, stream=True)

    response_content_generator = response.iter_content(32768)
    first_chunk = None
    while not first_chunk:  # filter out keep-alive new chunks
        first_chunk = next(response_content_generator)

    if RemoteFileHandler._quota_exceeded(first_chunk):
        msg = (
            f"The daily quota of the file {filename} is exceeded and "
            f"it can't be downloaded. This is a limitation of "
            f"Google Drive and can only be overcome by trying "
            f"again later."
        )
        raise RuntimeError(msg)

    RemoteFileHandler._save_response_content(
        itertools.chain(
            (first_chunk, ), response_content_generator
        ),
        fpath
    )
    response.close()

download_url(url, root, filename=None, max_redirect_hops=3, headers=None) staticmethod

Download a file from url and place it in root.

Parameters:

Name Type Description Default
url str

URL to download file from

required
root str

Directory to place downloaded file in

required
filename str

Name to save the file under. If None, use the basename of the URL

None
max_redirect_hops Optional[int]

Maximum number of redirect hops allowed

3
headers Optional[dict[str, str]]

Additional required headers - Authentication etc..

None
Source code in common/ayon_common/distribution/file_handler.py
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
@staticmethod
def download_url(
    url: str,
    root: str,
    filename: Optional[str] = None,
    max_redirect_hops: int = 3,
    headers: Optional[dict[str, str]] = None,
):
    """Download a file from url and place it in root.

    Args:
        url (str): URL to download file from
        root (str): Directory to place downloaded file in
        filename (str, optional): Name to save the file under.
            If None, use the basename of the URL
        max_redirect_hops (Optional[int]): Maximum number of redirect
            hops allowed
        headers (Optional[dict[str, str]]): Additional required headers
            - Authentication etc..

    """
    root = os.path.expanduser(root)
    if not filename:
        filename = os.path.basename(url)
    fpath = os.path.join(root, filename)

    if not os.path.exists(root):
        os.makedirs(root, exist_ok=True)

    # expand redirect chain if needed
    url = RemoteFileHandler._get_redirect_url(
        url, max_hops=max_redirect_hops, headers=headers)

    # check if file is located on Google Drive
    file_id = RemoteFileHandler._get_google_drive_file_id(url)
    if file_id is not None:
        return RemoteFileHandler.download_file_from_google_drive(
            file_id, root, filename)

    # download the file
    try:
        print(f"Downloading {url} to {fpath}")
        RemoteFileHandler._urlretrieve(url, fpath, headers=headers)
    except (urllib.error.URLError, IOError) as exc:
        if url[:5] != "https":
            raise exc

        url = url.replace("https:", "http:")
        print((
            "Failed download. Trying https -> http instead."
            f" Downloading {url} to {fpath}"
        ))
        RemoteFileHandler._urlretrieve(url, fpath, headers=headers)