Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plugin error handling #17

Merged
merged 9 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,5 +99,5 @@ If using option (a) HTTP Server for the provider, use yt-dlp like normal 🙂.
If using option (b) script for the provider, you need to pass extractor arguments including the path to the generation script for each yt-dlp call. Make sure to point to the transpiled version, `server/build/generate_once.js`
Brainicism marked this conversation as resolved.
Show resolved Hide resolved

```shell
./yt-dlp --extractor-args "youtube:getpot_bgutil_script=/home/user/bgutil-test/bgutil-ytdlp-pot-provider/server/build/generate_once.js"
./yt-dlp --extractor-args "youtube:getpot_bgutil_script=/home/user/bgutil-ytdlp-pot-provider/server/build/generate_once.js"
```
39 changes: 29 additions & 10 deletions plugin/yt_dlp_plugins/extractor/getpot_bgutil.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import json
import subprocess
import os.path
import shutil
from yt_dlp import YoutubeDL

from yt_dlp.networking.common import Request
Expand Down Expand Up @@ -27,22 +29,35 @@ def _get_pot(self, client: str, ydl: YoutubeDL, visitor_data=None, data_sync_id=
po_token = self._get_pot_via_http(ydl, client, visitor_data, data_sync_id)

return po_token
grqz marked this conversation as resolved.
Show resolved Hide resolved

def _get_pot_via_http(self, ydl, client, visitor_data, data_sync_id):
response = ydl.urlopen(Request('http://127.0.0.1:4416/get_pot', data=json.dumps({
'client': client,
'visitor_data': visitor_data,
'data_sync_id': data_sync_id
}).encode(), headers = {'Content-Type': 'application/json'}))

response_json = json.loads(response.read().decode('utf-8'))

def _get_pot_via_http(self, ydl, client, visitor_data, data_sync_id):
try:
response = ydl.urlopen(Request('http://127.0.0.1:4416/get_pot', data=json.dumps({
Brainicism marked this conversation as resolved.
Show resolved Hide resolved
'client': client,
'visitor_data': visitor_data,
'data_sync_id': data_sync_id
}).encode(), headers = {'Content-Type': 'application/json'}))
except Exception as e:
raise RequestError(f"Error reaching POST /get_pot: {str(e)}")

try:
response_json = json.loads(response.read().decode('utf-8'))
except Exception as e:
raise RequestError(f"Error parsing response JSON: {str(e)}. response = {response.read().decode('utf-8')}")
grqz marked this conversation as resolved.
Show resolved Hide resolved

if 'po_token' not in response_json:
raise RequestError('Server did not respond with a po_token')

return response_json["po_token"]

def _get_pot_via_script(self, script_path, visitor_data, data_sync_id):
if not os.path.isfile(script_path):
raise RequestError(f"Script path doesn't exist: {script_path}")
if os.path.basename(script_path) != 'generate_once.js':
raise RequestError(f"Incorrect script passed to extractor args. Path to generate_once.js required")
if shutil.which('node') is None:
raise RequestError(f"node is not in PATH")
grqz marked this conversation as resolved.
Show resolved Hide resolved

# possibly vulnerable to shell injection here? but risk is low
command_args = ['node', script_path]
if data_sync_id:
Expand All @@ -62,4 +77,8 @@ def _get_pot_via_script(self, script_path, visitor_data, data_sync_id):
# the JSON response is always the last line
script_data_resp = result.stdout.splitlines()[-1]
self._logger.debug(f"_get_pot_via_script response = {script_data_resp}")
return json.loads(script_data_resp)['poToken']
try:
return json.loads(script_data_resp)['poToken']
except:
raise RequestError("Error parsing JSON response from _get_pot_via_script")
grqz marked this conversation as resolved.
Show resolved Hide resolved