chorus / fetch_comments.py
tdve's picture
Be more selective with clusters, improve UI display
52de180
Raw
History Blame Contribute Delete
2.07 kB
#!/usr/bin/env python3
"""Fetch comments for a Reddit or YouTube URL and print them as JSON.
Usage:
python fetch_comments.py <url>
Credentials are read from the environment (see .env.example); a .env file is
loaded automatically if present.
"""
import json
import logging
import os
import sys
from datetime import datetime, timezone
from urllib.parse import urlparse
from dotenv import load_dotenv
from chorus.api.reddit import RedditClient
from chorus.api.youtube import YouTubeClient
from chorus.logging_config import configure_logging
log = logging.getLogger("chorus.fetch_comments")
def client_for(uri: str):
"""Return the right API client for the given URL based on its host."""
host = (urlparse(uri).hostname or "").lower()
log.debug("Selecting client for host %r", host)
if host.endswith("reddit.com") or host.endswith("redd.it"):
return RedditClient(
client_id=os.environ["REDDIT_CLIENT_ID"],
client_secret=os.environ["REDDIT_CLIENT_SECRET"],
user_agent=os.environ["REDDIT_USER_AGENT"],
)
if host.endswith("youtube.com") or host.endswith("youtu.be"):
return YouTubeClient(api_key=os.environ["YOUTUBE_API_KEY"])
raise ValueError(f"Unsupported URL host: {host!r}")
def main(argv):
if len(argv) != 2:
print(f"Usage: {argv[0]} <url>", file=sys.stderr)
return 1
configure_logging()
load_dotenv()
uri = argv[1]
log.info("Fetching comments for %s", uri)
try:
client = client_for(uri)
metadata = client.get_metadata_by_uri(uri)
comments = client.get_comments_by_uri(uri)
except Exception:
log.exception("Failed to fetch comments for %s", uri)
raise
log.info("Fetched %d comment(s) for %s", len(comments), uri)
output = {
**metadata,
"created_at": datetime.now(timezone.utc).isoformat(),
"comments": comments
}
print(json.dumps(output, indent=2, ensure_ascii=False))
return 0
if __name__ == "__main__":
sys.exit(main(sys.argv))