mirror of
https://github.com/ytdl-org/youtube-dl
synced 2025-07-21 02:44:14 +09:00
Compare commits
7 Commits
7e3e722022
...
89cf6afe6c
Author | SHA1 | Date | |
---|---|---|---|
![]() |
89cf6afe6c | ||
![]() |
3eb8d22ddb | ||
![]() |
4e714f9df1 | ||
![]() |
c1ea7f5a24 | ||
![]() |
796447b1a5 | ||
![]() |
e6abe3f933 | ||
![]() |
f8d189d8bd |
3
Makefile
3
Makefile
@ -49,7 +49,8 @@ offlinetest: codetest
|
|||||||
--exclude test_subtitles.py \
|
--exclude test_subtitles.py \
|
||||||
--exclude test_write_annotations.py \
|
--exclude test_write_annotations.py \
|
||||||
--exclude test_youtube_lists.py \
|
--exclude test_youtube_lists.py \
|
||||||
--exclude test_youtube_signature.py
|
--exclude test_youtube_signature.py \
|
||||||
|
--exclude test_post_hooks.py
|
||||||
|
|
||||||
tar: youtube-dl.tar.gz
|
tar: youtube-dl.tar.gz
|
||||||
|
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
@echo off
|
@echo off
|
||||||
|
|
||||||
rem Keep this list in sync with the `offlinetest` target in Makefile
|
rem Keep this list in sync with the `offlinetest` target in Makefile
|
||||||
set DOWNLOAD_TESTS="age_restriction^|download^|iqiyi_sdk_interpreter^|socks^|subtitles^|write_annotations^|youtube_lists^|youtube_signature"
|
set DOWNLOAD_TESTS="age_restriction^|download^|iqiyi_sdk_interpreter^|socks^|subtitles^|write_annotations^|youtube_lists^|youtube_signature^|post_hooks"
|
||||||
|
|
||||||
if "%YTDL_TEST_SET%" == "core" (
|
if "%YTDL_TEST_SET%" == "core" (
|
||||||
set test_set="-I test_("%DOWNLOAD_TESTS%")\.py"
|
set test_set="-I test_("%DOWNLOAD_TESTS%")\.py"
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
# Keep this list in sync with the `offlinetest` target in Makefile
|
# Keep this list in sync with the `offlinetest` target in Makefile
|
||||||
DOWNLOAD_TESTS="age_restriction|download|iqiyi_sdk_interpreter|socks|subtitles|write_annotations|youtube_lists|youtube_signature"
|
DOWNLOAD_TESTS="age_restriction|download|iqiyi_sdk_interpreter|socks|subtitles|write_annotations|youtube_lists|youtube_signature|post_hooks"
|
||||||
|
|
||||||
test_set=""
|
test_set=""
|
||||||
multiprocess_args=""
|
multiprocess_args=""
|
||||||
|
68
test/test_post_hooks.py
Normal file
68
test/test_post_hooks.py
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
import unittest
|
||||||
|
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||||
|
|
||||||
|
from test.helper import get_params, try_rm
|
||||||
|
import youtube_dl.YoutubeDL
|
||||||
|
from youtube_dl.utils import DownloadError
|
||||||
|
|
||||||
|
|
||||||
|
class YoutubeDL(youtube_dl.YoutubeDL):
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
super(YoutubeDL, self).__init__(*args, **kwargs)
|
||||||
|
self.to_stderr = self.to_screen
|
||||||
|
|
||||||
|
|
||||||
|
TEST_ID = 'gr51aVj-mLg'
|
||||||
|
EXPECTED_NAME = 'gr51aVj-mLg'
|
||||||
|
|
||||||
|
|
||||||
|
class TestPostHooks(unittest.TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
self.stored_name_1 = None
|
||||||
|
self.stored_name_2 = None
|
||||||
|
self.params = get_params({
|
||||||
|
'skip_download': False,
|
||||||
|
'writeinfojson': False,
|
||||||
|
'quiet': True,
|
||||||
|
'verbose': False,
|
||||||
|
'cachedir': False,
|
||||||
|
})
|
||||||
|
self.files = []
|
||||||
|
|
||||||
|
def test_post_hooks(self):
|
||||||
|
self.params['post_hooks'] = [self.hook_one, self.hook_two]
|
||||||
|
ydl = YoutubeDL(self.params)
|
||||||
|
ydl.download([TEST_ID])
|
||||||
|
self.assertEqual(self.stored_name_1, EXPECTED_NAME, 'Not the expected name from hook 1')
|
||||||
|
self.assertEqual(self.stored_name_2, EXPECTED_NAME, 'Not the expected name from hook 2')
|
||||||
|
|
||||||
|
def test_post_hook_exception(self):
|
||||||
|
self.params['post_hooks'] = [self.hook_three]
|
||||||
|
ydl = YoutubeDL(self.params)
|
||||||
|
self.assertRaises(DownloadError, ydl.download, [TEST_ID])
|
||||||
|
|
||||||
|
def hook_one(self, filename):
|
||||||
|
self.stored_name_1, _ = os.path.splitext(os.path.basename(filename))
|
||||||
|
self.files.append(filename)
|
||||||
|
|
||||||
|
def hook_two(self, filename):
|
||||||
|
self.stored_name_2, _ = os.path.splitext(os.path.basename(filename))
|
||||||
|
self.files.append(filename)
|
||||||
|
|
||||||
|
def hook_three(self, filename):
|
||||||
|
self.files.append(filename)
|
||||||
|
raise Exception('Test exception for \'%s\'' % filename)
|
||||||
|
|
||||||
|
def tearDown(self):
|
||||||
|
for f in self.files:
|
||||||
|
try_rm(f)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
unittest.main()
|
@ -264,6 +264,9 @@ class YoutubeDL(object):
|
|||||||
youtube_dl/postprocessor/__init__.py for a list.
|
youtube_dl/postprocessor/__init__.py for a list.
|
||||||
as well as any further keyword arguments for the
|
as well as any further keyword arguments for the
|
||||||
postprocessor.
|
postprocessor.
|
||||||
|
post_hooks: A list of functions that get called as the final step
|
||||||
|
for each video file, after all postprocessors have been
|
||||||
|
called. The filename will be passed as the only argument.
|
||||||
progress_hooks: A list of functions that get called on download
|
progress_hooks: A list of functions that get called on download
|
||||||
progress, with a dictionary with the entries
|
progress, with a dictionary with the entries
|
||||||
* status: One of "downloading", "error", or "finished".
|
* status: One of "downloading", "error", or "finished".
|
||||||
@ -382,6 +385,7 @@ class YoutubeDL(object):
|
|||||||
self._ies = []
|
self._ies = []
|
||||||
self._ies_instances = {}
|
self._ies_instances = {}
|
||||||
self._pps = []
|
self._pps = []
|
||||||
|
self._post_hooks = []
|
||||||
self._progress_hooks = []
|
self._progress_hooks = []
|
||||||
self._download_retcode = 0
|
self._download_retcode = 0
|
||||||
self._num_downloads = 0
|
self._num_downloads = 0
|
||||||
@ -467,6 +471,9 @@ class YoutubeDL(object):
|
|||||||
pp = pp_class(self, **compat_kwargs(pp_def))
|
pp = pp_class(self, **compat_kwargs(pp_def))
|
||||||
self.add_post_processor(pp)
|
self.add_post_processor(pp)
|
||||||
|
|
||||||
|
for ph in self.params.get('post_hooks', []):
|
||||||
|
self.add_post_hook(ph)
|
||||||
|
|
||||||
for ph in self.params.get('progress_hooks', []):
|
for ph in self.params.get('progress_hooks', []):
|
||||||
self.add_progress_hook(ph)
|
self.add_progress_hook(ph)
|
||||||
|
|
||||||
@ -519,6 +526,10 @@ class YoutubeDL(object):
|
|||||||
self._pps.append(pp)
|
self._pps.append(pp)
|
||||||
pp.set_downloader(self)
|
pp.set_downloader(self)
|
||||||
|
|
||||||
|
def add_post_hook(self, ph):
|
||||||
|
"""Add the post hook"""
|
||||||
|
self._post_hooks.append(ph)
|
||||||
|
|
||||||
def add_progress_hook(self, ph):
|
def add_progress_hook(self, ph):
|
||||||
"""Add the progress hook (currently only for the file downloader)"""
|
"""Add the progress hook (currently only for the file downloader)"""
|
||||||
self._progress_hooks.append(ph)
|
self._progress_hooks.append(ph)
|
||||||
@ -2233,6 +2244,12 @@ class YoutubeDL(object):
|
|||||||
except (PostProcessingError) as err:
|
except (PostProcessingError) as err:
|
||||||
self.report_error('postprocessing: %s' % error_to_compat_str(err))
|
self.report_error('postprocessing: %s' % error_to_compat_str(err))
|
||||||
return
|
return
|
||||||
|
try:
|
||||||
|
for ph in self._post_hooks:
|
||||||
|
ph(filename)
|
||||||
|
except Exception as err:
|
||||||
|
self.report_error('post hooks: %s' % str(err))
|
||||||
|
return
|
||||||
self.record_download_archive(info_dict)
|
self.record_download_archive(info_dict)
|
||||||
# avoid possible nugatory search for further items (PR #26638)
|
# avoid possible nugatory search for further items (PR #26638)
|
||||||
if self._num_downloads >= max_downloads:
|
if self._num_downloads >= max_downloads:
|
||||||
|
@ -32,7 +32,7 @@ class BokeCCBaseIE(InfoExtractor):
|
|||||||
|
|
||||||
|
|
||||||
class BokeCCIE(BokeCCBaseIE):
|
class BokeCCIE(BokeCCBaseIE):
|
||||||
_IE_DESC = 'CC视频'
|
IE_DESC = 'CC视频'
|
||||||
_VALID_URL = r'https?://union\.bokecc\.com/playvideo\.bo\?(?P<query>.*)'
|
_VALID_URL = r'https?://union\.bokecc\.com/playvideo\.bo\?(?P<query>.*)'
|
||||||
|
|
||||||
_TESTS = [{
|
_TESTS = [{
|
||||||
|
@ -9,7 +9,7 @@ from ..utils import (
|
|||||||
|
|
||||||
|
|
||||||
class CloudyIE(InfoExtractor):
|
class CloudyIE(InfoExtractor):
|
||||||
_IE_DESC = 'cloudy.ec'
|
IE_DESC = 'cloudy.ec'
|
||||||
_VALID_URL = r'https?://(?:www\.)?cloudy\.ec/(?:v/|embed\.php\?.*?\bid=)(?P<id>[A-Za-z0-9]+)'
|
_VALID_URL = r'https?://(?:www\.)?cloudy\.ec/(?:v/|embed\.php\?.*?\bid=)(?P<id>[A-Za-z0-9]+)'
|
||||||
_TESTS = [{
|
_TESTS = [{
|
||||||
'url': 'https://www.cloudy.ec/v/af511e2527aac',
|
'url': 'https://www.cloudy.ec/v/af511e2527aac',
|
||||||
|
@ -422,6 +422,8 @@ class InfoExtractor(object):
|
|||||||
_GEO_COUNTRIES = None
|
_GEO_COUNTRIES = None
|
||||||
_GEO_IP_BLOCKS = None
|
_GEO_IP_BLOCKS = None
|
||||||
_WORKING = True
|
_WORKING = True
|
||||||
|
# supply this in public subclasses: used in supported sites list, etc
|
||||||
|
# IE_DESC = 'short description of IE'
|
||||||
|
|
||||||
def __init__(self, downloader=None):
|
def __init__(self, downloader=None):
|
||||||
"""Constructor. Receives an optional downloader."""
|
"""Constructor. Receives an optional downloader."""
|
||||||
|
@ -35,15 +35,6 @@ from ..utils import (
|
|||||||
|
|
||||||
class ITVBaseIE(InfoExtractor):
|
class ITVBaseIE(InfoExtractor):
|
||||||
|
|
||||||
def _search_nextjs_data(self, webpage, video_id, **kw):
|
|
||||||
transform_source = kw.pop('transform_source', None)
|
|
||||||
fatal = kw.pop('fatal', True)
|
|
||||||
return self._parse_json(
|
|
||||||
self._search_regex(
|
|
||||||
r'''<script\b[^>]+\bid=('|")__NEXT_DATA__\1[^>]*>(?P<js>[^<]+)</script>''',
|
|
||||||
webpage, 'next.js data', group='js', fatal=fatal, **kw),
|
|
||||||
video_id, transform_source=transform_source, fatal=fatal)
|
|
||||||
|
|
||||||
def __handle_request_webpage_error(self, err, video_id=None, errnote=None, fatal=True):
|
def __handle_request_webpage_error(self, err, video_id=None, errnote=None, fatal=True):
|
||||||
if errnote is False:
|
if errnote is False:
|
||||||
return False
|
return False
|
||||||
@ -109,7 +100,9 @@ class ITVBaseIE(InfoExtractor):
|
|||||||
|
|
||||||
class ITVIE(ITVBaseIE):
|
class ITVIE(ITVBaseIE):
|
||||||
_VALID_URL = r'https?://(?:www\.)?itv\.com/(?:(?P<w>watch)|hub)/[^/]+/(?(w)[\w-]+/)(?P<id>\w+)'
|
_VALID_URL = r'https?://(?:www\.)?itv\.com/(?:(?P<w>watch)|hub)/[^/]+/(?(w)[\w-]+/)(?P<id>\w+)'
|
||||||
_IE_DESC = 'ITVX'
|
IE_DESC = 'ITVX'
|
||||||
|
_WORKING = False
|
||||||
|
|
||||||
_TESTS = [{
|
_TESTS = [{
|
||||||
'note': 'Hub URLs redirect to ITVX',
|
'note': 'Hub URLs redirect to ITVX',
|
||||||
'url': 'https://www.itv.com/hub/liar/2a4547a0012',
|
'url': 'https://www.itv.com/hub/liar/2a4547a0012',
|
||||||
@ -270,7 +263,7 @@ class ITVIE(ITVBaseIE):
|
|||||||
'ext': determine_ext(href, 'vtt'),
|
'ext': determine_ext(href, 'vtt'),
|
||||||
})
|
})
|
||||||
|
|
||||||
next_data = self._search_nextjs_data(webpage, video_id, fatal=False, default='{}')
|
next_data = self._search_nextjs_data(webpage, video_id, fatal=False, default={})
|
||||||
video_data.update(traverse_obj(next_data, ('props', 'pageProps', ('title', 'episode')), expected_type=dict)[0] or {})
|
video_data.update(traverse_obj(next_data, ('props', 'pageProps', ('title', 'episode')), expected_type=dict)[0] or {})
|
||||||
title = traverse_obj(video_data, 'headerTitle', 'episodeTitle')
|
title = traverse_obj(video_data, 'headerTitle', 'episodeTitle')
|
||||||
info = self._og_extract(webpage, require_title=not title)
|
info = self._og_extract(webpage, require_title=not title)
|
||||||
@ -323,7 +316,7 @@ class ITVIE(ITVBaseIE):
|
|||||||
|
|
||||||
class ITVBTCCIE(ITVBaseIE):
|
class ITVBTCCIE(ITVBaseIE):
|
||||||
_VALID_URL = r'https?://(?:www\.)?itv\.com/(?!(?:watch|hub)/)(?:[^/]+/)+(?P<id>[^/?#&]+)'
|
_VALID_URL = r'https?://(?:www\.)?itv\.com/(?!(?:watch|hub)/)(?:[^/]+/)+(?P<id>[^/?#&]+)'
|
||||||
_IE_DESC = 'ITV articles: News, British Touring Car Championship'
|
IE_DESC = 'ITV articles: News, British Touring Car Championship'
|
||||||
_TESTS = [{
|
_TESTS = [{
|
||||||
'note': 'British Touring Car Championship',
|
'note': 'British Touring Car Championship',
|
||||||
'url': 'https://www.itv.com/btcc/articles/btcc-2018-all-the-action-from-brands-hatch',
|
'url': 'https://www.itv.com/btcc/articles/btcc-2018-all-the-action-from-brands-hatch',
|
||||||
|
@ -47,7 +47,7 @@ class SenateISVPIE(InfoExtractor):
|
|||||||
['vetaff', '76462', 'http://vetaff-f.akamaihd.net'],
|
['vetaff', '76462', 'http://vetaff-f.akamaihd.net'],
|
||||||
['arch', '', 'http://ussenate-f.akamaihd.net/']
|
['arch', '', 'http://ussenate-f.akamaihd.net/']
|
||||||
]
|
]
|
||||||
_IE_NAME = 'senate.gov'
|
IE_NAME = 'senate.gov'
|
||||||
_VALID_URL = r'https?://(?:www\.)?senate\.gov/isvp/?\?(?P<qs>.+)'
|
_VALID_URL = r'https?://(?:www\.)?senate\.gov/isvp/?\?(?P<qs>.+)'
|
||||||
_TESTS = [{
|
_TESTS = [{
|
||||||
'url': 'http://www.senate.gov/isvp/?comm=judiciary&type=live&stt=&filename=judiciary031715&auto_play=false&wmode=transparent&poster=http%3A%2F%2Fwww.judiciary.senate.gov%2Fthemes%2Fjudiciary%2Fimages%2Fvideo-poster-flash-fit.png',
|
'url': 'http://www.senate.gov/isvp/?comm=judiciary&type=live&stt=&filename=judiciary031715&auto_play=false&wmode=transparent&poster=http%3A%2F%2Fwww.judiciary.senate.gov%2Fthemes%2Fjudiciary%2Fimages%2Fvideo-poster-flash-fit.png',
|
||||||
|
@ -686,6 +686,8 @@ class JSInterpreter(object):
|
|||||||
raise self.Exception('Cannot get index {idx!r:.100}'.format(**locals()), expr=repr(obj), cause=e)
|
raise self.Exception('Cannot get index {idx!r:.100}'.format(**locals()), expr=repr(obj), cause=e)
|
||||||
|
|
||||||
def _dump(self, obj, namespace):
|
def _dump(self, obj, namespace):
|
||||||
|
if obj is JS_Undefined:
|
||||||
|
return 'undefined'
|
||||||
try:
|
try:
|
||||||
return json.dumps(obj)
|
return json.dumps(obj)
|
||||||
except TypeError:
|
except TypeError:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user