Skip to content
Browse files

krautchan.net plugin for Qt4

  • Loading branch information...
1 parent 9e7ea7a commit ddc00f348e519525356bcba6ca0d6a0a7c936635 @J0s3f committed Dec 29, 2012
View
192 plugins/parserKrautchan/parserKrautchan.cpp
@@ -0,0 +1,192 @@
+#include "parserKrautchan.h"
+#include <QStringList>
+
+ParserKrautchan::ParserKrautchan()
+{
+ _html = "";
+ _statusCode.hasErrors = false;
+ _statusCode.hasImages = false;
+ _statusCode.hasTitle = false;
+ _statusCode.isFrontpage = false;
+ _errorCode = 0;
+ _redirect = QUrl();
+ _images.clear();
+}
+
+QString ParserKrautchan::getAuthor() {
+ return "Josef Schneider";
+}
+
+QString ParserKrautchan::getPluginName() {
+ return "krautchan.net Parser";
+}
+
+QString ParserKrautchan::getVersion() {
+ return _LIB_VERSION;
+}
+
+QString ParserKrautchan::getDomain() {
+ return "krautchan.net";
+}
+
+ParsingStatus ParserKrautchan::parseHTML(QString html) {
+ QStringList res;
+ QRegExp rxImageDiv("<div class=\"file_(?:reply|thread)\">(.*)</div>", Qt::CaseInsensitive, QRegExp::RegExp2);
+ rxImageDiv.setMinimal(true);
+ QRegExp rxImage("'/files/([^']+)'");
+ QRegExp rxThumbnail("'/thumbnails/([^']+)'");
+ QRegExp rxImageName("<span class=\"filename\"><a href=\"[^\"]+/([^/\"]+)\"[^>]*>");
+ QRegExp rxThreads("<a href=\"([^\"]+)\"[^>]*>Antworten</a>", Qt::CaseInsensitive, QRegExp::RegExp2);
+ QRegExp rxTitle("<span class=\"postsubject\">([^<]+)</span>");
+ QRegExp rxThreadPage("<h2>Antwortmodus\\s+\\(Thread \\d+\\)</h2>");
+
+ bool imagesAdded;
+ bool pageIsFrontpage;
+ int pos;
+ _IMAGE i;
+ QUrl u;
+ QString sUrl;
+
+ _html = html;
+ _images.clear();
+ _redirect.clear();
+ _urlList.clear();
+ _statusCode.hasErrors = false;
+ _statusCode.hasImages = false;
+ _statusCode.hasTitle = false;
+ _statusCode.isFrontpage = false;
+
+ imagesAdded = false;
+ pos = 0;
+ i.downloaded = false;
+ i.requested = false;
+ pageIsFrontpage = !html.contains(rxThreadPage);
+
+ if (pageIsFrontpage) {
+ pos = 0;
+ _statusCode.isFrontpage = true;
+
+ while (pos > -1) {
+ pos = rxThreads.indexIn(html, pos + 1);
+ res = rxThreads.capturedTexts();
+
+ if (!res.at(1).isEmpty()) {
+ sUrl = "http://krautchan.net"+res.at(1);
+ if (sUrl.endsWith("/")) {
+ sUrl.remove(sUrl.length()-1,1);
+ }
+
+ _urlList.append(QUrl(sUrl));
+ }
+ }
+ }
+ else {
+ // Checking for Images
+
+ //extract all <div> containing a single image each
+ QStringList images;
+ pos = rxImageDiv.indexIn(html);
+ while (pos > -1) {
+ res = rxImageDiv.capturedTexts();
+ images.append(res.at(1));
+ pos = rxImageDiv.indexIn(html, pos+1);
+ }
+ //extract name, url and thumbnail url for each image
+ QStringList::const_iterator image;
+ for (image = images.constBegin(); image != images.constEnd();
+ ++image) {
+ i.originalFilename = i.thumbURI = i.largeURI = "";
+ pos = rxImageName.indexIn(*image);
+ if (pos > -1) {
+ res = rxImageName.capturedTexts();
+ i.originalFilename = res.at(1);
+ }
+ pos = rxThumbnail.indexIn(*image);
+ if (pos > -1) {
+ res = rxThumbnail.capturedTexts();
+ i.thumbURI = "http://krautchan.net/thumbnails/"+res.at(1);
+ }
+ pos = rxImage.indexIn(*image);
+ if (pos > -1) {
+ res = rxImage.capturedTexts();
+ i.largeURI = "http://krautchan.net/files/"+ res.at(1);
+ _images.append(i);
+ _statusCode.hasImages = true;
+ }
+ }
+
+ pos = 0;
+ while (pos > -1) {
+ pos = rxTitle.indexIn(html,pos+1);
+ res = rxTitle.capturedTexts();
+
+ if (pos > -1 && res.at(1) != "") {
+ _threadTitle = res.at(1);
+ _statusCode.hasTitle = true;
+ pos = -1;
+ }
+ }
+ }
+
+ return _statusCode;
+}
+
+QString ParserKrautchan::getThreadTitle() {
+ return _threadTitle;
+}
+
+QList<_IMAGE> ParserKrautchan::getImageList() {
+ return _images;
+}
+
+QUrl ParserKrautchan::getRedirectURL() {
+ return _redirect;
+}
+
+int ParserKrautchan::getErrorCode() {
+ return _errorCode;
+}
+
+ParsingStatus ParserKrautchan::getStatusCode() {
+ return _statusCode;
+}
+
+QList<QUrl> ParserKrautchan::getUrlList() {
+ return _urlList;
+}
+
+void ParserKrautchan::setURL(QUrl url) {
+ _url = url;
+
+ boardName = _url.path().section("/",1,1);
+ threadNumber = "";
+ if (_url.path().contains("thread")) {
+ QRegExp rxTid("/thread-(\\d+)\\.html");
+ int pos = rxTid.indexIn(_url.path());
+ QStringList res = rxTid.capturedTexts();
+
+ if (pos > -1 && res.at(1) != "") {
+ threadNumber = res.at(1);
+ }
+ }
+}
+
+QString ParserKrautchan::parseSavepath(QString s) {
+ s.replace("%n", threadNumber);
+ s.replace("%b", boardName);
+ s.replace("%h", _url.host());
+
+ return s;
+}
+
+QMap<QString, QString> ParserKrautchan::getSupportedReplaceCharacters() {
+ QMap<QString, QString> ret;
+
+ ret.insert("%n", "Threadnumber");
+ ret.insert("%b", "Board");
+ ret.insert("%h", "Host");
+
+ return ret;
+}
+
+Q_EXPORT_PLUGIN2(pParserKrautchan, ParserKrautchan)
View
48 plugins/parserKrautchan/parserKrautchan.h
@@ -0,0 +1,48 @@
+#ifndef PARSERKRAUTCHAN_H
+#define PARSERKRAUTCHAN_H
+
+#include <QStringList>
+#include <QList>
+#include <QRegExp>
+#include <QtDebug>
+#include "../../gui/ParserPluginInterface.h"
+#include "../../gui/structs.h"
+
+class ParserKrautchan : public QObject, public ParserPluginInterface {
+ Q_OBJECT
+ Q_INTERFACES(ParserPluginInterface)
+
+public:
+ ParserKrautchan();
+ QString getAuthor(void);
+ QString getPluginName(void);
+ QString getVersion();
+ QString getDomain();
+ QString getInterfaceRevision() {return _PARSER_PLUGIN_INTERFACE_REVISION;}
+ ParsingStatus parseHTML(QString html);
+ void setURL(QUrl url);
+ QString getThreadTitle();
+ QList<_IMAGE> getImageList();
+ QList<QUrl> getUrlList();
+ QUrl getRedirectURL();
+ int getErrorCode();
+ ParsingStatus getStatusCode();
+ QString parseSavepath(QString s);
+ QMap<QString, QString> getSupportedReplaceCharacters();
+
+ QObject* createInstance() {return new ParserKrautchan();}
+
+private:
+ QString _html;
+ QString boardName;
+ QString threadNumber;
+ QList<_IMAGE> _images;
+ QList<QUrl> _urlList;
+ QUrl _redirect;
+ QUrl _url;
+ ParsingStatus _statusCode;
+ int _errorCode;
+ QString _threadTitle;
+};
+
+#endif // PARSERKRAUTCHAN_H
View
18 plugins/parserKrautchan/parserKrautchan.pro
@@ -0,0 +1,18 @@
+#-------------------------------------------------
+#
+# Project created by QtCreator 2011-09-21T19:19:51
+#
+#-------------------------------------------------
+
+QT -= gui
+
+win32:TARGET = ../../parserKrautchan
+else:TARGET = ../parserKrautchan
+TEMPLATE = lib
+CONFIG += dll plugin
+
+DEFINES += KRAUTCHANPARSER_LIBRARY _LIB_VERSION="\\\"0.9\\\""
+
+SOURCES += parserKrautchan.cpp
+
+HEADERS += parserKrautchan.h
View
3 plugins/plugins.pro
@@ -3,4 +3,5 @@ SUBDIRS = \
parser2chan \
parser4chan \
parserchanarchive \
- parser.archive.foolz.us
+ parser.archive.foolz.us \
+ parserKrautchan

0 comments on commit ddc00f3

Please sign in to comment.
Something went wrong with that request. Please try again.