1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
|
// -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// Author: Jose M. Vidal
// $Id: channel.C,v 1.8 2001/08/25 21:26:30 jmvidal Exp $
// This code is copyright of Jose M. Vidal and released under
// the GNU General Public License
#include "channel.H"
//defined in reference.C
extern string trim(const string & s);
bool channel::readChannel() {
if (read && title == "")
return false; //we failed before, dont try again
else if (read)
return true; //already read it OK.
read = true; //we will try to read it.
cout << "Reading channel " << name << endl;
iwebstream ifs(url);
if (ifs == 0) { //could not read url, try backup file
cerr << "Could not read channel=" << name << ", trying backup file" << endl;
iwebstream ifs2(backupFile); //backup file could also be a url!
if (ifs2 == 0) {
cerr << "Could not read backup file=" << backupFile << endl;
return false;
}
reference r;
readStream(ifs2);
while (r.readXml(ifs2)){
items.push_back(r);
r.comment = "";
r.title = "";
r.url = "";
}
}
else { //read url OK.
reference r;
if (readStream(ifs)){ //parsed <channel> section OK
while (r.readXml(ifs))
items.push_back(r);
r.comment = "";
r.title = "";
r.url = "";
}
else { //could not parse <channel> section
cerr << "Could not parse channel=" << name << ", trying backup file" << endl;
iwebstream ifs2(backupFile); //backup file could also be a url!
if (ifs2 == 0) {
cerr << "Could not read backup file=" << backupFile << endl;
return false;
}
reference r;
readStream(ifs2);
while (r.readXml(ifs2)){
items.push_back(r);
r.comment = "";
r.title = "";
r.url = "";
}
}
}
return true;
}
//not used, dont know if it works.
void channel::readImage() {
if (imageLink != "") {
iwebstream image(imageLink);
if (image != 0){
string filename = backupFile + "-image";
ofstream of(filename.c_str());
for (char c = image.get(); !image.eof(); c =image.get()) {
of << c;
}
of.close();
}
}
}
/**
Parse an RSS channel.
This should go away soon and be replaced with XMLParser.
*/
bool channel::readStream(iwebstream & is) {
const string channelT = "<channel";
const string titleT = "<title>";
const string titleET = "</title>";
const string linkT = "<link>";
const string linkET = "</link>";
const string descriptionT = "<description>";
const string descriptionET = "</description>";
const string languageT = "<language>";
const string languageET = "</language>";
const string imageT = "<image>";
const string imageET = "</image>";
const string urlT = "<url>";
const string urlET = "</url>";
if (!is.find(channelT)) {
cerr << "Channel file does not have " << channelT << " section" << endl;
cerr << "The channel we downloaded is:" << endl;
cerr << is.getData() << endl;
return false;
}
title = is.findTag(titleT, titleET, channelT);
title = trim(title);
siteUrl = is.findTag(linkT, linkET, channelT);
description = is.findTag(descriptionT, descriptionET, channelT);
description = trim(description);
language = is.findTag(languageT, languageET, channelT);
//if there is an image, read it
imageTitle = is.findTag(titleT, titleET, imageT);
imageUrl = is.findTag(urlT, urlET, imageT);
imageLink = is.findTag(linkT, linkET, imageT);
ofstream of(backupFile.c_str());
if (of == 0) {
cerr << "Could not write channel to " << backupFile << endl;
return true;
}
of << is.getData();
of.close();
return true;
}
void channel::increaseHits(const string & url, int x){
for (vector<reference>::iterator i = items.begin(); i!= items.end(); ++i){
if ((*i).url == url)
(*i).hits+= x;
};
}
|