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/

main.cpp

Committer:
togayan
Date:
2012-08-16
Revision:
0:1855a008f28e
Child:
1:1637e625b21b

File content as of revision 0:1855a008f28e:

#include "mbed.h"
#include "MSCFileSystem.h"
#include "EthernetInterface.h"
#include "HTTPClient.h"
#include "HTTPFile.h"
#include "BlinkLed.h"
#include "tinyxml2.h"

using namespace tinyxml2;

int GetFile(const char *path, const char *url);

EthernetInterface eth;
HTTPClient http;
MSCFileSystem usb("usb");
BlinkLed led1(LED1, 4);
DigitalOut fsusb30s(p9);
Timer timer;

const char* rssUrl = "http://www3.nhk.or.jp/rj/podcast/rss/english.xml";
const char* rssPath = "/usb/english.xml";
const char* mp3Path = "/usb/english.mp3";

int main()
{
    printf("\n\n================================\n");
    printf("mpod NHK English news Downloader\n");
    printf("================================\n\n");
    
    // FSUSB30 switches to HSD1 (mbed)
    printf("USB host was switched to HSD1.\n");
    fsusb30s = 0;
    
    // Network setup
    printf("Setup EtherNet with DHCP.\n");
    eth.init(); //Use DHCP
    eth.connect();
    
    
    // Obtain lastBuildDate
    const char* lastBuildDateOriginal;
    XMLDocument docOriginal;
    if(XML_SUCCESS != docOriginal.LoadFile(rssPath))
    {
        lastBuildDateOriginal = "No english.xml in USB memory";
    }
    XMLElement* lastBuildDateOriginalElement = docOriginal.FirstChildElement("rss")->FirstChildElement("channel")->FirstChildElement("lastBuildDate");
    if(NULL == lastBuildDateOriginalElement)
    {
        lastBuildDateOriginal = "No \"lastBuildDate\" element in RSS";
    }
    else
    {
        lastBuildDateOriginal = lastBuildDateOriginalElement->GetText();
    }
    printf("lastBuildDate (original): %s\n", lastBuildDateOriginal);
    
    
    // RSS download and parse
    GetFile(rssPath, rssUrl);
    const char* lastBuildDateCurrent;
    XMLDocument docCurrent;
    if(XML_SUCCESS != docCurrent.LoadFile(rssPath))
    {
        lastBuildDateCurrent = "No english.xml in USB memory";
    }
    XMLElement* lastBuildDateCurrentElement = docCurrent.FirstChildElement("rss")->FirstChildElement("channel")->FirstChildElement("lastBuildDate");
    if(NULL == lastBuildDateCurrentElement)
    {
        lastBuildDateCurrent = "No \"lastBuildDate\" element in RSS";
    }
    else
    {
        lastBuildDateCurrent = lastBuildDateCurrentElement->GetText();
    }
    printf("lastBuildDate (Current) : %s\n", lastBuildDateCurrent);
    
    
    if (strcmp(lastBuildDateOriginal, lastBuildDateCurrent) == 0)
    {
        printf("lastBuildDate (original) == lastBuildDate (current)\n");
        FILE* mp3fp = fopen(mp3Path, "r"); // check an existance of english.mp3
        if (mp3fp != NULL)
        {
            fclose(mp3fp);
        }
        else
        {
            printf("However, no enlish.mp3 in USB memory\n");
        }
    }
    else
    {
        const char* mp3Url;
        XMLElement* enclosureElement = docCurrent.FirstChildElement("rss")->FirstChildElement("channel")->FirstChildElement("item")->FirstChildElement("enclosure");
        if(NULL == enclosureElement)
        {
            while(1){};
        }
        else
        {
            mp3Url = enclosureElement->Attribute( "url" );
        }
        printf("mp3Url : %s\n", mp3Url);
        GetFile(mp3Path, mp3Url);
    }
    
    
    // Switch USB host from mbed to KWD
    printf("USB host was switched to HSD2.\n");
    fsusb30s = 1; // HSD2 (KWD)

    // blink LED
    led1.startBlink();
    
    while(true){}
}

int GetFile(const char *path, const char *url)
{
    HTTPFile file(path);
    printf("Getting %s\n", url);
    
    timer.stop();
    timer.reset();
    timer.start();
    
    HTTPResult retGet = http.get(url, &file);
    if (retGet != HTTP_OK) {
        error("Error in http.get in GetFile(): %d\n", retGet);
    }
    file.clear();
    
    timer.stop();
    printf("timer.read_ms(): %d\n", timer.read_ms());

    return (0);
}