Download picasa web albums photos 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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "MSCFileSystem.h"
00003 #include "EthernetInterface.h"
00004 #include "HTTPClient.h"
00005 #include "HTTPFile.h"
00006 #include "BlinkLed.h"
00007 #include <cstring>
00008 
00009 int GetFile(const char *path, const char *url);
00010 int summarizeRss(const char* dstPath, const char* srcPath);
00011 int removeContents(const char* url);
00012 const char* getExtensionFromUrl(const char* url);
00013 
00014 EthernetInterface eth;
00015 HTTPClient http;
00016 MSCFileSystem usb("usb");
00017 BlinkLed led3(LED3, 0.02);
00018 BlinkLed led4(LED4, 0.2);
00019 BlinkLed ethGreen(p26, 0.02);
00020 BlinkLed ethYellow(p25, 0.2);
00021 DigitalOut fsusb30s(p9);
00022 
00023 /***** Please specify "albumRssUrl" *****/
00024 const char* albumRssUrl  = "http://picasaweb.google.com/data/feed/base/user/*****/albumid/*****?alt=rss&kind=photo&authkey=*****&hl=ja";
00025 
00026 const char* albumRssPath = "/usb/album.xml";
00027 const char* summaryPath  = "/usb/summary.txt";
00028 const char* contentsFolder = "/usb/DCIM";
00029 
00030 int main()
00031 {
00032     puts("");
00033     puts("================================");
00034     puts("mpod Picasa Photo frame");
00035     puts("================================\n");
00036     
00037     // Check albumRssUrl
00038     if(NULL != strstr(albumRssUrl, "*****"))
00039     {
00040         error("[ERROR] Please specify the RSS of YOUR album to \"albumRssUrl\" in the firmware.\n");   
00041     }
00042     if(NULL != strstr(albumRssUrl, "https"))
00043     {
00044         error("[ERROR] Please specify the URL of the RSS in \"HTTP\" format.\n");
00045     }
00046     
00047     // Indicate downloading
00048     led4.startBlink();
00049     ethYellow.startBlink();
00050         
00051     // FSUSB30 switches to HSD1 (mbed)
00052     puts("USB host was switched to HSD1(mbed).\n");
00053     fsusb30s = 0; // HSD1
00054     
00055     // Network setup
00056     puts("Setup EtherNet with DHCP.");
00057     eth.init(); //Use DHCP
00058     eth.connect();
00059     printf("IP Address is %s\n\n", eth.getIPAddress());
00060     
00061     // Obtain original lastBuildDate
00062     char lastBuildDateOriginal[40] = {0};
00063     FILE* fpOriginal = fopen(summaryPath, "r");
00064     if (fpOriginal == NULL)
00065     {
00066         strcpy(lastBuildDateOriginal, "No summary.txt in USB memory");
00067     }
00068     else
00069     {
00070         if(fgets(lastBuildDateOriginal, 40, fpOriginal) == NULL)
00071         {
00072             strcpy(lastBuildDateOriginal, "No \"lastBuildDate\" in RSS");
00073         }
00074         else
00075         {
00076             lastBuildDateOriginal[strlen(lastBuildDateOriginal) - 1] = '\0';
00077         }
00078         fclose(fpOriginal);
00079     }
00080     printf("\nlastBuildDate (original): %s\n", lastBuildDateOriginal);
00081     
00082     // Download Album
00083     GetFile(albumRssPath, albumRssUrl);
00084     
00085     // Summarize RSS
00086     summarizeRss(summaryPath, albumRssPath);
00087     
00088     // Obtain current lastBuildDate
00089     char lastBuildDateCurrent[40] = {0};
00090     FILE* fpCurrent = fopen(summaryPath, "r");
00091     if (fpCurrent == NULL)
00092     {
00093         fsusb30s = 1; // HSD2
00094         error("No current summary.txt in USB memory.\n");
00095     }
00096     else
00097     {
00098         if(fgets(lastBuildDateCurrent, 40, fpCurrent) == NULL)
00099         {
00100             fsusb30s = 1; // HSD2
00101             error("No \"lastBuildDate\" element in current RSS.\n");
00102         }
00103         else
00104         {
00105             lastBuildDateCurrent[strlen(lastBuildDateCurrent) - 1] = '\0';
00106         }
00107     }
00108     printf("\nlastBuildDate (current) : %s\n", lastBuildDateCurrent);
00109     
00110     // Determine the necessity of downloading new Photos.
00111     bool flgDownloadPhoto = false;
00112     if ( strcmp(lastBuildDateOriginal, lastBuildDateCurrent) == 0 )
00113     {
00114         puts("lastBuildDate (original) == lastBuildDate (current)");
00115         if(NULL == opendir(contentsFolder)) // check an existance of DCIM folder
00116         {
00117             printf("However, no DCIM folder in USB memory\n");
00118             flgDownloadPhoto = true;
00119         }
00120         else
00121         {
00122             //Should be checked whether files exist.
00123             flgDownloadPhoto = false;
00124         }
00125     }
00126     else
00127     {
00128         puts("lastBuildDate (original) != lastBuildDate (current)");
00129         flgDownloadPhoto = true;
00130     }
00131     
00132     // Download new Photos
00133     if(flgDownloadPhoto)
00134     {
00135         if(removeContents(contentsFolder) < 0)
00136         {
00137             mkdir(contentsFolder, 0777);
00138         }
00139     
00140         char photoUrl[128] = {0};
00141         char photoPath[24] = {0};
00142         int photoNo = 1;
00143         while(fgets(photoUrl, 128, fpCurrent) != NULL)
00144         {
00145             photoUrl[strlen(photoUrl) - 1] = '\0';
00146             sprintf(photoPath, "%s/%08d.%s", contentsFolder, photoNo, getExtensionFromUrl(photoUrl));
00147             GetFile(photoPath, photoUrl);
00148             ++photoNo;
00149         }
00150     }
00151     fclose(fpCurrent);
00152     
00153     // Wait for the completion of writing to USB Mass Storage Device.
00154     wait(1);
00155     
00156     // FSUSB30 switches to HSD2 (External Device)
00157     puts("\nUSB host was switched to HSD2(External Device).\n");
00158     fsusb30s = 1; // HSD2
00159 
00160     // Indicate finish downloading
00161     led4.finishBlink();
00162     ethYellow.finishBlink();
00163     led3.startBlink();
00164     ethGreen.startBlink();
00165     
00166     while(true){}
00167 }
00168 
00169 int GetFile(const char *path, const char *url)
00170 {
00171     printf("Getting %s -> %s\n", url, path);
00172     
00173     HTTPFile file(path);
00174     HTTPResult retGet = http.get(url, &file);
00175     if (retGet != HTTP_OK)
00176     {
00177         fsusb30s = 1; // HSD2
00178         error("Error in http.get in GetFile(): %d\n", retGet);
00179     }
00180     file.clear();
00181     
00182     return (0);
00183 }
00184 
00185 int summarizeRss(const char* dstPath, const char* srcPath)
00186 {
00187     puts("Summarizing RSS.");
00188     
00189     FILE* fpSrc = fopen(srcPath, "r");
00190     if (fpSrc == NULL)
00191     {
00192         return -1;
00193     }
00194     
00195     FILE* fpDst = fopen(dstPath, "w");
00196     if (fpDst == NULL)
00197     {
00198         fclose(fpSrc);
00199         return -1;
00200     }
00201     
00202     char buff[1024] = {0};
00203     char* buffPos = buff;
00204     
00205     int present;
00206     int previous = '\0';
00207     while( (present = fgetc(fpSrc)) != EOF )
00208     {
00209         if(previous == '>' && present == '<')
00210         {
00211             if( strncmp(buff, "<lastBuildDate", 14) == 0 )
00212             {
00213                 *strchr(buff + 15, '<') = '\0';
00214                 fprintf(fpDst, "%s\n", buff + 15);
00215             }
00216             else if( strncmp(buff, "<enclosure", 10) == 0 )
00217             {
00218                 *strchr(buff + 34, '\'') = '\0';
00219                 fprintf(fpDst, "%s\n", buff + 34);
00220             }
00221             buffPos = buff;
00222         }
00223         *buffPos++ = present;
00224         previous = present;
00225     }
00226     
00227     fclose(fpDst);
00228     fclose(fpSrc);
00229     
00230     return 0;
00231 }
00232 
00233 int removeContents(const char* dirName)
00234 {
00235     if(DirHandle* dir = opendir(dirName))
00236     {
00237         int ret = 0;
00238         while(struct dirent* ent = dir->readdir())
00239         {
00240             char filename[32] = {0};
00241             sprintf(filename, "%s/%s", dirName, ent->d_name);
00242             printf("remove %s\n", filename);
00243             remove(filename);
00244             ++ret;
00245         }
00246         return ret;
00247     }
00248     else
00249     {
00250         return -1;
00251     }
00252 }
00253 
00254 const char* getExtensionFromUrl(const char* url)
00255 {
00256     const char* tail = url;
00257     while('\0' != *tail)
00258     {
00259         ++tail;
00260     }
00261     
00262     for(const char* p = tail; p >= url; --p)
00263     {
00264         if ('/' == *p)
00265         {
00266             return tail;
00267         }
00268         if ( '.' == *p )
00269         {
00270             return p+1;
00271         }
00272     }
00273     return tail;
00274 }