You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
198 lines
8.2 KiB
198 lines
8.2 KiB
20 years ago
|
//rssParser.java
|
||
|
//------------------------
|
||
|
//part of YaCy
|
||
17 years ago
|
//(C) by Michael Peter Christen; mc@yacy.net
|
||
20 years ago
|
//first published on http://www.anomic.de
|
||
|
//Frankfurt, Germany, 2005
|
||
|
//
|
||
|
//this file is contributed by Martin Thelian
|
||
16 years ago
|
//
|
||
|
// $LastChangedDate$
|
||
|
// $LastChangedRevision$
|
||
|
// $LastChangedBy$
|
||
20 years ago
|
//
|
||
|
//This program is free software; you can redistribute it and/or modify
|
||
|
//it under the terms of the GNU General Public License as published by
|
||
|
//the Free Software Foundation; either version 2 of the License, or
|
||
|
//(at your option) any later version.
|
||
|
//
|
||
|
//This program is distributed in the hope that it will be useful,
|
||
|
//but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
//GNU General Public License for more details.
|
||
|
//
|
||
|
//You should have received a copy of the GNU General Public License
|
||
|
//along with this program; if not, write to the Free Software
|
||
|
//Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||
|
|
||
15 years ago
|
package net.yacy.document.parser;
|
||
20 years ago
|
|
||
|
import java.io.ByteArrayInputStream;
|
||
17 years ago
|
import java.io.IOException;
|
||
20 years ago
|
import java.io.InputStream;
|
||
18 years ago
|
import java.io.Writer;
|
||
17 years ago
|
import java.nio.charset.Charset;
|
||
20 years ago
|
import java.util.HashMap;
|
||
16 years ago
|
import java.util.HashSet;
|
||
20 years ago
|
import java.util.LinkedList;
|
||
|
import java.util.Map;
|
||
16 years ago
|
import java.util.Set;
|
||
20 years ago
|
|
||
15 years ago
|
import net.yacy.document.AbstractParser;
|
||
|
import net.yacy.document.Document;
|
||
|
import net.yacy.document.Idiom;
|
||
|
import net.yacy.document.ParserException;
|
||
|
import net.yacy.document.content.RSSMessage;
|
||
|
import net.yacy.document.parser.html.AbstractScraper;
|
||
|
import net.yacy.document.parser.html.ContentScraper;
|
||
|
import net.yacy.document.parser.html.ImageEntry;
|
||
|
import net.yacy.document.parser.html.TransformerWriter;
|
||
|
import net.yacy.document.parser.xml.RSSFeed;
|
||
|
import net.yacy.document.parser.xml.RSSReader;
|
||
15 years ago
|
import net.yacy.kelondro.data.meta.DigestURI;
|
||
15 years ago
|
import net.yacy.kelondro.io.CharBuffer;
|
||
15 years ago
|
import net.yacy.kelondro.util.ByteBuffer;
|
||
|
import net.yacy.kelondro.util.FileUtils;
|
||
|
|
||
20 years ago
|
|
||
16 years ago
|
public class rssParser extends AbstractParser implements Idiom {
|
||
20 years ago
|
|
||
|
/**
|
||
|
* a list of mime types that are supported by this parser class
|
||
|
* @see #getSupportedMimeTypes()
|
||
|
*/
|
||
16 years ago
|
public static final Set<String> SUPPORTED_MIME_TYPES = new HashSet<String>();
|
||
|
public static final Set<String> SUPPORTED_EXTENSIONS = new HashSet<String>();
|
||
18 years ago
|
static {
|
||
16 years ago
|
SUPPORTED_EXTENSIONS.add("xml");
|
||
|
SUPPORTED_EXTENSIONS.add("rss");
|
||
|
SUPPORTED_EXTENSIONS.add("rdf");
|
||
|
SUPPORTED_MIME_TYPES.add("text/rss");
|
||
|
SUPPORTED_MIME_TYPES.add("application/rdf+xml");
|
||
|
SUPPORTED_MIME_TYPES.add("application/rss+xml");
|
||
|
SUPPORTED_MIME_TYPES.add("application/atom+xml");
|
||
16 years ago
|
}
|
||
20 years ago
|
|
||
|
public rssParser() {
|
||
16 years ago
|
super("Rich Site Summary/Atom Feed Parser");
|
||
20 years ago
|
}
|
||
|
|
||
15 years ago
|
public Document parse(final DigestURI location, final String mimeType, final String charset, final InputStream source) throws ParserException, InterruptedException {
|
||
20 years ago
|
|
||
|
try {
|
||
17 years ago
|
final LinkedList<String> feedSections = new LinkedList<String>();
|
||
15 years ago
|
final HashMap<DigestURI, String> anchors = new HashMap<DigestURI, String>();
|
||
16 years ago
|
final HashMap<String, ImageEntry> images = new HashMap<String, ImageEntry>();
|
||
16 years ago
|
final ByteBuffer text = new ByteBuffer();
|
||
15 years ago
|
final CharBuffer authors = new CharBuffer();
|
||
20 years ago
|
|
||
17 years ago
|
final RSSFeed feed = new RSSReader(source).getFeed();
|
||
16 years ago
|
if (feed == null) throw new ParserException("no feed in document",location);
|
||
|
if (feed.getChannel() == null) throw new ParserException("no channel in document",location);
|
||
20 years ago
|
|
||
|
// getting the rss feed title and description
|
||
17 years ago
|
final String feedTitle = feed.getChannel().getTitle();
|
||
20 years ago
|
|
||
18 years ago
|
// getting feed creator
|
||
17 years ago
|
final String feedCreator = feed.getChannel().getAuthor();
|
||
18 years ago
|
if (feedCreator != null && feedCreator.length() > 0) authors.append(",").append(feedCreator);
|
||
|
|
||
20 years ago
|
// getting the feed description
|
||
17 years ago
|
final String feedDescription = feed.getChannel().getDescription();
|
||
20 years ago
|
|
||
17 years ago
|
if (feed.getImage() != null) {
|
||
15 years ago
|
final DigestURI imgURL = new DigestURI(feed.getImage(), null);
|
||
16 years ago
|
images.put(imgURL.hash(), new ImageEntry(imgURL, feedTitle, -1, -1));
|
||
20 years ago
|
}
|
||
|
|
||
|
// loop through the feed items
|
||
17 years ago
|
for (final RSSMessage item: feed) {
|
||
18 years ago
|
// check for interruption
|
||
|
checkInterruption();
|
||
|
|
||
17 years ago
|
final String itemTitle = item.getTitle();
|
||
15 years ago
|
final DigestURI itemURL = new DigestURI(item.getLink(), null);
|
||
17 years ago
|
final String itemDescr = item.getDescription();
|
||
|
final String itemCreator = item.getCreator();
|
||
18 years ago
|
if (itemCreator != null && itemCreator.length() > 0) authors.append(",").append(itemCreator);
|
||
20 years ago
|
|
||
|
feedSections.add(itemTitle);
|
||
17 years ago
|
anchors.put(itemURL, itemTitle);
|
||
20 years ago
|
|
||
|
if ((text.length() != 0) && (text.byteAt(text.length() - 1) != 32)) text.append((byte) 32);
|
||
16 years ago
|
text.append(AbstractScraper.stripAll(itemDescr).trim()).append(' ');
|
||
20 years ago
|
|
||
17 years ago
|
final String itemContent = item.getDescription();
|
||
20 years ago
|
if ((itemContent != null) && (itemContent.length() > 0)) {
|
||
|
|
||
16 years ago
|
final ContentScraper scraper = new ContentScraper(itemURL);
|
||
|
final Writer writer = new TransformerWriter(null, null, scraper, null, false);
|
||
16 years ago
|
FileUtils.copy(new ByteArrayInputStream(itemContent.getBytes("UTF-8")), writer, Charset.forName("UTF-8"));
|
||
20 years ago
|
|
||
17 years ago
|
final String itemHeadline = scraper.getTitle();
|
||
20 years ago
|
if ((itemHeadline != null) && (itemHeadline.length() > 0)) {
|
||
|
feedSections.add(itemHeadline);
|
||
|
}
|
||
|
|
||
15 years ago
|
final Map<DigestURI, String> itemLinks = scraper.getAnchors();
|
||
20 years ago
|
if ((itemLinks != null) && (itemLinks.size() > 0)) {
|
||
|
anchors.putAll(itemLinks);
|
||
|
}
|
||
|
|
||
16 years ago
|
final HashMap<String, ImageEntry> itemImages = scraper.getImages();
|
||
20 years ago
|
if ((itemImages != null) && (itemImages.size() > 0)) {
|
||
16 years ago
|
ContentScraper.addAllImages(images, itemImages);
|
||
20 years ago
|
}
|
||
|
|
||
17 years ago
|
final byte[] extractedText = scraper.getText();
|
||
20 years ago
|
if ((extractedText != null) && (extractedText.length > 0)) {
|
||
|
if ((text.length() != 0) && (text.byteAt(text.length() - 1) != 32)) text.append((byte) 32);
|
||
|
text.append(scraper.getText());
|
||
|
}
|
||
|
|
||
|
}
|
||
|
}
|
||
|
|
||
16 years ago
|
final Document theDoc = new Document(
|
||
20 years ago
|
location,
|
||
|
mimeType,
|
||
18 years ago
|
"UTF-8",
|
||
20 years ago
|
null,
|
||
16 years ago
|
null,
|
||
20 years ago
|
feedTitle,
|
||
18 years ago
|
(authors.length() > 0)?authors.toString(1,authors.length()):"",
|
||
17 years ago
|
feedSections.toArray(new String[feedSections.size()]),
|
||
20 years ago
|
feedDescription,
|
||
|
text.getBytes(),
|
||
|
anchors,
|
||
|
images);
|
||
17 years ago
|
// close streams
|
||
|
text.close();
|
||
|
authors.close();
|
||
20 years ago
|
|
||
|
|
||
17 years ago
|
return theDoc;
|
||
18 years ago
|
|
||
17 years ago
|
} catch (final InterruptedException e) {
|
||
|
throw e;
|
||
|
} catch (final IOException e) {
|
||
18 years ago
|
throw new ParserException("Unexpected error while parsing rss file." + e.getMessage(),location);
|
||
20 years ago
|
}
|
||
|
}
|
||
|
|
||
16 years ago
|
public Set<String> supportedMimeTypes() {
|
||
|
return SUPPORTED_MIME_TYPES;
|
||
|
}
|
||
|
|
||
|
public Set<String> supportedExtensions() {
|
||
|
return SUPPORTED_EXTENSIONS;
|
||
|
}
|
||
20 years ago
|
|
||
16 years ago
|
@Override
|
||
20 years ago
|
public void reset() {
|
||
18 years ago
|
// Nothing todo here at the moment
|
||
|
super.reset();
|
||
20 years ago
|
}
|
||
|
|
||
|
}
|