Ticket #11195: 11195-v1.patch

File 11195-v1.patch, 8.2 KB (added by danielk, 11 years ago)

Eliminates use of temporary file in DataDirectProcessor::DDPost().

  • mythtv/libs/libmythtv/datadirect.cpp

    diff --git a/mythtv/libs/libmythtv/datadirect.cpp b/mythtv/libs/libmythtv/datadirect.cpp
    index 1870ff0..1bc12d4 100644
    a b  
    2525#include "exitcodes.h"
    2626#include "mythdownloadmanager.h"
    2727#include "mythtvexp.h"
     28#include "mythtimer.h"
    2829
    2930#define LOC QString("DataDirect: ")
    3031
    void DataDirectProcessor::authenticationCallback(QNetworkReply *reply, 
    976977    auth->setPassword(GetPassword());
    977978}
    978979
    979 bool DataDirectProcessor::DDPost(QString    ddurl,        QString   &inputFile,
    980                                  QDateTime  pstartDate,   QDateTime  pendDate,
    981                                  QString   &err_txt)
     980QByteArray DataDirectProcessor::DDPost(
     981    const QString &url,
     982    const QDateTime &startDate, const QDateTime &endDate,
     983    QString &err_txt)
    982984{
    983     if (!inputFile.isEmpty() && QFile(inputFile).exists())
    984     {
    985         return true;
    986     }
     985    QString startdatestr = startDate.toString(Qt::ISODate);
     986    if (startdatestr.right(1) != "Z")
     987        startdatestr += "Z"; // Z is only appended in some versions of Qt
     988
     989    QString enddatestr = endDate.toString(Qt::ISODate);
     990    if (enddatestr.right(1) != "Z")
     991        enddatestr += "Z";
    987992
    988     QString startdatestr = pstartDate.toString(Qt::ISODate) + "Z";
    989     QString enddatestr = pendDate.toString(Qt::ISODate) + "Z";
    990993    QByteArray postdata;
    991994    postdata  = "<?xml version='1.0' encoding='utf-8'?>\n";
    992995    postdata += "<SOAP-ENV:Envelope\n";
    bool DataDirectProcessor::DDPost(QString ddurl, QString &inputFile, 
    10061009    postdata += "</SOAP-ENV:Body>\n";
    10071010    postdata += "</SOAP-ENV:Envelope>\n";
    10081011
    1009     if (inputFile.isEmpty()) {
    1010         inputFile = QString("/tmp/mythtv_ddp_data");
    1011     }
    1012 
    10131012    QHash<QByteArray, QByteArray> headers;
    10141013    headers.insert("Accept-Encoding", "gzip");
    10151014    headers.insert("Content-Type", "application/soap+xml; charset=utf-8");
    10161015
    1017     LOG(VB_GENERAL, LOG_INFO, "Downloading DataDirect feed");
     1016    LOG(VB_GENERAL, LOG_INFO,
     1017        "Downloading DataDirect feed, this can take several minutes");
    10181018
    10191019    MythDownloadManager *manager = GetMythDownloadManager();
    10201020
    1021     if (!manager->postAuth(ddurl, &postdata, &::authenticationCallback, this,
    1022                            &headers))
     1021    MythTimer downloadTimer;
     1022    downloadTimer.start();
     1023
     1024    if (!manager->postAuth(
     1025            url, &postdata, &::authenticationCallback, this, &headers))
    10231026    {
    1024         err_txt = QString("Download error");
    1025         return false;
     1027        err_txt = "Download error";
     1028        return QByteArray();
    10261029    }
    10271030
    1028     LOG(VB_GENERAL, LOG_INFO, QString("Downloaded %1 bytes")
    1029         .arg(postdata.size()));
    1030 
    1031     LOG(VB_GENERAL, LOG_INFO, "Uncompressing DataDirect feed");
     1031    float downloadTime = downloadTimer.elapsed() * 0.001;
    10321032
    10331033    QByteArray uncompressed = gUncompress(postdata);
    10341034
    1035     LOG(VB_GENERAL, LOG_INFO, QString("Uncompressed to %1 bytes")
    1036         .arg(uncompressed.size()));
    1037 
    1038     if (uncompressed.size() == 0)
    1039         uncompressed = postdata;
    1040 
    1041     QFile file(inputFile);
    1042     file.open(QIODevice::WriteOnly);
    1043     file.write(uncompressed);
    1044     file.close();
     1035    LOG(VB_GENERAL, LOG_INFO,
     1036        QString("Downloaded %1 megabytes in %2 seconds, "
     1037                "decompressed to %3 megabytes")
     1038        .arg(postdata.size()/(1024.0*1024.0)).arg(downloadTime)
     1039        .arg(uncompressed.size()/(1024.0*1024.0)));
    10451040
    10461041    if (uncompressed.size() == 0)
    10471042    {
    1048         err_txt = QString("Error uncompressing data");
    1049         return false;
     1043        err_txt = "Error uncompressing data";
     1044        return QByteArray();
    10501045    }
    10511046
    1052     return true;
     1047    return uncompressed;
    10531048}
    10541049
    10551050bool DataDirectProcessor::GrabNextSuggestedTime(void)
    bool DataDirectProcessor::GrabNextSuggestedTime(void) 
    11481143    return nextSuggestedTime.isValid();
    11491144}
    11501145
    1151 bool DataDirectProcessor::GrabData(const QDateTime &pstartDate,
    1152                                    const QDateTime &pendDate)
     1146bool DataDirectProcessor::GrabData(
     1147    const QDateTime &startDate, const QDateTime &endDate)
    11531148{
    1154     QString msg = (pstartDate.addSecs(1) == pendDate) ? "channel" : "listing";
    1155     LOG(VB_GENERAL, LOG_INFO, LOC + "Grabbing " + msg + " data");
     1149    LOG(VB_GENERAL, LOG_INFO, LOC +
     1150        ((startDate.addSecs(1) == endDate) ?
     1151         "Grabbing channel data" : "Grabbing listing data"));
    11561152
    1157     QString err = "";
    11581153    QString ddurl = m_providers[m_listingsProvider].webServiceURL;
    11591154    QString inputfile = m_inputFilename;
    1160     QString cache_dd_data = QString::null;
    11611155
    11621156    if (m_cacheData)
    11631157    {
    11641158        QByteArray userid = GetUserID().toAscii();
    1165         cache_dd_data = m_tmpDir +
     1159        QString cache_dd_data = m_tmpDir +
    11661160            QString("/mythtv_dd_cache_%1_%2_UTC_%3_to_%4")
    11671161            .arg(GetListingsProvider())
    11681162            .arg(userid.constData())
    1169             .arg(MythDate::toString(pstartDate, MythDate::kFilename))
    1170             .arg(MythDate::toString(pendDate, MythDate::kFilename));
     1163            .arg(MythDate::toString(startDate, MythDate::kFilename))
     1164            .arg(MythDate::toString(endDate, MythDate::kFilename));
    11711165
    11721166        if (QFile(cache_dd_data).exists() && m_inputFilename.isEmpty())
    11731167        {
    1174             LOG(VB_GENERAL, LOG_INFO, LOC + "Using DD cache");
     1168            LOG(VB_GENERAL, LOG_INFO, LOC + "Using cached DataDirect data");
    11751169        }
    11761170
    11771171        if (m_inputFilename.isEmpty())
    11781172            inputfile = cache_dd_data;
    11791173    }
    11801174
    1181     if (!DDPost(ddurl, inputfile, pstartDate, pendDate, err))
     1175    QByteArray data;
     1176    if (!inputfile.isEmpty())
    11821177    {
    1183         LOG(VB_GENERAL, LOG_ERR, LOC + QString("Failed to get data: %1")
    1184                 .arg(err));
    1185         return false;
     1178        QFile file(inputfile);
     1179        if (file.open(QIODevice::ReadOnly))
     1180            data = file.readAll();
    11861181    }
    11871182
    1188     QFile file(inputfile);
    1189     file.open(QIODevice::ReadOnly);
    1190     QByteArray data = file.readAll();
    1191     file.close();
     1183    if (data.isEmpty())
     1184    {
     1185        QString err;
     1186
     1187        data = DDPost(ddurl, startDate, endDate, err);
     1188
     1189        if (!err.isEmpty())
     1190        {
     1191            LOG(VB_GENERAL, LOG_ERR, LOC +
     1192                QString("Failed to get data: %1").arg(err));
     1193            return false;
     1194        }
     1195        else if (!inputfile.isEmpty())
     1196        {
     1197            QFile file(inputfile);
     1198            if (file.open(QIODevice::WriteOnly))
     1199                file.write(data);
     1200        }
     1201    }
    11921202
    11931203    if (data.isEmpty())
    11941204    {
    bool DataDirectProcessor::GrabData(const QDateTime &pstartDate, 
    11961206        return false;
    11971207    }
    11981208
    1199     bool ok = true;
     1209    QXmlInputSource xmlsource;
     1210    xmlsource.setData(data);
    12001211
    1201     DDStructureParser ddhandler(*this);
    1202     QXmlInputSource  xmlsource;
    12031212    QXmlSimpleReader xmlsimplereader;
    1204 
    1205     xmlsource.setData(data);
     1213    DDStructureParser ddhandler(*this);
    12061214    xmlsimplereader.setContentHandler(&ddhandler);
     1215
     1216    LOG(VB_GENERAL, LOG_INFO, LOC +
     1217        "Parsing DataDirect XML, this can take a number of minutes");
     1218
    12071219    if (!xmlsimplereader.parse(xmlsource))
    12081220    {
    12091221        LOG(VB_GENERAL, LOG_ERR, LOC +
    1210             "DataDirect XML failed to properly parse, downloaded listings "
    1211             "were probably corrupt.");
    1212         ok = false;
     1222            "Failed to properly parse DataDirect XML");
     1223        return false;
     1224    }
     1225    else
     1226    {
     1227        LOG(VB_GENERAL, LOG_INFO, LOC + "Parsing DataDirect XML complete");
     1228        return true;
    12131229    }
    1214 
    1215     return ok;
    12161230}
    12171231
    12181232bool DataDirectProcessor::GrabLineupsOnly(void)
  • mythtv/libs/libmythtv/datadirect.h

    diff --git a/mythtv/libs/libmythtv/datadirect.h b/mythtv/libs/libmythtv/datadirect.h
    index 1cc84a1..b945fe7 100644
    a b class MTV_PUBLIC DataDirectProcessor 
    414414    static bool Post(QString url, const PostList &list, QString documentFile,
    415415                     QString inCookieFile, QString outCookieFile);
    416416
    417     bool DDPost(QString    url,          QString   &inputFilename,
    418                        QDateTime  pstartDate,   QDateTime  pendDate,
    419                        QString   &err_txt);
     417    QByteArray DDPost(
     418        const QString &url,
     419        const QDateTime &startDate, const QDateTime &endDate,
     420        QString &err_txt);
    420421
    421422
    422423  private: