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.
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?
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.
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
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!
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’!
David Livingstone
Works great on my Raspberry Pi - thanks for sharing the script!
Mauricio Taslik (Angacom)
This does not work… ssl certificate error and many other issues
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.
I found this script a couple years ago and it was exactly what I needed. I had this working using lynx for a couple years on a couple Raspberry Pi’s. As another user mentioned, had this running every night @3am via cron and wrote the output to a text file. I was pulling the sunrise and sunset times from within a project I had running.
However, sometime this past month something happened and now the times aren’t being generated. I get the following:
date: invalid date ‘ AM’
date: invalid date ‘ PM’
Sunrise for location USNJ0452:
Sunset for location USNJ0452:
As you can see, a couple error messages and no time displayed after colons. Has anyone else experienced this? Any idea what has changed to cause this to stop working (and no I didn’t change the script )?
Thanks for any feedback and help.
Same here - the script worked until it didn’t.
I suspect there may have been a change in the weather website itself that broke the scripts functionality
Hi Brossard77 and Lupus,
Welcome to our forums.
Checking the content delivered by lynx seems to verify this, there is no “<time> AM” or “<time> PM” in the html retrieved. I checked the site, and the sunset and sunrise times are still displayed, only in lowercase, but all other am/pm abbreviations are also in lowercase, so awk
will find no match to pattern.
I have only found the containing paragraph’s class that is “unique” at this two elements, so you could do something like the following (if we don’t want to go far from the original logic):
#!/bin/bash
# First obtain a location code from: https://weather.codes/search/
# Insert your location. For example LOXX0001 is a location code for Bratislava, Slovakia
location="LOXX0001"
tmpfile=/tmp/$location.out
# Obtain sunrise and sunset raw data from weather.com
wget -q "https://weather.com/weather/today/l/$location" -O "$tmpfile"
SUNR=$(grep -o '<p class="_-_-components-src-molecule-SunriseSunset-SunriseSunset--dateValue--3H780">.*am</p>' "$tmpfile" | grep -o '>.*</p>' | cut -c 2- | rev | cut -c5- | rev)
SUNS=$(grep -o '<p class="_-_-components-src-molecule-SunriseSunset-SunriseSunset--dateValue--3H780">.*pm</p>' "$tmpfile" | grep -o '>.*</p>' | cut -f10,1 -d'>' | cut -c2- | rev | cut -c4- | rev)
sunrise=$(date --date="$SUNR" +%R)
sunset=$(date --date="$SUNS" +%R)
# Use $sunrise and $sunset variables to fit your needs. Example:
echo "Sunrise for location $location: $sunrise"
echo "Sunset for location $location: $sunset"
But I’m afraid it will only work until the next (maybe style-only) update of the site.
Thanks, sandmann, that’s fixed it for me.
Like you say, it’s reliant on a third party website not updating any time soon, but on balance it’s worth the risk if it means having a pure shell solution and not having to install extra tools.
Thanks, fixed it for me also. Learned from this too so that next time I can fix myself.
All the best
hello-
have been using script successfully since the June change. But something changed as of august 29, script stopped working, all times are returned as 00;00 .
Thanks DAN
As predicted, a style change. The paragraph’s class is changed. You can change line 12 and 13 to:
SUNR=$(grep -o '<p class="_-_-node_modules-@wxu-components-src-molecule-SunriseSunset-SunriseSunset--dateValue--2nwgx">.*am</p>' "$tmpfile" | grep -o '>.*</p>' | cut -c 2- | rev | cut -c5- | rev)
SUNS=$(grep -o '<p class="_-_-node_modules-@wxu-components-src-molecule-SunriseSunset-SunriseSunset--dateValue--2nwgx">.*pm</p>' "$tmpfile" | grep -o '>.*</p>' | cut -f10,1 -d'>' | cut -c2- | rev | cut -c4- | rev)
And the script will work again.
Yes, I too noticed that. I noticed the wording in the wget tmp file was slightly different and that’s probably why the script is now failing. I was playing around with making those changes to now look for the correct text but still no luck.
Instead, I’m experimenting with an older C code (called sunriset.c) from the 1990’s to calculate the sunrise and sunset times, along with other astronomical values. The values I get are off by 20 min or so, but for me that isn’t a big issue. Close is good enough. In my head, being totally self contained might be more problem free.
Claude Quesnelle
Well thanks for the correction! works great now.
DAN
Thanks for the corrections Mr. Moderator. I made them, and it is now working. I was so close with my fix too. Maybe next time I’ll get it right, cause there will be a next time. Still, I’ll experiment with the standalone calculated method.
Stay safe!
Claude