citadel

My dotfiles, scripts and nix configs
git clone git://jb55.com/citadel
Log | Files | Refs | README | LICENSE

rssfeed (1676B)


      1 #!/usr/bin/env bash
      2 
      3 set -eou pipefail
      4 
      5 if [ -z "$1" ]; then
      6 	printf "usage: rssurl <some.link/rss.xml>\n"
      7 	exit 1
      8 fi
      9 
     10 name=${2:-feed}
     11 typ=${3:-type}
     12 
     13 if [ "$typ" = "@blog" ] || [ "$typ" = "@busy" ]
     14 then
     15 
     16 link=$(cat <<EOF
     17 (.comments | strings) //
     18 (.link | strings) //
     19 (.enclosure | objects | ."@url")
     20 EOF
     21 )
     22 
     23 else
     24 
     25 link=$(cat <<EOF
     26 (.enclosure | objects | ."@url") //
     27 (.link | strings)
     28 EOF
     29 )
     30 
     31 fi
     32 
     33 jqs=$(cat <<EOF
     34 def month_to_num:
     35   if   . == "Jan" then "01"
     36   elif . == "Feb" then "02"
     37   elif . == "Mar" then "03"
     38   elif . == "Apr" then "04"
     39   elif . == "May" then "05"
     40   elif . == "Jun" then "06"
     41   elif . == "Jul" then "07"
     42   elif . == "Aug" then "08"
     43   elif . == "Sep" then "09"
     44   elif . == "Oct" then "10"
     45   elif . == "Nov" then "11"
     46   elif . == "Dec" then "12"
     47   else "01"
     48   end
     49 ;
     50 
     51 def parse_date:
     52   (.pubDate | strings | 
     53   capture("^[a-zA-Z]{3}, (?<day>[0-9]+) (?<month>[a-zA-Z]{3}) (?<year>[0-9]+) (?<h>[0-9]+):(?<m>[0-9]+):(?<s>[0-9]+)")
     54   | "\(.year)-\(.month | month_to_num)-\(.day)T\(.h):\(.m):\(.s)Z"
     55   | fromdateiso8601) //
     56 
     57   (."dc:date" 
     58   | strings
     59   | capture("^(?<year>[0-9]{4})-(?<month>[0-9]{2})-(?<day>[0-9]{2})")
     60   | "\(.year)-\(.month)-\(.day)T00:00:00Z"
     61   | fromdateiso8601
     62   ) //
     63 
     64   ((.published // ."dc:date") | strings | sub(".00:00$"; "Z") | fromdateiso8601) 
     65 
     66 ;
     67 
     68 def parse_url:
     69 	.
     70 	| objects
     71 	| ( ($link // (.link | objects | ."@href"))  //
     72 	    (.link[] | select(."@type" == "application/pdf") | ."@href")
     73 	  )
     74 ;
     75 
     76 (.rss.channel.item // .feed.entry // ."rdf:RDF".item) |
     77 .[] |
     78 [(. | parse_date), "$name", .title, (. | parse_url), "$typ"] |
     79 @tsv
     80 EOF
     81 )
     82 
     83 curl --compressed -sL "$1" | xq -rc "$jqs" || (printf "failed fetching $name\n" >&2 ; exit 2)