Skip to content
This repository has been archived by the owner on Oct 13, 2024. It is now read-only.

Commit

Permalink
Add error handling and logging for Youtube-DL (#200)
Browse files Browse the repository at this point in the history
Co-authored-by: ReenigneArcher <[email protected]>
  • Loading branch information
zdimension and ReenigneArcher authored Nov 7, 2023
1 parent f32a6c9 commit 4feddda
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
31 changes: 24 additions & 7 deletions Contents/Code/youtube_dl_helper.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# -*- coding: utf-8 -*-

# standard imports
import logging

# plex debugging
try:
import plexhints # noqa: F401
Expand All @@ -10,9 +13,13 @@
from plexhints.prefs_kit import Prefs # prefs kit

# imports from Libraries\Shared
from constants import plugin_identifier
from typing import Optional
import youtube_dl

# get the plugin logger
plugin_logger = logging.getLogger(plugin_identifier)


def process_youtube(url):
# type: (str) -> Optional[str]
Expand All @@ -35,19 +42,29 @@ def process_youtube(url):
...
"""
youtube_dl_params = dict(
outmpl='%(id)s.%(ext)s',
youtube_include_dash_manifest=False,
username=Prefs['str_youtube_user'] if Prefs['str_youtube_user'] else None,
logger=plugin_logger,
outtmpl=u'%(id)s.%(ext)s',
password=Prefs['str_youtube_passwd'] if Prefs['str_youtube_passwd'] else None,
socket_timeout=10,
username=Prefs['str_youtube_user'] if Prefs['str_youtube_user'] else None,
youtube_include_dash_manifest=False,
)

ydl = youtube_dl.YoutubeDL(params=youtube_dl_params)

with ydl:
result = ydl.extract_info(
url=url,
download=False # We just want to extract the info
)
try:
result = ydl.extract_info(
url=url,
download=False # We just want to extract the info
)
except Exception as exc:
if isinstance(exc, youtube_dl.utils.ExtractorError) and exc.expected:
Log.Info('YDL returned YT error while downloading %s: %s' % (url, exc))
else:
Log.Exception('YDL returned an unexpected error while downloading %s: %s' % (url, exc))
return None

if 'entries' in result:
# Can be a playlist or a list of videos
video_data = result['entries'][0]
Expand Down
8 changes: 2 additions & 6 deletions tests/unit/test_youtube_dl_helper.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
# -*- coding: utf-8 -*-
# lib imports
import pytest
from youtube_dl import DownloadError

# local imports
from Code import youtube_dl_helper

Expand All @@ -26,5 +22,5 @@ def test_process_youtube_invalid():
'https://blahblahblah',
]
for url in invalid_urls:
with pytest.raises(DownloadError):
youtube_dl_helper.process_youtube(url=url)
audio_url = youtube_dl_helper.process_youtube(url=url)
assert audio_url is None

0 comments on commit 4feddda

Please sign in to comment.