[outtmpl] Add type `link` for internet shortcut files

and refactor related code
Closes #1405
pull/1386/head
pukkandan 3 years ago
parent 7de837a5e3
commit 08438d2ca5
No known key found for this signature in database
GPG Key ID: 0F00D95A001F4698

@ -1034,7 +1034,7 @@ To summarize, the general syntax for a field is:
%(name[.keys][addition][>strf][,alternate][|default])[flags][width][.precision][length]type %(name[.keys][addition][>strf][,alternate][|default])[flags][width][.precision][length]type
``` ```
Additionally, you can set different output templates for the various metadata files separately from the general output template by specifying the type of file followed by the template separated by a colon `:`. The different file types supported are `subtitle`, `thumbnail`, `description`, `annotation` (deprecated), `infojson`, `pl_thumbnail`, `pl_description`, `pl_infojson`, `chapter`. For example, `-o '%(title)s.%(ext)s' -o 'thumbnail:%(title)s\%(title)s.%(ext)s'` will put the thumbnails in a folder with the same name as the video. If any of the templates (except default) is empty, that type of file will not be written. Eg: `--write-thumbnail -o "thumbnail:"` will write thumbnails only for playlists and not for video. Additionally, you can set different output templates for the various metadata files separately from the general output template by specifying the type of file followed by the template separated by a colon `:`. The different file types supported are `subtitle`, `thumbnail`, `description`, `annotation` (deprecated), `infojson`, `link`, `pl_thumbnail`, `pl_description`, `pl_infojson`, `chapter`. For example, `-o '%(title)s.%(ext)s' -o 'thumbnail:%(title)s\%(title)s.%(ext)s'` will put the thumbnails in a folder with the same name as the video. If any of the templates (except default) is empty, that type of file will not be written. Eg: `--write-thumbnail -o "thumbnail:"` will write thumbnails only for playlists and not for video.
The available fields are: The available fields are:

@ -56,9 +56,6 @@ from .utils import (
DEFAULT_OUTTMPL, DEFAULT_OUTTMPL,
determine_ext, determine_ext,
determine_protocol, determine_protocol,
DOT_DESKTOP_LINK_TEMPLATE,
DOT_URL_LINK_TEMPLATE,
DOT_WEBLOC_LINK_TEMPLATE,
DownloadError, DownloadError,
encode_compat_str, encode_compat_str,
encodeFilename, encodeFilename,
@ -77,6 +74,7 @@ from .utils import (
iri_to_uri, iri_to_uri,
ISO3166Utils, ISO3166Utils,
LazyList, LazyList,
LINK_TEMPLATES,
locked_file, locked_file,
make_dir, make_dir,
make_HTTPS_handler, make_HTTPS_handler,
@ -2665,53 +2663,41 @@ class YoutubeDL(object):
return return
# Write internet shortcut files # Write internet shortcut files
url_link = webloc_link = desktop_link = False def _write_link_file(link_type):
if self.params.get('writelink', False):
if sys.platform == "darwin": # macOS.
webloc_link = True
elif sys.platform.startswith("linux"):
desktop_link = True
else: # if sys.platform in ['win32', 'cygwin']:
url_link = True
if self.params.get('writeurllink', False):
url_link = True
if self.params.get('writewebloclink', False):
webloc_link = True
if self.params.get('writedesktoplink', False):
desktop_link = True
if url_link or webloc_link or desktop_link:
if 'webpage_url' not in info_dict: if 'webpage_url' not in info_dict:
self.report_error('Cannot write internet shortcut file because the "webpage_url" field is missing in the media information') self.report_error('Cannot write internet shortcut file because the "webpage_url" field is missing in the media information')
return return False
ascii_url = iri_to_uri(info_dict['webpage_url']) linkfn = replace_extension(self.prepare_filename(info_dict, 'link'), link_type, info_dict.get('ext'))
def _write_link_file(extension, template, newline, embed_filename):
linkfn = replace_extension(full_filename, extension, info_dict.get('ext'))
if self.params.get('overwrites', True) and os.path.exists(encodeFilename(linkfn)): if self.params.get('overwrites', True) and os.path.exists(encodeFilename(linkfn)):
self.to_screen('[info] Internet shortcut is already present') self.to_screen(f'[info] Internet shortcut (.{link_type}) is already present')
else: return True
try: try:
self.to_screen('[info] Writing internet shortcut to: ' + linkfn) self.to_screen(f'[info] Writing internet shortcut (.{link_type}) to: {linkfn}')
with io.open(encodeFilename(to_high_limit_path(linkfn)), 'w', encoding='utf-8', newline=newline) as linkfile: with io.open(encodeFilename(to_high_limit_path(linkfn)), 'w', encoding='utf-8',
template_vars = {'url': ascii_url} newline='\r\n' if link_type == 'url' else '\n') as linkfile:
if embed_filename: template_vars = {'url': iri_to_uri(info_dict['webpage_url'])}
template_vars['filename'] = linkfn[:-(len(extension) + 1)] if link_type == 'desktop':
linkfile.write(template % template_vars) template_vars['filename'] = linkfn[:-(len(link_type) + 1)]
except (OSError, IOError): linkfile.write(LINK_TEMPLATES[link_type] % template_vars)
self.report_error('Cannot write internet shortcut ' + linkfn) except (OSError, IOError):
return False self.report_error(f'Cannot write internet shortcut {linkfn}')
return False
return True return True
if url_link: write_links = {
if not _write_link_file('url', DOT_URL_LINK_TEMPLATE, '\r\n', embed_filename=False): 'url': self.params.get('writeurllink'),
return 'webloc': self.params.get('writewebloclink'),
if webloc_link: 'desktop': self.params.get('writedesktoplink'),
if not _write_link_file('webloc', DOT_WEBLOC_LINK_TEMPLATE, '\n', embed_filename=False): }
return if self.params.get('writelink'):
if desktop_link: link_type = ('webloc' if sys.platform == 'darwin'
if not _write_link_file('desktop', DOT_DESKTOP_LINK_TEMPLATE, '\n', embed_filename=True): else 'desktop' if sys.platform.startswith('linux')
return else 'url')
write_links[link_type] = True
if any(should_write and not _write_link_file(link_type)
for link_type, should_write in write_links.items()):
return
try: try:
info_dict, files_to_move = self.pre_process(info_dict, 'before_dl', files_to_move) info_dict, files_to_move = self.pre_process(info_dict, 'before_dl', files_to_move)

@ -4503,6 +4503,7 @@ OUTTMPL_TYPES = {
'description': 'description', 'description': 'description',
'annotation': 'annotations.xml', 'annotation': 'annotations.xml',
'infojson': 'info.json', 'infojson': 'info.json',
'link': None,
'pl_thumbnail': None, 'pl_thumbnail': None,
'pl_description': 'description', 'pl_description': 'description',
'pl_infojson': 'info.json', 'pl_infojson': 'info.json',
@ -6238,6 +6239,12 @@ URL=%(url)s
Icon=text-html Icon=text-html
'''.lstrip() '''.lstrip()
LINK_TEMPLATES = {
'url': DOT_URL_LINK_TEMPLATE,
'desktop': DOT_DESKTOP_LINK_TEMPLATE,
'webloc': DOT_WEBLOC_LINK_TEMPLATE,
}
def iri_to_uri(iri): def iri_to_uri(iri):
""" """

Loading…
Cancel
Save