00001
00002
00003
00004
00005
00006
00007
00009
00010 #include <QTextStream>
00011 #include <QDir>
00012 #include <QFile>
00013 #include <QRegExp>
00014 #include <QBuffer>
00015 #include <QEventLoop>
00016 #include <QImage>
00017
00018 #include "internetContent.h"
00019
00020 #include "mythcorecontext.h"
00021 #include "mythmiscutil.h"
00022 #include "mythsystem.h"
00023 #include "mythdirs.h"
00024
00025 #include "rssparse.h"
00026 #include "netutils.h"
00027 #include "netgrabbermanager.h"
00028
00030
00032
00033 InternetContent::InternetContent( const QString &sSharePath)
00034 : HttpServerExtension( "InternetContent", sSharePath)
00035 {
00036 }
00037
00039
00041
00042 InternetContent::~InternetContent()
00043 {
00044 }
00045
00047
00049
00050 QStringList InternetContent::GetBasePaths()
00051 {
00052 return QStringList( "/InternetContent" );
00053 }
00054
00056
00058
00059 bool InternetContent::ProcessRequest( HTTPRequest *pRequest )
00060 {
00061 try
00062 {
00063 if (pRequest)
00064 {
00065 if (pRequest->m_sBaseUrl != "/InternetContent")
00066 return false;
00067
00068 LOG(VB_UPNP, LOG_INFO,
00069 QString("InternetContent::ProcessRequest: %1 : %2")
00070 .arg(pRequest->m_sMethod)
00071 .arg(pRequest->m_sRawRequest));
00072
00073
00074
00075 if (pRequest->m_sMethod == "GetInternetSearch")
00076 {
00077 GetInternetSearch( pRequest );
00078 return true;
00079 }
00080
00081
00082
00083 if (pRequest->m_sMethod == "GetInternetSources")
00084 {
00085 GetInternetSources( pRequest );
00086 return true;
00087 }
00088
00089
00090
00091 if (pRequest->m_sMethod == "GetInternetContent")
00092 {
00093 GetInternetContent( pRequest );
00094 return true;
00095 }
00096 }
00097 }
00098 catch( ... )
00099 {
00100 LOG(VB_GENERAL, LOG_ERR,
00101 "InternetContent::ProcessRequest() - Unexpected Exception" );
00102 }
00103
00104 return false;
00105 }
00106
00107
00108
00109
00110
00112
00114
00115 void InternetContent::GetInternetSearch( HTTPRequest *pRequest )
00116 {
00117 pRequest->m_eResponseType = ResponseTypeHTML;
00118
00119 QString grabber = pRequest->m_mapParams[ "Grabber" ];
00120 QString query = pRequest->m_mapParams[ "Query" ];
00121 QString page = pRequest->m_mapParams[ "Page" ];
00122
00123 if (grabber.isEmpty() || query.isEmpty() || page.isEmpty())
00124 return;
00125
00126 uint pagenum = page.toUInt();
00127 QString command = QString("%1internetcontent/%2").arg(GetShareDir())
00128 .arg(grabber);
00129
00130 if (!QFile::exists(command))
00131 {
00132 pRequest->FormatRawResponse( QString("<HTML>Grabber %1 does "
00133 "not exist!</HTML>").arg(command) );
00134 return;
00135 }
00136
00137 LOG(VB_GENERAL, LOG_INFO,
00138 QString("InternetContent::GetInternetSearch Executing "
00139 "Command: %1 -p %2 -S '%3'")
00140 .arg(command).arg(pagenum).arg(query));
00141
00142 Search *search = new Search();
00143 QEventLoop loop;
00144
00145 QObject::connect(search, SIGNAL(finishedSearch(Search *)),
00146 &loop, SLOT(quit(void)));
00147 QObject::connect(search, SIGNAL(searchTimedOut(Search *)),
00148 &loop, SLOT(quit(void)));
00149
00150 search->executeSearch(command, query, pagenum);
00151 loop.exec();
00152
00153 search->process();
00154
00155 QDomDocument ret;
00156 ret.setContent(search->GetData());
00157
00158 delete search;
00159
00160 if (ret.isNull())
00161 return;
00162
00163 pRequest->FormatRawResponse( ret.toString() );
00164 }
00165
00167
00169
00170 void InternetContent::GetInternetSources( HTTPRequest *pRequest )
00171 {
00172 pRequest->m_eResponseType = ResponseTypeHTML;
00173
00174 QString ret;
00175 QString GrabberDir = QString("%1/internetcontent/").arg(GetShareDir());
00176 QDir GrabberPath(GrabberDir);
00177 QStringList Grabbers = GrabberPath.entryList(QDir::Files | QDir::Executable);
00178
00179 for (QStringList::const_iterator i = Grabbers.begin();
00180 i != Grabbers.end(); ++i)
00181 {
00182 QString commandline = GrabberDir + (*i);
00183 MythSystem scriptcheck(commandline, QStringList("-v"),
00184 kMSRunShell | kMSStdOut | kMSBuffered);
00185 scriptcheck.Run();
00186 scriptcheck.Wait();
00187 QByteArray result = scriptcheck.ReadAll();
00188
00189 if (!result.isEmpty() && result.toLower().startsWith("<grabber>"))
00190 ret += result;
00191 }
00192
00193 NameValues list;
00194
00195 list.push_back( NameValue( "InternetContent", ret ));
00196
00197 pRequest->FormatActionResponse( list );
00198 }
00199
00201
00203
00204 void InternetContent::GetInternetContent( HTTPRequest *pRequest )
00205 {
00206 pRequest->m_eResponseType = ResponseTypeHTML;
00207
00208 QString grabber = pRequest->m_mapParams[ "Grabber" ];
00209
00210 if (grabber.isEmpty())
00211 return;
00212
00213 QString contentDir = QString("%1internetcontent/").arg(GetShareDir());
00214 QString htmlFile(contentDir + grabber);
00215
00216
00217 QFileInfo fileInfo(htmlFile);
00218 if (fileInfo.canonicalFilePath().startsWith(contentDir) &&
00219 QFile::exists( htmlFile ))
00220 {
00221 pRequest->m_eResponseType = ResponseTypeFile;
00222 pRequest->m_nResponseStatus = 200;
00223 pRequest->m_sFileName = htmlFile;
00224 }
00225 else
00226 {
00227 pRequest->FormatRawResponse( QString("<HTML>File %1 does "
00228 "not exist!</HTML>").arg(htmlFile) );
00229 }
00230 }
00231
00232