1
0
Fork 0
mirror of https://github.com/Findus23/cr-search.git synced 2024-09-19 15:23:44 +02:00
cr-search/fetch.py

104 lines
3.8 KiB
Python
Raw Normal View History

import hashlib
2020-08-08 15:26:13 +02:00
import os
2020-08-11 12:19:01 +02:00
from datetime import datetime
2020-08-11 11:53:23 +02:00
from shutil import move
2020-03-07 10:45:39 +01:00
from subprocess import run
import youtube_dl
2020-04-15 18:11:45 +02:00
from peewee import DoesNotExist
2020-03-07 10:45:39 +01:00
2020-08-13 17:11:18 +02:00
from data import series_data
from models import Episode, Series, Line, Phrase
2021-05-25 20:53:09 +02:00
from utils import srtdir, pretty_title, title_to_episodenumber
2020-03-07 10:45:39 +01:00
2020-03-08 18:48:14 +01:00
2020-08-30 22:11:28 +02:00
def main() -> None:
2020-08-08 15:26:13 +02:00
os.nice(15)
2020-04-15 18:11:45 +02:00
for series in series_data:
2020-08-30 22:11:28 +02:00
name = series.name
playlist_id = series.playlist_id
2020-04-15 18:11:45 +02:00
is_campaign = "Campaign" in name
try:
s = Series.select().where(Series.title == name).get()
except DoesNotExist:
s = Series()
s.title = name
s.is_campaign = is_campaign
2020-08-30 22:11:28 +02:00
s.single_speaker = series.single_speaker
2020-04-15 18:11:45 +02:00
s.save()
2020-03-08 18:48:14 +01:00
ydl_opts = {
'extract_flat': True
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
2020-04-15 18:11:45 +02:00
playlist = ydl.extract_info("https://www.youtube.com/playlist?list=" + playlist_id, download=False)
2020-03-08 18:48:14 +01:00
videos = playlist["entries"]
print(v["url"] for v in videos)
2020-08-30 22:11:28 +02:00
ydl_opts_download = {
2020-03-08 18:48:14 +01:00
"writesubtitles": True,
2020-08-11 11:53:23 +02:00
"subtitleslangs": ["en", "en-US"],
2020-03-08 18:48:14 +01:00
"skip_download": True,
}
for nr, video in enumerate(videos, 1):
try:
2020-04-15 18:11:45 +02:00
e = Episode.select().where((Episode.series == s) & (Episode.video_number == nr)).get()
2021-06-06 18:55:33 +02:00
# if (e.series.id == 1) or (e.series.id == 2 and e.video_number < 90):
# continue
2020-08-30 21:33:23 +02:00
# if e.downloaded:
# continue
2020-04-15 18:11:45 +02:00
except DoesNotExist:
e = Episode()
e.series = s
e.video_number = nr
e.title = video["title"]
e.pretty_title = pretty_title(video["title"])
2020-04-15 18:11:45 +02:00
if s.is_campaign:
2021-06-06 19:02:49 +02:00
if e.series.id == 1 and ("Search For Grog" in e.title or "Search For Bob" in e.title):
2021-06-06 18:55:09 +02:00
continue
2021-05-25 20:53:09 +02:00
e.episode_number = title_to_episodenumber(e.title, e.video_number)
2020-04-15 18:11:45 +02:00
else:
e.episode_number = e.video_number
2020-03-08 18:48:14 +01:00
e.youtube_id = video["url"]
e.save()
2020-08-30 21:33:23 +02:00
print(e.series.id, e.episode_number, e.pretty_title)
2020-04-15 18:11:45 +02:00
vttfile = srtdir / str(e.id)
2020-08-30 22:11:28 +02:00
ydl_opts_download["outtmpl"] = str(vttfile)
with youtube_dl.YoutubeDL(ydl_opts_download) as ydl:
2020-03-08 18:48:14 +01:00
ydl.download([f'https://www.youtube.com/watch?v={e.youtube_id}'])
2020-08-11 11:53:23 +02:00
if vttfile.with_suffix(".en-US.vtt").exists():
# few videos have en-US as language code instead of en
move(vttfile.with_suffix(".en-US.vtt"), vttfile.with_suffix(".en.vtt"))
2020-08-30 21:33:23 +02:00
output = run(
["ffmpeg", "-y", "-i", vttfile.with_suffix(".en.vtt"), vttfile.with_suffix(".srt")],
capture_output=True
)
e.downloaded = True
try:
vttfile.with_suffix(".en.vtt").unlink()
with vttfile.with_suffix(".srt").open("rb") as f:
file_hash = hashlib.sha256()
while True:
chunk = f.read(8192)
if not chunk:
break
file_hash.update(chunk)
if e.subtitle_hash != file_hash.hexdigest():
2020-08-30 21:33:23 +02:00
print("subtitle hash changed, deleting imported data")
Line.delete().where(Line.episode == e)
Phrase.delete().where(Phrase.episode == e)
e.phrases_imported = False
e.text_imported = False
e.subtitle_hash = file_hash.hexdigest()
2020-08-11 12:19:01 +02:00
e.last_updated = datetime.now()
except FileNotFoundError:
e.downloaded = False
e.save()
2020-03-08 18:48:14 +01:00
if __name__ == '__main__':
main()