diff --git a/app/download.py b/app/download.py index 95fb63d..7e8b7e9 100644 --- a/app/download.py +++ b/app/download.py @@ -39,68 +39,66 @@ class grab(): print("Downloaded MP4 but no thumbnail found.") class dropout(): - def show(show,season,episode): - directory='/tv/'+show+'/Season '+season+'/' + def show(show, season, episode_start): + directory = f'/tv/{show}/Season {season}/' + if not os.path.exists(directory): + os.makedirs(directory) + with open('/data/dropout.json', 'r') as json_file: url_mapping = json.load(json_file) url = next((item['URL'] for item in url_mapping if item['SHOW'] == show), None) - if url is not None: - url = f'{url}/season:{season}' - else: + if url is None: raise ValueError(f"Show '{show}' not found in the JSON data.") - if not os.path.exists(directory): - os.makedirs(directory) - if episode is None or episode == '': - episode = '%(playlist_index)02d' - else: - try: - dl_ops['playliststart'] = int(episode) - except ValueError: - # Handle the error, e.g., log it or set a default value - dl_ops['playliststart'] = 0 # or some appropriate default value - dl_ops = { - 'format': 'bestvideo+bestaudio/best', - 'audio_quality': '256K', - 'paths': { - 'temp': '/temp', - 'home': directory + + playlist_url = f'{url}/season:{season}' + + # Create match_filter + filter_pattern = ( + "title !~= " + r"'(?i).*behind.?the.?scenes.*" + r"|.*trailer.*" + r"|.*recap.*" + r"|.*last.looks.*'" + ) + match_filter = yt_dlp.utils.match_filter_func(filter_pattern) + + ydl_opts = { + 'quiet': True, + 'skip_download': True, + } + + # Step 1: Extract playlist info + with yt_dlp.YoutubeDL(ydl_opts) as ydl: + playlist_info = ydl.extract_info(playlist_url, download=False) + + entries = playlist_info.get('entries', []) + filtered_entries = [] + for entry in entries: + if match_filter(entry) is None: # Not filtered out + filtered_entries.append(entry) + + # Step 2: Download filtered entries with corrected episode numbers + episode_start = int(episode_start) if episode_start else 1 + + for i, entry in enumerate(filtered_entries, start=episode_start): + episode_number = f"{i:02}" + filename_template = f"{show} - S{int(season):02}E{episode_number} - %(title)s.%(ext)s" + + dl_opts = { + 'format': 'bestvideo+bestaudio/best', + 'audio_quality': '256K', + 'paths': { + 'temp': '/temp', + 'home': directory }, 'cookiefile': '/data/dropout.cookies.txt', - 'match_filter': yt_dlp.utils.match_filter_func("title !~= '(?i).*behind.?the.?scenes.*|.*trailer.*|.*recap.*|.*last.looks.*'"), - # 'reject_title': [ - # r'(?i).*behind.?the.?scenes.*', # Reject titles with "behind the scenes" (case-insensitive) - # r'(?i).*trailer.*', # Reject titles with "trailer" (case-insensitive) - # r'(?i).*recap.*', # Reject titles with "recap" (case-insensitive) - # r'(?i).*last.looks.*' # Reject titles with "last looks" (case-insensitive) - # ], - 'outtmpl': show + ' - S'+f"{int(season):02}"+'E'+episode+' - %(title)s.%(ext)s', - 'noplaylist': True, - # Additional options for downloading subtitles - 'writesubtitles': True, # Download subtitles - 'subtitleslangs': ['en'] # Specify the language for subtitles (e.g., 'en' for English) + 'writesubtitles': True, + 'subtitleslangs': ['en'], + 'outtmpl': filename_template, } - with yt_dlp.YoutubeDL(dl_ops) as ydl: - ydl.download([url]) - # ydl_opts = { - # 'quiet': True, # Turn off download progress messages - # 'skip_download': True, # Don't download anything - # 'cookiefile': '/data/dropout.cookies.txt', - # } - - # with yt_dlp.YoutubeDL(ydl_opts) as ydl: - # info = ydl.extract_info(url, download=False) - - # print(f"Playlist Title: {info.get('title')}") - # print(f"Uploader: {info.get('uploader')}") - # print(f"Number of videos: {len(info.get('entries', []))}") - # print() - - # for i, entry in enumerate(info['entries'], 1): - # print(f"{i}. {entry.get('title')}") - # print(f" URL: {entry.get('webpage_url')}") - # print(f" Duration: {entry.get('duration')} seconds") - # print() + with yt_dlp.YoutubeDL(dl_opts) as ydl: + ydl.download([entry['webpage_url']]) def series():