A ReceiveHTTPMemory() Example - Weather App

Share your advanced PureBasic knowledge/code with the community.
User avatar
TI-994A
Addict
Addict
Posts: 2727
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

A ReceiveHTTPMemory() Example - Weather App

Post by TI-994A »

After posting the recent REST API example which consumes my own script, on my own server, through PureBasic's HTTPRequest() functions, I wanted to make another with a publicly accessible API. Having worked with it in the past, I thought that the AccuWeather service would have made a good example, but unpaid access to it is very limited. So, I started on a workaround to capture the data through web access instead. However, their site appears to be fully JavaScript-rendered, making this unfeasible.

Focused on this approach now, I have found a free and open weather service at timeanddate.com, to illustrate this web-access model. The app downloads a file containing a pre-formatted list of cities which is displayed in a ListView(), and the corresponding webpage for a selected city will then be streamed through the ReceiveHTTPMemory() function. The weather information will then be extracted and displayed.

Code: Select all

EnableExplicit

CompilerIf #PB_Compiler_Version < 600
  InitNetwork()
CompilerEndIf

Global Dim cities.s(149)
Define event, appQuit, cityList, cityLabel, forecastLabel, urlLabel

Procedure initData()
  Shared cityList  
  Define i, citiesFile.s
  
  citiesFile = GetTemporaryDirectory() + "cities.txt"  
  If FileSize(citiesFile) < 1  
    ReceiveHTTPFile("syed.sg/tutorials/cities.txt", citiesFile)
  EndIf
  
  If ReadFile(0, citiesFile)
    While Eof(0) = 0       
      cities(i) = Trim(ReadString(0)) 
      i + 1      
    Wend
    CloseFile(0) 
  EndIf
  
  For i = 0 To 149
    AddGadgetItem (cityList, -1, cities(i))
  Next i
  
EndProcedure

Procedure getWeather(city.s)
  Shared cityList, cityLabel, forecastLabel, urlLabel
  Define.s weatherURL, rawResponse, hiLow = "HTTP error!"
  Define *buffer, responseSize.q, hiLowPos
    
  SetGadgetText(cityLabel, city)
  city = ReplaceString(ReplaceString(city, ", ", "/"), " ", "-")
  weatherURL = "https://www.timeanddate.com/weather/" + LCase(city)   
  SetGadgetText(urlLabel, weatherURL)   
  
  *buffer = ReceiveHTTPMemory(weatherURL)
  If *buffer        
    responseSize = MemorySize(*buffer)
    rawResponse = PeekS(*buffer, responseSize, #PB_UTF8 | #PB_ByteLength)
    FreeMemory(*buffer)    
    If Not FindString(rawResponse, "Unknown address", 0)
      hiLowPos = FindString(rawResponse, "High and low forecasted temperature today", 0)
      hiLow = Trim(Mid(rawResponse, hiLowPos + 53, 7), "&") + " °C"    
    EndIf  
  EndIf 
    
  SetGadgetText(forecastLabel, hiLow)  
EndProcedure

OpenWindow(0, 0, 0, 900, 500, "ReceiveHTTPMemory() Example - Weather App", 
           #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
cityList = ListViewGadget(#PB_Any, 10, 10, 350, 420)
cityLabel = TextGadget(#PB_Any, 400, 30, 480, 100, "Select a city...")
forecastLabel = TextGadget(#PB_Any, 450, 135, 430, 300, "High / Low")
urlLabel =  TextGadget(#PB_Any, 10, 460, 880, 30,
                       "https://timeanddate.com/weather", #PB_Text_Center)

If LoadFont(0, "Arial", 30)
  SetGadgetFont(cityLabel, FontID(0))
EndIf

If LoadFont(1, "Arial", 70)
  SetGadgetFont(forecastLabel, FontID(1))
EndIf

initData()

Repeat
  event = WaitWindowEvent()
  Select event
    Case #PB_Event_CloseWindow
      appQuit = #True
    Case #PB_Event_Gadget
      Select EventGadget()
        Case cityList          
          If EventType() = #PB_EventType_LeftClick            
            getWeather(GetGadgetText(cityList))
          EndIf
      EndSelect          
  EndSelect   
Until appQuit
Just a simple demonstration on how to hijack publicly available information from their websites. :lol:
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
User avatar
Sofos
New User
New User
Posts: 6
Joined: Tue Aug 05, 2025 1:28 pm

Re: A ReceiveHTTPMemory() Example - Weather App

Post by Sofos »

Hello! On recommendation, I am trying out PureBasic. I was pleased to obtain a result for AccuWeather because I am currently working on a related project. But it seems that this post does not really work with AccuWeather.

I will still give your code a try. Not sure if it will work on the free demo version, though.

Is your code returning live results or only static data sets?

Thank you.

Sofia
infratec
Always Here
Always Here
Posts: 7598
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: A ReceiveHTTPMemory() Example - Weather App

Post by infratec »

No one has said or written, that this code example works with any available weather forecast site on the world.
It is just an example using www.timeanddate.com/weather/

You have to know how to get the data from AccuWeather, than you can write a code for it.
User avatar
JHPJHP
Addict
Addict
Posts: 2256
Joined: Sat Oct 09, 2010 3:47 am

Re: A ReceiveHTTPMemory() Example - Weather App

Post by JHPJHP »

Hi Sofos,

First off, welcome to PureBasic :!:

So as not to hijack this thread, I sent you a PM (private message) with some code to get you started.

If you're not investing in yourself, you're falling behind.

My PureBasic StuffFREE STUFF, Scripts & Programs.
My PureBasic Forum ➤ Questions, Requests & Comments.
User avatar
Sofos
New User
New User
Posts: 6
Joined: Tue Aug 05, 2025 1:28 pm

Re: A ReceiveHTTPMemory() Example - Weather App

Post by Sofos »

infratec wrote: Tue Aug 05, 2025 8:42 pm No one has said or written, that this code example works with any available weather forecast site on the world.
It is just an example using www.timeanddate.com/weather/

You have to know how to get the data from AccuWeather, than you can write a code for it.
A scolding welcome. Nice.
JHPJHP wrote: Tue Aug 05, 2025 10:47 pm Hi Sofos,

First off, welcome to PureBasic :!:

So as not to hijack this thread, I sent you a PM (private message) with some code to get you started.
Thank you, JHPJHP. I've downloaded your code and I'll study it. Seems complex.
infratec
Always Here
Always Here
Posts: 7598
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: A ReceiveHTTPMemory() Example - Weather App

Post by infratec »

This is not a welcome section, it is a coding section.
And you started not with Hi I'm ... and I ... I willl ask soon some questions.
You told us nothing about your knowledge and coding level.

You pointed out:
But it seems that this post does not really work with AccuWeather.
And I told you why it does not work. And how you can proceed.

Better: go to coding questions and ask if someone alreday used AccuWeather and how to do it.

Tip:
Go to https://developer.accuweather.com/ and get an API token, then you can use the API of AccuWeather for 50 requests per day.
User avatar
Sofos
New User
New User
Posts: 6
Joined: Tue Aug 05, 2025 1:28 pm

Re: A ReceiveHTTPMemory() Example - Weather App

Post by Sofos »

@JHPJHP, your code is elegantly structured and easy to follow. Thank you!

I'm assuming that the App ID is yours?

But since my needs are still on AccuWeather, I've read up on their APIs, but it seems that there is no clear cut way to get the current weather just by providing the city and country names, as you have done with OpenWeather.

The AccuWeather documentation indicates that some other API has to be called first, with either lat/lng values and regional codes, in order to obtain some location code to that city/state/country before calling the current weather API. Seems very troublesome.

Could anyone confirm if this is indeed the correct approach for AccuWeather or is there a simpler way like the ones used by OpenWeather and timeanddate.com?

Thanks again.

Sofia
infratec
Always Here
Always Here
Posts: 7598
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: A ReceiveHTTPMemory() Example - Weather App

Post by infratec »

You should open a new thread in the coding section, because you hijack this 'timeanddate' post.

If you can read here:
https://developer.accuweather.com/apis

You first have to call something out of the Location API, then you can call
the Forecast API or
the Current Conditions API

depending on your intention.
If your loaction is restricted to one location, you can call it once and store the result in an ini file.
User avatar
TI-994A
Addict
Addict
Posts: 2727
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: A ReceiveHTTPMemory() Example - Weather App

Post by TI-994A »

Sofos wrote: Tue Aug 05, 2025 2:03 pm Hello! On recommendation, I am trying out PureBasic. I was pleased to obtain a result for AccuWeather because I am currently working on a related project. But it seems that this post does not really work with AccuWeather.
Hi Sofia, and welcome to PureBasic!

The example in this thread does not employ the REST interface, but instead, it simply scans and parses the results returned to the timeanddate.com website for specific city/state queries. The city/state values are a list of static, preconfigured strings from a text file downloaded from my server. Very simple. :wink:

I originally wanted to do this through the AccuWeather website, but their results were too obfuscated to be parsed. Even their API services are a little convoluted, as they require proprietary location keys in order to return forecasts, instead of simple text-based city/state values. Moreover, although they offer a free-tier for these API services, only registered users with valid API keys would be able to access them.

Nevertheless, I have launched another thread with a working example to demonstrate REST API calls to AccuWeather services. It currently uses an API key from a demo account which I had created, but it is limited to only 50 API calls daily. However, I would suggest registering a personal demo account for a better testing experience.

This is the new thread:

> PureBasic and AccuWeather APIs - A LIVE Working Example

I hope it addresses your needs. :D
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
threedslider
Enthusiast
Enthusiast
Posts: 393
Joined: Sat Feb 12, 2022 7:15 pm

Re: A ReceiveHTTPMemory() Example - Weather App

Post by threedslider »

Hi and welcome @Sofos :D

If you want more call from weather api here is the link to try it : https://openweathermap.org/api

Hope you can find here useful !

Good luck.
User avatar
Sofos
New User
New User
Posts: 6
Joined: Tue Aug 05, 2025 1:28 pm

Re: A ReceiveHTTPMemory() Example - Weather App

Post by Sofos »

TI-994A wrote: Mon Aug 11, 2025 9:24 am
Sofos wrote: Tue Aug 05, 2025 2:03 pm Hello! On recommendation, I am trying out PureBasic. I was pleased to obtain a result for AccuWeather because I am currently working on a related project. But it seems that this post does not really work with AccuWeather.
Hi Sofia, and welcome to PureBasic!

The example in this thread does not employ the REST interface, but instead, it simply scans and parses the results returned to the timeanddate.com website for specific city/state queries. The city/state values are a list of static, preconfigured strings from a text file downloaded from my server. Very simple. :wink:

I originally wanted to do this through the AccuWeather website, but their results were too obfuscated to be parsed. Even their API services are a little convoluted, as they require proprietary location keys in order to return forecasts, instead of simple text-based city/state values. Moreover, although they offer a free-tier for these API services, only registered users with valid API keys would be able to access them.

Nevertheless, I have launched another thread with a working example to demonstrate REST API calls to AccuWeather services. It currently uses an API key from a demo account which I had created, but it is limited to only 50 API calls daily. However, I would suggest registering a personal demo account for a better testing experience.

This is the new thread:

> PureBasic and AccuWeather APIs - A LIVE Working Example

I hope it addresses your needs. :D
Hi Syed! Yes, I visited your web page. Interesting.

Thank you for your great example. I was looking for some pointers, but I hardly expected a complete working example.

It does exactly what I need, although I am trying to understand the workings of the HTTP and JSON functions. I may have questions.

Just wow! Thanks again, Syed.


Sofia
User avatar
TI-994A
Addict
Addict
Posts: 2727
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: A ReceiveHTTPMemory() Example - Weather App

Post by TI-994A »

Sofos wrote: Tue Aug 12, 2025 7:41 amHi Syed! Yes, I visited your web page. Interesting.

Thank you for your great example. I was looking for some pointers, but I hardly expected a complete working example.

It does exactly what I need, although I am trying to understand the workings of the HTTP and JSON functions. I may have questions.
Thank you for saying so, Sofia. I'm glad to hear that it meets your needs. We're here to answer any relevant questions that you might have.

And I hope you enjoyed my web page as well. :lol:
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
Post Reply