Weather Fetching Service with C Programmming / CURL / API

Objectives


The goal for this project was to integrate API (Application Programming Interface) with C programming language and come up with a simple functional Text-based User Interface application.

Programming


Unlike other programming languages, especially Python, C was quite tricky to get things work with API. Through extensive googling, I found out that libcurl is the most efficient and known way for integrating API to the program written in C.

Code Sample

                            ...


                              output = output_zip(zipcode); //store website text
      p_pos = strstr(output, "\"Type\":\"PostalCode\"");
      p_pos = CopyTextFromWebData(location_name, p_pos, "\"LocalizedName\":\"", "\",\"EnglishName\""); //store location name on location_name by using website text
      p_pos = CopyTextFromWebData(location_code, p_pos, "\"ParentCity\":{\"Key\":\"", "\",\"LocalizedName\""); //store location code on location_code by using website text
      
      weather_output = current_weather_location_code(location_code); //stores website text
      p_pos2 = strstr(weather_output, "\"LocalObservationDateTime\"");
      p_pos2 = CopyTextFromWebData(current_weather->weather_summary, p_pos2, "\"WeatherText\":\"", "\",\"WeatherIcon\""); //store weather summary by using website text
      p_pos2 = CopyTextFromWebData(current_weather->hasPrecipitation, p_pos2, "\"HasPrecipitation\":", ",\"PrecipitationType\""); //store hasPrecipitation by using website text
      p_pos2 = CopyTextFromWebData(current_weather->temp, p_pos2, "\"Imperial\":{\"Value\":", ",\"Unit\":\"F\""); //store temp by using website text
      p_pos2 = CopyTextFromWebData(current_weather->windchill, p_pos2, ":17},\"Imperial\":{\"Value\":", ",\"Unit\":\"F\""); //store windchill by using website text
      p_pos2 = CopyTextFromWebData(current_weather->humidity, p_pos2, "\"RelativeHumidity\":", ",\"IndoorRelativeHumidity\""); //store humidity by using website text 
      p_pos2 = CopyTextFromWebData(current_weather->windspeed, p_pos2, "\"Speed\":{\"Metric\":{\"Value\":", ",\"Unit\":\"km/h\""); //store windspeed by using website text
      
      //print weather information
      printf("\n\n\n\n* * * * * * * * * * * * * * * * * * * * * * * *\n");
      printf("%s Weather Information\n", location_name);
      printf("* * * * * * * * * * * * * * * * * * * * * * * *\n\n");
      printf("Weather Summary = %s\n", current_weather->weather_summary);
      printf("Current Temperature = %s F\n", current_weather->temp);
      printf("Current WindChill Temperature = %s F\n", current_weather->windchill);
      printf("Current Humidity = %s percent \n", current_weather->humidity);
      printf("Current Wind Speed = %s km/s\n", current_weather->windspeed);
      printf("Raining? = %s\n", current_weather->hasPrecipitation);
      printf("\n* * * * * * * * * * * * * * * * * * * * * * * *\n\n");



      ...
 
                          


As you can see above, the program makes the API call using curl library, fetches and stores raw HTML data, and the data is substringed so that necessary information can be printed in the console.

Full source code can be found here.

Demonstration