Skip to content

Commit 3e5e91f

Browse files
committed
2.5.017
1 parent 187ac13 commit 3e5e91f

File tree

6 files changed

+59
-37
lines changed

6 files changed

+59
-37
lines changed

.github/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ To stop playing press Ctrl+C in either the terminal or mpv
9595
<details><summary>List all subcommands</summary>
9696

9797
$ library
98-
xk media library subcommands (v2.5.016)
98+
xk media library subcommands (v2.5.017)
9999

100100
Create database subcommands:
101101
╭───────────────┬────────────────────────────────────────────────────╮

xklb/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "2.5.016"
1+
__version__ = "2.5.017"

xklb/db_media.py

+1
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ def add(args, entry):
185185

186186
tags = entry.pop("tags", None) or ""
187187
chapters = entry.pop("chapters", None) or []
188+
entry.pop("subtitles", None)
188189
entry.pop("description", None)
189190

190191
media_id = args.db.pop("select id from media where path = ?", [entry["path"]])

xklb/search.py

+16-15
Original file line numberDiff line numberDiff line change
@@ -96,29 +96,30 @@ def construct_query(args) -> Tuple[str, dict]:
9696
args.filter_sql.extend([" and " + w for w in args.where])
9797

9898
table = "captions"
99-
if args.db["captions"].detect_fts():
100-
if args.include:
101-
args.table, search_bindings = db_utils.fts_search_sql(
102-
"captions",
103-
fts_table=args.db["captions"].detect_fts(),
104-
include=args.include,
105-
exclude=args.exclude,
106-
flexible=args.flexible_search,
107-
)
108-
args.filter_bindings = {**args.filter_bindings, **search_bindings}
109-
c_columns = {**c_columns, "rank": int}
110-
elif args.exclude:
111-
db_utils.construct_search_bindings(args, ["text"])
99+
cols = args.cols or ["path", "text", "time", "title"]
100+
101+
is_fts = args.db["captions"].detect_fts()
102+
if is_fts and args.include:
103+
table, search_bindings = db_utils.fts_search_sql(
104+
"captions",
105+
fts_table=is_fts,
106+
include=args.include,
107+
exclude=args.exclude,
108+
flexible=args.flexible_search,
109+
)
110+
args.filter_bindings = {**args.filter_bindings, **search_bindings}
111+
c_columns = {**c_columns, "rank": int}
112+
cols.append("id")
113+
cols.append("rank")
112114
else:
113115
db_utils.construct_search_bindings(args, ["text"])
114116

115-
cols = args.cols or ["path", "text", "time", "rank", "title"]
116117
args.select = [c for c in cols if c in {**c_columns, **m_columns, **{"*": "Any"}}]
117118

118119
select_sql = "\n , ".join(args.select)
119120
limit_sql = "LIMIT " + str(args.limit) if args.limit else ""
120121
query = f"""WITH c as (
121-
SELECT id, * FROM {table}
122+
SELECT * FROM {table}
122123
WHERE 1=1
123124
{db_media.filter_args_sql(args, c_columns)}
124125
)

xklb/tube_backend.py

+38-18
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import json, sqlite3, sys
1+
import json, sys
22
from copy import deepcopy
33
from pathlib import Path
44
from types import ModuleType
@@ -178,18 +178,26 @@ def run(self, info) -> Tuple[list, dict]: # pylint: disable=arguments-renamed
178178
db_playlists.increase_update_delay(args, playlist_path)
179179

180180

181+
def yt_subs_config(args):
182+
if args.subs:
183+
return {"writesubtitles": True, "writeautomaticsub": True}
184+
elif args.auto_subs:
185+
return {"writesubtitles": False, "writeautomaticsub": True}
186+
return {}
187+
188+
181189
def get_extra_metadata(args, playlist_path, playlist_dl_opts=None) -> Optional[List[Dict]]:
182190
yt_dlp = load_module_level_yt_dlp()
183191

192+
tables = args.db.table_names()
184193
m_columns = db_utils.columns(args, "media")
185194

186195
with yt_dlp.YoutubeDL(
187196
tube_opts(
188197
args,
189198
func_opts={
190199
"subtitlesformat": "srt/best",
191-
"writesubtitles": args.subs,
192-
"writeautomaticsub": args.auto_subs,
200+
**yt_subs_config(args),
193201
"subtitleslangs": args.subtitle_languages,
194202
"extract_flat": False,
195203
"lazy_playlist": False,
@@ -203,27 +211,40 @@ def get_extra_metadata(args, playlist_path, playlist_dl_opts=None) -> Optional[L
203211
"ignoreerrors": True,
204212
},
205213
playlist_opts=playlist_dl_opts,
206-
),
214+
)
207215
) as ydl:
208-
try:
209-
videos = args.db.execute(
216+
videos = set(
217+
args.db.execute(
210218
f"""
211219
SELECT
212-
id
213-
, path
214-
, playlists_id
220+
id
221+
, path
222+
, null as playlists_id
215223
FROM media
216-
WHERE 1=1
217-
AND COALESCE(time_deleted, 0)=0
224+
WHERE COALESCE(time_deleted, 0)=0
225+
AND path = ?
218226
{'and width is null' if 'width' in m_columns else ''}
219-
and path not like '%playlist%'
220-
and playlists_id = (select id from playlists where path = ?)
221-
ORDER by random()
222227
""",
223228
[playlist_path],
224229
).fetchall()
225-
except sqlite3.OperationalError:
226-
videos = []
230+
)
231+
232+
if "playlists" in tables:
233+
videos |= set(
234+
args.db.execute(
235+
f"""
236+
SELECT
237+
id
238+
, path
239+
, playlists_id
240+
FROM media
241+
WHERE COALESCE(time_deleted, 0)=0
242+
AND playlists_id = (select id from playlists where path = ?)
243+
{'AND width is null' if 'width' in m_columns else ''}
244+
""",
245+
[playlist_path, playlist_path],
246+
).fetchall()
247+
)
227248

228249
current_video_count = 0
229250
for id, path, playlists_id in videos:
@@ -353,8 +374,7 @@ def out_dir(p):
353374
if args.profile != DBType.audio:
354375
func_opts["subtitlesformat"] = "srt/best"
355376
func_opts["subtitleslangs"] = args.subtitle_languages
356-
func_opts["writesubtitles"] = args.subs
357-
func_opts["writeautomaticsub"] = args.auto_subs
377+
func_opts |= yt_subs_config(args)
358378
func_opts["postprocessors"].append({"key": "FFmpegEmbedSubtitle"})
359379

360380
def yt_cli_to_api(opts):

xklb/tube_extract.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ def tube_add(args=None) -> None:
108108

109109
tube_backend.get_playlist_metadata(args, path, tube_backend.tube_opts(args))
110110

111-
if args.extra:
111+
if args.extra or args.subs or args.auto_subs:
112112
log.warning("[%s]: Getting extra metadata", path)
113113
tube_backend.get_extra_metadata(args, path)
114114

@@ -136,6 +136,6 @@ def tube_update(args=None) -> None:
136136
),
137137
)
138138

139-
if args.extra:
139+
if args.extra or args.subs or args.auto_subs:
140140
log.warning("[%s]: Getting extra metadata", d["path"])
141141
tube_backend.get_extra_metadata(args, d["path"], playlist_dl_opts=d.get("extractor_config", "{}"))

0 commit comments

Comments
 (0)