File indexing completed on 2024-04-28 04:55:52

0001 /*
0002     This file is part of Choqok, the KDE micro-blogging client
0003 
0004     SPDX-FileCopyrightText: 2008-2012 Mehrdad Momeny <mehrdad.momeny@gmail.com>
0005 
0006     SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0007 */
0008 
0009 #include "untiny.h"
0010 
0011 #include <QTimer>
0012 
0013 #include <KIO/MimetypeJob>
0014 #include <KPluginFactory>
0015 
0016 #include "choqokuiglobal.h"
0017 #include "postwidget.h"
0018 #include "shortenmanager.h"
0019 
0020 #include "untinysettings.h"
0021 
0022 K_PLUGIN_CLASS_WITH_JSON(UnTiny, "choqok_untiny.json")
0023 
0024 UnTiny::UnTiny(QObject* parent, const QList< QVariant >& )
0025     : Choqok::Plugin(QLatin1String("choqok_untiny"), parent)
0026     , state(Stopped)
0027 {
0028     connect(Choqok::UI::Global::SessionManager::self(), &Choqok::UI::Global::SessionManager::newPostWidgetAdded,
0029             this, &UnTiny::slotAddNewPostWidget);
0030 }
0031 
0032 UnTiny::~UnTiny()
0033 {
0034 
0035 }
0036 
0037 void UnTiny::slotAddNewPostWidget(Choqok::UI::PostWidget* newWidget)
0038 {
0039     postsQueue.enqueue(newWidget);
0040     if(state == Stopped){
0041         state = Running;
0042         QTimer::singleShot(1000, this, SLOT(startParsing()));
0043     }
0044 }
0045 
0046 void UnTiny::startParsing()
0047 {
0048     int i = 8;
0049     while( !postsQueue.isEmpty() && i>0 ){
0050         parse(postsQueue.dequeue());
0051         --i;
0052     }
0053 
0054     if(postsQueue.isEmpty())
0055         state = Stopped;
0056     else
0057         QTimer::singleShot(500, this, SLOT(startParsing()));
0058 }
0059 
0060 void UnTiny::parse(QPointer<Choqok::UI::PostWidget> postToParse)
0061 {
0062     if(!postToParse)
0063         return;
0064     QStringList redirectList, pureList = postToParse->urls();
0065     QString content = postToParse->currentPost()->content;
0066     for (int i=0; i < pureList.count(); ++i) {
0067         if(pureList[i].length() > 30){
0068             continue;
0069         }
0070         if(!pureList[i].startsWith(QLatin1String("http"), Qt::CaseInsensitive)){
0071             pureList[i].prepend(QLatin1String("http://"));
0072         }
0073         redirectList << pureList[i];
0074     }
0075     for (const QString &url: redirectList) {
0076         KIO::MimetypeJob *job = KIO::mimetype( QUrl::fromUserInput(url), KIO::HideProgressInfo );
0077         if ( !job ) {
0078             qCritical() << "Cannot create a http header request!";
0079             break;
0080         }
0081         connect( job, &KIO::MimetypeJob::permanentRedirection, this, &UnTiny::slot301Redirected );
0082         mParsingList.insert(job, postToParse);
0083         job->start();
0084     }
0085 }
0086 
0087 void UnTiny::slot301Redirected(KIO::Job* job, QUrl fromUrl, QUrl toUrl)
0088 {
0089     QPointer<Choqok::UI::PostWidget> postToParse = mParsingList.take(job);
0090     job->kill();
0091     if(postToParse){
0092         QString content = postToParse->content();
0093         QString fromUrlStr = fromUrl.url();
0094         content.replace(QRegExp(QStringLiteral("title='%1\'").arg(fromUrlStr)), QStringLiteral("title='%1\'").arg(toUrl.url()));
0095         content.replace(QRegExp(QStringLiteral("href='%1\'").arg(fromUrlStr)), QStringLiteral("href='%1\'").arg(toUrl.url()));
0096         postToParse->setContent(content);
0097         Choqok::ShortenManager::self()->emitNewUnshortenedUrl(postToParse, fromUrl, toUrl);
0098         if (toUrl.url().length() < 30 && fromUrl.host() == QLatin1String("t.co")){
0099             KIO::TransferJob *job = KIO::mimetype( toUrl, KIO::HideProgressInfo );
0100             if ( job ) {
0101                 connect( job, &KIO::MimetypeJob::permanentRedirection, this, &UnTiny::slot301Redirected );
0102                 mParsingList.insert(job, postToParse);
0103                 job->start();
0104             }
0105         }
0106     }
0107 }
0108 
0109 #include "moc_untiny.cpp"
0110 #include "untiny.moc"