[GAE/Java] 指定したRSSフィードのタイトルとURLを取り込んだり表示したりする


指定したRSSフィードのタイトルとURLを取り込んだり表示したりする。

RSSの取得にHorroRSSを使用


実際のEntityはKind名 "Link"だけだけど、親Keyとして"Feed"というKind名を割り当てて保存


ParentKey                                                        Key
======================================     =========================================
Kind       Name(ID)                                          /  Kind:    Name(ID)
======  ===============================     =====  =================================
"Feed"  http://hogehoge1.example.com/?xml   / "Link" http://hogehoge1.example.com/ent1.html

"Feed"  http://hogehoge1.example.com/?xml   "Link" http://hogehoge1.example.com/ent2.html
"Feed"  http://hogehoge1.example.com/?xml   "Link" http://hogehoge1.example.com/ent3.html
"Feed"  http://hogehoge2.example.com/?xml   "Link" http://hogehoge2.example.com/ent1.html
"Feed"  http://hogehoge2.example.com/?xml   "Link" http://hogehoge2.example.com/ent1.html
"Feed"  http://hogehoge3.example.com/?xml   "Link" http://hogehoge3.example.com/ent6.html


class StoreRSS
-------------------------------------------------------------

import java.util.*;
import org.horrabin.horrorss.*;
import com.google.appengine.api.datastore.*;

public class StoreRSS {
DatastoreService ds;
String[] rsslist= {"...", "...", "...", "..."}; //ここにRSSFeedのURL。Datastore指定するのめんどかったから。
ArrayList<String> entitylist;
int MAXENTNUM = 5;
public StoreRSS(){
    ds = DatastoreServiceFactory.getDatastoreService();
}

public void collect(){
for (String rssfeed:getRssList()){
Key ribkey = KeyFactory.createKey("Feed",rssfeed);
List<RssItemBean> rib = pullEntityList(rssfeed);
List<Entity> entl = new ArrayList<Entity>();
for(RssItemBean item:rib){
Entity ent = new Entity("Link",item.getLink(),ribkey);
ent.setProperty("title",item.getTitle());
ent.setProperty("link", item.getLink());
entl.add(ent);
}
ds.put(entl);
}
}

public List<String> getRssList(){
// TODO: to get from Datastore: now still get from variable...  
return Arrays.asList(rsslist);
}

public List<RssItemBean> pullEntityList(String rssfeed){
RssParser rss = new RssParser(rssfeed);
   rss.setCharset("utf8");
   try{
      rss.parse();
      List<RssItemBean> tempitems= rss.getItems().subList(0, MAXENTNUM); //Obtain a Vector of item elements (RssItemBean)
      return tempitems;
   }catch(Exception e){
    e.printStackTrace();
    return null;
      //Treat the exception as you want
   }
}

public List<Entity> getEntityList(String rssfeed){
Query rssquery = new Query("Link");
rssquery.setAncestor(KeyFactory.createKey("Feed", rssfeed));
PreparedQuery pq = ds.prepare(rssquery);
List<Entity> results = pq.asList(FetchOptions.Builder.withLimit(MAXENTNUM));//念のため個数制限
return results;
}
}
------------------------------------------------------

コメント

このブログの人気の投稿

4.3.0 Temporary Lookup Failureでドツボってた話

tomcat起動時の環境変数でJRE_HOMEを指定するときに

何が得られて何処へ向かうかだけを問うべき