#!/bin/bash
# Parses downloaded Telugu lyrics HTML pages into JSON.
# Reads from data/telugulyrics/page_*.html
# Outputs JSON array of {title, url, lyrics, movie, singers, lyricist, ...}
# Usage: ./pipelines/telugulyrics-parse.sh | db-push telugulyrics
#        ./pipelines/telugulyrics-parse.sh > data/telugulyrics.json

DATA_DIR="$(dirname "$0")/../data/telugulyrics"
TMP_PREFIX="/tmp/avkf_post"

for html_file in "$DATA_DIR"/page_*.html; do
    echo "▶ Parsing $(basename "$html_file") ..." >&2

    # Split page into one file per post
    rm -f "${TMP_PREFIX}"_*.html
    awk "/itemprop='blogPost'/{n++; f=\"${TMP_PREFIX}_\" n \".html\"} f{print > f}" "$html_file"

    for post_file in "${TMP_PREFIX}"_*.html; do
        [ -f "$post_file" ] || continue

        title=$(htmlq 'h3.post-title a' --text  < "$post_file" 2>/dev/null | head -1)
        url=$(htmlq   'h3.post-title a' --attribute href < "$post_file" 2>/dev/null | head -1)
        lyrics=$(sed 's/<br[^>]*>/\n/gI' < "$post_file" | htmlq 'div.post-body' --text 2>/dev/null)
        raw_labels=$(htmlq '.post-labels a' --text < "$post_file" 2>/dev/null)

        echo "$raw_labels" | jq -Rn \
            --arg title  "$title" \
            --arg url    "$url" \
            --arg lyrics "$lyrics" \
            '[inputs] |
             {
               title:          $title,
               url:            $url,
               lyrics:         $lyrics,
               movie:          (map(select(startswith("Movie - ")))          | map(ltrimstr("Movie - "))          | first),
               music_director: (map(select(startswith("Music Director - "))) | map(ltrimstr("Music Director - ")) | first),
               lyricist:       (map(select(startswith("Lyrics - ")))         | map(ltrimstr("Lyrics - "))         | first),
               singers:        (map(select(startswith("Singer - ")))         | map(ltrimstr("Singer - "))),
               letter:         (map(select(startswith("Letter - ")))         | map(ltrimstr("Letter - ") | ltrimstr("\"") | rtrimstr("\"")) | first)
             }'
    done

    rm -f "${TMP_PREFIX}"_*.html
done | jq -s '.'
