Sunrise and Sunset Times

  • 1
  • Question
  • Updated 5 years ago
I'm having some issues with Sunrise and Sunset. Other data, I'm able to grep what I want ie. temp_f, then awk or cut the returned line to fetch the numbers. The Sunrise being reported separately as hour then minutes is proving difficult for me.

Any clever hints would be appreciated.

Dave
Photo of roc4fun

roc4fun

  • 2 Posts
  • 0 Reply Likes

Posted 5 years ago

  • 1
Photo of afelicioni

afelicioni

  • 227 Posts
  • 43 Reply Likes
Shall try to help, if some more information could be revealed, like the following

Can you edit somehow the code of your application?

Does your software relies over particular language or library, with JSON or XML response?

Your project will going to parse the whole response as a buffered string, or it will load in memory and fed to a function for programmatic parsing? Cutting, awk'ing, reguar expression matching: how you plan to deal with them?
Photo of roc4fun

roc4fun

  • 2 Posts
  • 0 Reply Likes
Thanks for the help. I'm not much of a coder, but I'll try and reveal as much as I can...

I'm using wget to put the API into a file. I'm using JSON, but I don't think it would matter.

Then I use grep to search for expressions and extract the line containing them. Then I awk and cut the line until it only has the data I want. I then write just the data to another file. Usually, I can pipe the grep, cut, awk, etc in a single statement.

Here is an example...

"wget -O ~/accuweather_conky_USA/wun http://api.wunderground.com/api/mykey..."

"grep -i 'temp_f' ~/accuweather_conky_USA/wun | awk -F: '{print $NF}' |cut -c1-4 > ~/accuweather_conky_USA/currenttemp"

The various file contents are then used by Conky to display on my desktop.

Here is an older example...



I can do this with the Sunrise/Sunset data, but having the separate hour and minutes would call for a lot more futzing. Also, when I call the API, the sunset data is on multiple lines... once again, requiring a lot on clumsy manipulation.
Photo of afelicioni

afelicioni

  • 227 Posts
  • 43 Reply Likes
Ok, following your broad explanation I think it's clear your target: provide datas to Conky

Like you underlined, the provided data tree will bring you parsing code headaches... so I'm afraid I can just suggest you should pick a bash JSON parser and to setup it in the host machine. I know adding extra software can be painful, but right now I can't provide a different option.

Since I previously used for projects the jq parser (find it at http://stedolan.github.io/jq/ ), I ran a pattern that may give some useful ideas; basically instead the line writing the temperature file, a code like this can provice sunset time (as single line command, file paths may need to be fixed)


<~/accuweather_conky_USA/wun jq -r '([.sun_phase|.sunset|.hour] + [":"] + [.sun_phase|.sunset|.minute])|add' >~/accuweather_conky_USA/sunset

So the writte command output is something like
20:12


Note that jq input can also come from piped command, sources are MIT-style licensed and portable too.

Not checked if any Conky user has already written a plugin for this, so similar code may be available from software community as a plug-and-play solution.