weather.c
/* poll openweathermap.org for current weather */
poll openweathermap.org for current weather
poll openweathermap . org for current weather
void weather_initialize(Weather *w)
{
	char key[33] = {'\0'};
	get_api_key(key);
	char digits[6] = {'\0'};
	get_zip(digits);
	char *url;
	if (asprintf(&url,
				 "https://api.openweathermap.org/data/2.5/weather?zip=%s,us&APPID=%s",
				 digits,
				 key) < 0)
		fputs("Url string not formatted", stderr);
	CURL *curl;
	CURLcode res;
	curl = curl_easy_init();
	get_request req = {.buffer = NULL, .len = 0, .buflen = 0};
	if (curl)
	{
		curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "GET");
		curl_easy_setopt(curl, CURLOPT_URL, url);
		curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
		curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https");
		struct curl_slist *headers = NULL;
		headers = curl_slist_append(headers, "length: 20");
		headers = curl_slist_append(headers, "numbers: true");
		curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
		req.buffer = malloc(CHUNK_SIZE);
		req.buflen = CHUNK_SIZE;
		curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_callback);
		curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&req);
		res = curl_easy_perform(curl);
		char *t_target = "feels_like";
		char *h_target = "humidity";
		char *t_index = strstr(req.buffer, t_target) + 12;
		char temp[4] = {'\0'};
		for (int i = 0; i < 3; i++)
			temp[i] = *t_index++;
		char *h_index = strstr(req.buffer, h_target) + 10;
		char humi[3] = {'\0'};
		for (int i = 0; i < 2; i++)
			humi[i] = *h_index++;
		w->temperature = (int)((atoi(temp) - 273.15) * 1.8000 + 32.00);
		w->humidity = atoi(humi);
		free(req.buffer);
	}
	curl_easy_cleanup(curl);
	free(url);
}
void weather_initialize ( Weather * w ) { char key [ 33 ] = { ' \0 ' } ; get_api_key ( key ) ; char digits [ 6 ] = { ' \0 ' } ; get_zip ( digits ) ; char * url ; if ( asprintf ( & url , " " , digits , key ) < 0 ) fputs ( " " , stderr ) ; CURL * curl ; CURLcode res ; curl = curl_easy_init ( ) ; get_request req = { . buffer = NULL , . len = 0 , . buflen = 0 } ; if ( curl ) { curl_easy_setopt ( curl , CURLOPT_CUSTOMREQUEST , " " ) ; curl_easy_setopt ( curl , CURLOPT_URL , url ) ; curl_easy_setopt ( curl , CURLOPT_FOLLOWLOCATION , 1L ) ; curl_easy_setopt ( curl , CURLOPT_DEFAULT_PROTOCOL , " " ) ; struct curl_slist * headers = NULL ; headers = curl_slist_append ( headers , " " ) ; headers = curl_slist_append ( headers , " " ) ; curl_easy_setopt ( curl , CURLOPT_HTTPHEADER , headers ) ; req . buffer = malloc ( CHUNK_SIZE ) ; req . buflen = CHUNK_SIZE ; curl_easy_setopt ( curl , CURLOPT_WRITEFUNCTION , curl_callback ) ; curl_easy_setopt ( curl , CURLOPT_WRITEDATA , ( void * ) & req ) ; res = curl_easy_perform ( curl ) ; char * t_target = " " ; char * h_target = " " ; char * t_index = strstr ( req . buffer , t_target ) + 12 ; char temp [ 4 ] = { ' \0 ' } ; for ( int i = 0 ; i < 3 ; i ++ ) temp [ i ] = * t_index ++ ; char * h_index = strstr ( req . buffer , h_target ) + 10 ; char humi [ 3 ] = { ' \0 ' } ; for ( int i = 0 ; i < 2 ; i ++ ) humi [ i ] = * h_index ++ ; w -> temperature = ( int ) ( ( atoi ( temp ) - 273.15 ) * 1.8000 + 32.00 ) ; w -> humidity = atoi ( humi ) ; free ( req . buffer ) ; } curl_easy_cleanup ( curl ) ; free ( url ) ; }
poll openweathermap.org for current weather
poll openweathermap . org for current weather
// printf("%s\n", url); /*printf("Result = %u\n", res);
		printf("Total received bytes: %zu\n", req.len);
		printf("Received data:/n%s\n", req.buffer);*/ // printf("%s %s\n", temp, humi);