Here is a summary of the code:
#include <QHttp>
#include <QUrl>
#include <QBuffer>
#include <QFile>
#include <QTextStream>
#include <QXmlStreamReader>
#include <QHttp>
#include <QByteArray>
class ColumnListing
{
Q_OBJECT
public:
ColumnListing();
public slots:
void fetch();
private:
void parseXml();
QXmlStreamReader xml;
int subconnectionId;
int connectionId;
};
#include <QHttp>
#include <QUrl>
#include <QBuffer>
#include <QFile>
#include <QTextStream>
#include <QXmlStreamReader>
#include <QHttp>
#include <QByteArray>
class ColumnListing
{
Q_OBJECT
public:
ColumnListing();
public slots:
void fetch();
void readData(const QHttpResponseHeader &);
private:
void parseXml();
QXmlStreamReader xml;
QString currentTag;
QString linkString;
QString titleString;
QString descriptionString;
QString authorString;
QString urltext;
QString inputNews;
QString searchedAuthor;
QFile file;
QFile *htmlFile;
QHttp httpInstance;
int subconnectionId;
QUrl urlColumnToGo;
QHttp http;
int connectionId;
};
To copy to clipboard, switch view to plain text mode
constructor part:
ColumnListing
::ColumnListing(QWidget *parent
){
char *input;
char *author;
input = new char[100];
author = new char[100];
cout<<"Newspaper ?\n";
cin>>input;
inputNews=input;//inputNews is global QString
cout<<"Author ?\n";
cin>>author;
searchedAuthor=author;//searchedAuthor is global QString
if (inputNews=="milliyet"){
urltext.append("http://www.milliyet.com.tr/D/rss/rss/RssY.xml?ver=51");
}
else if (inputNews=="sabah"){
urltext.append("http://www.sabah.com.tr/rss/yazarlar.xml");
}
else if (inputNews=="radikal"){
urltext.append("http://www.radikal.com.tr/radikal_yazar.xml");
}
/*** More News sites here....***/
file.setFileName("output.txt");//file is global QFile
if (!file.
open(QFile::ReadWrite |
QFile::Truncate)) return;
htmlFile
= new QFile("htmloutput.html");
//htmlFile is global QFile if (!htmlFile
->open
(QFile::ReadWrite |
QFile::Truncate)) return;
}
ColumnListing::ColumnListing(QWidget *parent)
: QWidget(parent)
{
connect(&http, SIGNAL(readyRead(const QHttpResponseHeader &)),
this, SLOT(readData(const QHttpResponseHeader &)));
char *input;
char *author;
input = new char[100];
author = new char[100];
cout<<"Newspaper ?\n";
cin>>input;
inputNews=input;//inputNews is global QString
cout<<"Author ?\n";
cin>>author;
searchedAuthor=author;//searchedAuthor is global QString
if (inputNews=="milliyet"){
urltext.append("http://www.milliyet.com.tr/D/rss/rss/RssY.xml?ver=51");
}
else if (inputNews=="sabah"){
urltext.append("http://www.sabah.com.tr/rss/yazarlar.xml");
}
else if (inputNews=="radikal"){
urltext.append("http://www.radikal.com.tr/radikal_yazar.xml");
}
/*** More News sites here....***/
file.setFileName("output.txt");//file is global QFile
if (!file.open(QFile::ReadWrite | QFile::Truncate))
return;
htmlFile= new QFile("htmloutput.html");//htmlFile is global QFile
if (!htmlFile->open(QFile::ReadWrite | QFile::Truncate))
return;
}
To copy to clipboard, switch view to plain text mode
fetching:
void ColumnListing::fetch()
{
xml.clear();
http.setHost(url.host());
connectionId = http.get(url.path());
}
void ColumnListing::fetch()
{
xml.clear();
QUrl url(urltext);
http.setHost(url.host());
connectionId = http.get(url.path());
}
To copy to clipboard, switch view to plain text mode
parsing rs doc:
void ColumnListing::parseXml()
{
while (!xml.atEnd()) {
xml.readNext();
if (xml.isStartElement()) {
if (xml.name() == "item")
linkString = xml.attributes().value("rss:about").toString();
currentTag = xml.name().toString();
} else if (xml.isEndElement()) {
if (xml.name() == "item") {
if(authorString.contains(searchedAuthor,Qt::CaseInsensitive) ){
inputText << titleString<< " "<<linkString <<" "<< descriptionString << authorString <<"\n";
httpInstance.setHost(url.host());
subconnectionId = http.get(url.path(),htmlFile);//write the author page into html file
}
titleString.clear();
linkString.clear();
descriptionString.clear();
authorString.clear();
}
} else if (xml.isCharacters() && !xml.isWhitespace()) {
if (currentTag == "title"){
titleString += xml.text().toString();
}
else if (currentTag == "link"){
linkString += xml.text().toString();
}
else if (currentTag == "description"){
descriptionString += xml.text().toString();
}
else if (currentTag == "dc:creator"){
authorString += xml.text().toString();
}
}
}
if (xml.error() && xml.error() != QXmlStreamReader::PrematureEndOfDocumentError) {
qWarning() << "XML ERROR:" << xml.lineNumber() << ": " << xml.errorString();
http.abort();
}
}
void ColumnListing::parseXml()
{
QTextStream inputText(&file);
while (!xml.atEnd()) {
xml.readNext();
if (xml.isStartElement()) {
if (xml.name() == "item")
linkString = xml.attributes().value("rss:about").toString();
currentTag = xml.name().toString();
} else if (xml.isEndElement()) {
if (xml.name() == "item") {
if(authorString.contains(searchedAuthor,Qt::CaseInsensitive) ){
inputText << titleString<< " "<<linkString <<" "<< descriptionString << authorString <<"\n";
QUrl url(linkString);
httpInstance.setHost(url.host());
subconnectionId = http.get(url.path(),htmlFile);//write the author page into html file
}
titleString.clear();
linkString.clear();
descriptionString.clear();
authorString.clear();
}
} else if (xml.isCharacters() && !xml.isWhitespace()) {
if (currentTag == "title"){
titleString += xml.text().toString();
}
else if (currentTag == "link"){
linkString += xml.text().toString();
}
else if (currentTag == "description"){
descriptionString += xml.text().toString();
}
else if (currentTag == "dc:creator"){
authorString += xml.text().toString();
}
}
}
if (xml.error() && xml.error() != QXmlStreamReader::PrematureEndOfDocumentError) {
qWarning() << "XML ERROR:" << xml.lineNumber() << ": " << xml.errorString();
http.abort();
}
}
To copy to clipboard, switch view to plain text mode
main.cpp
int main(int argc, char **argv)
{
q = q->codecForName("ISO-8859-9");
ColumnListing *columnlisting = new ColumnListing;
columnlisting->fetch();
return app.exec();
}
int main(int argc, char **argv)
{
QApplication app(argc, argv);
QTextCodec *q;
q = q->codecForName("ISO-8859-9");
QTextCodec::setCodecForCStrings(q);
ColumnListing *columnlisting = new ColumnListing;
columnlisting->fetch();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks