Announcement

Collapse
No announcement yet.

CURL n00b pollen.com results

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • CURL n00b pollen.com results

    I am pretty n00b to curl. I managed to install it and get it running.

    Now my question is how can I pass the results I see in Command Prompt when I execute this curl command:

    Code:
    curl -H "Referer: https://www.pollen.com/api/forecast/current/pollen/10001" -H "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36" https://www.pollen.com/api/forecast/current/pollen/10001
    Valid JSON result:
    Code:
    {"Type":"pollen","ForecastDate":"2019-04-02T00:00:00-04:00","Location":{"ZIP":"10001","City":"NEW YORK","State":"NY","periods":[{"Triggers":[{"LGID":4,"Name":"Maple","Genus":"Acer","PlantType":"Tree"},{"LGID":272,"Name":"Juniper","Genus":"Juniperus","PlantType":"Tree"}],"Period":"0001-01-01T00:00:00","Type":"Yesterday","Index":11.8},{"Triggers":[{"LGID":4,"Name":"Maple","Genus":"Acer","PlantType":"Tree"},{"LGID":272,"Name":"Juniper","Genus":"Juniperus","PlantType":"Tree"}],"Period":"0001-01-01T00:00:00","Type":"Today","Index":10.2},{"Triggers":[{"LGID":4,"Name":"Maple","Genus":"Acer","PlantType":"Tree"},{"LGID":272,"Name":"Juniper","Genus":"Juniperus","PlantType":"Tree"}],"Period":"0001-01-01T00:00:00","Type":"Tomorrow","Index":9.9}],"DisplayLocation":"New York, NY"}}
    I code in VB.net (beginner), I can parse JSON data from the web and put the results into HS3 devices, but curl is new to me, just need some guidance.

    Examples would be greatly appreciated.

    Thank you in advance.

  • #2
    You could use a vb.net script, rather than CURL to retrieve the web results and then populate HS devices. There is the hs.getURL command and other vb.net functions to retrieve the web page: https://help.homeseer.com/help/HS3/static/#.geturl and the following to put the values into HS devices: https://help.homeseer.com/help/HS3/s...or_last_change.
    HS 3.0.0.548: 1976 Devices 1156 Events
    Z-Wave 3.0.1.262: 123 Nodes on one Z-Net

    Comment


    • #3
      I can't help you with your question, but I was wondering where you found the documentation for pollen.com... I've looked at that site a few times thinking it would be nice to retrieve their data in homeseer, but didn't find an API.

      Comment


      • #4
        Originally posted by prsmith777 View Post
        I can't help you with your question, but I was wondering where you found the documentation for pollen.com... I've looked at that site a few times thinking it would be nice to retrieve their data in homeseer, but didn't find an API.
        It's not public.

        Comment


        • #5
          Originally posted by HS14 View Post

          It's not public.
          looks like it is now...

          Comment


          • #6
            Originally posted by prsmith777 View Post

            looks like it is now...
            Lol true. Now just have to figure out URLAction function.

            Comment


            • #7
              Originally posted by sparkman View Post
              You could use a vb.net script, rather than CURL to retrieve the web results and then populate HS devices. There is the hs.getURL command and other vb.net functions to retrieve the web page: https://help.homeseer.com/help/HS3/static/#.geturl and the following to put the values into HS devices: https://help.homeseer.com/help/HS3/s...or_last_change.
              Thank you for those links.

              How can I post multiple headers in a request? ( I am guessing I would have to pass the "referer" and "user-agent" through the "header" variable)

              Comment


              • #8
                You could try Big5 plugin if this approach doesn't work out.

                Comment


                • #9
                  Originally posted by HS14 View Post

                  Thank you for those links.

                  How can I post multiple headers in a request? ( I am guessing I would have to pass the "referer" and "user-agent" through the "header" variable)
                  A simple solution will be to use tthe Big5 PI

                  ---
                  John

                  Comment


                  • #10
                    Originally posted by HS14 View Post

                    Thank you for those links.

                    How can I post multiple headers in a request? ( I am guessing I would have to pass the "referer" and "user-agent" through the "header" variable)
                    Take a look at the vb.net HttpWebRequest commands. Here's a snippet from one of my scripts:

                    Code:
                        Dim strURL As String = "http://" & player & "/cgi-bin/toServerValue.cgi"
                        Dim myWebReq As HttpWebRequest
                        Dim myWebResp As HttpWebResponse
                        Dim encoding As New System.Text.UTF8Encoding
                        Dim sr As StreamReader
                    
                        Try
                            Dim data As Byte() = encoding.GetBytes(json)
                            myWebReq = DirectCast(WebRequest.Create(strURL), HttpWebRequest)
                            myWebReq.ContentType = "application/json; charset=utf-8"
                            myWebReq.ContentLength = data.Length
                            myWebReq.Method = "POST"
                            Dim myStream As Stream = myWebReq.GetRequestStream()
                    
                            If data.Length > 0 Then
                                myStream.Write(data, 0, data.Length)
                                myStream.Close()
                            End If
                    
                            myWebResp = DirectCast(myWebReq.GetResponse(), HttpWebResponse)
                            sr = New StreamReader(myWebResp.GetResponseStream())
                            Dim responseText As String = sr.ReadToEnd()
                    I stole this from something I found on the internet. Google is your friend when it comes to finding examples like this. Lots out there. There are commands to specify different kinds of headers: https://docs.microsoft.com/en-us/dot...ramework-4.7.2
                    HS 3.0.0.548: 1976 Devices 1156 Events
                    Z-Wave 3.0.1.262: 123 Nodes on one Z-Net

                    Comment


                    • #11
                      Thank you sparkman for pointing me in the right direction <3

                      Below is a working example of today's pollen index along with saving a copy of the JSON file into the HTML folder for anyone else looking to learn:

                      Code:
                      Imports System.Web.Script.Serialization
                      Sub Main(ByVal Parms As Object)
                          ' Create a WebRequest to the remote site, specifying Referer and UserAgent
                          Dim getRequest As System.Net.HttpWebRequest = System.Net.HttpWebRequest.Create("https://www.pollen.com/api/forecast/current/pollen/10001")
                          getRequest.Referer="https://www.pollen.com/api/forecast/current/pollen/10001"
                          getRequest.UserAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36"
                          Dim getResponse As System.Net.HttpWebResponse = getRequest.GetResponse()
                      
                          If getResponse.StatusCode = System.Net.HttpStatusCode.OK Then
                            ' Parse the contents from the response to a stream object
                            Dim stream As System.IO.Stream = getResponse.GetResponseStream()
                      
                            ' Create a reader for the stream object
                            Dim reader As New System.IO.StreamReader(stream)
                      
                            ' Read from the stream object using the reader, put the contents in a string
                            Dim contents As String = reader.ReadToEnd()
                      
                            ' Save JSON string into a JSON file
                            My.Computer.FileSystem.WriteAllText(hs.GetAppPath & "\html\json\Pollen.json", contents, False)
                      
                            ' Convert JSON string to object
                            Dim jsonData As Object = New JavaScriptSerializer().Deserialize(Of Object)(contents)
                      
                            ' Get Pollen Index value
                            Dim pollenIndex As Decimal = CDec(jsonData("Location")("periods")("1")("Index"))
                      
                            Dim pollenRating As String
                            If pollenIndex <= 2.4 Then
                              pollenRating = "very low"
                            Else If pollenIndex <= 4.8 Then
                              pollenRating = "low"
                            Else If pollenIndex <= 7.2 Then
                              pollenRating = "moderate"
                            Else If pollenIndex <= 9.6 Then
                              pollenRating = "high"
                            Else If pollenIndex <= 12 Then
                              pollenRating = "very high"
                            End If
                      
                            ' Log
                            hs.WriteLogEx("Pollen Script", "Today's pollen index is " & pollenRating & " at " & pollenIndex & " out of 12 value", "#FF0000")
                          End If
                      End Sub

                      Comment


                      • #12
                        Originally posted by HS14 View Post
                        Thank you sparkman for pointing me in the right direction <3
                        Glad to help!
                        HS 3.0.0.548: 1976 Devices 1156 Events
                        Z-Wave 3.0.1.262: 123 Nodes on one Z-Net

                        Comment


                        • #13
                          Originally posted by HS14 View Post

                          Below is a working example of today's pollen index along with saving a copy of the JSON file into the HTML folder for anyone else looking to learn:
                          Thanks. Will put good use to this .

                          Comment


                          • #14
                            Script works great. Thanks!

                            Had to include System.Web.Extensions;System.Web.Extensions.dll in settings.ini file for it to work in case anyone tries the script.

                            Comment


                            • #15
                              I'm interested in pulling the allergens from the JSON data. It seems sometimes there are three allergens and sometimes two or even one allergen.

                              I'm a complete novice when it comes to JSON. I managed to get these lines of code to pull the data when there are three allergens, but when there are less I get an error.

                              Code:
                                       Dim pollenTrigger0 as String = jsonData("Location")("periods")("1")("Triggers")("0")("Name")
                                       Dim pollenTrigger1 as String = jsonData("Location")("periods")("1")("Triggers")("1")("Name")
                                       Dim pollenTrigger2 as String = jsonData("Location")("periods")("1")("Triggers")("2")("Name")
                              I tried to use Try Catch logic but it gave an error.

                              Any help would be appreciated.

                              Comment

                              Working...
                              X