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/

Committer:
togayan
Date:
Thu Sep 06 14:38:33 2012 +0000
Revision:
8:a9541e8897f5
Parent:
7:ad9fcf0e1bc5
Using external library spxml.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
togayan 0:1855a008f28e 1 #include "mbed.h"
togayan 0:1855a008f28e 2 #include "MSCFileSystem.h"
togayan 0:1855a008f28e 3 #include "EthernetInterface.h"
togayan 0:1855a008f28e 4 #include "HTTPClient.h"
togayan 0:1855a008f28e 5 #include "HTTPFile.h"
togayan 0:1855a008f28e 6 #include "BlinkLed.h"
togayan 4:7dae52cf560f 7 #include "spdomparser.hpp"
togayan 4:7dae52cf560f 8 #include "spxmlnode.hpp"
togayan 4:7dae52cf560f 9 #include "spxmlhandle.hpp"
togayan 0:1855a008f28e 10
togayan 0:1855a008f28e 11 int GetFile(const char *path, const char *url);
togayan 0:1855a008f28e 12
togayan 0:1855a008f28e 13 EthernetInterface eth;
togayan 0:1855a008f28e 14 HTTPClient http;
togayan 0:1855a008f28e 15 MSCFileSystem usb("usb");
togayan 8:a9541e8897f5 16 BlinkLed led3(LED3, 0.02);
togayan 8:a9541e8897f5 17 BlinkLed led4(LED4, 0.2);
togayan 3:07562878d3c3 18 BlinkLed ethGreen(p26, 0.02);
togayan 3:07562878d3c3 19 BlinkLed ethYellow(p25, 0.2);
togayan 0:1855a008f28e 20 DigitalOut fsusb30s(p9);
togayan 0:1855a008f28e 21
togayan 0:1855a008f28e 22 const char* rssUrl = "http://www3.nhk.or.jp/rj/podcast/rss/english.xml";
togayan 0:1855a008f28e 23 const char* rssPath = "/usb/english.xml";
togayan 0:1855a008f28e 24 const char* mp3Path = "/usb/english.mp3";
togayan 0:1855a008f28e 25
togayan 0:1855a008f28e 26 int main()
togayan 0:1855a008f28e 27 {
togayan 5:e87211ff9c23 28 printf("\n\n===========================================\n");
togayan 5:e87211ff9c23 29 printf("mpod NHK English news Downloader with spxml\n");
togayan 5:e87211ff9c23 30 printf("===========================================\n\n");
togayan 0:1855a008f28e 31
togayan 7:ad9fcf0e1bc5 32 // Indicate downloading
togayan 7:ad9fcf0e1bc5 33 led4.startBlink();
togayan 7:ad9fcf0e1bc5 34 ethYellow.startBlink();
togayan 7:ad9fcf0e1bc5 35
togayan 0:1855a008f28e 36 // FSUSB30 switches to HSD1 (mbed)
togayan 1:1637e625b21b 37 printf("USB host was switched to HSD1(mbed).\n\n");
togayan 1:1637e625b21b 38 fsusb30s = 0; // HSD1
togayan 0:1855a008f28e 39
togayan 0:1855a008f28e 40 // Network setup
togayan 0:1855a008f28e 41 printf("Setup EtherNet with DHCP.\n");
togayan 0:1855a008f28e 42 eth.init(); //Use DHCP
togayan 0:1855a008f28e 43 eth.connect();
togayan 8:a9541e8897f5 44 printf("IP Address is %s\n\n", eth.getIPAddress());
togayan 0:1855a008f28e 45
togayan 1:1637e625b21b 46 // Obtain original lastBuildDate
togayan 3:07562878d3c3 47 char lastBuildDateOriginal[128] = {0};
togayan 0:1855a008f28e 48 {
togayan 5:e87211ff9c23 49 FILE * fpOriginal = fopen ( rssPath, "r" );
togayan 5:e87211ff9c23 50 if( NULL == fpOriginal ) {
togayan 5:e87211ff9c23 51 printf( "cannot not open %s\n", rssPath );
togayan 5:e87211ff9c23 52 sprintf(lastBuildDateOriginal, "cannot not open original %s", rssPath );
togayan 5:e87211ff9c23 53 }
togayan 5:e87211ff9c23 54 else
togayan 2:0da3a4508b46 55 {
togayan 5:e87211ff9c23 56 fseek(fpOriginal, 0, SEEK_END); // seek to end of file
togayan 5:e87211ff9c23 57 unsigned int size = ftell(fpOriginal);
togayan 5:e87211ff9c23 58 fseek(fpOriginal, 0, SEEK_SET); // seek to head of file
togayan 5:e87211ff9c23 59
togayan 5:e87211ff9c23 60 char * source = NULL;
togayan 5:e87211ff9c23 61 source = ( char * ) malloc ( size + 1 );
togayan 5:e87211ff9c23 62 fread ( source, size, sizeof ( char ), fpOriginal );
togayan 5:e87211ff9c23 63 fclose ( fpOriginal );
togayan 5:e87211ff9c23 64 source[ size ] = 0;
togayan 5:e87211ff9c23 65
togayan 5:e87211ff9c23 66 SP_XmlDomParser parser;
togayan 5:e87211ff9c23 67 parser.append( source, strlen( source ) );
togayan 5:e87211ff9c23 68 free( source );
togayan 5:e87211ff9c23 69
togayan 5:e87211ff9c23 70 SP_XmlHandle rootHandle( parser.getDocument()->getRootElement() );
togayan 5:e87211ff9c23 71
togayan 5:e87211ff9c23 72 SP_XmlCDataNode * lastBuildDateNode = rootHandle.getChild( "channel" ).getChild( "lastBuildDate" ).getChild(0).toCData();
togayan 5:e87211ff9c23 73 if( NULL != lastBuildDateNode )
togayan 5:e87211ff9c23 74 {
togayan 5:e87211ff9c23 75 strcpy(lastBuildDateOriginal, lastBuildDateNode->getText());
togayan 5:e87211ff9c23 76 } else {
togayan 6:84749589b532 77 sprintf(lastBuildDateOriginal, "Cannot found /rss/channel/lastBuildDate");
togayan 5:e87211ff9c23 78 }
togayan 2:0da3a4508b46 79 }
togayan 0:1855a008f28e 80 }
togayan 3:07562878d3c3 81 printf("\nlastBuildDate (original): %s\n", lastBuildDateOriginal);
togayan 0:1855a008f28e 82
togayan 1:1637e625b21b 83 // Download RSS
togayan 0:1855a008f28e 84 GetFile(rssPath, rssUrl);
togayan 1:1637e625b21b 85
togayan 1:1637e625b21b 86 // Obtain current lastBuildDate
togayan 3:07562878d3c3 87 char lastBuildDateCurrent[128] = {0};
togayan 3:07562878d3c3 88 char mp3Url[256] = {0};
togayan 3:07562878d3c3 89 char mp3Length[32] = {0};
togayan 0:1855a008f28e 90 {
togayan 5:e87211ff9c23 91 FILE * fpCurrent = fopen ( rssPath, "r" );
togayan 5:e87211ff9c23 92 if( NULL == fpCurrent ) {
togayan 2:0da3a4508b46 93 fsusb30s = 1; // HSD2
togayan 2:0da3a4508b46 94 error("No current english.xml in USB memory.\n");
togayan 2:0da3a4508b46 95 }
togayan 5:e87211ff9c23 96 else
togayan 2:0da3a4508b46 97 {
togayan 5:e87211ff9c23 98 fseek(fpCurrent, 0, SEEK_END); // seek to end of file
togayan 5:e87211ff9c23 99 unsigned int size = ftell(fpCurrent);
togayan 5:e87211ff9c23 100 fseek(fpCurrent, 0, SEEK_SET); // seek to head of file
togayan 5:e87211ff9c23 101
togayan 5:e87211ff9c23 102 char * source = NULL;
togayan 5:e87211ff9c23 103 source = ( char * ) malloc ( size + 1 );
togayan 5:e87211ff9c23 104 fread ( source, size, sizeof ( char ), fpCurrent );
togayan 5:e87211ff9c23 105 fclose ( fpCurrent );
togayan 5:e87211ff9c23 106 source[ size ] = 0;
togayan 5:e87211ff9c23 107
togayan 5:e87211ff9c23 108 SP_XmlDomParser parser;
togayan 5:e87211ff9c23 109 parser.append( source, strlen( source ) );
togayan 5:e87211ff9c23 110 free( source );
togayan 5:e87211ff9c23 111
togayan 5:e87211ff9c23 112 SP_XmlHandle rootHandle( parser.getDocument()->getRootElement() );
togayan 5:e87211ff9c23 113
togayan 5:e87211ff9c23 114 SP_XmlCDataNode * lastBuildDateNode = rootHandle.getChild( "channel" ).getChild( "lastBuildDate" ).getChild(0).toCData();
togayan 5:e87211ff9c23 115 if( NULL != lastBuildDateNode )
togayan 5:e87211ff9c23 116 {
togayan 5:e87211ff9c23 117 strcpy(lastBuildDateCurrent, lastBuildDateNode->getText());
togayan 5:e87211ff9c23 118 } else {
togayan 5:e87211ff9c23 119 fsusb30s = 1; // HSD2
togayan 5:e87211ff9c23 120 error("No \"lastBuildDate\" element in current RSS.\n");
togayan 5:e87211ff9c23 121 }
togayan 5:e87211ff9c23 122
togayan 5:e87211ff9c23 123 SP_XmlElementNode * enclosureNode = rootHandle.getChild( "channel" ).getChild( "item" ).getChild("enclosure").toElement();
togayan 5:e87211ff9c23 124 if( NULL != enclosureNode )
togayan 5:e87211ff9c23 125 {
togayan 5:e87211ff9c23 126 strcpy(mp3Url, enclosureNode->getAttrValue("url"));
togayan 5:e87211ff9c23 127 strcpy(mp3Length, enclosureNode->getAttrValue("length"));
togayan 5:e87211ff9c23 128 } else {
togayan 5:e87211ff9c23 129 fsusb30s = 1; // HSD2
togayan 5:e87211ff9c23 130 error("No \"lastBuildDate\" element in current RSS.\n");
togayan 5:e87211ff9c23 131 }
togayan 5:e87211ff9c23 132
togayan 2:0da3a4508b46 133 }
togayan 0:1855a008f28e 134 }
togayan 3:07562878d3c3 135 printf("\nlastBuildDate (current) : %s\n", lastBuildDateCurrent);
togayan 0:1855a008f28e 136
togayan 2:0da3a4508b46 137 // Determine the necessity of downloading new MP3.
togayan 2:0da3a4508b46 138 bool flgDownloadMp3 = false;
togayan 3:07562878d3c3 139 if ( strcmp(lastBuildDateOriginal, lastBuildDateCurrent) == 0 )
togayan 0:1855a008f28e 140 {
togayan 3:07562878d3c3 141 printf("lastBuildDate (original) == lastBuildDate (current)\n");
togayan 0:1855a008f28e 142 FILE* mp3fp = fopen(mp3Path, "r"); // check an existance of english.mp3
togayan 0:1855a008f28e 143 if (mp3fp != NULL)
togayan 0:1855a008f28e 144 {
togayan 2:0da3a4508b46 145 fseek(mp3fp, 0, SEEK_END); // seek to end of file
togayan 3:07562878d3c3 146 if (ftell(mp3fp) != atol(mp3Length))
togayan 2:0da3a4508b46 147 {
togayan 3:07562878d3c3 148 printf("MP3 file size is invalid.\n");
togayan 2:0da3a4508b46 149 flgDownloadMp3 = true;
togayan 2:0da3a4508b46 150 }
togayan 0:1855a008f28e 151 fclose(mp3fp);
togayan 0:1855a008f28e 152 }
togayan 0:1855a008f28e 153 else
togayan 0:1855a008f28e 154 {
togayan 3:07562878d3c3 155 printf("However, no enlish.mp3 in USB memory\n");
togayan 2:0da3a4508b46 156 flgDownloadMp3 = true;
togayan 0:1855a008f28e 157 }
togayan 0:1855a008f28e 158 }
togayan 0:1855a008f28e 159 else
togayan 0:1855a008f28e 160 {
togayan 3:07562878d3c3 161 printf("lastBuildDate (original) != lastBuildDate (current)\n");
togayan 2:0da3a4508b46 162 flgDownloadMp3 = true;
togayan 0:1855a008f28e 163 }
togayan 0:1855a008f28e 164
togayan 2:0da3a4508b46 165 // Download new MP3
togayan 2:0da3a4508b46 166 if(flgDownloadMp3 == true)
togayan 2:0da3a4508b46 167 {
togayan 3:07562878d3c3 168 GetFile(mp3Path, mp3Url);
togayan 2:0da3a4508b46 169 }
togayan 2:0da3a4508b46 170
togayan 2:0da3a4508b46 171 // Wait for the completion of writing to USB Mass Storage Device.
togayan 2:0da3a4508b46 172 wait(1);
togayan 2:0da3a4508b46 173
togayan 1:1637e625b21b 174 // FSUSB30 switches to HSD2 (External Device)
togayan 3:07562878d3c3 175 printf("\nUSB host was switched to HSD2(External Device).\n");
togayan 1:1637e625b21b 176 fsusb30s = 1; // HSD2
togayan 0:1855a008f28e 177
togayan 7:ad9fcf0e1bc5 178 // Indicate finish downloading
togayan 7:ad9fcf0e1bc5 179 led4.finishBlink();
togayan 7:ad9fcf0e1bc5 180 ethYellow.finishBlink();
togayan 7:ad9fcf0e1bc5 181 led3.startBlink();
togayan 3:07562878d3c3 182 ethGreen.startBlink();
togayan 0:1855a008f28e 183
togayan 0:1855a008f28e 184 while(true){}
togayan 0:1855a008f28e 185 }
togayan 0:1855a008f28e 186
togayan 0:1855a008f28e 187 int GetFile(const char *path, const char *url)
togayan 0:1855a008f28e 188 {
togayan 7:ad9fcf0e1bc5 189 printf("Getting %s -> %s\n", url, path);
togayan 0:1855a008f28e 190
togayan 2:0da3a4508b46 191 HTTPFile file(path);
togayan 0:1855a008f28e 192 HTTPResult retGet = http.get(url, &file);
togayan 2:0da3a4508b46 193 if (retGet != HTTP_OK)
togayan 2:0da3a4508b46 194 {
togayan 1:1637e625b21b 195 fsusb30s = 1; // HSD2
togayan 0:1855a008f28e 196 error("Error in http.get in GetFile(): %d\n", retGet);
togayan 0:1855a008f28e 197 }
togayan 0:1855a008f28e 198 file.clear();
togayan 0:1855a008f28e 199
togayan 0:1855a008f28e 200 return (0);
togayan 0:1855a008f28e 201 }