diff --git a/config.yaml.dist b/config.yaml.dist index 3b00d0e..d1dc97c 100644 --- a/config.yaml.dist +++ b/config.yaml.dist @@ -10,6 +10,7 @@ api_url: # where frontend connects to, example: http://api.example.com/ base_url: # used to form links to frontend, example: http://example.com/ data_url: # used to form links to posts and avatars, example: http://example.com/data/ data_dir: # absolute path for posts and avatars storage, example: /srv/www/booru/client/public/data/ +user_agent: # user agent name used to download files from the web on behalf of the api users # usage: schema://user:password@host:port/database_name diff --git a/server/szurubooru/func/net.py b/server/szurubooru/func/net.py index a6e1821..e6326c0 100644 --- a/server/szurubooru/func/net.py +++ b/server/szurubooru/func/net.py @@ -1,10 +1,13 @@ import urllib.request +from szurubooru import config from szurubooru import errors def download(url: str) -> bytes: assert url request = urllib.request.Request(url) + if config.config['user_agent']: + request.add_header('User-Agent', config.config['user_agent']) request.add_header('Referer', url) try: with urllib.request.urlopen(request) as handle: diff --git a/server/szurubooru/tests/func/test_net.py b/server/szurubooru/tests/func/test_net.py index f749d38..fb149b0 100644 --- a/server/szurubooru/tests/func/test_net.py +++ b/server/szurubooru/tests/func/test_net.py @@ -1,7 +1,10 @@ from szurubooru.func import net -def test_download(): +def test_download(config_injector): + config_injector({ + 'user_agent': None + }) url = 'http://info.cern.ch/hypertext/WWW/TheProject.html' expected_content = (