1
0

style: Run linter and fix code formatting in youtube.py

This commit is contained in:
Jon Michael Aanes (aider) 2025-03-15 21:53:27 +01:00
parent dbc663cbbc
commit 965689df7a

View File

@ -1,15 +1,17 @@
import csv import csv
import json import json
import subprocess
import logging import logging
import subprocess
from dataclasses import dataclass from dataclasses import dataclass
from personal_data.data import DeduplicateMode, Scraper from personal_data.data import DeduplicateMode, Scraper
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@dataclass(frozen=True) @dataclass(frozen=True)
class YoutubeFavoritesScraper(Scraper): class YoutubeFavoritesScraper(Scraper):
dataset_name: str = "youtube_favorites" dataset_name: str = 'youtube_favorites'
deduplicate_mode: DeduplicateMode = DeduplicateMode.BY_ALL_COLUMNS deduplicate_mode: DeduplicateMode = DeduplicateMode.BY_ALL_COLUMNS
def fetch_data(self) -> list[dict]: def fetch_data(self) -> list[dict]:
@ -20,7 +22,12 @@ class YoutubeFavoritesScraper(Scraper):
try: try:
# Replace 'YOUR_FAVORITES_ID' with your actual favorites playlist ID. # Replace 'YOUR_FAVORITES_ID' with your actual favorites playlist ID.
result = subprocess.run( result = subprocess.run(
['yt-dlp', '--flat-playlist', '--dump-json', 'https://www.youtube.com/playlist?list=YOUR_FAVORITES_ID'], [
'yt-dlp',
'--flat-playlist',
'--dump-json',
'https://www.youtube.com/playlist?list=YOUR_FAVORITES_ID',
],
capture_output=True, capture_output=True,
check=True, check=True,
text=True, text=True,
@ -28,7 +35,7 @@ class YoutubeFavoritesScraper(Scraper):
videos = [json.loads(line) for line in result.stdout.splitlines()] videos = [json.loads(line) for line in result.stdout.splitlines()]
return videos return videos
except Exception as e: except Exception as e:
logger.error("Failed to fetch YouTube favorites: %s", e) logger.error('Failed to fetch YouTube favorites: %s', e)
raise raise
def to_csv(self, videos: list[dict]) -> str: def to_csv(self, videos: list[dict]) -> str:
@ -36,16 +43,19 @@ class YoutubeFavoritesScraper(Scraper):
Convert the list of videos to CSV format. Convert the list of videos to CSV format.
""" """
output = [] output = []
headers = ["id", "title", "url", "upload_date"] headers = ['id', 'title', 'url', 'upload_date']
output.append(headers) output.append(headers)
for video in videos: for video in videos:
output.append([ output.append(
video.get("id"), [
video.get("title"), video.get('id'),
video.get("url"), video.get('title'),
video.get("upload_date"), video.get('url'),
]) video.get('upload_date'),
],
)
from io import StringIO from io import StringIO
sio = StringIO() sio = StringIO()
csv_writer = csv.writer(sio) csv_writer = csv.writer(sio)
csv_writer.writerows(output) csv_writer.writerows(output)
@ -54,7 +64,7 @@ class YoutubeFavoritesScraper(Scraper):
def run(self) -> None: def run(self) -> None:
videos = self.fetch_data() videos = self.fetch_data()
csv_data = self.to_csv(videos) csv_data = self.to_csv(videos)
logger.info("Fetched and converted %d videos to CSV", len(videos)) logger.info('Fetched and converted %d videos to CSV', len(videos))
with open("youtube_favorites.csv", "w", encoding="utf-8") as f: with open('youtube_favorites.csv', 'w', encoding='utf-8') as f:
f.write(csv_data) f.write(csv_data)
logger.info("CSV file written to youtube_favorites.csv") logger.info('CSV file written to youtube_favorites.csv')