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:
Mon Aug 20 13:27:17 2012 +0000
Revision:
4:7dae52cf560f
Parent:
3:07562878d3c3
Child:
5:e87211ff9c23
1st revision

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 3:07562878d3c3 16 BlinkLed led1(LED1, 0.02);
togayan 3:07562878d3c3 17 BlinkLed led2(LED2, 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 Timer timer;
togayan 0:1855a008f28e 22
togayan 0:1855a008f28e 23 const char* rssUrl = "http://www3.nhk.or.jp/rj/podcast/rss/english.xml";
togayan 0:1855a008f28e 24 const char* rssPath = "/usb/english.xml";
togayan 0:1855a008f28e 25 const char* mp3Path = "/usb/english.mp3";
togayan 0:1855a008f28e 26
togayan 0:1855a008f28e 27 int main()
togayan 0:1855a008f28e 28 {
togayan 0:1855a008f28e 29 printf("\n\n================================\n");
togayan 0:1855a008f28e 30 printf("mpod NHK English news Downloader\n");
togayan 0:1855a008f28e 31 printf("================================\n\n");
togayan 0:1855a008f28e 32
togayan 0:1855a008f28e 33 // FSUSB30 switches to HSD1 (mbed)
togayan 1:1637e625b21b 34 printf("USB host was switched to HSD1(mbed).\n\n");
togayan 1:1637e625b21b 35 fsusb30s = 0; // HSD1
togayan 0:1855a008f28e 36
togayan 0:1855a008f28e 37 // Network setup
togayan 0:1855a008f28e 38 printf("Setup EtherNet with DHCP.\n");
togayan 0:1855a008f28e 39 eth.init(); //Use DHCP
togayan 0:1855a008f28e 40 eth.connect();
togayan 0:1855a008f28e 41
togayan 1:1637e625b21b 42 // Obtain original lastBuildDate
togayan 4:7dae52cf560f 43
togayan 3:07562878d3c3 44 char lastBuildDateOriginal[128] = {0};
togayan 0:1855a008f28e 45 {
togayan 4:7dae52cf560f 46 FILE * fp = fopen ( rssPath, "r" );
togayan 4:7dae52cf560f 47 if( NULL == fp ) {
togayan 4:7dae52cf560f 48 printf( "cannot not open %s\n", rssPath );
togayan 4:7dae52cf560f 49 sprintf(lastBuildDateOriginal, "cannot not open original %s", rssPath );
togayan 4:7dae52cf560f 50 }
togayan 4:7dae52cf560f 51 else
togayan 4:7dae52cf560f 52 {
togayan 4:7dae52cf560f 53 fseek(fp, 0, SEEK_END); // seek to end of file
togayan 4:7dae52cf560f 54 unsigned int size = ftell(fp);
togayan 4:7dae52cf560f 55 fseek(fp, 0, SEEK_SET); // seek to head of file
togayan 4:7dae52cf560f 56
togayan 4:7dae52cf560f 57 char * source = NULL;
togayan 4:7dae52cf560f 58 source = ( char * ) malloc ( size + 1 );
togayan 4:7dae52cf560f 59 fread ( source, size, sizeof ( char ), fp );
togayan 4:7dae52cf560f 60 fclose ( fp );
togayan 4:7dae52cf560f 61 source[ size ] = 0;
togayan 4:7dae52cf560f 62
togayan 4:7dae52cf560f 63 SP_XmlDomParser parser;
togayan 4:7dae52cf560f 64 parser.append( source, strlen( source ) );
togayan 4:7dae52cf560f 65 free( source );
togayan 4:7dae52cf560f 66
togayan 4:7dae52cf560f 67 SP_XmlHandle rootHandle( parser.getDocument()->getRootElement() );
togayan 4:7dae52cf560f 68
togayan 4:7dae52cf560f 69 SP_XmlCDataNode * lastBuildDateNode = rootHandle.getChild( "channel" ).getChild( "lastBuildDate" ).getChild(0).toCData();
togayan 4:7dae52cf560f 70 if( NULL != lastBuildDateNode )
togayan 2:0da3a4508b46 71 {
togayan 4:7dae52cf560f 72 printf( "Find /rss/channel/lastBuildDate/text()\n");
togayan 4:7dae52cf560f 73 printf("text() of lastBuildDate: %s\n", lastBuildDateNode->getText());
togayan 4:7dae52cf560f 74 strcpy(lastBuildDateOriginal, lastBuildDateNode->getText());
togayan 4:7dae52cf560f 75 } else {
togayan 4:7dae52cf560f 76 printf( "Cannot found /rss/channel/lastBuildDate/text()\n" );
togayan 4:7dae52cf560f 77 sprintf(lastBuildDateOriginal, "Cannot found /rss/channel/lastBuildDate/text()");
togayan 2:0da3a4508b46 78 }
togayan 0:1855a008f28e 79 }
togayan 4:7dae52cf560f 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 4:7dae52cf560f 86 #if 0
togayan 1:1637e625b21b 87 // Obtain current lastBuildDate
togayan 3:07562878d3c3 88 char lastBuildDateCurrent[128] = {0};
togayan 3:07562878d3c3 89 char mp3Url[256] = {0};
togayan 3:07562878d3c3 90 char mp3Length[32] = {0};
togayan 0:1855a008f28e 91 {
togayan 2:0da3a4508b46 92 XMLDocument docCurrent;
togayan 2:0da3a4508b46 93 if(XML_SUCCESS != docCurrent.LoadFile(rssPath))
togayan 2:0da3a4508b46 94 {
togayan 2:0da3a4508b46 95 fsusb30s = 1; // HSD2
togayan 2:0da3a4508b46 96 error("No current english.xml in USB memory.\n");
togayan 2:0da3a4508b46 97 }
togayan 2:0da3a4508b46 98
togayan 2:0da3a4508b46 99 XMLElement* lastBuildDateCurrentElement = docCurrent.FirstChildElement("rss")->FirstChildElement("channel")->FirstChildElement("lastBuildDate");
togayan 2:0da3a4508b46 100 if(NULL == lastBuildDateCurrentElement)
togayan 2:0da3a4508b46 101 {
togayan 2:0da3a4508b46 102 fsusb30s = 1; // HSD2
togayan 2:0da3a4508b46 103 error("No \"lastBuildDate\" element in current RSS.\n");
togayan 2:0da3a4508b46 104 }
togayan 3:07562878d3c3 105 strcpy(lastBuildDateCurrent, lastBuildDateCurrentElement->GetText());
togayan 2:0da3a4508b46 106
togayan 2:0da3a4508b46 107 XMLElement* enclosureElement = docCurrent.FirstChildElement("rss")->FirstChildElement("channel")->FirstChildElement("item")->FirstChildElement("enclosure");
togayan 2:0da3a4508b46 108 if(NULL == enclosureElement)
togayan 2:0da3a4508b46 109 {
togayan 2:0da3a4508b46 110 fsusb30s = 1; // HSD2
togayan 2:0da3a4508b46 111 error("No \"enclosure\" element in current RSS.\n");
togayan 2:0da3a4508b46 112 }
togayan 3:07562878d3c3 113 strcpy(mp3Url, enclosureElement->Attribute("url"));
togayan 3:07562878d3c3 114 strcpy(mp3Length, enclosureElement->Attribute("length"));
togayan 0:1855a008f28e 115 }
togayan 3:07562878d3c3 116 printf("\nlastBuildDate (current) : %s\n", lastBuildDateCurrent);
togayan 0:1855a008f28e 117
togayan 2:0da3a4508b46 118 // Determine the necessity of downloading new MP3.
togayan 2:0da3a4508b46 119 bool flgDownloadMp3 = false;
togayan 3:07562878d3c3 120 if ( strcmp(lastBuildDateOriginal, lastBuildDateCurrent) == 0 )
togayan 0:1855a008f28e 121 {
togayan 3:07562878d3c3 122 printf("lastBuildDate (original) == lastBuildDate (current)\n");
togayan 0:1855a008f28e 123 FILE* mp3fp = fopen(mp3Path, "r"); // check an existance of english.mp3
togayan 0:1855a008f28e 124 if (mp3fp != NULL)
togayan 0:1855a008f28e 125 {
togayan 2:0da3a4508b46 126 fseek(mp3fp, 0, SEEK_END); // seek to end of file
togayan 3:07562878d3c3 127 if (ftell(mp3fp) != atol(mp3Length))
togayan 2:0da3a4508b46 128 {
togayan 3:07562878d3c3 129 printf("MP3 file size is invalid.\n");
togayan 2:0da3a4508b46 130 flgDownloadMp3 = true;
togayan 2:0da3a4508b46 131 }
togayan 0:1855a008f28e 132 fclose(mp3fp);
togayan 0:1855a008f28e 133 }
togayan 0:1855a008f28e 134 else
togayan 0:1855a008f28e 135 {
togayan 3:07562878d3c3 136 printf("However, no enlish.mp3 in USB memory\n");
togayan 2:0da3a4508b46 137 flgDownloadMp3 = true;
togayan 0:1855a008f28e 138 }
togayan 0:1855a008f28e 139 }
togayan 0:1855a008f28e 140 else
togayan 0:1855a008f28e 141 {
togayan 3:07562878d3c3 142 printf("lastBuildDate (original) != lastBuildDate (current)\n");
togayan 2:0da3a4508b46 143 flgDownloadMp3 = true;
togayan 0:1855a008f28e 144 }
togayan 0:1855a008f28e 145
togayan 2:0da3a4508b46 146 // Download new MP3
togayan 2:0da3a4508b46 147 if(flgDownloadMp3 == true)
togayan 2:0da3a4508b46 148 {
togayan 3:07562878d3c3 149 GetFile(mp3Path, mp3Url);
togayan 2:0da3a4508b46 150 }
togayan 4:7dae52cf560f 151 #endif
togayan 2:0da3a4508b46 152
togayan 2:0da3a4508b46 153 // Wait for the completion of writing to USB Mass Storage Device.
togayan 2:0da3a4508b46 154 wait(1);
togayan 2:0da3a4508b46 155
togayan 1:1637e625b21b 156 // FSUSB30 switches to HSD2 (External Device)
togayan 3:07562878d3c3 157 printf("\nUSB host was switched to HSD2(External Device).\n");
togayan 1:1637e625b21b 158 fsusb30s = 1; // HSD2
togayan 0:1855a008f28e 159
togayan 0:1855a008f28e 160 // blink LED
togayan 0:1855a008f28e 161 led1.startBlink();
togayan 3:07562878d3c3 162 ethGreen.startBlink();
togayan 0:1855a008f28e 163
togayan 0:1855a008f28e 164 while(true){}
togayan 0:1855a008f28e 165 }
togayan 0:1855a008f28e 166
togayan 0:1855a008f28e 167 int GetFile(const char *path, const char *url)
togayan 0:1855a008f28e 168 {
togayan 2:0da3a4508b46 169 led2.startBlink();
togayan 3:07562878d3c3 170 ethYellow.startBlink();
togayan 3:07562878d3c3 171 printf("\nGetting %s\n", url);
togayan 0:1855a008f28e 172
togayan 0:1855a008f28e 173 timer.stop();
togayan 0:1855a008f28e 174 timer.reset();
togayan 0:1855a008f28e 175 timer.start();
togayan 0:1855a008f28e 176
togayan 2:0da3a4508b46 177 HTTPFile file(path);
togayan 0:1855a008f28e 178 HTTPResult retGet = http.get(url, &file);
togayan 2:0da3a4508b46 179 if (retGet != HTTP_OK)
togayan 2:0da3a4508b46 180 {
togayan 1:1637e625b21b 181 fsusb30s = 1; // HSD2
togayan 0:1855a008f28e 182 error("Error in http.get in GetFile(): %d\n", retGet);
togayan 0:1855a008f28e 183 }
togayan 0:1855a008f28e 184 file.clear();
togayan 0:1855a008f28e 185
togayan 0:1855a008f28e 186 timer.stop();
togayan 3:07562878d3c3 187 printf("timer.read_ms(): %d\n", timer.read_ms());
togayan 2:0da3a4508b46 188
togayan 2:0da3a4508b46 189 led2.finishBlink();
togayan 3:07562878d3c3 190 ethYellow.finishBlink();
togayan 0:1855a008f28e 191 return (0);
togayan 0:1855a008f28e 192 }