Advertisement
Guest User

Untitled

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