Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.60 KB | None | 0 0
  1. #include <mysql/mysql.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <sys/wait.h>
  6. #include <sys/types.h>
  7. #include <string.h>
  8.  
  9. typedef struct{ //Structure pour la météo
  10.  
  11. int temp;
  12. float pressure;
  13. int humidity;
  14. char * main_description;
  15. char * description;
  16. char * icon;
  17. float wind_speed;
  18. float wind_deg;
  19. char * date;
  20. char * moment;
  21. int cloud;
  22. float rain;
  23. float snow;
  24.  
  25. }METEO;
  26.  
  27. int get_json();
  28.  
  29. char * cut_file(char * source, char * string_search);
  30. char * check_brackets(char * string);
  31. char * take_value(char * source, char * word_title);
  32. char * clean_string(char * string);
  33. void get_config(char * address,char * user,char * password,char * bddname,char * filename);
  34. char ** get_keyword();
  35. int test_json(char * buffer);
  36.  
  37. int main(int argc, char ** argv)
  38. {
  39.  
  40. METEO * meteo = malloc(sizeof(METEO));
  41. meteo->main_description = malloc(50 * sizeof(char));
  42. meteo->description = malloc(50 * sizeof(char));
  43. meteo->date = malloc(50 * sizeof(char));
  44. meteo->date = malloc(50 * sizeof(char));
  45. meteo->icon = malloc(50 * sizeof(char));//Charge le contenu du fichier dans un buffer(char));
  46. FILE * file;
  47. int size;
  48. char * buffer;
  49. char * buf2;
  50. char * buf3 = malloc(200 * sizeof(char));
  51. char * buf4;
  52. char * tab = malloc(200 * sizeof(char));
  53. char tab2[20];
  54. char requete[300] = "";
  55. int ok;
  56. //Tableau qui contient tous les mots clés
  57. char ** word_list = NULL;
  58.  
  59.  
  60. int i = 0;
  61. char * buf5 = malloc(100 * sizeof(char));
  62. char * string_clean = malloc(100 * sizeof(char));
  63. int value;
  64. int j = 20;
  65. int k = 0;
  66.  
  67. char * address = malloc(50 * sizeof(char));
  68. char * user = malloc(50 * sizeof(char));
  69. char * password = malloc(50 * sizeof(char));
  70. char * bddname = malloc(50 * sizeof(char));
  71. char * filename = malloc(50 * sizeof(char));
  72.  
  73.  
  74. MYSQL mysql;
  75. mysql_init(&mysql);
  76. mysql_options(&mysql,MYSQL_READ_DEFAULT_GROUP,"option");
  77.  
  78. /*
  79. if(get_json() == 1) //Appel de la fonction pour récuperer le JSON
  80. {
  81. printf("erreur\n");
  82. return 0;
  83. }
  84. */
  85. get_config(address,user,password,bddname,filename);
  86.  
  87. file = fopen(filename,"r"); //On ouvre le fichier
  88.  
  89. //Récuperation de la taille du fichier
  90. fseek(file,0,SEEK_END);
  91. size = ftell(file);
  92. fseek(file,0,SEEK_SET);
  93.  
  94. buffer = malloc((size + 1) * sizeof(char)); //Allocation d'un buffer avec la taille récuperer juste avant
  95.  
  96. fread(buffer,size,1,file); //Charge le contenu du fichier dans un buffer
  97. buffer[size] = '\0'; //Fin de chaine
  98.  
  99. ok = test_json(buffer);
  100. if( !ok )
  101. return 0;
  102.  
  103.  
  104. if(mysql_real_connect(&mysql,address,user,password,bddname,0,NULL,0) != NULL)
  105. {
  106. printf("/////////////////// CONNECT BDD OK \\\\\\\\\\\\\\\\\\\\\n");
  107. //mysql_close(&mysql);
  108. }
  109. else
  110. {
  111. printf("Une erreur s'est produite lors de la connexion à la BDD!");
  112. }
  113. word_list = get_keyword();
  114.  
  115. int count = 0;
  116. while((buffer = strstr(buffer,"\"dt\"")) != NULL) //"dt" est le délimiteur de chaque infos différentes
  117. {
  118.  
  119.  
  120. buffer += 4;
  121. buf2 = cut_file(buffer,"\"dt\""); //Appel de la fonction cut_file qui permet de découper le JSON
  122. if(buf2 == NULL) //S'il n'y a plus de "dt" on s'arrête
  123. break;
  124.  
  125. if(count > 0){
  126.  
  127.  
  128. for(i = 0 ; i<12; i++) //Taille du tableau de mots clés
  129. {
  130. if(i >= 10)
  131. {
  132. //buf3 = strstr(buf2,word_list[i]);
  133. if((tab = take_value(buf2,word_list[i])) != NULL )
  134. {
  135. if((buf3 = check_brackets(tab)) != NULL)
  136. {
  137. strcat(tab,",");
  138.  
  139. buf4 = take_value(tab,"3h");
  140. if(buf4 != NULL)
  141. {
  142. string_clean = clean_string(buf4);
  143. }else{
  144. string_clean = clean_string("0.00");
  145. }
  146.  
  147. string_clean = clean_string(buf4);
  148.  
  149.  
  150. }else{
  151. string_clean = clean_string("0.00");
  152. }
  153. }else{
  154. string_clean = clean_string("0.00");
  155. }
  156. }else{
  157. if(i == 9)
  158. buf2 = strstr(buf2, "weather");
  159.  
  160.  
  161. buf4 = take_value(buf2, word_list[i]); //On récupère la chaine de caractère avec le mot clé et sa valeur
  162.  
  163. string_clean = clean_string(buf4); //Appel de la fontion qui permet de supprimer tous les caractères en trop
  164. }
  165.  
  166. switch(i)
  167. {
  168. case 0: //Temperature
  169. meteo->temp = atof(string_clean); //Transforme la valeur en float
  170. meteo->temp -= 273.15; //Calcul pour passer du Kelvin en °C
  171. break;
  172. case 1: //Pression
  173. meteo->pressure = atof(string_clean);
  174. break;
  175. case 2: //Humidite
  176. meteo->humidity = atoi(string_clean); //Transforme la valeur en int
  177. break;
  178. case 3: //Description
  179. meteo->description = string_clean;
  180. break;
  181. case 4: //Vitesse du vent
  182. meteo->wind_speed = atof(string_clean);
  183. break;
  184. case 5: //Orientation du vent
  185. meteo->wind_deg = atof(string_clean);
  186. break;
  187. case 6: //Date et heure de l'info
  188. while(string_clean[k] != ' ')
  189. {
  190. meteo->date[k] = string_clean[k];
  191. k++;
  192. }
  193. meteo->date[k] = '\0';
  194. k=0;
  195.  
  196. meteo->moment = strchr(string_clean,' ');
  197.  
  198. break;
  199. case 7: //id de l'icone
  200. meteo->icon = string_clean;
  201. strcat(meteo->icon,".png");
  202. break;
  203. case 8: //Nuage
  204. meteo->cloud = atoi(string_clean);
  205. break;
  206. case 9: //Information principale
  207. //printf("BUF 2 : %s",buf2);
  208. meteo->main_description = string_clean;
  209. break;
  210. case 10: //Pluie
  211. meteo->rain = atof(string_clean);
  212. break;
  213. case 11: //Neige
  214. meteo->snow = atof(string_clean);
  215. break;
  216. default:
  217. break;
  218.  
  219. }
  220.  
  221. }
  222. sprintf(requete,"INSERT INTO weather (DateJ,Moment,City,Temperature,Trend,Description,Icon,Humidity,Wind_Speed,Wind_Direction,Cloud,Pressure,Rain,Snow) VALUES ('%s','%s','Evreux',%d,'%s','%s','%s',%d,%.2f,%.2f,%d,%.2f,%.2f,%.2f)",meteo->date,meteo->moment,meteo->temp,meteo->main_description,meteo->description,meteo->icon,meteo->humidity,meteo->wind_speed,meteo->wind_deg,meteo->cloud,meteo->pressure,meteo->rain,meteo->snow);
  223. //printf("%s\n",requete);
  224.  
  225. mysql_query(&mysql,requete);
  226.  
  227. printf("Temperature : %d degres\n",meteo->temp);
  228. printf("Pression : %.2f\n",meteo->pressure);
  229. printf("Humidite : %d\n",meteo->humidity);
  230. printf("Principale : %s\n",meteo->main_description);
  231. printf("Description : %s\n",meteo->description);
  232. printf("Vitesse vent : %.2f m/s\n",meteo->wind_speed);
  233. printf("Degre vent : %.2f\n",meteo->wind_deg);
  234. printf("Date : %s\n",meteo->date);
  235. printf("Moment : %s\n",meteo->moment);
  236. printf("Icon : %s\n",meteo->icon);
  237. printf("Nuage : %d\n",meteo->cloud);
  238. printf("Pluie : %.2f\n",meteo->rain);
  239. printf("Neige : %.2f\n\n",meteo->snow);
  240. }
  241. count++;
  242. }
  243. //key_word = get_keyword();
  244. //free(buffer);
  245. //free(buf2);
  246. free(buf3);
  247. //free(buf4);
  248. free(buf5);
  249.  
  250. if(string_clean != NULL)
  251. free(string_clean);
  252. fclose(file);
  253.  
  254. mysql_close(&mysql);
  255. return 0;
  256. }
  257.  
  258.  
  259. int get_json()
  260. {
  261. system("curl -o meteo.txt 'http://api.openweathermap.org/data/2.5/forecast/city?id=3019265&APPID=10f30537f293b707454512bebd2a54c8&lang=fr'");
  262.  
  263. return 0;
  264. }
  265.  
  266. char * cut_file(char * source, char * string_search)
  267. {
  268. char * buf = malloc((strlen(source) + 1) * sizeof(char));
  269. //printf( "TAILLE SOURCE : %d \n", strlen(source));
  270. char * buf2;
  271. int size_info;
  272.  
  273. buf = strstr(source,string_search);
  274. if(buf == NULL)
  275. return NULL;
  276. size_info = strlen(source) - strlen(buf);
  277.  
  278.  
  279. buf2= malloc((size_info + 1) * sizeof(char));
  280.  
  281. strncpy(buf2,source,size_info);
  282. buf2[size_info] = '\0';
  283.  
  284. return buf2;
  285. }
  286.  
  287. char * check_brackets(char * string)
  288. {
  289. char * buf = malloc(400 * sizeof(char));
  290. char * buf2;
  291.  
  292. int size;
  293. if((buf = strchr(string,'{') ) == NULL)
  294. return NULL;
  295.  
  296. buf2 = strchr(string,'}');
  297. size = strlen(buf) - strlen(buf2);
  298.  
  299. if(size <= 2)
  300. return NULL;
  301.  
  302. buf2 = malloc((size+1) * sizeof(char));
  303.  
  304. strncpy(buf2,buf,size);
  305. buf2[size] = '\0';
  306.  
  307. //free(buf);
  308. return buf2;
  309. }
  310.  
  311. char * take_value(char * source, char * word_title)
  312. {
  313. char * buf = malloc(400 * sizeof(char));
  314. char * buf2 = malloc(100 * sizeof(char));
  315. char * buf3 = malloc(100 * sizeof(char));
  316. int i = 0;
  317. int size = 0 ;
  318.  
  319. buf = strstr(source,word_title);
  320. if(buf == NULL)
  321. return NULL;
  322. /*
  323. buf2 = strchr(buf,',');
  324. size = strlen(buf) - strlen(buf2);
  325. strncpy(buf2,buf,size);
  326. buf2[size] = '\0';
  327. */
  328. //printf("buf : %s \n",buf);
  329. while(buf[i] != ',') //Ou ' \" '
  330. {
  331. *(buf2 + i) = *(buf + i);
  332. i++;
  333. }
  334. *(buf2 + i) = '\0';
  335. buf3 = strchr(buf2,':'); //On récupère juste la valeur de la chaine qui est situé après le ":"
  336.  
  337. //free(buf);
  338. return buf3;
  339. }
  340.  
  341. char * clean_string(char * string)
  342. {
  343. char * buf = malloc(100 * sizeof(char));
  344. int i = 0;
  345. int j = 0;
  346.  
  347. while(string[i] != '\0')
  348. {
  349. if((string[i] >= 'a' && string[i] <= 'z' ) || (string[i] >= 'A' && string[i] <= 'Z' ) || ( string[i] >= '0' && string[i] <= '9' ) || string[i] == '-' || string[i] == '.' || string[i] == ' '|| string[i] == 130|| string[i] == 138)
  350. {
  351. buf[j] = string[i];
  352. j++;
  353. }
  354. i++;
  355. }
  356. //printf("%s\n",buf);
  357. return buf;
  358. }
  359.  
  360. void get_config(char * address,char * user,char * password,char * bddname, char * filename)
  361. {
  362. FILE * config = fopen("conf.ini","r");
  363. char * value = malloc(50 * sizeof(char));
  364. int i = 0;
  365.  
  366. while(fgets(value,50,config) != NULL)
  367. {
  368.  
  369. if(strncmp(value,"#ADDRESS",strlen("#ADDRESS")) == 0){
  370. fgets(value,50,config);
  371. if(value[strlen(value) - 1] == '\n')
  372. {
  373. value[strlen(value) - 1] = '\0';
  374. }
  375. strcpy(address,value);
  376. }
  377.  
  378. if(strncmp(value,"#HOSTNAME",strlen("#HOSTNAME")) == 0){
  379. fgets(value,50,config);
  380. if(value[strlen(value) - 1] == '\n')
  381. {
  382. printf("1\n");
  383. value[strlen(value) - 1] = '\0';
  384. }
  385. strcpy(user,value);
  386. }
  387.  
  388. if(strncmp(value,"#PASSWORD",strlen("#PASSWORD")) == 0){
  389. fgets(value,50,config);
  390. if(value[strlen(value) - 1] == '\n')
  391. {
  392. value[strlen(value) - 1] = '\0';
  393. }
  394. strcpy(password,value);
  395. }
  396.  
  397. if(strncmp(value,"#BDDNAME",strlen("#BDDNAME")) == 0){
  398. fgets(value,50,config);
  399. if(value[strlen(value) - 1] == '\n')
  400. {
  401. value[strlen(value) - 1] = '\0';
  402. }
  403. strcpy(bddname,value);
  404. }
  405.  
  406. if(strncmp(value,"#FILENAME",strlen("#FILENAME")) == 0){
  407. fgets(value,50,config);
  408. if(value[strlen(value) - 1] == '\n')
  409. {
  410. value[strlen(value) - 1] = '\0';
  411. }
  412. strcpy(filename,value);
  413.  
  414. }
  415.  
  416. }
  417.  
  418.  
  419. fclose(config);
  420. }
  421.  
  422.  
  423. char ** get_keyword()
  424. {
  425. FILE * config = fopen("conf.ini","r");
  426. //char ** keys = malloc(1 * sizeof(char *));
  427. char ** keys = NULL;
  428. char * value = malloc(50 * sizeof(char));
  429. int i = 0;
  430.  
  431. while(fgets(value,50,config) != NULL)
  432. {
  433. if(strncmp(value,"#KEYWORD",strlen("#KEYWORD")) == 0)
  434. {
  435. while(fgets(value,50,config) != NULL, strcmp(value,"\n") != 0)
  436. {
  437. if(value[strlen(value) - 1] == '\n')
  438. {
  439. value[strlen(value) - 1] = '\0';
  440. }
  441.  
  442. keys = realloc(keys, (i +1) * sizeof(char *) );
  443. *(keys + i) = malloc(50 * sizeof(50));
  444. strcpy(keys[i],value);
  445.  
  446. i++;
  447.  
  448. /*
  449. fgets(value,50,config);
  450. if(value[strlen(value) - 1] == '\n')
  451. {
  452. value[strlen(value) - 1] = '\0';
  453. }
  454. strcpy(filename,value);
  455. */
  456. }
  457.  
  458. }
  459.  
  460. }
  461. int j;
  462. printf("i = %d\n",i);
  463. for(j = 0; j < i; j++){
  464. printf("%s\n", keys[j]);
  465. }
  466.  
  467. fclose(config);
  468. return keys;
  469. }
  470.  
  471.  
  472. int test_json(char * buffer) //S'il y a une erreur dans le fichier, on arrete le programme
  473. {
  474.  
  475. if(strstr(buffer,"Error") != NULL || strstr(buffer,"Invalid") != NULL || strlen(buffer) < 1000)
  476. {
  477. printf("****************************\n");
  478. printf("* *\n");
  479. printf("* ERREUR JSON *\n");
  480. printf("* *\n");
  481. printf("****************************\n\n");
  482. return 0;
  483. }
  484. return 1;
  485. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement