Main Page | Modules | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members | Related Pages

omgscrapersplink.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2005 by Tim Sutton   *
00003  *   tim@linfiniti.com   *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 
00021 #include "omgscrapersplink.h"
00022 #include "omgwebpagefetcher.h"
00023 
00024 //QT Includes
00025 #include <QTimer>
00026 #include <QDebug>
00027 #include <QFileInfo>
00028 #include <QFile>
00029 #include <QTextStream>
00030 #include <QDomDocument>
00031 #include <QDomElement>
00032 //needed for Q_EXPORT_PLUGIN macro at the end of this file
00033 #include <QtPlugin> 
00034 OmgScraperSplink::OmgScraperSplink(QObject * parent) :
00035   QObject(parent),
00036   OmgScraperPluginInterface()
00037 {
00038   qDebug("OmgScraperSplink constructor called...");
00039 }
00040 
00041 
00042 OmgScraperSplink::~OmgScraperSplink()
00043 {
00044 }
00045 
00046 const QString  OmgScraperSplink::getName() 
00047 { 
00048   return QString("speciesLink Plugin"); 
00049 }
00050 
00051 const QString OmgScraperSplink::getLicense()
00052 {
00053   QFile myQFile( ":/splink_terms.txt" );
00054   QString myString;
00055   if ( myQFile.open( QIODevice::ReadOnly ) ) 
00056   {
00057     //now we parse the loc file, checking each line for its taxon
00058     QTextStream myStream( &myQFile );
00059     myString = myStream.readAll();
00060     myQFile.close();
00061   }
00062   else
00063   {
00064     myString="Terms and conditions document for speciesLink could not be retrieved.";
00065   }
00066   return myString; 
00067 }
00068 
00069 bool OmgScraperSplink::search(QString theTaxonName, QString theFileName)
00070 {
00071 
00072   mTaxonName = theTaxonName;
00073   mFileName = theFileName;
00074 
00075   if (mTaxonName.isEmpty())
00076   {
00077     mMessenger.emitError("Taxon name is empty!");
00078     return false;
00079   }
00080 
00081   if (mFileName.isEmpty())
00082   {
00083     mMessenger.emitError("File name is empty!");
00084     return false;
00085   }
00086 
00087   mTaxonName = theTaxonName;
00088   mFileName = theFileName;
00089   QString mySearch=theTaxonName.simplified();
00090   mySearch=mySearch.replace(" ","&species=");
00091   //QString myUrl= "http://www.google.com.br";
00092   QString myUrl= "http://splink.cria.org.br/bdworld/service?";
00093   myUrl+= "genus=";
00094   myUrl+= mySearch;
00095 
00096   qDebug ("SPlink scraper plugin URL for Search string = " + myUrl.toLocal8Bit());
00097   OmgWebPageFetcher myWebPageFetcher;
00098   connect(&myWebPageFetcher, SIGNAL(statusChanged(QString)),
00099           this, SLOT(setStatus(QString)));
00100   QString myResult = myWebPageFetcher.getPage(myUrl);
00101   qDebug("Web request Result");
00102   qDebug() << myResult;
00103   int myCount=0; 
00104   QDomDocument myDocument("mydocument");
00105   myDocument.setContent(myResult);
00106   QDomElement myTopElement = myDocument.firstChildElement("ResultSet");
00107   if (myTopElement.isNull())
00108   {
00109     qDebug("Top (ResultSet) element could not be found!");
00110     return false;
00111   }
00112   QDomElement myElement = myTopElement.firstChildElement();
00113   while(!myElement.isNull()) 
00114   {
00115     if (myElement.tagName()!="Record")
00116     {
00117       myElement = myElement.nextSiblingElement();
00118       continue;
00119     }
00120     qDebug("SPLink Parser found a Record");
00121     OmgLocality myLocality;
00122     QString myId = myElement.firstChildElement("AccessionCode").text();
00123     myLocality.setId(myId);
00124     QString myGenus = myElement.firstChildElement("Genus").text();
00125     QString mySpecies = myElement.firstChildElement("Species").text();
00126     myLocality.setLabel(myGenus + " " + mySpecies);
00127     QString myLatitude = myElement.firstChildElement("Latitude").text();
00128     myLocality.setLatitude(myLatitude.toFloat());
00129     QString myLongitude = myElement.firstChildElement("Longitude").text();
00130     myLocality.setLongitude(myLongitude.toFloat());
00131     myElement = myElement.nextSiblingElement();
00132     if (!myLocality.isValid())
00133     {
00134       continue;
00135     }
00136     mLocalityVector.push_back(myLocality);
00137     myCount++;    // count the number of matches
00138   }
00139   qDebug() <<   myCount << " records found" ;
00140   //
00141   // Now build the shapefile
00142   //
00143   QString myTextFileName = createTextFile(mFileName);
00144   if (myTextFileName.isEmpty())
00145   {
00146     mMessenger.emitFileNotWritten(mTaxonName);
00147   }
00148   else
00149   {
00150     createShapefile(mFileName);
00151     mMessenger.emitFileWritten(mFileName, myTextFileName,mTaxonName,myCount);
00152   }
00154   mLocalityVector.clear();
00155   return true;
00156 }
00157 Q_EXPORT_PLUGIN2(splink_scraper_plugin, OmgScraperSplink );

Generated on Mon Apr 28 15:09:36 2008 for openModellerDesktop by  doxygen 1.4.1-20050210