#!/bin/bash
# usage: list_channel.sh <handle-without-@> <research_dir> [n_long=40] [n_shorts=60]
# Writes raw/list-videos.tsv, raw/list-shorts.tsv (id, title, views, duration) and raw/ids.txt (longform first).
# Public read only (yt-dlp flat playlist); no login, no cookies. --ignore-config keeps a personal yt-dlp config (cookies) out.
set -euo pipefail
[ $# -ge 2 ] || { sed -n 2,4p "$0"; exit 2; }
H=$1; R=$2; NL=${3:-40}; NS=${4:-60}
mkdir -p "$R/raw"
for t in videos shorts; do
  n=$NL; [ $t = shorts ] && n=$NS
  yt-dlp --ignore-config --flat-playlist --playlist-end "$n" --print "%(id)s	%(title)s	%(view_count)s	%(duration)s" \
    "https://www.youtube.com/@$H/$t" > "$R/raw/list-$t.tsv"
done
cat "$R/raw/list-videos.tsv" "$R/raw/list-shorts.tsv" | cut -f1 | grep -v '^$' > "$R/raw/ids.txt"
date -u +%Y-%m-%dT%H:%M:%SZ > "$R/raw/LISTED_AT.txt"
wc -l "$R/raw/list-videos.tsv" "$R/raw/list-shorts.tsv" "$R/raw/ids.txt"
