Download NHK English news podcast automatically. This application requires mpod mother board. See also http://mbed.org/users/geodenx/notebook/mpod/

Dependencies:   BlinkLed HTTPClient EthernetInterface FatFileSystemCpp MSCFileSystem mbed-rtos mbed

Download NHK English news podcast automatically. This application requires mpod mother board. See also http://mbed.org/users/geodenx/notebook/mpod/

main.cpp

Committer:
togayan
Date:
2012-08-18
Revision:
2:0da3a4508b46
Parent:
1:1637e625b21b
Child:
3:07562878d3c3

File content as of revision 2:0da3a4508b46:

#include "mbed.h"
#include "MSCFileSystem.h"
#include "EthernetInterface.h"
#include "HTTPClient.h"
#include "HTTPFile.h"
#include "BlinkLed.h"
#include "tinyxml2.h"
#include <string>
#include <iostream>

using std::string;
using std::cout;
using std::endl;
using namespace tinyxml2;

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

EthernetInterface eth;
HTTPClient http;
MSCFileSystem usb("usb");
BlinkLed led1(LED1, 6);
BlinkLed led2(LED2, 1);
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(mbed).\n\n");
    fsusb30s = 0; // HSD1
    
    // Network setup
    printf("Setup EtherNet with DHCP.\n");
    eth.init(); //Use DHCP
    eth.connect();
    
    // Obtain original lastBuildDate
    string lastBuildDateOriginal;
    {
        XMLDocument docOriginal;
        if(XML_SUCCESS != docOriginal.LoadFile(rssPath))
        {
            lastBuildDateOriginal = "No original english.xml in USB memory";
        }
        else
        {
            XMLElement* lastBuildDateOriginalElement = docOriginal.FirstChildElement("rss")->FirstChildElement("channel")->FirstChildElement("lastBuildDate");
            if(NULL == lastBuildDateOriginalElement)
            {
                lastBuildDateOriginal = "No \"lastBuildDate\" element in original RSS";
            }
            else
            {
                lastBuildDateOriginal = lastBuildDateOriginalElement->GetText();
            }
        }
    }
    cout << endl << "lastBuildDate (original): " << lastBuildDateOriginal << endl;
    
    // Download RSS
    GetFile(rssPath, rssUrl);
    
    // Obtain current lastBuildDate 
    string lastBuildDateCurrent;
    string mp3Url;
    string mp3Length;
    {
        XMLDocument docCurrent;
        if(XML_SUCCESS != docCurrent.LoadFile(rssPath))
        {
            fsusb30s = 1; // HSD2
            error("No current english.xml in USB memory.\n");
        }
        
        XMLElement* lastBuildDateCurrentElement = docCurrent.FirstChildElement("rss")->FirstChildElement("channel")->FirstChildElement("lastBuildDate");
        if(NULL == lastBuildDateCurrentElement)
        {
            fsusb30s = 1; // HSD2
            error("No \"lastBuildDate\" element in current RSS.\n");
        }
        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");
        }
        mp3Url = enclosureElement->Attribute( "url" );
        mp3Length = enclosureElement->Attribute( "length" );
    }
    cout << endl << "lastBuildDate (current) : " << lastBuildDateCurrent << endl;
    
    // Determine the necessity of downloading new MP3.
    bool flgDownloadMp3 = false;
    if (lastBuildDateOriginal == lastBuildDateCurrent)
    {
        cout << "lastBuildDate (original) == lastBuildDate (current)" << endl;
        FILE* mp3fp = fopen(mp3Path, "r"); // check an existance of english.mp3
        if (mp3fp != NULL)
        {
            fseek(mp3fp, 0, SEEK_END); // seek to end of file
            if (ftell(mp3fp) != atol(mp3Length.c_str()))
            {
                cout << "MP3 file size is invalid." << endl;
                flgDownloadMp3 = true;
            }
            fclose(mp3fp);
        }
        else
        {
            cout << "However, no enlish.mp3 in USB memory" << endl;
            flgDownloadMp3 = true;
        }
    }
    else
    {
        cout << "lastBuildDate (original) != lastBuildDate (current)" << endl;
        flgDownloadMp3 = true;
    }
    
    // Download new MP3
    if(flgDownloadMp3 == true)
    {
        GetFile(mp3Path, mp3Url.c_str());
    }
    
    // Wait for the completion of writing to USB Mass Storage Device.
    wait(1);
    
    // FSUSB30 switches to HSD2 (External Device)
    cout << endl << "USB host was switched to HSD2(External Device)." << endl;
    fsusb30s = 1; // HSD2

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

int GetFile(const char *path, const char *url)
{
    led2.startBlink();
    cout << endl << "Getting " << url << endl;
    
    timer.stop();
    timer.reset();
    timer.start();
    
    HTTPFile file(path);
    HTTPResult retGet = http.get(url, &file);
    if (retGet != HTTP_OK)
    {
        // FSUSB30 switches to HSD2 (External Device)
        cout << "USB host was switched to HSD2(External Device)." << endl;
        fsusb30s = 1; // HSD2
        error("Error in http.get in GetFile(): %d\n", retGet);
    }
    file.clear();
    
    timer.stop();
    cout << "timer.read_ms(): " << timer.read_ms() << endl;
    
    led2.finishBlink();
    return (0);
}