public ArrayList<Feedback> readFeedbacks(long idPost) { RssParser parser = null; Rss rss = null; // System.out.println("right? "+post); try { parser = RssParserFactory.createDefault(); rss = parser.parse(new URL(boardAddress + "feedbacks?action=READ&FeedbackName=" + idPost)); } catch (RssParserException e) { System.out.println(boardAddress + "feedbacks?action=READ&FeedbackName=" + idPost); System.out.println("RssParserException"); return null; } catch (MalformedURLException e) { System.out.println("MalformedURLException"); return null; } catch (IOException e) { System.out.println("IOException"); return null; } Channel c = rss.getChannel(); if (c.getItems() == null) { System.out.println("Non ci sono Feedback"); return new ArrayList<Feedback>(); } ArrayList<Item> items = new ArrayList<Item>(c.getItems()); Iterator<Item> iter = items.iterator(); ArrayList<Feedback> lista = new ArrayList<Feedback>(); while (iter.hasNext()) { Item x = iter.next(); String description = ""; String feed = ""; Title titolo = Title.AGREE; try { description = x.getDescription().getText(); } catch (NullPointerException e) { } try { feed = x.getTitle().getText(); } catch (NullPointerException e) { } if (feed != null) { if (!feed.isEmpty()) { if (feed.equals("AGREE")) titolo = Title.AGREE; else if (feed.equals("DISAGREE")) titolo = Title.DISAGREE; else if (feed.equals("PARTIALLY_AGREE")) titolo = Title.PARTIALLY_AGREE; else if (feed.equals("DETRACTOR")) titolo = Title.DETRACTOR; else continue; } } else continue; Date data = getPubDate(x); lista.add(new Feedback(description, titolo, data)); } return lista; }
@SuppressWarnings("unchecked") public static void main(String[] args) throws MalformedURLException, RssParserException, IOException { TipiRssComponent trc = new TipiRssComponent(); Rss r = trc.createRssFeed("http://search-result.com/directhit/xml/NL_algemeen.xml"); // http://www.nytimes.com/services/xml/rss/nyt/HomePage.xml Channel ccc = r.getChannel(); // Navajo n = trc.getRssNavajo(ccc,null); Collection<Item> s = ccc.getItems(); for (Iterator<Item> iterator = s.iterator(); iterator.hasNext(); ) { Item i = iterator.next(); Title t = i.getTitle(); logger.info("TT: " + t.getText()); } logger.info("s: " + s); }
private ArrayList<String> getCategories(Item item) { ArrayList<String> lista = new ArrayList<String>(); Iterator<Category> it = item.getCategories().iterator(); while (it.hasNext()) { lista.add(it.next().getText()); } return lista; }
private Date getPubDate(Item item) { String data = item.getPubDate().getText(); SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.ENGLISH); Date x = new Date(); try { x = new Date(sdf.parse(data).getTime()); } catch (ParseException e) { } return x; }
private void addItem(Message m, Item i) throws NavajoException { Navajo n = m.getRootDoc(); Message itemMessage = NavajoFactory.getInstance().createMessage(n, "Rss", Message.MSG_TYPE_ARRAY_ELEMENT); m.addMessage(itemMessage); addProperty(itemMessage, "Title", i.getTitle(), Property.STRING_PROPERTY); addProperty(itemMessage, "Link", i.getLink(), Property.STRING_PROPERTY); addProperty(itemMessage, "Description", i.getDescription(), Property.MEMO_PROPERTY); addProperty(itemMessage, "Author", i.getAuthor(), Property.STRING_PROPERTY); addProperty(itemMessage, "PubDate", i.getPubDate(), Property.STRING_PROPERTY); addProperty(itemMessage, "Source", i.getSource(), Property.STRING_PROPERTY); }
public ArrayList<Post> readPosts(boolean advanced) { RssParser parser = null; Rss rss = null; try { parser = RssParserFactory.createDefault(); System.out.println(boardAddress); rss = parser.parse(new URL(boardAddress + "postboard?action=READ")); } catch (RssParserException e) { System.out.println("RssParserException"); return null; } catch (MalformedURLException e) { System.out.println("MalformedURLException"); return null; } catch (IOException e) { System.out.println("IOException"); return null; } Channel c = rss.getChannel(); if (c.getItems() == null) { System.out.println("Non ci sono Post"); return new ArrayList<Post>(); } ArrayList<Item> items = new ArrayList<Item>(c.getItems()); Iterator<Item> iter = items.iterator(); ArrayList<Post> res = new ArrayList<Post>(); while (iter.hasNext()) { Item i = (Item) iter.next(); if (advanced) if ((!bannished.equals("")) && (i.getText().indexOf(bannished) != -1)) continue; long id = getFeedbackName(i); // id sempre presente String titolo = ""; String link = ""; String description = ""; ArrayList<String> categories = new ArrayList<String>(); String enclosure = ""; String source = ""; Date date = getPubDate(i); // data sempre presente try { titolo = i.getTitle().getText(); } catch (NullPointerException e) { } try { link = i.getLink().getText(); } catch (NullPointerException e) { } try { description = i.getDescription().getText(); } catch (NullPointerException e) { } try { categories = getCategories(i); } catch (NullPointerException e) { } try { enclosure = i.getEnclosure().getText(); } catch (NullPointerException e) { } try { source = i.getSource().getText(); } catch (NullPointerException e) { } if (advanced) if (!(match(categories) && timestamp.before(date))) continue; Post p = new Post( id, titolo, link, description, "", categories, enclosure, source, date, readFeedbacks(id)); res.add(p); } if (advanced) if (res.size() > 0) timestamp = res.get(res.size() - 1).getPubDate(); return res; }
private long getFeedbackName(Item x) { String guid = x.getGuid().getText(); return Long.valueOf(guid.substring(guid.indexOf('=') + 1)).longValue(); }