Brainicism / bgutil-ytdlp-pot-provider

Proof-of-origin token provider plugin for yt-dlp
GNU General Public License v3.0
13 stars 1 forks source link

[server] add HTTP `GET /ping` #20

Closed grqz closed 6 days ago

grqz commented 1 week ago

plugin side implementation based on #19 :

diff --git a/plugin/yt_dlp_plugins/extractor/getpot_bgutil_http.py b/plugin/yt_dlp_plugins/extractor/getpot_bgutil_http.py
--- plugin/yt_dlp_plugins/extractor/getpot_bgutil_http.py
+++ plugin/yt_dlp_plugins/extractor/getpot_bgutil_http.py
@@ -18,9 +18,22 @@
             'getpot_bgutil_baseurl', ['http://127.0.0.1:4416'], casesense=True)[0]
         if not data_sync_id and not visitor_data:
             raise UnsupportedRequest(
                 'One of [data_sync_id, visitor_data] must be passed')
-        # TODO: Ping the server
+        try:
+            response = ydl.urlopen(Request(f'{self.base_url}/ping'))
+        except Exception as e:
+            raise UnsupportedRequest(f'Error reaching GET /ping (caused by {str(e)})') from e
+        try:
+            response = json.load(response)
+        except json.JSONDecodeError as e:
+            raise UnsupportedRequest(
+                f'Error parsing response JSON (caused by {str(e)})'
+                f', response: {response.read()}') from e
+        if response.get('version') != self.VERSION:
+            self._logger.warning(
+                'The provider plugin and the http server aren\'t on the same version, '
+                'this may cause compatibility issues.', once=True)
         self.base_url = base_url

     def _get_pot(self, client: str, ydl: YoutubeDL, visitor_data=None, data_sync_id=None, player_url=None, **kwargs) -> str:
         self._logger.info('Generating POT via HTTP server')
grqz commented 6 days ago

strange. autopep8 is suggesting this indentation

diff --git a/plugin/yt_dlp_plugins/extractor/getpot_bgutil_http.py b/plugin/yt_dlp_plugins/extractor/getpot_bgutil_http.py
index 5a298cb..425ad58 100644
--- a/plugin/yt_dlp_plugins/extractor/getpot_bgutil_http.py
+++ b/plugin/yt_dlp_plugins/extractor/getpot_bgutil_http.py
@@ -49,7 +49,7 @@ class BgUtilHTTPPotProviderRH(GetPOTProvider):
                 'visitor_data': visitor_data,
                 'data_sync_id': data_sync_id,
             }).encode(), headers={'Content-Type': 'application/json'},
-            extensions={'timeout': 5.0}))
+                extensions={'timeout': 5.0}))
         except Exception as e:
             raise RequestError(
                 f'Error reaching POST /get_pot (caused by {e!s})') from e