#!/usr/bin/env python3 import subprocess import sys import os import glob def get_clipboard(): result = subprocess.run(['pbpaste'], capture_output=True, text=True) return result.stdout.strip() def set_clipboard(text): subprocess.run(['pbcopy'], input=text, text=True) def get_url_from_dialog(): applescript = ''' display dialog "Enter YouTube URL:" default answer "" with title "Download Swedish Subtitles" buttons {"Cancel", "OK"} default button "OK" set userInput to text returned of result return userInput ''' try: result = subprocess.run(['osascript', '-e', applescript], capture_output=True, text=True, check=True) return result.stdout.strip() except subprocess.CalledProcessError: return None def clean_subtitles(srt_content): lines = srt_content.strip().split('\n') text_lines = [] seen_lines = set() for line in lines: line = line.strip() if not line or line.isdigit() or '-->' in line: continue if line in seen_lines: continue seen_lines.add(line) text_lines.append(line) return ' '.join(text_lines) def download_subtitles(url): cmd = [ 'yt-dlp', '--write-auto-sub', '--sub-lang', 'sv', '--skip-download', '--convert-subs', 'srt', '-o', '%(id)s.%(ext)s', url ] subprocess.run(cmd, capture_output=True, text=True, check=True) srt_files = glob.glob('*.sv.srt') if srt_files: subtitle_file = srt_files[0] with open(subtitle_file, 'r', encoding='utf-8') as f: raw_content = f.read() cleaned_content = clean_subtitles(raw_content) os.remove(subtitle_file) return cleaned_content sys.exit(1) if __name__ == '__main__': url = get_clipboard() if not url or 'youtube.com' not in url and 'youtu.be' not in url: url = get_url_from_dialog() if not url: sys.exit(1) subtitles = download_subtitles(url) set_clipboard(subtitles)