From ba4c894865589f7e813fca21c4995db15f8efbf1 Mon Sep 17 00:00:00 2001 From: Armaan Bhojwani Date: Tue, 6 Jul 2021 19:58:06 -0400 Subject: [PATCH] rss: implement parallel downloads --- man/rss.1.scd | 8 +++++--- rss | 41 +++++++++++++++++++++++++---------------- 2 files changed, 30 insertions(+), 19 deletions(-) diff --git a/man/rss.1.scd b/man/rss.1.scd index 9ea9f7c..ba58ab2 100644 --- a/man/rss.1.scd +++ b/man/rss.1.scd @@ -4,11 +4,13 @@ rss(1) rss - fetch rss feeds and launch sfeed_curses # SYNOPSIS -rss [path to feeds] +rss [path to feeds] [number of threads] # DESCRIPTION -Downloads feeds from the first argument (defaults to ~/org/feeds), and opens -them in sfeed_curses. Depends on sfeed_curses and libxml2. +Downloads RSS feeds listed in the file provided by the first argument (defaults +to ~/org/feeds), and opens them in sfeed_curses. Download with as many parallel +jobs as specified in the second argument (defaults to 4). Depends on +sfeed_curses and libxml2. # COPYRIGHT This is free and unencumbered software released into the public domain. For more diff --git a/rss b/rss index cfd93e6..5a1da2d 100755 --- a/rss +++ b/rss @@ -1,24 +1,33 @@ #!/usr/bin/env sh -file=${2:-$HOME/org/feeds} -total=$(wc -l "$file" | cut -d " " -f 1) -cachedir=${XDG_CACHE_HOME:-$HOME/.cache/}/sfeed +dl() { + while read -r feed; do + printf "Processing %s\n" "$feed" + curl -s "$feed" | xmllint --format - 1> /tmp/sfeed 2> /dev/null + + title="$(grep -m 1 title /tmp/sfeed | \ + xmllint --xpath "string(//title)" - 2> /dev/null)" + domain=$(echo "$feed" | cut -d/ -f 3) + fname=$(echo "${title:-$domain}" | sed 's|/|!|g' | head -c 25) + sfeed < /tmp/sfeed > "$cachedir/$fname" + done < "$1" +} -mkdir -p "$cachedir" +trap 'trap - SIGTERM && kill -- -$$' INT TERM EXIT -i=1 -while read -r feed; do - printf "Downloading %s\n%s/%s\r" "$feed" "$i" "$total" - curl -s "$feed" | xmllint --format - > /tmp/sfeed - - title="$(grep -m 1 title /tmp/sfeed | xmllint --xpath "string(//title)" -)" - domain=$(echo "$feed" | cut -d/ -f 3) - fname=$(echo "${title:-$domain}" | sed 's|/|!|g' | head -c 25) - sfeed < /tmp/sfeed > "$cachedir/$fname" +file=${1:-$HOME/org/feeds} +total=$(wc -l "$file" | cut -d " " -f 1) +cachedir=${XDG_CACHE_HOME:-$HOME/.cache/}/sfeed +threads=${2:-4} +pere=$(($((total + threads - 1)) / threads)) - i=$((i+1)) -done < "$file" +mkdir -p "$cachedir/split" +(cd "$cachedir/split" || exit; split -l "$pere" "$file") -rm /tmp/sfeed +for j in "$cachedir/split"/*; do + dl "$j" & +done +wait +rm -rf /tmp/sfeed "$cachedir/split" sfeed_curses "$cachedir"/* -- 2.39.2