How to obtain Sunrise & Sunset time for any location from Linux command line - LinuxConfig.org


#1
The objective is to use command line and bash shell script to obtain Sunrise & Sunset time information for any given location.
This is a companion discussion topic for the original entry at https://linuxconfig.org/how-to-obtain-sunrise-sunset-time-for-any-location-from-linux-command-line

NOTE: SOME OF THE POSTS BELOW ARE TRANSFERRED FROM LINUXCONFIG DISQUS COMMENTS.

#2

Rob Robinett

This works great on my Pi, but I am distributing a script to a large number of users and requiring lynx is too intrusive for many of them. Could lynx be replaced with curl, which is part of the standard distro?


#3

Lubos Rendek -> Rob Robinett

Please note that disqus comment ate most of the code hence this is a reason why we are transferring all comments to https://forum.linuxconfig.org/. The comments for this article will be transferred soon.

For now the safest is to clone the git repository to get a working code.


#4

Lubos Rendek -> Rob Robinett

Hi Rob,

thanks for the feedback. Lynx is extremely convenient in this case. However, I’ve got good results with curl/wget too. Try to replace the lynx line with this:

sun_times=$( curl -s  https://weather.com/weather/today/l/$location | sed 's/<span/\n/g' | sed 's/<\/span>/\n/g'  | grep -E "dp0-details-sunrise|dp0-details-sunset" | tr -d '\n' | sed 's/>/ /g' | cut -d " " -f 4,8 )`

It is not the prettiest code but it worked for me. Since we now work with the html output we can as well use wget to the exactly the same query:

sun_times=$( wget -qO-  https://weather.com/weather/today/l/$location | sed 's/<span/\n/g' | sed 's/<\/span>/\n/g'  | grep -E "dp0-details-sunrise|dp0-details-sunset" | tr -d '\n' | sed 's/>/ /g' | cut -d " " -f 4,8 )

GNU/Linux is about choices and that is why I like it. I still see the lynx option more reliable as the curl and wget options where we rely on the html code.

PS: I will update this article to include curl and wget option shortly. However, the git repository is updated already.

Hope this helps…

Lubos


#5

Rob Robinett -> Lubos Rendek

Hi Lubos,
I appreciate why lynx is better and even more that you responded so quickly. I think a space character got lost in your posting since your first sed ‘s//\ng’ is missing a search term.
But you can simplify your code and execute only two instances of sed by assigning the parsed html to a bash array which you reference later:

sun_times=($( wget -qO- https://weather.com/weather/today/l/$location | sed 's/ /\n/g' | sed -n '/p0-details-sunrise\|dp0-details-sunset/s/[^>]*>//p' ))
# Extract sunrise and sunset times and convert to 24 hour format
sunrise=$(date --date="${sun_times[0]} AM" +%R)
sunset=$(date --date="${sun_times[1]} PM" +%R)

Thanks!


#6

Fraser

Works on my raspberry pi, thanks!

As a nice little addition I set it to run at 3am in crontab:

0 3 * * * fraser /home/fraser/bin/sunrise >/dev/null 2>&1

and added a line to schedule power led to turn on at sunrise:

# echo "echo input | sudo tee /sys/devices/platform/leds/leds/led1/trigger" | at $sunrise

You’ll need the ‘at’ command, install with apt-get --no-install-recommends to avoid about 24MB of mail servers which are recommended with ‘at’!


#7

David Livingstone

Works great on my Raspberry Pi - thanks for sharing the script!


#8

Mauricio Taslik (Angacom)

This does not work… ssl certificate error and many other issues


#9

OldandGrumpy -> Mauricio Taslik (Angacom)

Not sure why you are having issues with this. The only thing requiring the cert is the https call, but that’s not something you can control. Lynx has been around for ages and this should work just fine. I did not try the file in the git repo, but the code above worked great. I did start by just testing to make sure the lynx call worked, first. Once I knew the lynx call would work (just type "lynx --dump "), then I knew the rest would work, as it’s just using local tools to process the lynx output.