00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "omgscrapersplink.h"
00022 #include "omgwebpagefetcher.h"
00023
00024
00025 #include <QTimer>
00026 #include <QDebug>
00027 #include <QFileInfo>
00028 #include <QFile>
00029 #include <QTextStream>
00030 #include <QDomDocument>
00031 #include <QDomElement>
00032
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
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
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++;
00138 }
00139 qDebug() << myCount << " records found" ;
00140
00141
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 );