diff --git a/src/danbooru_backend.py b/src/danbooru_backend.py index 86661b6..4bb4688 100644 --- a/src/danbooru_backend.py +++ b/src/danbooru_backend.py @@ -21,6 +21,9 @@ import requests import os +import string +import os +import random def random_tag(*tags): @@ -67,22 +70,22 @@ def select_from_response(response, profile, max_size=None): return post return None +def random_alpha_numeric_string(n=16): + random.seed(os.urandom(16)) + return ''.join(random.choices(string.ascii_uppercase + string.ascii_lowercase + string.digits, k=n)) + class downloader: - username = None - password = None - max_size = None - tmp = None - url = "" - api_endpoint = "posts.json?random=true&limit=100" - api_tags = "&tags={}" - def __init__(self, backend_credentials): self.username = backend_credentials["username"] self.password = backend_credentials["password"] self.max_size = backend_credentials["max_size"] self.tmp = backend_credentials["tmp_dir"] self.url = backend_credentials["url"] + self.api_endpoint = "posts.json?random=true&limit=100" + self.api_tags = "&tags={}" + # Generate random user-agent string since danbooru disabled api access to browser agent-user strings + self.header = {"user-agent": random_alpha_numeric_string(16)} def download_post(self, post): @@ -113,10 +116,11 @@ class downloader: search_request = None if self.username and self.password: search_request = requests.get(search_url, - auth=(self.username, self.password) + auth=(self.username, self.password), + headers=self.header ) else: - search_request = requests.get(search_url) + search_request = requests.get(search_url, headers=self.header) if search_request.status_code != 200: print("Search request returned:", search_request.status_code)