File size: 527 Bytes
604617b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import io
from urllib.parse import urlparse

import requests


def load_file(path_or_url: str) -> io.BytesIO:
    if _is_url(path_or_url):
        response = requests.get(path_or_url)
        response.raise_for_status()
        return io.BytesIO(response.content)
    else:
        with open(path_or_url, 'rb') as f:
            return io.BytesIO(f.read())


def _is_url(path_or_url):
    try:
        result = urlparse(path_or_url)
        return result.scheme in ('http', 'https')
    except ValueError:
        return False