Download NHK English news podcast automatically. XML Parser "spxml" is used. This application requires mpod mother board. See also http://mbed.org/users/geodenx/notebook/mpod/

Dependencies:   BlinkLed HTTPClient EthernetInterface FatFileSystemCpp MSCFileSystem spxml mbed-rtos mbed

Fork of mpod_nhk_english by Satoshi Togawa

Download NHK English news podcast automatically.
XML Parser "spxml" is used.
This application requires mpod mother board.
See also http://mbed.org/users/geodenx/notebook/mpod/

Files at this revision

API Documentation at this revision

Comitter:
togayan
Date:
Mon Aug 20 13:50:00 2012 +0000
Parent:
4:7dae52cf560f
Child:
6:84749589b532
Commit message:
Refactoring

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Mon Aug 20 13:27:17 2012 +0000
+++ b/main.cpp	Mon Aug 20 13:50:00 2012 +0000
@@ -26,9 +26,9 @@
 
 int main()
 {
-    printf("\n\n================================\n");
-    printf("mpod NHK English news Downloader\n");
-    printf("================================\n\n");
+    printf("\n\n===========================================\n");
+    printf("mpod NHK English news Downloader with spxml\n");
+    printf("===========================================\n\n");
     
     // FSUSB30 switches to HSD1 (mbed)
     printf("USB host was switched to HSD1(mbed).\n\n");
@@ -40,78 +40,98 @@
     eth.connect();
     
     // Obtain original lastBuildDate
-    
     char lastBuildDateOriginal[128] = {0};
     {
-    FILE * fp = fopen ( rssPath, "r" );
-    if( NULL == fp ) {
-        printf( "cannot not open %s\n", rssPath );
-        sprintf(lastBuildDateOriginal, "cannot not open original %s", rssPath );
-    }
-    else
-    {
-        fseek(fp, 0, SEEK_END); // seek to end of file
-        unsigned int size = ftell(fp);
-        fseek(fp, 0, SEEK_SET); // seek to head of file
-        
-        char * source = NULL;
-        source = ( char * ) malloc ( size + 1 );
-        fread ( source, size, sizeof ( char ), fp );
-        fclose ( fp );
-        source[ size ] = 0;
-        
-        SP_XmlDomParser parser;
-        parser.append( source, strlen( source ) );
-        free( source );
-        
-        SP_XmlHandle rootHandle( parser.getDocument()->getRootElement() );
-        
-        SP_XmlCDataNode * lastBuildDateNode = rootHandle.getChild( "channel" ).getChild( "lastBuildDate" ).getChild(0).toCData();
-        if( NULL != lastBuildDateNode )
+        FILE * fpOriginal = fopen ( rssPath, "r" );
+        if( NULL == fpOriginal ) {
+            printf( "cannot not open %s\n", rssPath );
+            sprintf(lastBuildDateOriginal, "cannot not open original %s", rssPath );
+        }
+        else
         {
-            printf( "Find /rss/channel/lastBuildDate/text()\n");
-            printf("text() of lastBuildDate: %s\n", lastBuildDateNode->getText());
-            strcpy(lastBuildDateOriginal, lastBuildDateNode->getText());
-        } else {
-            printf( "Cannot found /rss/channel/lastBuildDate/text()\n" );
-            sprintf(lastBuildDateOriginal, "Cannot found /rss/channel/lastBuildDate/text()");
+            fseek(fpOriginal, 0, SEEK_END); // seek to end of file
+            unsigned int size = ftell(fpOriginal);
+            fseek(fpOriginal, 0, SEEK_SET); // seek to head of file
+            
+            char * source = NULL;
+            source = ( char * ) malloc ( size + 1 );
+            fread ( source, size, sizeof ( char ), fpOriginal );
+            fclose ( fpOriginal );
+            source[ size ] = 0;
+            
+            SP_XmlDomParser parser;
+            parser.append( source, strlen( source ) );
+            free( source );
+            
+            SP_XmlHandle rootHandle( parser.getDocument()->getRootElement() );
+            
+            SP_XmlCDataNode * lastBuildDateNode = rootHandle.getChild( "channel" ).getChild( "lastBuildDate" ).getChild(0).toCData();
+            if( NULL != lastBuildDateNode )
+            {
+                printf( "Find /rss/channel/lastBuildDate/text()\n");
+                printf("text() of lastBuildDate: %s\n", lastBuildDateNode->getText());
+                strcpy(lastBuildDateOriginal, lastBuildDateNode->getText());
+            } else {
+                printf( "Cannot found /rss/channel/lastBuildDate/text()\n" );
+                sprintf(lastBuildDateOriginal, "Cannot found /rss/channel/lastBuildDate/text()");
+            }
         }
     }
-    }
     printf("\nlastBuildDate (original): %s\n", lastBuildDateOriginal);
     
     // Download RSS
     GetFile(rssPath, rssUrl);
     
-    #if 0
     // Obtain current lastBuildDate 
     char lastBuildDateCurrent[128] = {0};
     char mp3Url[256] = {0};
     char mp3Length[32] = {0};
     {
-        XMLDocument docCurrent;
-        if(XML_SUCCESS != docCurrent.LoadFile(rssPath))
-        {
+        FILE * fpCurrent = fopen ( rssPath, "r" );
+        if( NULL == fpCurrent ) {
             fsusb30s = 1; // HSD2
             error("No current english.xml in USB memory.\n");
         }
-        
-        XMLElement* lastBuildDateCurrentElement = docCurrent.FirstChildElement("rss")->FirstChildElement("channel")->FirstChildElement("lastBuildDate");
-        if(NULL == lastBuildDateCurrentElement)
+        else
         {
-            fsusb30s = 1; // HSD2
-            error("No \"lastBuildDate\" element in current RSS.\n");
+            fseek(fpCurrent, 0, SEEK_END); // seek to end of file
+            unsigned int size = ftell(fpCurrent);
+            fseek(fpCurrent, 0, SEEK_SET); // seek to head of file
+            
+            char * source = NULL;
+            source = ( char * ) malloc ( size + 1 );
+            fread ( source, size, sizeof ( char ), fpCurrent );
+            fclose ( fpCurrent );
+            source[ size ] = 0;
+            
+            SP_XmlDomParser parser;
+            parser.append( source, strlen( source ) );
+            free( source );
+            
+            SP_XmlHandle rootHandle( parser.getDocument()->getRootElement() );
+            
+            SP_XmlCDataNode * lastBuildDateNode = rootHandle.getChild( "channel" ).getChild( "lastBuildDate" ).getChild(0).toCData();
+            if( NULL != lastBuildDateNode )
+            {
+                printf( "Find /rss/channel/lastBuildDate/text()\n");
+                printf("text() of lastBuildDate: %s\n", lastBuildDateNode->getText());
+                strcpy(lastBuildDateCurrent, lastBuildDateNode->getText());
+            } else {
+                fsusb30s = 1; // HSD2
+                error("No \"lastBuildDate\" element in current RSS.\n");
+            }
+            
+            SP_XmlElementNode * enclosureNode = rootHandle.getChild( "channel" ).getChild( "item" ).getChild("enclosure").toElement();
+            if( NULL != enclosureNode )
+            {
+                strcpy(mp3Url, enclosureNode->getAttrValue("url"));
+                strcpy(mp3Length, enclosureNode->getAttrValue("length"));
+            } else {
+                fsusb30s = 1; // HSD2
+                error("No \"lastBuildDate\" element in current RSS.\n");
+            }
+            
         }
-        strcpy(lastBuildDateCurrent, lastBuildDateCurrentElement->GetText());
-        
-        XMLElement* enclosureElement = docCurrent.FirstChildElement("rss")->FirstChildElement("channel")->FirstChildElement("item")->FirstChildElement("enclosure");
-        if(NULL == enclosureElement)
-        {
-            fsusb30s = 1; // HSD2
-            error("No \"enclosure\" element in current RSS.\n");
-        }
-        strcpy(mp3Url, enclosureElement->Attribute("url"));
-        strcpy(mp3Length, enclosureElement->Attribute("length"));
     }
     printf("\nlastBuildDate (current) : %s\n", lastBuildDateCurrent);
     
@@ -148,7 +168,6 @@
     {
         GetFile(mp3Path, mp3Url);
     }
-    #endif
     
     // Wait for the completion of writing to USB Mass Storage Device.
     wait(1);