1
0
personal-data/personal_data/fetchers/youtube.py

62 lines
2.0 KiB
Python

import csv
import json
import logging
import subprocess
import datetime
from dataclasses import dataclass
from personal_data.data import DeduplicateMode, Scraper
from ..util import safe_del
logger = logging.getLogger(__name__)
PLAYLIST_ID='PLAfDVJvDKCvOMvfoTL7eW8GkWNJwd90eV'
#PLAYLIST_ID='LL'
@dataclass(frozen=True)
class YoutubeFavoritesScraper(Scraper):
dataset_name: str = 'youtube_favorites'
deduplicate_mode: DeduplicateMode = DeduplicateMode.BY_ALL_COLUMNS
deduplicate_ignore_columns = []
watch_history: bool = False
def scrape(self) -> list[dict]:
"""Use yt-dlp to fetch the list of favorited videos. This is a placeholder for invoking yt-dlp and parsing its output."""
if self.watch_history:
url = 'https://www.youtube.com/feed/history'
ytdlp_args = [
'yt-dlp',
'--dump-json',
url,
]
else:
url = f'https://www.youtube.com/playlist?list={PLAYLIST_ID}'
ytdlp_args = [
'yt-dlp',
'--flat-playlist',
'--dump-json',
url,
]
result = subprocess.run(
ytdlp_args,
capture_output=True,
text=True,
)
if result.returncode != 0:
raise RuntimeError(f'Non-zero returncode in command: {result.returncode}\n\n{result.stderr}')
output = []
for line in result.stdout.splitlines():
data = json.loads(line)
if self.watch_history:
if 'thumbnails' in data and data['thumbnails']:
data['thumbnail'] = data['thumbnails'][-1]['url']
if 'timestamp' in data:
data['watch_datetime'] = datetime.datetime.fromtimestamp(int(data['timestamp'])).isoformat()
else:
data['thumbnail'] = data['thumbnails'][-1]['url']
safe_del(data, '_type', '_version', 'thumbnails')
output.append(data)
return output