Example #1
0
int
parse_and_write_detail_data(const char *station_id, htmlDocPtr doc, const char *result_file){
    char       buff[256];
    char       buffer[buff_size];
    char       temp_buffer[buff_size];
    int        j, k, l;

    xmlXPathContextPtr xpathCtx; 
    xmlXPathObjectPtr xpathObj = NULL; 
    xmlXPathObjectPtr xpathObj2 = NULL; 
    xmlNodeSetPtr nodes;
    char       *temp_char;
    char       *temp_char2;
    int        pressure; 
    int        speed;
    int        ppcp;

    char       *image = NULL;
    double      time_diff = 0;
    time_t      loc_time;
    time_t      utc_time;
    int        location_timezone = 0;
    int    flag;
    struct tm   tmp_tm = {0};
    struct tm   *tm;
    time_t      t_start = 0, t_end = 0,
                t_sunrise = 0, t_sunset = 0,
                current_time = 0;
    FILE        *file_out;
    int index = 1;
    struct tm time_tm1;
    struct tm time_tm2;
    int localtimezone = 0;
    int remotetimezone = 0;
    int hour = 0;
    int day = 0;
    int month = 0;
    char buff_day[256];


    /* fprintf(stderr, "parse_and_write_detail_data()\n"); */
    file_out = fopen(result_file, "a");
    if (!file_out)
        return -1;
    /* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */
    hash_for_icons = hash_icons_forecacom_table_create();

    /* Create xpath evaluation context */
    xpathCtx = xmlXPathNewContext(doc);
    if(xpathCtx == NULL) {
        fprintf(stderr,"Error: unable to create new XPath context\n");
         return(-1);
    }
    /* Register namespaces from list (if any) */
    xmlXPathRegisterNs(xpathCtx, (const xmlChar*)"html",
                                (const xmlChar*)"http://www.w3.org/1999/xhtml");
    /* Current weather forecast */
    /* Evaluate xpath expression */
    xpathObj = xmlXPathEvalExpression((const xmlChar*)"/html/body/div[@id='cc']/div[@class='cctext']/p/text()[3]", xpathCtx);
  
    if(xpathObj == NULL) {
        fprintf(stderr,"Error: unable to evaluate xpath expression \"%s\"\n", " /html/body/div[@id='cc']/div[@class='cctext']/p/text()[3]");
        xmlXPathFreeContext(xpathCtx); 
        return(-1);
    }

    if (xpathObj->nodesetval == NULL || xpathObj->nodesetval->nodeTab == NULL || xpathObj->nodesetval->nodeTab[0] ==NULL || xpathObj->nodesetval->nodeTab[0]->content == NULL){
        
        xpathObj = xmlXPathEvalExpression((const xmlChar*)"/html/body/div[@id='cc']/div[@class='cctext']/p/text()[2]", xpathCtx);

        if (xpathObj->nodesetval == NULL || xpathObj->nodesetval->nodeTab == NULL || xpathObj->nodesetval->nodeTab[0] ==NULL || xpathObj->nodesetval->nodeTab[0]->content == NULL){
            xmlXPathFreeContext(xpathCtx); 
            return -1;
        }
    }
    /* Set localtimezone */
    current_time = time(NULL);
    gmtime_r(&current_time, &time_tm1);
    localtime_r(&current_time, &time_tm2);
    localtimezone = (mktime(&time_tm2) - mktime(&time_tm1))/3600; 
    /* fprintf(stderr,"Local Time Zone %i\n", localtimezone); */

    if (xpathObj && xpathObj->nodesetval->nodeTab[0]->content){
        tm = localtime(&current_time);
        snprintf(buffer, sizeof(buffer)-1,"%i %s", tm->tm_year + 1900, xpathObj->nodesetval->nodeTab[0]->content);
        /* fprintf(stderr, "Time %s", buffer); */
        setlocale(LC_TIME, "POSIX");
        strptime((const char*)buffer, "%Y%n%d/%m %H", &tmp_tm);
        hour = tmp_tm.tm_hour; 
        day = tmp_tm.tm_mday;
        month = tmp_tm.tm_mon;
        if (xpathObj)
            xmlXPathFreeObject(xpathObj);
        xpathObj = xmlXPathEvalExpression((const xmlChar*)"/html/body/div[@class='hourlyfc']/*[@class='symbcol']/preceding-sibling::div[@class='timecol']/p/text()", xpathCtx);
        if (xpathObj && xpathObj->nodesetval->nodeTab[0]->content){
            int hour = atoi((const char*)xpathObj->nodesetval->nodeTab[0]->content) -1;
            tmp_tm.tm_hour = hour;
        }
        time_tm1.tm_min = 0;
        tmp_tm.tm_min = 0;
        remotetimezone = (mktime(&tmp_tm) - mktime(&time_tm1))/3600; 
        if (abs(remotetimezone) < 13)
           fprintf(file_out,"  <timezone>%i</timezone>\n", remotetimezone);
        /* fprintf(stderr,"Remote timezone %i\n", remotetimezone); */
        strptime((const char*)buffer, "%Y%n%d/%m %H:%M", &tmp_tm);
        /* fprintf (stderr, "Time %s\n", buffer); */

        setlocale(LC_TIME, "");
        /* set begin of day in localtime */
        //tmp_tm.tm_year = tm->tm_year;

        t_start = mktime(&tmp_tm) - 3600*remotetimezone + 3600*localtimezone;
        fprintf(file_out,"    <period start=\"%li\"", (t_start + 1  - 2*3600));
        /* set end of current time in localtime */
        t_end = t_start + 3600*4 - 1;
        fprintf(file_out," end=\"%li\" current=\"true\" >\n", t_end);

    }
    if (xpathObj)
        xmlXPathFreeObject(xpathObj);

    xpathObj = xmlXPathEvalExpression((const xmlChar*)"/html/body/div[@id='cc']/div[@class='right']/span/text()", xpathCtx);
    /* added temperature */
    if (xpathObj && !xmlXPathNodeSetIsEmpty(xpathObj->nodesetval) &&
        xpathObj->nodesetval->nodeTab[0] && xpathObj->nodesetval->nodeTab[0]->content){
        /* fprintf (stderr, "temperature %s\n", xpathObj->nodesetval->nodeTab[0]->content); */
        snprintf(buffer, sizeof(buffer)-1,"%s", xpathObj->nodesetval->nodeTab[0]->content);
        memset(temp_buffer, 0, sizeof(temp_buffer));
        for (j = 0 ; (j<(strlen(buffer)) && j < buff_size); j++ ){
            if (buffer[j] == '&')
               break;
            if ((uint)buffer[j] == 226 ||  buffer[j] == '-' || 
                (buffer[j]>='0' && buffer[j]<='9')){
                if ((uint)buffer[j] == 226)
                   sprintf(temp_buffer,"%s-",temp_buffer);
                else
                   sprintf(temp_buffer,"%s%c",temp_buffer, buffer[j]);
            }
        }
        /* fprintf(stderr, "     <temperature>%s</temperature>\n", temp_buffer); */
        fprintf(file_out,"     <temperature>%s</temperature>\n", temp_buffer); 
    }
    if (xpathObj)
        xmlXPathFreeObject(xpathObj);

    xpathObj = xmlXPathEvalExpression((const xmlChar*)"/html/body/div[@id='cc']/div[@class='right wind']/div/@class", xpathCtx);
    /*         fprintf(stderr,"sss %s", xpathObj->nodesetval->nodeTab[0]->children->content); */
    /* added wind direction */
    if (xpathObj && !xmlXPathNodeSetIsEmpty(xpathObj->nodesetval) &&
        xpathObj->nodesetval->nodeTab[0] && 
        xpathObj->nodesetval->nodeTab[0]->children->content){
             temp_char = strstr((char*)xpathObj->nodesetval->nodeTab[0]->children->content, " ");
             temp_char++;
             temp_char++;
             switch (atoi(temp_char)){
                case 0:
                    snprintf(buff, sizeof(buff)-1,"N");
                    break;
                case 45:
                    snprintf(buff, sizeof(buff)-1,"NE");
                    break;
                case 90:
                    snprintf(buff, sizeof(buff)-1,"E");
                    break;
                case 135:
                    snprintf(buff, sizeof(buff)-1,"SE");
                    break;
                case 180:
                    snprintf(buff, sizeof(buff)-1,"S");
                    break;
                case 225:
                    snprintf(buff, sizeof(buff)-1,"SW");
                    break;
                case 270:
                    snprintf(buff, sizeof(buff)-1,"W");
                    break;
                case 315:
                    snprintf(buff, sizeof(buff)-1,"NW");
                    break;
                case 360:
                    snprintf(buff, sizeof(buff)-1,"N");
                    break;
                default:
                    snprintf(buff, sizeof(buff)-1,"");
                    break;
             }

       /* fprintf(stderr, "Wind  direction  .%s.  \n", xpathObj->nodesetval->nodeTab[0]->children->content); */
            fprintf(file_out,"     <wind_direction>%s</wind_direction>\n",  buff);
    }
    if (xpathObj)
        xmlXPathFreeObject(xpathObj);

    xpathObj = xmlXPathEvalExpression((const xmlChar*)"/html/body/div[@id='cc']/div[@class='right wind']/strong/text()", xpathCtx);
    /* added wind speed */
     if (xpathObj && !xmlXPathNodeSetIsEmpty(xpathObj->nodesetval) &&
         xpathObj->nodesetval->nodeTab[0] && xpathObj->nodesetval->nodeTab[0]->content){
        /* fprintf(stderr, "Wind  direction  %s  \n", xpathObj4->nodesetval->nodeTab[i]->children->content);  */
        fprintf(file_out,"     <wind_speed>%i</wind_speed>\n",  
                               atoi((const char*)xpathObj->nodesetval->nodeTab[0]->content));
     }
    if (xpathObj)
        xmlXPathFreeObject(xpathObj);

    xpathObj = xmlXPathEvalExpression((const xmlChar*)"/html/body/div[@id='cc']/div[@class='left']/img/@src", xpathCtx);
    /* added icon */
    if (xpathObj && !xmlXPathNodeSetIsEmpty(xpathObj->nodesetval) &&
        xpathObj->nodesetval->nodeTab[0] && 
        xpathObj->nodesetval->nodeTab[0]->children->content){
       temp_char = strrchr((char*)xpathObj->nodesetval->nodeTab[0]->children->content, '/');
       temp_char ++;
       /*  fprintf (stderr, "icon %s %s \n", xpathObj6->nodesetval->nodeTab[i]->children->content, choose_hour_weather_icon(hash_for_icons, temp_char)); */ 
//       fprintf(file_out,"     <icon>%s</icon>\n",  
//                              choose_hour_weather_icon(temp_char).toStdString().c_str());
       if ((char*)xmlHashLookup(hash_for_icons, (const xmlChar*)temp_char)){
            fprintf(file_out,"     <icon>%s</icon>\n",  
             (char*)xmlHashLookup(hash_for_icons, (const xmlChar*)temp_char));
       }else 
            fprintf(file_out,"     <icon>49</icon>\n");  

    }
    if (xpathObj)
        xmlXPathFreeObject(xpathObj);
    
    index = 1;
//    snprintf(buffer, sizeof(buffer)-1,"/html/body/div[@id='cc']/div[@class='cctext']/p/strong/text()", index);
    snprintf(buffer, sizeof(buffer)-1,"/html/body/div[@id='cc']/div[@class='cctext']/p[not(contains(text(), ':'))]/strong/text()");
    xpathObj = xmlXPathEvalExpression((const xmlChar*)buffer, xpathCtx);
    /* added text */
    if (xpathObj && !xmlXPathNodeSetIsEmpty(xpathObj->nodesetval) &&
        xpathObj->nodesetval->nodeTab[0] && xpathObj->nodesetval->nodeTab[0]->content){
            /* fprintf (stderr, "description %s\n", xpathObj7->nodesetval->nodeTab[i]->content); */
            fprintf(file_out,"     <description>%s</description>\n", xpathObj->nodesetval->nodeTab[0]->content);
    }
    if (xpathObj)
        xmlXPathFreeObject(xpathObj);
    
    snprintf(buffer, sizeof(buffer)-1,"/html/body/div[@id='cc']/div[@class='cctext']/p/strong[1]/text()");
    xpathObj = xmlXPathEvalExpression((const xmlChar*)buffer, xpathCtx);
    if (xpathObj && !xmlXPathNodeSetIsEmpty(xpathObj->nodesetval) &&
        xpathObj->nodesetval->nodeTab[1] && xpathObj->nodesetval->nodeTab[1]->content){
        fprintf(file_out,"     <flike>%i</flike>\n", atoi((char*)xpathObj->nodesetval->nodeTab[1]->content)); 
    }
    if (xpathObj)
        xmlXPathFreeObject(xpathObj);

    snprintf(buffer, sizeof(buffer)-1,"/html/body/div[@id='cc']/div[@class='cctext']/p/strong/text()");
    xpathObj = xmlXPathEvalExpression((const xmlChar*)buffer, xpathCtx);
    if (xpathObj && !xmlXPathNodeSetIsEmpty(xpathObj->nodesetval) &&
        xpathObj->nodesetval->nodeTab[2] && xpathObj->nodesetval->nodeTab[2]->content){
        fprintf(file_out,"     <humidity>%i</humidity>\n", atoi((char*)xpathObj->nodesetval->nodeTab[2]->content)); 
    }
    if (xpathObj)
        xmlXPathFreeObject(xpathObj);

    snprintf(buffer, sizeof(buffer)-1,"/html/body/div[@id='cc']/div[@class='cctext']/p/strong/text()");
    xpathObj = xmlXPathEvalExpression((const xmlChar*)buffer, xpathCtx);
    if (xpathObj && !xmlXPathNodeSetIsEmpty(xpathObj->nodesetval) &&
        xpathObj->nodesetval->nodeTab[3] && xpathObj->nodesetval->nodeTab[3]->content){
        fprintf(file_out,"     <pressure>%i</pressure>\n", atoi((char*)xpathObj->nodesetval->nodeTab[3]->content)); 
    }
    if (xpathObj)
        xmlXPathFreeObject(xpathObj);

    snprintf(buffer, sizeof(buffer)-1,"/html/body/div[@id='cc']/div[@class='cctext']/p/strong/text()");
    xpathObj = xmlXPathEvalExpression((const xmlChar*)buffer, xpathCtx);
    if (xpathObj && !xmlXPathNodeSetIsEmpty(xpathObj->nodesetval) &&
        xpathObj->nodesetval->nodeTab[4] && xpathObj->nodesetval->nodeTab[4]->content){
        fprintf(file_out,"     <visible>%i</visible>\n", atoi((char*)xpathObj->nodesetval->nodeTab[4]->content)*1000); 
    }
    if (xpathObj)
        xmlXPathFreeObject(xpathObj);

    fprintf(file_out,"    </period>\n");


    //return 1;

    /* To DO sunrise and sunset */

    xpathObj = xmlXPathEvalExpression((const xmlChar*)"/html/body/div[@class='hourlyfc']/*[@class='symbcol']/preceding-sibling::div[@class='timecol']/p/strong/text()", xpathCtx);
    snprintf(buff_day, sizeof(buff_day)-1,"%s", (const char*)xpathObj->nodesetval->nodeTab[0]->content);
    /* fprintf(stderr, "buff_day %s\n", buff_day); */
    if (xpathObj)
        xmlXPathFreeObject(xpathObj);

    snprintf(buffer, sizeof(buffer)-1,"(/html/body/div[@class='hourlyfc']/*[@class='symbcol']/preceding-sibling::div[@class='timecol']/p/strong/text() | /html/body/div[@class='hourlyfc']/*[@class='symbcol']/preceding-sibling::div[@class='timecol']/p/text()  | /html/body/div[@class='hourlyfc']/*[@class='symbcol']/img/@title | /html/body/div[@class='hourlyfc']/*[@class='symbcol']/img/@src | /html/body/div[@class='hourlyfc']/*[@class='symbcol']/following-sibling::div/span/text() | /html/body/div[@class='hourlyfc']/*[@class='symbcol']/following-sibling::div/div/@class | /html/body/div[@class='hourlyfc']/*[@class='symbcol']/following-sibling::div/strong/text() ) ");
    xpathObj2 = xmlXPathEvalExpression((const xmlChar*)buffer, xpathCtx);
    k = 0; 
       
   nodes   = xpathObj2->nodesetval;
   flag = true;
   for (j = 0; j <(nodes->nodeNr); ++j){
      if (k >6)
           flag = false;
       
      if (flag){
           switch (k){
                case 1: {
                    current_time = time(NULL);
                    tm = localtime(&current_time);
                    snprintf(buffer, sizeof(buffer)-1,"%i %i %s", month + 1, day, xpathObj2->nodesetval->nodeTab[j]->content);
                    /* fprintf(stderr," Buffer %s\n", buffer); */
                    setlocale(LC_TIME, "POSIX");
                    strptime((const char*)buffer, "%m %d %H:%M", &tmp_tm);
                    setlocale(LC_TIME, "");
                    /* set begin of day in localtime */
                    tmp_tm.tm_year = tm->tm_year;

                    t_start = mktime(&tmp_tm) - 3600*remotetimezone + 3600*localtimezone;
                    if (strcmp((const char*)xpathObj2->nodesetval->nodeTab[j-1]->content, buff_day)){
                        t_start = t_start + 24*3600;
                    }
                    fprintf(file_out,"    <period start=\"%li\"", (t_start));
                    /* set end of current time in localtime */
                    t_end = t_start + 3600*3 - 1;
                    fprintf(file_out," end=\"%li\" hour=\"true\">\n", t_end);
                    break;
                    }
                case 2: {
                        if (xpathObj2->nodesetval->nodeTab[j] && xpathObj2->nodesetval->nodeTab[j]->children && xpathObj2->nodesetval->nodeTab[j]->children->content && strlen((char*)xpathObj2->nodesetval->nodeTab[j]->children->content)>0){
                            temp_char = strrchr((char*)xpathObj2->nodesetval->nodeTab[j]->children->content, '/');
                            temp_char ++;
                           // fprintf(file_out,"     <icon>%s</icon>\n",  
                           // choose_hour_weather_icon(temp_char).toStdString().c_str());
                           if ((char*)xmlHashLookup(hash_for_icons, (const xmlChar*)temp_char)){
                                fprintf(file_out,"     <icon>%s</icon>\n",  
                                 (char*)xmlHashLookup(hash_for_icons, (const xmlChar*)temp_char));
                           }else 
                                fprintf(file_out,"     <icon>49</icon>\n");  
                         } 
                }
                break;
                case 3: {
                    if (strlen((char*)xpathObj2->nodesetval->nodeTab[j]->children->content)>0){
                        fprintf(file_out,"     <description>%s</description>\n", xpathObj2->nodesetval->nodeTab[j]->children->content);
                    }
            }
            break;

            case 4: {
                    /* added temperature */
                    if (xpathObj2->nodesetval->nodeTab[j]->content){
                        snprintf(buffer, sizeof(buffer)-1,"%s", xpathObj2->nodesetval->nodeTab[j]->content);
                        memset(temp_buffer, 0, sizeof(temp_buffer));
                        for (l = 0 ; (l<(strlen(buffer)) && l < buff_size); l++ ){
                            if (buffer[l] == '&')
                               break;
                            if ((uint)buffer[l] == 226 ||  buffer[l] == '-' || 
                                (buffer[l]>='0' && buffer[l]<='9')){
                                if ((uint)buffer[l] == 226)
                                   sprintf(temp_buffer,"%s-",temp_buffer);
                                else
                                   sprintf(temp_buffer,"%s%c",temp_buffer, buffer[l]);
                            }
                        }
                        fprintf(file_out,"     <temperature>%s</temperature>\n", temp_buffer); 
                    }
                    break;
                }
            case 5: {
                if (strlen((char*)xpathObj2->nodesetval->nodeTab[j]->children->content)>0){
                    temp_char = strstr((char*)xpathObj2->nodesetval->nodeTab[j]->children->content, " ");
                    temp_char++;
                    temp_char++;
                    switch (atoi(temp_char)){
                        case 0:
                            snprintf(buff, sizeof(buff)-1,"N");
                            break;
                        case 45:
                            snprintf(buff, sizeof(buff)-1,"NE");
                            break;
                        case 90:
                            snprintf(buff, sizeof(buff)-1,"E");
                            break;
                        case 135:
                            snprintf(buff, sizeof(buff)-1,"SE");
                            break;
                        case 180:
                            snprintf(buff, sizeof(buff)-1,"S");
                            break;
                        case 225:
                            snprintf(buff, sizeof(buff)-1,"SW");
                            break;
                        case 270:
                            snprintf(buff, sizeof(buff)-1,"W");
                            break;
                        case 315:
                            snprintf(buff, sizeof(buff)-1,"NW");
                            break;
                        case 360:
                            snprintf(buff, sizeof(buff)-1,"N");
                            break;
                        default:
                            snprintf(buff, sizeof(buff)-1,"");
                            break;
                    }
                    fprintf(file_out,"     <wind_direction>%s</wind_direction>\n",  buff);
                    }
                    break;
                    }
           case 6: {
                if (strlen((char*)xpathObj2->nodesetval->nodeTab[j]->content)>0){
                    fprintf(file_out,"     <wind_speed>%s</wind_speed>\n",  xpathObj2->nodesetval->nodeTab[j]->content);
                }
                k = -1;
                fprintf(file_out,"    </period>\n");
            }
            break;
           }
        k++;
       }
    }
    fclose(file_out);
    if (xpathObj2)
        xmlXPathFreeObject(xpathObj2);
    xmlHashFree(hash_for_icons, NULL);
    xmlXPathFreeContext(xpathCtx); 
    return 1;
}
Example #2
0
gint
parse_and_write_detail_data(const gchar *station_id, htmlDocPtr doc, const gchar *result_file){
    gchar       buff[256],
                buffer[buff_size],
                current_temperature[20],
                current_icon[10],
                current_title[1024],
                current_pressure[15],
                current_humidity[15],
                current_wind_direction[15],
                current_wind_speed[15];
    gchar       temp_buffer[buff_size];
    GSList      *forecast = NULL;
    GSList      *tmp = NULL;
    GHashTable  *day = NULL;
    gboolean    night_flag;
    gint        size;
    gint        i, j, k, l;
    GHashTable *hash_for_translate;
    GHashTable *hash_for_icons;
    xmlXPathContextPtr xpathCtx; 
    xmlXPathObjectPtr xpathObj = NULL; 
    xmlXPathObjectPtr xpathObj2 = NULL; 
    xmlXPathObjectPtr xpathObj3 = NULL; 
    xmlXPathObjectPtr xpathObj4 = NULL; 
    xmlXPathObjectPtr xpathObj5 = NULL; 
    xmlXPathObjectPtr xpathObj6 = NULL; 
    xmlXPathObjectPtr xpathObj7 = NULL; 
    xmlXPathObjectPtr xpathObj8 = NULL; 
    xmlXPathObjectPtr xpathObj9 = NULL; 
    xmlNodeSetPtr nodes;
    gchar       *temp_char;
    gchar       *temp_char2;
    gint        pressure; 
    gint        speed;
    gint        ppcp;

    gchar       *image = NULL;
    double      time_diff = 0;
    time_t      loc_time;
    time_t      utc_time;
    gint        location_timezone = 0;
    gboolean timezone_flag = FALSE;
    gboolean sunrise_flag = FALSE;
    gboolean    flag;
    struct tm   tmp_tm_loc = {0};
    struct tm   tmp_tm = {0};
    struct tm   current_tm = {0};
    struct tm   tm_l = {0};
    struct tm   tmp_tm2 = {0};
    struct tm   *tm;
    time_t      t_start = 0, t_end = 0,
                t_sunrise = 0, t_sunset = 0,
                current_time = 0;
    FILE        *file_out;
    int index = 1;

    file_out = fopen(result_file, "a");
    if (!file_out)
        return -1;

    hash_for_icons = hash_icons_forecacom_table_create();
    /* Create xpath evaluation context */
    xpathCtx = xmlXPathNewContext(doc);
    if(xpathCtx == NULL) {
        fprintf(stderr,"Error: unable to create new XPath context\n");
         return(-1);
    }
    /* Register namespaces from list (if any) */
    xmlXPathRegisterNs(xpathCtx, (const xmlChar*)"html",
                                (const xmlChar*)"http://www.w3.org/1999/xhtml");
    /* Current weather forecast */
    /* Evaluate xpath expression */
    xpathObj = xmlXPathEvalExpression((const xmlChar*)"/html/body/div/div/table//tr[1]/th[@colspan='4']/text()", xpathCtx);
  
    if(xpathObj == NULL) {
        fprintf(stderr,"Error: unable to evaluate xpath expression \"%s\"\n", " /html/body/div/div/table//tr[1]/th[@colspan='4']/text()");
        xmlXPathFreeContext(xpathCtx); 
        return(-1);
    }
    temp_char = strchr(xpathObj->nodesetval->nodeTab[0]->content, ' ');
    if (temp_char == NULL || strlen(temp_char)<2)
        return -1;
    temp_char = temp_char + 1;
    temp_char = strchr(temp_char, ' ');
    if (temp_char != NULL){
        for (j=0; j<strlen(temp_char)-1; j++){
            if (temp_char[j] == ' ' || temp_char[j] == '\n')
                continue; 
            else{
                temp_char = temp_char + j;
                break;
            }
        }
    }


    xpathObj = xmlXPathEvalExpression((const xmlChar*)"/html/body/div/div/table//tr[3]/td[@class='in']/text()", xpathCtx);
    if (xpathObj && xpathObj->nodesetval->nodeTab[0]->content){
        snprintf(buffer, sizeof(buffer)-1,"%s %s", temp_char, xpathObj->nodesetval->nodeTab[0]->content);
        current_time = time(NULL);
        tm = localtime(&current_time);

        setlocale(LC_TIME, "POSIX");
        strptime((const char*)buffer, "%d/%m %H:%M", &tmp_tm);
        setlocale(LC_TIME, "");
        /* set begin of day in localtime */
        tmp_tm.tm_year = tm->tm_year;

        t_start = mktime(&tmp_tm);
        fprintf(file_out,"    <period start=\"%li\"", (t_start + 1 - 2*3600));
        /* set end of current time in localtime */
        t_end = t_start + 3600*4 - 1;
        fprintf(file_out," end=\"%li\" current=\"true\" >\n", t_end);

    }
    if (xpathObj)
        xmlXPathFreeObject(xpathObj);

    xpathObj = xmlXPathEvalExpression((const xmlChar*)"/html/body/div/div/table//tr[3]/td[2]/span/text()", xpathCtx);
    /* added temperature */
    if (xpathObj && !xmlXPathNodeSetIsEmpty(xpathObj->nodesetval) &&
        xpathObj->nodesetval->nodeTab[0] && xpathObj->nodesetval->nodeTab[0]->content){
        /* fprintf (stderr, "temperature %s\n", xpathObj->nodesetval->nodeTab[0]->content); */
        snprintf(buffer, sizeof(buffer)-1,"%s", xpathObj->nodesetval->nodeTab[0]->content);
        memset(temp_buffer, 0, sizeof(temp_buffer));
        for (j = 0 ; (j<(strlen(buffer)) && j < buff_size); j++ ){
            if (buffer[j] == '&')
               break;
            if ((uint)buffer[j] == 226 ||  buffer[j] == '-' || 
                (buffer[j]>='0' && buffer[j]<='9')){
                if ((uint)buffer[j] == 226)
                   sprintf(temp_buffer,"%s-",temp_buffer);
                else
                   sprintf(temp_buffer,"%s%c",temp_buffer, buffer[j]);
            }
        }
        /* fprintf(stderr, "     <temperature>%s</temperature>\n", temp_buffer); */
        fprintf(file_out,"     <temperature>%s</temperature>\n", temp_buffer); 
    }
    if (xpathObj)
        xmlXPathFreeObject(xpathObj);

    xpathObj = xmlXPathEvalExpression((const xmlChar*)"/html/body/div/div/table//tr[3]/td[3]/img/@alt", xpathCtx);
    /* added wind direction */
    if (xpathObj && !xmlXPathNodeSetIsEmpty(xpathObj->nodesetval) &&
        xpathObj->nodesetval->nodeTab[0] && 
        xpathObj->nodesetval->nodeTab[0]->children->content){
       /* fprintf(stderr, "Wind  direction  .%s.  \n", xpathObj->nodesetval->nodeTab[0]->children->content); */
       if (strlen(xpathObj->nodesetval->nodeTab[0]->children->content)>0)
            fprintf(file_out,"     <wind_direction>%s</wind_direction>\n",  xpathObj->nodesetval->nodeTab[0]->children->content);
    }
    if (xpathObj)
        xmlXPathFreeObject(xpathObj);

    xpathObj = xmlXPathEvalExpression((const xmlChar*)"/html/body/div/div/table//tr[3]/td[3]/text()", xpathCtx);
    /* added wind speed */
     if (xpathObj && !xmlXPathNodeSetIsEmpty(xpathObj->nodesetval) &&
         xpathObj->nodesetval->nodeTab[0] && xpathObj->nodesetval->nodeTab[0]->content){
        /* fprintf(stderr, "Wind  direction  %s  \n", xpathObj4->nodesetval->nodeTab[i]->children->content);  */
        fprintf(file_out,"     <wind_speed>%s</wind_speed>\n",  
                               xpathObj->nodesetval->nodeTab[0]->content);
     }
    if (xpathObj)
        xmlXPathFreeObject(xpathObj);

    xpathObj = xmlXPathEvalExpression((const xmlChar*)"/html/body/div/div/table//tr[3]/td[4]/img/@src", xpathCtx);
    /* added icon */
    if (xpathObj && !xmlXPathNodeSetIsEmpty(xpathObj->nodesetval) &&
        xpathObj->nodesetval->nodeTab[0] && 
        xpathObj->nodesetval->nodeTab[0]->children->content){
       temp_char = strrchr((char*)xpathObj->nodesetval->nodeTab[0]->children->content, '/');
       temp_char ++;
       /*  fprintf (stderr, "icon %s %s \n", xpathObj6->nodesetval->nodeTab[i]->children->content, choose_hour_weather_icon(hash_for_icons, temp_char)); */ 
       fprintf(file_out,"     <icon>%s</icon>\n",  
                              choose_hour_weather_icon(hash_for_icons, temp_char));
    }
    if (xpathObj)
        xmlXPathFreeObject(xpathObj);
    
    index = 1;
    snprintf(buffer, sizeof(buffer)-1,"/html/body/div/div/table//tr[4]/td/text()[%i]", index);
    xpathObj = xmlXPathEvalExpression((const xmlChar*)buffer, xpathCtx);
    /* added text */
    if (xpathObj && !xmlXPathNodeSetIsEmpty(xpathObj->nodesetval) &&
        xpathObj->nodesetval->nodeTab[0] && xpathObj->nodesetval->nodeTab[0]->content){
        if (strstr(xpathObj->nodesetval->nodeTab[0]->content, "Feels Like:") == NULL){
            /* fprintf (stderr, "description %s\n", xpathObj7->nodesetval->nodeTab[i]->content); */

            snprintf(buffer, sizeof(buffer)-1,"%s", xpathObj->nodesetval->nodeTab[0]->content);
            memset(temp_buffer, 0, sizeof(temp_buffer));
            for (j = 0 ; (j<(strlen(buffer)) && j < buff_size); j++ ){
               if (buffer[j] == 13 || buffer[j] == 10)
                    continue;
               sprintf(temp_buffer,"%s%c",temp_buffer, buffer[j]);
            }
            index ++;
            fprintf(file_out,"     <description>%s</description>\n", temp_buffer);
        }
    }
    if (xpathObj)
        xmlXPathFreeObject(xpathObj);
    snprintf(buffer, sizeof(buffer)-1,"/html/body/div/div/table//tr[4]/td/text()[%i]", index);
    xpathObj = xmlXPathEvalExpression((const xmlChar*)buffer, xpathCtx);

    temp_char = strstr(xpathObj->nodesetval->nodeTab[0]->content, "Feels Like:");
    if (temp_char != NULL){
        temp_char = strchr(temp_char, ':');
        temp_char = temp_char + 2;
        snprintf(buffer, sizeof(buffer)-1,"%s", temp_char);
        memset(temp_buffer, 0, sizeof(temp_buffer));
        for (j = 0 ; (j<(strlen(buffer)) && j < buff_size); j++ ){
            if (buffer[j] == '&')
               break;
            if ((uint)buffer[j] == 226 ||  buffer[j] == '-' || 
                (buffer[j]>='0' && buffer[j]<='9')){
                if ((uint)buffer[j] == 226)
                   sprintf(temp_buffer,"%s-",temp_buffer);
                else
                   sprintf(temp_buffer,"%s%c",temp_buffer, buffer[j]);
            }
        }
        /* fprintf(stderr, "     <temperature>%s</temperature>\n", temp_buffer); */
        fprintf(file_out,"     <flike>%s</flike>\n", temp_buffer); 
    }
    fprintf(file_out,"    </period>\n");



    /* To DO sunrise and sunset */

    xpathObj = xmlXPathEvalExpression("/html/body/div/div/table//tr[th]/*/text()", xpathCtx);
    /* Day weather forecast */
    nodes   = xpathObj->nodesetval;
    size = (nodes) ? nodes->nodeNr : 0; 
    snprintf(buffer, sizeof(buffer)-1,"(/html/body/div/div/table//tr/th/text() | /html/body/div/div/table//tr/td[@class='in']/text() | /html/body/div/div/table//tr/td/span/text() | /html/body/div/div/table//tr/td/img/@alt | /html/body/div/div/table//tr/td/text() | /html/body/div/div/table//tr/td/img/@src )");
    xpathObj2 = xmlXPathEvalExpression(buffer, xpathCtx);
    k = 0; 
    for(i = 1; i < (size) ; ++i) {
       
       nodes   = xpathObj2->nodesetval;
       flag = FALSE;
       for (j = 0; j <(nodes->nodeNr); ++j){
           if (!flag && xpathObj2->nodesetval->nodeTab[j]->content && 
               xpathObj->nodesetval->nodeTab[i]->content &&
               !strcmp(xpathObj->nodesetval->nodeTab[i]->content, 
                       xpathObj2->nodesetval->nodeTab[j]->content)){
               flag = TRUE;
               k = 0;
           }
           if (flag && i+1 < size && xpathObj2->nodesetval->nodeTab[j]->content && 
               xpathObj->nodesetval->nodeTab[i+1]->content &&
               !strcmp(xpathObj->nodesetval->nodeTab[i+1]->content, 
                       xpathObj2->nodesetval->nodeTab[j]->content)){
               flag = FALSE;
           }
           if (flag){
               switch (k){
                case 1: {
                            current_time = time(NULL);
                            tm = localtime(&current_time);

                            snprintf(buffer, sizeof(buffer)-1,"%s %s", xpathObj->nodesetval->nodeTab[i]->content, xpathObj2->nodesetval->nodeTab[j]->content);
                            fprintf(stderr," Buffer %s\n", buffer);
                            setlocale(LC_TIME, "POSIX");
                            strptime((const char*)buffer, "%A %b %d %H:%M", &tmp_tm);
                            setlocale(LC_TIME, "");
                            /* set begin of day in localtime */
                            tmp_tm.tm_year = tm->tm_year;

                            t_start = mktime(&tmp_tm);
                            fprintf(file_out,"    <period start=\"%li\"", (t_start));
                            /* set end of current time in localtime */
                            t_end = t_start + 3600*6 - 1;
                            fprintf(file_out," end=\"%li\" hour=\"true\">\n", t_end);

                            break;
                        }
                case 2: {
                            /* added temperature */
                            if (xpathObj2->nodesetval->nodeTab[j]->content){
                                /* fprintf (stderr, "temperature %s\n", xpathObj->nodesetval->nodeTab[0]->content); */
                                snprintf(buffer, sizeof(buffer)-1,"%s", xpathObj2->nodesetval->nodeTab[j]->content);
                                memset(temp_buffer, 0, sizeof(temp_buffer));
                                for (l = 0 ; (l<(strlen(buffer)) && l < buff_size); l++ ){
                                    if (buffer[l] == '&')
                                       break;
                                    if ((uint)buffer[l] == 226 ||  buffer[l] == '-' || 
                                        (buffer[l]>='0' && buffer[l]<='9')){
                                        if ((uint)buffer[l] == 226)
                                           sprintf(temp_buffer,"%s-",temp_buffer);
                                        else
                                           sprintf(temp_buffer,"%s%c",temp_buffer, buffer[l]);
                                    }
                                }
                                /* fprintf(stderr, "     <temperature>%s</temperature>\n", temp_buffer); */
                                fprintf(file_out,"     <temperature>%s</temperature>\n", temp_buffer); 
                            }
                            break;
                        }
                case 4: {
                            if (strlen(xpathObj2->nodesetval->nodeTab[j]->children->content)>0)
                                fprintf(file_out,"     <wind_direction>%s</wind_direction>\n",  xpathObj2->nodesetval->nodeTab[j]->children->content);

                            break;
                        }
                case 5: { 
                            if (xpathObj2->nodesetval->nodeTab[j] && xpathObj2->nodesetval->nodeTab[j]->content){
                                /* fprintf(stderr, "Wind  direction  %s  \n", xpathObj4->nodesetval->nodeTab[i]->children->content);  */
                                fprintf(file_out,"     <wind_speed>%s</wind_speed>\n",  
                                                       xpathObj2->nodesetval->nodeTab[j]->content);
                             }
                             break;
                }
                case 6: {
                            if (strlen(xpathObj2->nodesetval->nodeTab[j]->children->content)>0){
                                temp_char = strrchr((char*)xpathObj2->nodesetval->nodeTab[j]->children->content, '/');
                                temp_char ++;
                                fprintf(file_out,"     <icon>%s</icon>\n",  
                                choose_hour_weather_icon(hash_for_icons, temp_char));
                             } 
                            break;
                }
                case 7: {
                        if (strlen(xpathObj2->nodesetval->nodeTab[j]->children->content)>0){
                            fprintf(file_out,"     <description>%s</description>\n", xpathObj2->nodesetval->nodeTab[j]->children->content);
                        }
                        k = 0;
                        fprintf(file_out,"    </period>\n");

                }

               }
            k++;
           }
       }
    }
    fclose(file_out);
  return 1;
}
Example #3
0
int
parse_and_write_xml_data(const char *station_id, htmlDocPtr doc, const char *result_file){
    char        buff[256],
                buffer[buff_size];
    char        temp_buffer[buff_size];
    int         size;
    int         i,j;
    xmlXPathContextPtr xpathCtx; 
    xmlXPathObjectPtr xpathObj = NULL; 
    xmlXPathObjectPtr xpathObj2 = NULL; 
    xmlXPathObjectPtr xpathObj3 = NULL; 
    xmlXPathObjectPtr xpathObj4 = NULL; 
    xmlXPathObjectPtr xpathObj5 = NULL; 
    xmlXPathObjectPtr xpathObj6 = NULL; 
    xmlXPathObjectPtr xpathObj7 = NULL; 
    xmlNodeSetPtr nodes;
    char       *temp_char;

    struct tm   tmp_tm = {0};
    struct tm   *tm;
    time_t      t_start = 0, t_end = 0,
                current_time = 0;
    FILE        *file_out;

    file_out = fopen(result_file, "w");
    if (!file_out)
        return -1;
    fprintf(file_out,"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<station name=\"Station name\" id=\"%s\" xmlns=\"http://omweather.garage.maemo.org/schemas\">\n", station_id);
    fprintf(file_out," <units>\n  <t>C</t>\n  <ws>m/s</ws>\n  <wg>m/s</wg>\n  <d>km</d>\n");
    fprintf(file_out,"  <h>%%</h>  \n  <p>mmHg</p>\n </units>\n");
    /* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */
    hash_for_icons = hash_icons_forecacom_table_create();
    /* Create xpath evaluation context */
    xpathCtx = xmlXPathNewContext(doc);
    if(xpathCtx == NULL) {
        fprintf(stderr,"Error: unable to create new XPath context\n");
         return(-1);
    }
    /* Register namespaces from list (if any) */
    xmlXPathRegisterNs(xpathCtx, (const xmlChar*)"html",
                                (const xmlChar*)"http://www.w3.org/1999/xhtml");
    /* Day weather forecast */
    /* Evaluate xpath expression */
    xpathObj = xmlXPathEvalExpression((const xmlChar*)"/html/body/*/div[(@data-ts)]/@data-ts", xpathCtx);
//    xpathObj = xmlXPathEvalExpression((const xmlChar*)"/html/body/div/div/table//tr/th[@colspan='3']", xpathCtx);
  
    if(xpathObj == NULL) {
        fprintf(stderr,"Error: unable to evaluate xpath expression \"%s\"\n", "/html/body/*/div[(@data-ts)]/@data-ts");
        xmlXPathFreeContext(xpathCtx); 
        return(-1);
    }

    nodes   = xpathObj->nodesetval;
    size = (nodes) ? nodes->nodeNr : 0;
    if (size > 14)
        size = 14;
    /* fprintf(stderr, "SIZE!!!!!!!!!!!!!!: %i\n", size); */
    xpathObj2 = xmlXPathEvalExpression((const xmlChar*)"/html/body/*/div[(@data-ts)]/div/span[(@class='temp_max temp_warm') or (@class='temp_max temp_cold')]/text()", xpathCtx);
    xpathObj3 = xmlXPathEvalExpression((const xmlChar*)"/html/body/*/div[(@data-ts)]/div/span[(@class='temp_min')]/text()", xpathCtx);
    xpathObj4 = xmlXPathEvalExpression((const xmlChar*)"/html/body/*/div[(@data-ts)]/div/div/@class", xpathCtx);
    xpathObj5 = xmlXPathEvalExpression((const xmlChar*)"/html/body/*/div[(@data-ts)]/div/strong/text()", xpathCtx);
    xpathObj6 = xmlXPathEvalExpression((const xmlChar*)"/html/body/*/div[(@data-ts)]/div/img/@src", xpathCtx);
    xpathObj7 = xmlXPathEvalExpression((const xmlChar*)"/html/body/*/div[(@data-ts)]/div/img/@title", xpathCtx);
  
    /* fprintf(stderr, "Result (%d nodes):\n", size); */
    for(i = 0; i < size; ++i) {

         /* Take time: */
         if (!nodes->nodeTab[i]->children->content){
             fprintf(stderr,"ERRROR");
             continue;
         }
         /* fprintf(stderr," TEXT %s\n", nodes->nodeTab[i]->children->content); */ 
         current_time = time(NULL);
         tm = localtime(&current_time);

         setlocale(LC_TIME, "POSIX");
         strptime((const char*)nodes->nodeTab[i]->children->content, "%Y%m%d", &tmp_tm);
         setlocale(LC_TIME, "");
         /* set begin of day in localtime */
         tmp_tm.tm_year = tm->tm_year;
         tmp_tm.tm_hour = 0; tmp_tm.tm_min = 0; tmp_tm.tm_sec = 0;

         t_start = mktime(&tmp_tm);
         fprintf(file_out,"    <period start=\"%li\"", t_start);
         /* set end of day in localtime */
         t_end = t_start + 3600*24 - 1;
         fprintf(file_out," end=\"%li\">\n", t_end);
     
         /* added hi temperature */
         if (xpathObj2 && !xmlXPathNodeSetIsEmpty(xpathObj2->nodesetval) &&
             xpathObj2->nodesetval->nodeTab[i] && xpathObj2->nodesetval->nodeTab[i]->content){
             /* fprintf (stderr, "temperature %s\n", xpathObj2->nodesetval->nodeTab[i]->content); */
             snprintf(buffer, sizeof(buffer)-1,"%s", xpathObj2->nodesetval->nodeTab[i]->content);
             memset(temp_buffer, 0, sizeof(temp_buffer));
             for (j = 0 ; (j<(strlen(buffer)) && j < buff_size); j++ ){
                 if (buffer[j] == '&')
                    break;
                 if ((uint)buffer[j] == 226 ||  buffer[j] == '-' || 
                     (buffer[j]>='0' && buffer[j]<='9')){
                     if ((uint)buffer[j] == 226)
                        sprintf(temp_buffer,"%s-",temp_buffer);
                     else
                        sprintf(temp_buffer,"%s%c",temp_buffer, buffer[j]);
                 }
             }
             /* fprintf(stderr, "     <temperature>%s</temperature>\n", temp_buffer); */
             fprintf(file_out,"     <temperature_hi>%s</temperature_hi>\n", temp_buffer); 
         }
         /* added lo temperature */
         if (xpathObj3 && !xmlXPathNodeSetIsEmpty(xpathObj3->nodesetval) &&
             xpathObj3->nodesetval->nodeTab[i] && xpathObj3->nodesetval->nodeTab[i]->content){
             /* fprintf (stderr, "temperature %s\n", xpathObj3->nodesetval->nodeTab[i]->content); */
             snprintf(buffer, sizeof(buffer)-1,"%s", xpathObj3->nodesetval->nodeTab[i]->content);
             memset(temp_buffer, 0, sizeof(temp_buffer));
             for (j = 0 ; (j<(strlen(buffer)) && j < buff_size); j++ ){
                 if (buffer[j] == '&')
                    break;
                 if ((uint)buffer[j] == 226 ||  buffer[j] == '-' ||
                     (buffer[j]>='0' && buffer[j]<='9')){
                     if ((uint)buffer[j] == 226)
                        sprintf(temp_buffer,"%s-",temp_buffer);
                     else
                        sprintf(temp_buffer,"%s%c",temp_buffer, buffer[j]);
                 }
             }
             /* fprintf(stderr, "     <temperature>%s</temperature>\n", temp_buffer); */
             fprintf(file_out,"     <temperature_low>%s</temperature_low>\n", temp_buffer); 
         }
         /* added wind direction */
         if (xpathObj4 && !xmlXPathNodeSetIsEmpty(xpathObj4->nodesetval) &&
             xpathObj4->nodesetval->nodeTab[i] && 
             xpathObj4->nodesetval->nodeTab[i]->children->content){
             temp_char = strstr((char*)xpathObj4->nodesetval->nodeTab[i]->children->content, " ");
             temp_char++;
             temp_char++;
             switch (atoi(temp_char)){
                case 0:
                    snprintf(buff, sizeof(buff)-1,"N");
                    break;
                case 45:
                    snprintf(buff, sizeof(buff)-1,"NE");
                    break;
                case 90:
                    snprintf(buff, sizeof(buff)-1,"E");
                    break;
                case 135:
                    snprintf(buff, sizeof(buff)-1,"SE");
                    break;
                case 180:
                    snprintf(buff, sizeof(buff)-1,"S");
                    break;
                case 225:
                    snprintf(buff, sizeof(buff)-1,"SW");
                    break;
                case 270:
                    snprintf(buff, sizeof(buff)-1,"W");
                    break;
                case 315:
                    snprintf(buff, sizeof(buff)-1,"NW");
                    break;
                case 360:
                    snprintf(buff, sizeof(buff)-1,"N");
                    break;
                default:
                    snprintf(buff, sizeof(buff)-1,"");
                    break;
             }
             /* fprintf(stderr, "Wind  direction %s %s  \n",temp_char, buff); */
             fprintf(file_out,"     <wind_direction>%s</wind_direction>\n", buff);
         }

        /* added wind speed */
         if (xpathObj5 && !xmlXPathNodeSetIsEmpty(xpathObj5->nodesetval) &&
             xpathObj5->nodesetval->nodeTab[i] && xpathObj5->nodesetval->nodeTab[i]->content){
            /* fprintf(stderr, "Wind  direction  %s  \n", xpathObj4->nodesetval->nodeTab[i]->children->content);  */
            fprintf(file_out,"     <wind_speed>%s</wind_speed>\n",  
                                   xpathObj5->nodesetval->nodeTab[i]->content);
         }

         /* added icon */
         if (xpathObj6 && !xmlXPathNodeSetIsEmpty(xpathObj6->nodesetval) &&
             xpathObj6->nodesetval->nodeTab[i] && 
             xpathObj6->nodesetval->nodeTab[i]->children->content){
            temp_char = strrchr((char*)xpathObj6->nodesetval->nodeTab[i]->children->content, '/');
            temp_char ++;
            /*  fprintf (stderr, "icon %s %s \n", xpathObj6->nodesetval->nodeTab[i]->children->content, choose_hour_weather_icon(hash_for_icons, temp_char)); */ 
            //fprintf(file_out,"     <icon>%s</icon>\n",  
            //                       choose_hour_weather_icon(temp_char).toStdString().c_str());

             if ((char*)xmlHashLookup(hash_for_icons, (const xmlChar*)temp_char)){
                fprintf(file_out,"     <icon>%s</icon>\n",  
                     (char*)xmlHashLookup(hash_for_icons, (const xmlChar*)temp_char));
             }else 
                fprintf(file_out,"     <icon>49</icon>\n");  


         }
         /* added text */
         if (xpathObj7 && !xmlXPathNodeSetIsEmpty(xpathObj7->nodesetval) &&
             xpathObj7->nodesetval->nodeTab[i] && xpathObj7->nodesetval->nodeTab[i]->children->content){
            snprintf(buffer, sizeof(buffer)-1,"%s", xpathObj7->nodesetval->nodeTab[i]->children->content);
            memset(temp_buffer, 0, sizeof(temp_buffer));
            for (j = 0 ; (j<(strlen(buffer)) && j < buff_size); j++ ){
               if (buffer[j] == 13 || buffer[j] == 10)
                    continue;
               sprintf(temp_buffer,"%s%c",temp_buffer, buffer[j]);
            }
            fprintf(file_out,"     <description>%s</description>\n", temp_buffer);
         }
         fprintf(file_out,"    </period>\n");
  }	
  /* Cleanup */
  fclose(file_out);
  if (xpathObj)
    xmlXPathFreeObject(xpathObj);
  if (xpathObj2)
    xmlXPathFreeObject(xpathObj2);
  if (xpathObj3)
    xmlXPathFreeObject(xpathObj3);
  if (xpathObj4)
    xmlXPathFreeObject(xpathObj4);
  if (xpathObj5)
    xmlXPathFreeObject(xpathObj5);
  if (xpathObj6)
    xmlXPathFreeObject(xpathObj6);
  if (xpathObj7)
    xmlXPathFreeObject(xpathObj7);
  if (xpathCtx)
    xmlXPathFreeContext(xpathCtx);

  xmlHashFree(hash_for_icons, NULL);

  return size;
}
Example #4
0
gint
parse_and_write_xml_data(const gchar *station_id, htmlDocPtr doc, const gchar *result_file){
    gchar       buff[256],
                buffer[buff_size],
                current_temperature[20],
                current_icon[10],
                current_title[1024],
                current_pressure[15],
                current_humidity[15],
                current_wind_direction[15],
                current_wind_speed[15];
    gchar       temp_buffer[buff_size];
    GSList      *forecast = NULL;
    GSList      *tmp = NULL;
    GHashTable  *day = NULL;
    gboolean    flag;
    gboolean    night_flag;
    gint        size;
    gint        i, j;
    GHashTable *hash_for_translate;
    GHashTable *hash_for_icons;
    xmlXPathContextPtr xpathCtx; 
    xmlXPathObjectPtr xpathObj = NULL; 
    xmlXPathObjectPtr xpathObj2 = NULL; 
    xmlXPathObjectPtr xpathObj3 = NULL; 
    xmlXPathObjectPtr xpathObj4 = NULL; 
    xmlXPathObjectPtr xpathObj5 = NULL; 
    xmlXPathObjectPtr xpathObj6 = NULL; 
    xmlXPathObjectPtr xpathObj7 = NULL; 
    xmlXPathObjectPtr xpathObj8 = NULL; 
    xmlXPathObjectPtr xpathObj9 = NULL; 
    xmlNodeSetPtr nodes;
    gchar       *temp_char;
    gchar       *temp_char2;
    gint        pressure; 
    gint        speed;
    gint        ppcp;

    gchar       *image = NULL;
    double      time_diff = 0;
    time_t      loc_time;
    time_t      utc_time;
    gint        location_timezone = 0;
    gboolean timezone_flag = FALSE;
    gboolean sunrise_flag = FALSE;
    struct tm   tmp_tm_loc = {0};
    struct tm   tmp_tm = {0};
    struct tm   current_tm = {0};
    struct tm   tm_l = {0};
    struct tm   tmp_tm2 = {0};
    struct tm   *tm;
    time_t      t_start = 0, t_end = 0,
                t_sunrise = 0, t_sunset = 0,
                current_time = 0;
    FILE        *file_out;

    file_out = fopen(result_file, "w");
    if (!file_out)
        return -1;
    fprintf(file_out,"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<station name=\"Station name\" id=\"%s\" xmlns=\"http://omweather.garage.maemo.org/schemas\">\n", station_id);
    fprintf(file_out," <units>\n  <t>C</t>\n  <ws>m/s</ws>\n  <wg>m/s</wg>\n  <d>km</d>\n");
    fprintf(file_out,"  <h>%%</h>  \n  <p>mmHg</p>\n </units>\n");

    hash_for_icons = hash_icons_forecacom_table_create();
    /* Create xpath evaluation context */
    xpathCtx = xmlXPathNewContext(doc);
    if(xpathCtx == NULL) {
        fprintf(stderr,"Error: unable to create new XPath context\n");
         return(-1);
    }
    /* Register namespaces from list (if any) */
    xmlXPathRegisterNs(xpathCtx, (const xmlChar*)"html",
                                (const xmlChar*)"http://www.w3.org/1999/xhtml");
    /* Day weather forecast */
    /* Evaluate xpath expression */
    xpathObj = xmlXPathEvalExpression((const xmlChar*)"/html/body/div/div/table//tr/th[@colspan='3']", xpathCtx);
  
    if(xpathObj == NULL) {
        fprintf(stderr,"Error: unable to evaluate xpath expression \"%s\"\n", "/html/body/div/div/table//tr/th[@colspan='3']/text()");
        xmlXPathFreeContext(xpathCtx); 
        return(-1);
    }

    nodes   = xpathObj->nodesetval;
    size = (nodes) ? nodes->nodeNr : 0;
    if (size > 10)
        size = 10;
    /* fprintf(stderr, "SIZE!!!!!!!!!!!!!!: %i\n", size); */
    xpathObj2 = xmlXPathEvalExpression("/html/body/div/div/table//tr/td[@class='in']//span[1]/text()", xpathCtx);
    xpathObj3 = xmlXPathEvalExpression("/html/body/div/div/table//tr/td[@class='in']//span[2]/text()", xpathCtx);
    xpathObj4 = xmlXPathEvalExpression("/html/body/div/div/table//tr/td[2]/img/@alt", xpathCtx);
    xpathObj5 = xmlXPathEvalExpression("/html/body/div/div/table//tr/td[2]/text()", xpathCtx);
    xpathObj6 = xmlXPathEvalExpression("/html/body/div/div/table//tr/td[3]/img/@src", xpathCtx);
    xpathObj7 = xmlXPathEvalExpression("/html/body/div/div/table//tr/td[@class='in2']/text()[1]", xpathCtx);
    xpathObj8 = xmlXPathEvalExpression("/html/body/div/div/table//tr/td[@class='in2']/text()[2]", xpathCtx);
  
    /* fprintf(stderr, "Result (%d nodes):\n", size); */
    for(i = 0; i < size; ++i) {
         day = NULL;

         /* Take time: */
         if (!nodes->nodeTab[i]->children->content)
             continue;
         temp_char = strstr(nodes->nodeTab[i]->children->content, " ");
         int j = 0;
         if (temp_char != NULL){
             for (j=0; j<strlen(temp_char)-1; j++){
                 if (temp_char[j] == ' ' || temp_char[j] == '\n')
                     continue; 
                 else{
                     temp_char = temp_char + j;
                     break;
                 }
             }
         }
         if (temp_char == NULL)
            continue;
         current_time = time(NULL);
         tm = localtime(&current_time);

         setlocale(LC_TIME, "POSIX");
         strptime((const char*)temp_char, "%b %d", &tmp_tm);
         setlocale(LC_TIME, "");
         /* set begin of day in localtime */
         tmp_tm.tm_year = tm->tm_year;
         tmp_tm.tm_hour = 0; tmp_tm.tm_min = 0; tmp_tm.tm_sec = 0;

         t_start = mktime(&tmp_tm);
         fprintf(file_out,"    <period start=\"%li\"", (t_start +1));
         /* set end of day in localtime */
         t_end = t_start + 3600*24 - 1;
         fprintf(file_out," end=\"%li\">\n", t_end);
     
           
         /* added hi temperature */
         if (xpathObj2 && !xmlXPathNodeSetIsEmpty(xpathObj2->nodesetval) &&
             xpathObj2->nodesetval->nodeTab[i] && xpathObj2->nodesetval->nodeTab[i]->content){
             /* fprintf (stderr, "temperature %s\n", xpathObj2->nodesetval->nodeTab[i]->content); */
             snprintf(buffer, sizeof(buffer)-1,"%s", xpathObj2->nodesetval->nodeTab[i]->content);
             memset(temp_buffer, 0, sizeof(temp_buffer));
             for (j = 0 ; (j<(strlen(buffer)) && j < buff_size); j++ ){
                 if (buffer[j] == '&')
                    break;
                 if ((uint)buffer[j] == 226 ||  buffer[j] == '-' || 
                     (buffer[j]>='0' && buffer[j]<='9')){
                     if ((uint)buffer[j] == 226)
                        sprintf(temp_buffer,"%s-",temp_buffer);
                     else
                        sprintf(temp_buffer,"%s%c",temp_buffer, buffer[j]);
                 }
             }
             /* fprintf(stderr, "     <temperature>%s</temperature>\n", temp_buffer); */
             fprintf(file_out,"     <temperature_hi>%s</temperature_hi>\n", temp_buffer); 
         }
         /* added lo temperature */
         if (xpathObj3 && !xmlXPathNodeSetIsEmpty(xpathObj3->nodesetval) &&
             xpathObj3->nodesetval->nodeTab[i] && xpathObj3->nodesetval->nodeTab[i]->content){
             /* fprintf (stderr, "temperature %s\n", xpathObj3->nodesetval->nodeTab[i]->content); */
             snprintf(buffer, sizeof(buffer)-1,"%s", xpathObj3->nodesetval->nodeTab[i]->content);
             memset(temp_buffer, 0, sizeof(temp_buffer));
             for (j = 0 ; (j<(strlen(buffer)) && j < buff_size); j++ ){
                 if (buffer[j] == '&')
                    break;
                 if ((uint)buffer[j] == 226 ||  buffer[j] == '-' ||
                     (buffer[j]>='0' && buffer[j]<='9')){
                     if ((uint)buffer[j] == 226)
                        sprintf(temp_buffer,"%s-",temp_buffer);
                     else
                        sprintf(temp_buffer,"%s%c",temp_buffer, buffer[j]);
                 }
             }
             /* fprintf(stderr, "     <temperature>%s</temperature>\n", temp_buffer); */
             fprintf(file_out,"     <temperature_low>%s</temperature_low>\n", temp_buffer); 
         }
         /* added wind direction */
         if (xpathObj4 && !xmlXPathNodeSetIsEmpty(xpathObj4->nodesetval) &&
             xpathObj4->nodesetval->nodeTab[i] && 
             xpathObj4->nodesetval->nodeTab[i]->children->content){
            /* fprintf(stderr, "Wind  direction  %s  \n", xpathObj4->nodesetval->nodeTab[i]->children->content);  */
            fprintf(file_out,"     <wind_direction>%s</wind_direction>\n",  xpathObj4->nodesetval->nodeTab[i]->children->content);
         }

        /* added wind speed */
         if (xpathObj5 && !xmlXPathNodeSetIsEmpty(xpathObj5->nodesetval) &&
             xpathObj5->nodesetval->nodeTab[i] && xpathObj5->nodesetval->nodeTab[i]->content){
            /* fprintf(stderr, "Wind  direction  %s  \n", xpathObj4->nodesetval->nodeTab[i]->children->content);  */
            fprintf(file_out,"     <wind_speed>%s</wind_speed>\n",  
                                   xpathObj5->nodesetval->nodeTab[i]->content);
         }

         /* added icon */
         if (xpathObj6 && !xmlXPathNodeSetIsEmpty(xpathObj6->nodesetval) &&
             xpathObj6->nodesetval->nodeTab[i] && 
             xpathObj6->nodesetval->nodeTab[i]->children->content){
            temp_char = strrchr((char*)xpathObj6->nodesetval->nodeTab[i]->children->content, '/');
            temp_char ++;
            /*  fprintf (stderr, "icon %s %s \n", xpathObj6->nodesetval->nodeTab[i]->children->content, choose_hour_weather_icon(hash_for_icons, temp_char)); */ 
            fprintf(file_out,"     <icon>%s</icon>\n",  
                                   choose_hour_weather_icon(hash_for_icons, temp_char));
         }
         /* added text */
         if (xpathObj7 && !xmlXPathNodeSetIsEmpty(xpathObj7->nodesetval) &&
             xpathObj7->nodesetval->nodeTab[i] && xpathObj7->nodesetval->nodeTab[i]->content){
            snprintf(buffer, sizeof(buffer)-1,"%s", xpathObj7->nodesetval->nodeTab[i]->content);
            memset(temp_buffer, 0, sizeof(temp_buffer));
            for (j = 0 ; (j<(strlen(buffer)) && j < buff_size); j++ ){
               if (buffer[j] == 13 || buffer[j] == 10)
                    continue;
               sprintf(temp_buffer,"%s%c",temp_buffer, buffer[j]);
            }
            fprintf(file_out,"     <description>%s</description>\n", temp_buffer);
         }
         /* added ppcp */
         if (xpathObj8 && !xmlXPathNodeSetIsEmpty(xpathObj8->nodesetval) &&
             xpathObj8->nodesetval->nodeTab[i] && xpathObj8->nodesetval->nodeTab[i]->content){
             snprintf(buffer, sizeof(buffer) - 1, "%s", xpathObj8->nodesetval->nodeTab[i]->content); 
             memset(temp_buffer, 0, sizeof(temp_buffer));
             for (j = 0 ; (j<(strlen(buffer)) && j < strlen(buffer)); j++ ){
                 if (buffer[j] == '%')
                    break;
                 if ( (buffer[j]>='0' && buffer[j]<='9')){
                        sprintf(temp_buffer,"%s%c",temp_buffer, buffer[j]);
                 }
             }
             fprintf(file_out,"     <ppcp>%s</ppcp>\n", temp_buffer);
         }
      fprintf(file_out,"    </period>\n");

  }	
  /* Cleanup */
  if (xpathObj)
    xmlXPathFreeObject(xpathObj);
  if (xpathObj2)
    xmlXPathFreeObject(xpathObj2);
  if (xpathObj3)
    xmlXPathFreeObject(xpathObj3);
  if (xpathObj4)
    xmlXPathFreeObject(xpathObj4);
  if (xpathObj5)
    xmlXPathFreeObject(xpathObj5);
  if (xpathObj6)
    xmlXPathFreeObject(xpathObj6);
  if (xpathObj7)
    xmlXPathFreeObject(xpathObj7);
  if (xpathObj8)
    xmlXPathFreeObject(xpathObj8);
  
  /* Clean */
  g_hash_table_destroy(hash_for_icons);

  fclose(file_out);

  return size;
}