From 57c90379ebf844f0ca61757905dd329a6b79baee Mon Sep 17 00:00:00 2001 From: CodeBoxer Date: Tue, 5 May 2026 14:26:43 +0600 Subject: [PATCH] Hypnotix: Add fallback User-Agent to Xtream API requests to prevent 403 errors (#274) --- usr/lib/hypnotix/xtream.py | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/usr/lib/hypnotix/xtream.py b/usr/lib/hypnotix/xtream.py index 05775cac..0eda8db7 100644 --- a/usr/lib/hypnotix/xtream.py +++ b/usr/lib/hypnotix/xtream.py @@ -29,6 +29,10 @@ import requests +HEADERS = { + 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36' +} + class Channel: # Required by Hypnotix @@ -433,7 +437,7 @@ def authenticate(self): self.auth_data = {} try: # Request authentication, wait 4 seconds maximum - r = requests.get(self.get_authenticate_URL(), timeout=(4), headers={'User-Agent': self.user_agent }) + r = requests.get(self.get_authenticate_URL(), timeout=(4), headers=self._get_headers()) # If the answer is ok, process data and change state if r.ok: self.auth_data = r.json() @@ -735,6 +739,21 @@ def get_series_info_by_id(self, get_series: dict): ) season.episodes[episode_info["title"]] = new_episode_channel + def _get_headers(self): + """Build the headers dictionary for outbound requests. + + Uses the module-level HEADERS as a base. If the user has configured + a custom User-Agent (via Hypnotix preferences), it overrides the + default. + + Returns: + dict: Headers dictionary suitable for requests.get() + """ + headers = dict(HEADERS) + if self.user_agent: + headers['User-Agent'] = self.user_agent + return headers + def _get_request(self, URL: str, timeout: Tuple = (2, 15)): """Generic GET Request with Error handling @@ -746,7 +765,7 @@ def _get_request(self, URL: str, timeout: Tuple = (2, 15)): [type]: JSON dictionary of the loaded data, or None """ try: - r = requests.get(URL, timeout=timeout, headers={'User-Agent': self.user_agent }) + r = requests.get(URL, timeout=timeout, headers=self._get_headers()) if r.status_code == 200: return r.json()