Skip to content

Instantly share code, notes, and snippets.

@aborruso
Last active December 21, 2017 08:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aborruso/45fd178aa911e8b06945caa5c99ba16b to your computer and use it in GitHub Desktop.
Save aborruso/45fd178aa911e8b06945caa5c99ba16b to your computer and use it in GitHub Desktop.
#!/bin/bash
# scarico la pagina
curl -sL "http://www.enricobergamini.it/scrape_feed_tutorial.html" | \
# estraggo i div che contengono gli elementi che voglio trasformare in feed RSS e li converto in JSON
pup 'div[itemprop="articleBody"] json{}' | \
# ristrutturo il JSON e converto le date in formato rfc822, ad esempio 'Fri, 24 Mar 2017 02:00:00 +0200'
jq '[.[] | {title:.children[0].text,href:.children[1].href,pubDate:.children[2].text|strptime("%m/%d/%Y") | strftime("%a, %d %b %Y 02:00:00 +0200")}]' | \
# converto il JSON in CSV
in2csv -I -f json > ./enri_tmp.csv
# cambio il separatore in tab
csvformat -T ./enri_tmp.csv > ./enri.csv
# rimuovo la prima riga
sed -i 1d ./enri.csv
# scarico un template di feed, che userò per costruire quello finale
curl -sL "https://gist.githubusercontent.com/aborruso/74502d4d9508a9fb695bd6d04fb1f665/raw/4dd8bc42e35cd3420d78018faf7fa8603c1fb1d6/feedTemplate.xml" > ./feedTemplate.xml
# creo una copia del template del feed
cp ./feedTemplate.xml ./feed.xml
# inserisco gli attributi di base nel feed
xmlstarlet ed -L --subnode "//channel" --type elem -n title -v "A casa di Enrico i rubinetti non possono perdere" ./feed.xml;
xmlstarlet ed -L --subnode "//channel" --type elem -n description -v "A casa di Enrico i rubinetti non possono perdere" ./feed.xml;
xmlstarlet ed -L --subnode "//channel" --type elem -n link -v "http://evvivaenrico.it/feed.xml" ./feed.xml;
# leggo in loop i dati del file CSV e li uso per creare nuovi item nel file XML
newcounter=0
while IFS=$'\t' read -r title href pubDate
do
newcounter=`expr $newcounter + 1`;
xmlstarlet ed -L --subnode "//channel" --type elem -n item -v "" \
--subnode "//item[$newcounter]" --type elem -n title -v "$title" \
--subnode "//item[$newcounter]" --type elem -n link -v "$href" \
--subnode "//item[$newcounter]" --type elem -n pubDate -v "$pubDate" \
--subnode "//item[$newcounter]" --type elem -n guid -v "$href" \
./feed.xml;
done < ./enri.csv
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment