#!/bin/bash

# Title......: rss2md
# Description: Creates a markdown formatted out put from a listing of rss feeds
# Author.....: Mitchell Johnston - uid 0
# Contact....: mitch@crn.hopto.org
# Updated....: Sat 22 Feb 2025 04:17:06 AM CST
#----------------------------------

# Sections: Changes variables functions setup main cartoon
# use '#' in vi/vim to jump to word under cursor

: ' Changes:
Thu Feb 20 2025 Added national forecast map to Weather
Tue Dec 24 2024 Added Toms Hardware to tech
Wed Aug 28 2024 Added rss podcasts
Sun Jun 02 2024 Added quote from Dr Vernon
Sat Feb 10 2024 Added version() to display version and changes
Sat Feb 03 2024 Updated RSS function to test site availability
Sat Feb 03 2024 Added new section for OpEd
Thu Jan 25 2024 Code clean up and new debug settings
Tue Jan 16 2024 Added Crossword
Fri Dec 15 2023 Removed Distro Watch and add Command Line Fu
Tue Dec 05 2023 Added moonphase
Thu Nov 23 2023 Added time and sun info to weather section
Sat Nov 11 2023 Added Ephesians 6:12 
Fri Nov 03 2023 Added earthquake feed 
Sat Oct 28 2023 Fixed an issue with ansiweather not displaying todays hi/lo
Wed Oct 25 2023 Removed /. Linux News feed, does not put much out
Sat Oct 21 2023 Added Distrowatch and EFF to Tech News
Fri Oct 20 2023 Changed Weather layout on bottom of page (some sed action)
Mon Oct 16 2023 Switched to a "here doc" format, easier to maintain
Sun Oct 15 2023 Added tech news section and added local weather (Lisbon, ND)
'

# variables
#----------------------------------
[ "$1" == "-D" ] && DEBUG=1 && shift 1     # -D to turn on debug mode
[ "$DEBUG" == 1 ] && echo "---Variables---" && set -x 
PS4='$SECONDS $LINENO: '                   # debug prompt
DOW=$(date +%a)                            # day of week: Thu
TODAY=$(date +%m/%d)                       # month/day: 03/25
DOM=$(date +%d)                            # day of month: 25
OS=$(uname -s)                             # OS type: SunOS Linux
NAME=${0##*/}                              # name of the script
COUNT=5                                    # number of articles to pull
PIC=$(cat /var/tmp/ctoon.txt)                  # sets the cartoon for the day

# Colors - uncomment if needed
R=$(tput setaf 1)                          # red
BR=$(tput setaf 1; tput bold)              # bold red
G=$(tput setaf 2)                          # green
BG=$(tput setaf 2; tput bold)              # bold green
Y=$(tput setaf 3)                          # yellow
BY=$(tput setaf 3; tput bold)              # bold yellow
B=$(tput setaf 4)                          # blue
BM=$(tput setaf 5; tput bold)              # bold magenta
BC=$(tput setaf 6; tput bold)              # bold cyan
C=$(tput setaf 6)                          # cyan
BL=$(tput setaf 7; tput bold)              # bold light grey
BLD=$(tput bold)                           # bold
N=$(tput sgr0)                             # normal
SIT=$(tput sitm)                           # italics
RIT=$(tput ritm)                           # remove italics
UL=$(tput smul)                            # turn underline on
NL=$(tput rmul)                            # turn underline off
RV=$(tput rev)                             # turn on reverse mode
ROWS=$(tput lines)
COLS=$(tput cols)
[ "$DEBUG" == 1 ] && set +x && read -p "${N}-Continue-" -n 1 x

# functions
#----------------------------------

rss(){ ## simple rss reader
    # use: rss {url} {optional count}
[ "$#" -eq 2 ] && COUNT=${2}

if $(curl -m 3 -Is $1 >/dev/null 2>&1)
then
    rsstail --time-format '%r %m/%d/%Y' --initial "$COUNT" -e 1 --format '#. [{title}]({link}) {updated:>10}\n' "$1" 2>/dev/null
else
    echo "___Site is down___"
fi

}

bl(){ ## write a blank line
    # Use: bl
    [ "$DEBUG" == "1" ] && set -x
    echo ""
}

html(){ ## mark up code
    vim -f +"syn on" +"set nonu" +"set foldenable!" +"set nospell" +"run! syntax/2html.vim" +"wq" +"q" $1
}

log(){ ## creates a basic log entry $LOG must be defined
    # Use: log {entry}  
    [ "$DEBUG" == "1" ] && set -x
    logger -i -t "$NAME" "$*"
}

pause(){ ## simple pause routine
    # Use: pause  {optional number of seconds} or "-nt" for no time out 
    [ "$DEBUG" == "1" ] && set -x
    [ "$1" == "-nt" ] && TMOUT="" && shift
    echo "$BY";
    if [ $# -gt 0 ]
    then
        read -t $1 -r -p "${C}Hit any key (${BY}$1${C} second timeout)${N}" -n 1 FOO;
    else
        read -r -p "${C}Hit any key${N}" -n 1 FOO;
    fi;
    bl
}

strip-tags(){ ## remove HTML tags
  # Use: curl https://linuxhint.com/bash_cut_command/ --silent | strip-tags
  sed -e 's/<[^>]*.//g' -
}

xtitle(){ ## set window title
    # Use: xtitle "Text to display"
    printf "\033]0;%s\007" "$*"
}

version(){ ## display version and change history
    grep -E '^# Updated' $0
    bl
    sed -n "/' Changes/,/^ *$/p" <$0 |grep  -E -v 'sed -n|exit 0|}'
    exit 0
}

# setup
#----------------------------------

# this provides a quick way to edit all my scripts on the fly
if [ "$1" == "-E" ]
then
    vim $0
    sed -i -e "7s/.*/# Updated....: $(date)/" $0
    log "Updated $0"
    html $0
    cp $0 /var/www/unix
    mv $0.html /var/www/unix
    exit
fi

# display help if needed
if [ "$1" == "-h" ] || [ "$1" == "--help" ]
then
fmt -s -w $(tput cols) <<END
$NAME

No options - This is called from the "files" command

END
exit
fi

# display version and change history
if [ "$1" == "-v" ] || [ "$1" == "--version" ]
then
    version
fi

# listing of required apps
command -v bash >/dev/null || sudo apt install bash -qyy
command -v rsstail >/dev/null || sudo pip install rsstail

# main
#--------------------------- 
[ "$DEBUG" == 1 ] && echo "---Functions---" && typeset -F && read -p "${N}-Continue-" -n 1 x
[ "$DEBUG" == 1 ] && echo "---Main---" && set -x

case $1 in
    -o) # OpEd
cat <<EOL

### πŸ“° __OpEd:__ {#oped}

#### _In an empire of lies, truth is treason._
#
[__corbettreport.com__](https://www.corbettreport.com/) _Opensource Intelligence News_
$(rss https://www.corbettreport.com/feed/ 3)

[__Eric Peters__](https://www.ericpetersautos.com/) _Automobiles, Motocycles, and Libertarian Politics_
$(rss https://feeds.feedburner.com/ericpetersautos/ 3)

[__The Organic Prepper__](https://www.theorganicprepper.com/) _The Official Blog of the Apocalypse_ [(__Classes and Books__)](https://selfrelianceandsurvival.com/)
$(rss https://www.theorganicprepper.com/feed/ 3)

[πŸ”](#)
<HR>

### 🎧 __Podcasts:__ {#podcasts}

#### _Words will always retain their power. Words offer the means to meaning, and, for those who will listen, the enunciation of truth._ -V for Vendetta

##### [πŸ”— Mises Institute](https://mises.org/){target="blank"} _Austrian Economics, Freedom, and Peace_

[The Human Action Podcast](https://mises.org/podcasts/human-action-podcast)

$(rss https://mises.org/podcast-feed/127168 3)

[Radio Rothbard](https://mises.org/podcasts/radio-rothbard)

$(rss https://mises.org/podcast-feed/127225 3)

[πŸ”](#)
<HR>

### πŸ’° Metals Prices {#metals}

<div style="text-align: center;"> 

### US Dept:

<iframe src="http://www.USADebtClock.com/us-debt-clock-widget.php" height="80" width="350" scrolling="no" frameborder="0">
</iframe>
<p>Visit <a href="http://www.USADebtClock.com/" target="_blank">USADebtClock.com</a> to learn more!</p>
</div>

<iframe src="https://goldbroker.com/widget/live/XAG?currency=USD&height=320" scrolling="no" frameborder="0" width="100%" height="320" style="border: 0; overflow: hidden;"></iframe><br>Silver price by <a href="https://goldbroker.com/" style="text-transform: uppercase">GoldBroker.com</a>

<iframe src="https://goldbroker.com/widget/live/XAU?currency=USD&height=320" scrolling="no" frameborder="0" width="100%" height="320" style="border: 0; overflow: hidden;"></iframe><br>Gold price by <a href="https://goldbroker.com/" style="text-transform: uppercase">GoldBroker.com</a>

[πŸ”](#)

EOL
    ;;
    -t) # tech news

cat <<EOL
<hr class="pacman"> 

### πŸ“° __Tech News:__ {#tech}

#### _If you can not live without technology, it is no longer a convenience;its enslavement._

[__Distrowatch__](www.distrowatch.com/) _Put the fun back into computing, use Linux & BSD_
$(rss https://distrowatch.com/news/dw.xml)

[__Hacker News__](https://news.ycombinator.com/news) _News for Nerds_
$(rss https://news.ycombinator.com/rss)

[__Tech Crunch__](https://techcrunch.com/)  _Startup and Technology News_
$(rss https://techcrunch.com/feed/)

[__Tom's Hardware__](https://www.tomshardware.com/) _For The Hardcore PC Enthusiast_
$(rss https://www.tomshardware.com/feeds/all)

[πŸ”](#)
<HR>
 

### β›… Weather in North Dakota {#weather}

<a href="https://time.is/Lisbon,_North_Dakota" id="time_is_link" rel="nofollow">Time is:</a>
<span id="Lisbon__North_Dakota_z123"></span>
<script src="//widget.time.is/en.js"></script> 
<script>
time_is_widget.init({Lisbon__North_Dakota_z123:{template:"TIME<br>DATE<br>SUN", date_format:"dayname, monthname dnum, year, week", sun_format:"Sunrise: srhour:srminute Sunset: sshour:ssminute<br>Day length: dlhoursh dlminutesm", coords:"46.4416300,-97.6812100"}});
</script>

$(pom)

$(ansiweather -l 5060162 -u imperial -s true -a false |sed -e 's/Weather in Lisbon/Temp/g' -e 's/- /  \n/g')

$(ansiweather -l 5060162 -u imperial -a false -h true -p true -s true -f4 |sed -e 's/Lisbon forecast => //g' -e 's/- /  \n/g'  |tail -4)

#### National Forecast Map

<div style="text-align: center;">
[!["National Forecast Map"](https://www.wpc.ncep.noaa.gov//noaa/noaa.gif){width=100% height=auto}](https://www.wpc.ncep.noaa.gov/national_forecast/natfcst.php){width=auto% height=auto target="blank"}
</div>

[πŸ”](#)
<HR>
 

### βœ’οΈ  Crossword {#puzzle}

<div style="text-align: center;">
<script type="text/javascript">
boatload_puzzles_format = 'Portrait';</script>
<p>Loading <a href="https://www.boatloadpuzzles.com/playcrossword">
crossword puzzle</a>. One moment please.</p>
<script type="text/javascript"
src="//www.boatloadpuzzles.com/Crossword.js"></script>

[Crossword Nexus](https://www.crosswordnexus.com/ "Crossword Dictionary"){target="blank"}
[___New Jigsaw Puzzle___](/apps/jigsaw){target="blank"}
</div>

[πŸ”](#)
<HR>

### 🎨 Cartoon of the day {#cartoon}

<div style="text-align: center;">
[!["Cartoon of the day"](${PIC}){width=85% height=auto}](/include/pics/ "Cartoon of the day")
</div>

EOL
    ;;
*) # default

    echo "<p class="blue" >__Please remember you are not alone. More and more people are waking up. And once they’re awake they don’t go back to sleep. Distrust the Government, avoid mass media and fight the lies. Remember: if you’re not paranoid by now then you’ve not been paying attention.__ [_- Dr Vernon Coleman_](https://www.vernoncoleman.com/)</p>

    "

    echo "## πŸƒ [Alt-News](#altnews) [OpEd](#oped) [Podcasts](#podcasts) [Metals](#metals) [Tech News](#tech) [Weather](#weather) [Crossword](#puzzle) [Cartoon](#cartoon)"
    echo " "

cat <<EOL

### πŸ“° __Alternative News:__ {#altnews}

#### \`Mat 24:7\` _For nation shall rise against nation, and kingdom against kingdom: and there shall be famines, and pestilences, and earthquakes, in divers places._

[__Activist Post__](https://www.activistpost.com/) _Alternative Independent News - Creative commons_
$(rss https://feeds.feedburner.com/ActivistPost)

[__Antiwar.com__](https://www.antiwar.com/) _News_
$(rss http://news.antiwar.com/feed/)

[__Retraction Watch__](https://retractionwatch.com/feed/) _Tracking retractions as a window into the scientific process_
$(rss https://retractionwatch.com/feed/)

[__🚧 M4.5+ Earthquakes:__](https://earthquake.usgs.gov/earthquakes/map/?extent=-43.58039,-188.4375&extent=71.69129,5.625 "USGS Magnitude 2.5+ Earthquakes, Past Day")

$(rss https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/4.5_day.atom)

\`Ephesians 6:12\` _For we wrestle not against flesh and blood, but against principalities, against powers, against the rulers of the darkness of this world, against spiritual wickedness in high places._

EOL
;;
esac

# That's all folks! 
cat <<ENDING 
<hr class="three">

ENDING
exit