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
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/
main.cpp@7:ad9fcf0e1bc5, 2012-09-01 (annotated)
- Committer:
- togayan
- Date:
- Sat Sep 01 04:09:48 2012 +0000
- Revision:
- 7:ad9fcf0e1bc5
- Parent:
- 6:84749589b532
- Child:
- 8:a9541e8897f5
HTTPFile was changed to follow the latest HTTPClient library.; BlinkLed was isolated as a library.; Change LED port from LED1,2 to LED3,4 for the specification of pwmout.
Who changed what in which revision?
User | Revision | Line number | New 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 | 7:ad9fcf0e1bc5 | 16 | BlinkLed led3(LED1, 0.02); |
togayan | 7:ad9fcf0e1bc5 | 17 | BlinkLed led4(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 | |
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 | 0:1855a008f28e | 44 | |
togayan | 1:1637e625b21b | 45 | // Obtain original lastBuildDate |
togayan | 3:07562878d3c3 | 46 | char lastBuildDateOriginal[128] = {0}; |
togayan | 0:1855a008f28e | 47 | { |
togayan | 5:e87211ff9c23 | 48 | FILE * fpOriginal = fopen ( rssPath, "r" ); |
togayan | 5:e87211ff9c23 | 49 | if( NULL == fpOriginal ) { |
togayan | 5:e87211ff9c23 | 50 | printf( "cannot not open %s\n", rssPath ); |
togayan | 5:e87211ff9c23 | 51 | sprintf(lastBuildDateOriginal, "cannot not open original %s", rssPath ); |
togayan | 5:e87211ff9c23 | 52 | } |
togayan | 5:e87211ff9c23 | 53 | else |
togayan | 2:0da3a4508b46 | 54 | { |
togayan | 5:e87211ff9c23 | 55 | fseek(fpOriginal, 0, SEEK_END); // seek to end of file |
togayan | 5:e87211ff9c23 | 56 | unsigned int size = ftell(fpOriginal); |
togayan | 5:e87211ff9c23 | 57 | fseek(fpOriginal, 0, SEEK_SET); // seek to head of file |
togayan | 5:e87211ff9c23 | 58 | |
togayan | 5:e87211ff9c23 | 59 | char * source = NULL; |
togayan | 5:e87211ff9c23 | 60 | source = ( char * ) malloc ( size + 1 ); |
togayan | 5:e87211ff9c23 | 61 | fread ( source, size, sizeof ( char ), fpOriginal ); |
togayan | 5:e87211ff9c23 | 62 | fclose ( fpOriginal ); |
togayan | 5:e87211ff9c23 | 63 | source[ size ] = 0; |
togayan | 5:e87211ff9c23 | 64 | |
togayan | 5:e87211ff9c23 | 65 | SP_XmlDomParser parser; |
togayan | 5:e87211ff9c23 | 66 | parser.append( source, strlen( source ) ); |
togayan | 5:e87211ff9c23 | 67 | free( source ); |
togayan | 5:e87211ff9c23 | 68 | |
togayan | 5:e87211ff9c23 | 69 | SP_XmlHandle rootHandle( parser.getDocument()->getRootElement() ); |
togayan | 5:e87211ff9c23 | 70 | |
togayan | 5:e87211ff9c23 | 71 | SP_XmlCDataNode * lastBuildDateNode = rootHandle.getChild( "channel" ).getChild( "lastBuildDate" ).getChild(0).toCData(); |
togayan | 5:e87211ff9c23 | 72 | if( NULL != lastBuildDateNode ) |
togayan | 5:e87211ff9c23 | 73 | { |
togayan | 5:e87211ff9c23 | 74 | strcpy(lastBuildDateOriginal, lastBuildDateNode->getText()); |
togayan | 5:e87211ff9c23 | 75 | } else { |
togayan | 6:84749589b532 | 76 | sprintf(lastBuildDateOriginal, "Cannot found /rss/channel/lastBuildDate"); |
togayan | 5:e87211ff9c23 | 77 | } |
togayan | 2:0da3a4508b46 | 78 | } |
togayan | 0:1855a008f28e | 79 | } |
togayan | 3:07562878d3c3 | 80 | printf("\nlastBuildDate (original): %s\n", lastBuildDateOriginal); |
togayan | 0:1855a008f28e | 81 | |
togayan | 1:1637e625b21b | 82 | // Download RSS |
togayan | 0:1855a008f28e | 83 | GetFile(rssPath, rssUrl); |
togayan | 1:1637e625b21b | 84 | |
togayan | 1:1637e625b21b | 85 | // Obtain current lastBuildDate |
togayan | 3:07562878d3c3 | 86 | char lastBuildDateCurrent[128] = {0}; |
togayan | 3:07562878d3c3 | 87 | char mp3Url[256] = {0}; |
togayan | 3:07562878d3c3 | 88 | char mp3Length[32] = {0}; |
togayan | 0:1855a008f28e | 89 | { |
togayan | 5:e87211ff9c23 | 90 | FILE * fpCurrent = fopen ( rssPath, "r" ); |
togayan | 5:e87211ff9c23 | 91 | if( NULL == fpCurrent ) { |
togayan | 2:0da3a4508b46 | 92 | fsusb30s = 1; // HSD2 |
togayan | 2:0da3a4508b46 | 93 | error("No current english.xml in USB memory.\n"); |
togayan | 2:0da3a4508b46 | 94 | } |
togayan | 5:e87211ff9c23 | 95 | else |
togayan | 2:0da3a4508b46 | 96 | { |
togayan | 5:e87211ff9c23 | 97 | fseek(fpCurrent, 0, SEEK_END); // seek to end of file |
togayan | 5:e87211ff9c23 | 98 | unsigned int size = ftell(fpCurrent); |
togayan | 5:e87211ff9c23 | 99 | fseek(fpCurrent, 0, SEEK_SET); // seek to head of file |
togayan | 5:e87211ff9c23 | 100 | |
togayan | 5:e87211ff9c23 | 101 | char * source = NULL; |
togayan | 5:e87211ff9c23 | 102 | source = ( char * ) malloc ( size + 1 ); |
togayan | 5:e87211ff9c23 | 103 | fread ( source, size, sizeof ( char ), fpCurrent ); |
togayan | 5:e87211ff9c23 | 104 | fclose ( fpCurrent ); |
togayan | 5:e87211ff9c23 | 105 | source[ size ] = 0; |
togayan | 5:e87211ff9c23 | 106 | |
togayan | 5:e87211ff9c23 | 107 | SP_XmlDomParser parser; |
togayan | 5:e87211ff9c23 | 108 | parser.append( source, strlen( source ) ); |
togayan | 5:e87211ff9c23 | 109 | free( source ); |
togayan | 5:e87211ff9c23 | 110 | |
togayan | 5:e87211ff9c23 | 111 | SP_XmlHandle rootHandle( parser.getDocument()->getRootElement() ); |
togayan | 5:e87211ff9c23 | 112 | |
togayan | 5:e87211ff9c23 | 113 | SP_XmlCDataNode * lastBuildDateNode = rootHandle.getChild( "channel" ).getChild( "lastBuildDate" ).getChild(0).toCData(); |
togayan | 5:e87211ff9c23 | 114 | if( NULL != lastBuildDateNode ) |
togayan | 5:e87211ff9c23 | 115 | { |
togayan | 5:e87211ff9c23 | 116 | strcpy(lastBuildDateCurrent, lastBuildDateNode->getText()); |
togayan | 5:e87211ff9c23 | 117 | } else { |
togayan | 5:e87211ff9c23 | 118 | fsusb30s = 1; // HSD2 |
togayan | 5:e87211ff9c23 | 119 | error("No \"lastBuildDate\" element in current RSS.\n"); |
togayan | 5:e87211ff9c23 | 120 | } |
togayan | 5:e87211ff9c23 | 121 | |
togayan | 5:e87211ff9c23 | 122 | SP_XmlElementNode * enclosureNode = rootHandle.getChild( "channel" ).getChild( "item" ).getChild("enclosure").toElement(); |
togayan | 5:e87211ff9c23 | 123 | if( NULL != enclosureNode ) |
togayan | 5:e87211ff9c23 | 124 | { |
togayan | 5:e87211ff9c23 | 125 | strcpy(mp3Url, enclosureNode->getAttrValue("url")); |
togayan | 5:e87211ff9c23 | 126 | strcpy(mp3Length, enclosureNode->getAttrValue("length")); |
togayan | 5:e87211ff9c23 | 127 | } else { |
togayan | 5:e87211ff9c23 | 128 | fsusb30s = 1; // HSD2 |
togayan | 5:e87211ff9c23 | 129 | error("No \"lastBuildDate\" element in current RSS.\n"); |
togayan | 5:e87211ff9c23 | 130 | } |
togayan | 5:e87211ff9c23 | 131 | |
togayan | 2:0da3a4508b46 | 132 | } |
togayan | 0:1855a008f28e | 133 | } |
togayan | 3:07562878d3c3 | 134 | printf("\nlastBuildDate (current) : %s\n", lastBuildDateCurrent); |
togayan | 0:1855a008f28e | 135 | |
togayan | 2:0da3a4508b46 | 136 | // Determine the necessity of downloading new MP3. |
togayan | 2:0da3a4508b46 | 137 | bool flgDownloadMp3 = false; |
togayan | 3:07562878d3c3 | 138 | if ( strcmp(lastBuildDateOriginal, lastBuildDateCurrent) == 0 ) |
togayan | 0:1855a008f28e | 139 | { |
togayan | 3:07562878d3c3 | 140 | printf("lastBuildDate (original) == lastBuildDate (current)\n"); |
togayan | 0:1855a008f28e | 141 | FILE* mp3fp = fopen(mp3Path, "r"); // check an existance of english.mp3 |
togayan | 0:1855a008f28e | 142 | if (mp3fp != NULL) |
togayan | 0:1855a008f28e | 143 | { |
togayan | 2:0da3a4508b46 | 144 | fseek(mp3fp, 0, SEEK_END); // seek to end of file |
togayan | 3:07562878d3c3 | 145 | if (ftell(mp3fp) != atol(mp3Length)) |
togayan | 2:0da3a4508b46 | 146 | { |
togayan | 3:07562878d3c3 | 147 | printf("MP3 file size is invalid.\n"); |
togayan | 2:0da3a4508b46 | 148 | flgDownloadMp3 = true; |
togayan | 2:0da3a4508b46 | 149 | } |
togayan | 0:1855a008f28e | 150 | fclose(mp3fp); |
togayan | 0:1855a008f28e | 151 | } |
togayan | 0:1855a008f28e | 152 | else |
togayan | 0:1855a008f28e | 153 | { |
togayan | 3:07562878d3c3 | 154 | printf("However, no enlish.mp3 in USB memory\n"); |
togayan | 2:0da3a4508b46 | 155 | flgDownloadMp3 = true; |
togayan | 0:1855a008f28e | 156 | } |
togayan | 0:1855a008f28e | 157 | } |
togayan | 0:1855a008f28e | 158 | else |
togayan | 0:1855a008f28e | 159 | { |
togayan | 3:07562878d3c3 | 160 | printf("lastBuildDate (original) != lastBuildDate (current)\n"); |
togayan | 2:0da3a4508b46 | 161 | flgDownloadMp3 = true; |
togayan | 0:1855a008f28e | 162 | } |
togayan | 0:1855a008f28e | 163 | |
togayan | 2:0da3a4508b46 | 164 | // Download new MP3 |
togayan | 2:0da3a4508b46 | 165 | if(flgDownloadMp3 == true) |
togayan | 2:0da3a4508b46 | 166 | { |
togayan | 3:07562878d3c3 | 167 | GetFile(mp3Path, mp3Url); |
togayan | 2:0da3a4508b46 | 168 | } |
togayan | 2:0da3a4508b46 | 169 | |
togayan | 2:0da3a4508b46 | 170 | // Wait for the completion of writing to USB Mass Storage Device. |
togayan | 2:0da3a4508b46 | 171 | wait(1); |
togayan | 2:0da3a4508b46 | 172 | |
togayan | 1:1637e625b21b | 173 | // FSUSB30 switches to HSD2 (External Device) |
togayan | 3:07562878d3c3 | 174 | printf("\nUSB host was switched to HSD2(External Device).\n"); |
togayan | 1:1637e625b21b | 175 | fsusb30s = 1; // HSD2 |
togayan | 0:1855a008f28e | 176 | |
togayan | 7:ad9fcf0e1bc5 | 177 | // Indicate finish downloading |
togayan | 7:ad9fcf0e1bc5 | 178 | led4.finishBlink(); |
togayan | 7:ad9fcf0e1bc5 | 179 | ethYellow.finishBlink(); |
togayan | 7:ad9fcf0e1bc5 | 180 | led3.startBlink(); |
togayan | 3:07562878d3c3 | 181 | ethGreen.startBlink(); |
togayan | 0:1855a008f28e | 182 | |
togayan | 0:1855a008f28e | 183 | while(true){} |
togayan | 0:1855a008f28e | 184 | } |
togayan | 0:1855a008f28e | 185 | |
togayan | 0:1855a008f28e | 186 | int GetFile(const char *path, const char *url) |
togayan | 0:1855a008f28e | 187 | { |
togayan | 7:ad9fcf0e1bc5 | 188 | printf("Getting %s -> %s\n", url, path); |
togayan | 0:1855a008f28e | 189 | |
togayan | 2:0da3a4508b46 | 190 | HTTPFile file(path); |
togayan | 0:1855a008f28e | 191 | HTTPResult retGet = http.get(url, &file); |
togayan | 2:0da3a4508b46 | 192 | if (retGet != HTTP_OK) |
togayan | 2:0da3a4508b46 | 193 | { |
togayan | 1:1637e625b21b | 194 | fsusb30s = 1; // HSD2 |
togayan | 0:1855a008f28e | 195 | error("Error in http.get in GetFile(): %d\n", retGet); |
togayan | 0:1855a008f28e | 196 | } |
togayan | 0:1855a008f28e | 197 | file.clear(); |
togayan | 0:1855a008f28e | 198 | |
togayan | 0:1855a008f28e | 199 | return (0); |
togayan | 0:1855a008f28e | 200 | } |