]> gitweb.morketsmerke.org Git - immudex-testing.git/commitdiff
Aktualizacja narzędzia immudex-newsfeed - BT #107. Do przesłania na public
authorxf0r3m <jakubstasinski@protonmail.com>
Fri, 15 Sep 2023 09:04:16 +0000 (11:04 +0200)
committerxf0r3m <jakubstasinski@protonmail.com>
Fri, 15 Sep 2023 09:04:16 +0000 (11:04 +0200)
tools/immudex-newsfeed

index 9ed98ff5f682b589ec35c09db3be7efd0d0a7e32..df85686c419d16432fbf993bf29f1fe07d79c594 100755 (executable)
@@ -1,8 +1,70 @@
 #!/bin/bash
 
+function createBufferFile() {
+  count=0;
+  for mark in $markList; do
+    if [ $count -gt 0 ]; then
+      sed -i "s,$mark,${mark}\n,g" ${1}.buffer;
+    else
+      sed "s,$mark,${mark}\n,g" $1 > ${1}.buffer;
+      count=$((count + 1));
+    fi;
+  done
+}
+
+function descSanitize() {
+  desc=$(echo $@ | sed -e 's,<!\[CDATA\[,,' -e 's,<img.*/>,,' -e 's,\]\]>,,' -e 's,<description>,,' -e 's,</description>,,' -e 's,&lt;,<,g' -e 's,&gt;,>,g' -e 's,<p>,,g' -e 's,<a href=".*">,,' -e 's,</a>,,' -e 's,</p>,,' -e 's/^[[:space:]]*//g');
+  echo -n $desc;
+}
+
+function titleSanitize() {
+  title=$(echo $@ | sed -e 's,<title>,,' -e 's,</title>,,' -e 's,&quot;,",g' -e 's/^[[:space:]]*//g');
+  echo -n $title;
+}
+
+function pubDateSanitize() {
+  pubDate=$(echo $@ | sed -e 's,<pubDate>,,' -e 's,</pubDate>,,' -e 's/^[[:space:]]*//g');
+  echo -n $pubDate;
+}
+
+function linkSanitize() {
+  link=$(echo $@ | sed -e 's,<link>,,' -e 's,</link>,,' -e 's/^[[:space:]]*//g');
+  echo -n $link;
+}
+
+function show() {
+  bufferFile="${1}.buffer";
+  if grep -q '<lastBuildDate>' $bufferFile; then
+    sed -i -e 's,<lastBuildDate>,<pubDate>,g' -e 's,</lastBuildDate>,</pubDate>,g' $bufferFile;
+  fi
+  newsCount=$(grep -o '<title>.*</title>' $bufferFile | wc -l | awk '{printf $1}');
+  count=1
+  while [ $count -le $newsCount ]; do
+    t=$(grep -o '<title>.*</title>' $bufferFile | sed -n "${count}p");
+    title=$(titleSanitize $t);
+    pD=$(grep -o '<pubDate>.*</pubDate>' $bufferFile | sed -n "${count}p")
+    pubDate=$(pubDateSanitize $pD);
+    d=$(grep -o '<description>.*</description>' $bufferFile | sed -n "${count}p")
+    desc=$(descSanitize $d);
+    echo -e "\t$((count - 1)) (Link ID: $count): $title";
+    echo -e "\t -> $pubDate";
+    echo -e "\t >>> $desc";
+    echo;
+    count=$((count + 1))
+  done
+}
+
+function getLink() {
+  bufferFile="${1}.buffer";
+  newsNumber=$2;
+  linkLine=$(grep -o '<link>.*</link>' $bufferFile | sed -n "${newsNumber}p");
+  link=$(linkSanitize $linkLine);
+  echo -n $link;
+}
+
 function help() {
-  echo "immudex-newsfeed - fetch and browse news feed from rss and atom channels";
-  echo "@ 2023 morketsmerke.org";
+  echo "newsfeed - fetch and browse news feed from rss and atom channels";
+  echo "morketsmerke.net; COPYLEFT @ 2022";
   echo;
   echo "Options:";
   echo "  --list - shows numbered list of names, saved in ~/.newsfeed names and";
@@ -21,7 +83,8 @@ function help() {
   echo "  contains: name and URL of rss feed. For example:";
   echo;
   echo "    News Feed;https://newsfeed.example.org/rss";
-} 
+}
 if [ ! -s ~/.newsfeed ]; then
   help;
   exit 1;
@@ -53,7 +116,7 @@ else
           notify-send "$nameOfSubscription" "New $amountOfNewNewses newses" --icon=/usr/share/icons/rss.png;
           j=1;
           sleep 1;
-          while [ $j -le $amountOfNewNewses ]; do
+          while [ $j -le 1 ]; do
             news=$(diff /tmp/new_newsfeed_${i}.xml /tmp/newsfeed_${i}.xml | \
                      grep '<title>' | sed -n "${j}p" | sed 's/<title>//' | \
                       sed 's/<\/title>//' | sed 's/[<>]//' | \
@@ -74,6 +137,9 @@ else
         notify-send "$nameOfSubscription" "New $amountOfNews newses" --icon=/usr/share/icons/rss.png;
       fi
     fi
+    fname="/tmp/newsfeed_${i}.xml";
+    export markList=$(grep -o '</[a-z]*\:*[a-z]*[A-Z]*[a-z]*[A-Z]*[a-z]*>' $fname | sort | uniq | awk '{printf $1" "}');
+    createBufferFile $fname;
     i=$((i + 1));
   done
   if [ "$1" ] && [ "$1" = "--show" ] && [ "$2" ] && [ $2 -gt 0 ]; then
@@ -82,33 +148,12 @@ else
     #Thanks to the linuxhint.com for command:
     #sed 's/^[[:space]]*//g'
     #https://linuxhint.com/trim_string_bash
-
-      if grep -q 'CDATA' /tmp/newsfeed_${2}.xml; then
-        #sed 's/<\/title>/\n/g' /tmp/newsfeed_${2}.xml | \
-        #sed 's/<item>/\n/g' | grep '<title>' | sed -n '2,$p' | \
-        #sed 's/<title>//' | sed 's/&quot;/"/g' | \
-        #sed 's/<!\[CDATA\[//g' | sed 's/\]\]>//g' | \
-        #sed 's/^[[:space:]]*//g' | nl | less;
-        grep -e '<title>' -e '<description>' /tmp/newsfeed_${2}.xml |\
-        sed -n '3,$p' | sed -e 's/<title>//g' -e 's/<description>/>>>/g' |\
-        sed 's/&#[0-9]*;//g' | \
-        sed 's/&quot;//g' | sed -e 's/<!\[CDATA\[//g' -e 's/\]\]>//g' |\
-        sed -e 's/<img.*br\/>/<obrazek>/g' -e 's/^[[:space:]]*//g' \
-        -e 's/<\/description>/\n/g' -e 's/<\/title>//g' | nl -b p^[[:alnum:]] | less
-      else 
-        sed 's/<\/title>/\n/g' /tmp/newsfeed_${2}.xml | \
-        sed 's/<item>/\n/g' | grep '<title>' | sed -n '2,$p' | \
-        sed 's/<title>//' | sed 's/&quot;/"/g' | \
-        sed 's/^[[:space:]]*//g' | nl | less;
-      fi
-      echo $2 > /tmp/lastShowedNewsFeed;
+    show /tmp/newsfeed_${2}.xml | less
+    echo $2 > /tmp/lastShowedNewsFeed;
   elif [ "$1" ] && [ "$1" = "--open" ] && [ "$2" ] && [ $2 -gt 0 ]; then
     subscriptionNumber=$(cat /tmp/lastShowedNewsFeed);
-    if ! grep -q 'CDATA' /tmp/newsfeed_${subscriptionNumber}.xml; then
-      link=$(sed 's/<\/link>/\n/g' /tmp/newsfeed_${subscriptionNumber}.xml | sed 's/<\/title>/\n/g' | grep '<link>' | sed -n '2,$p' | sed 's/<link>//g' | sed -n "${2}p")
-    else
-      link=$(sed 's/<\/link>/\n/g' /tmp/newsfeed_${subscriptionNumber}.xml | sed 's/<\/description>/\n/g' | grep '<link>' | sed -n '2,$p' | sed 's/<link>//g' | sed -n "${2}p")
-    fi
+    fname="/tmp/newsfeed_${subscriptionNumber}.xml";
+    link=$(getLink $fname $2); 
     exo-open --launch WebBrowser "$link";
   fi
 fi