From ad8a8d2f7385ec4377b92547d2f8680b60c31f14 Mon Sep 17 00:00:00 2001 From: cxchu Date: Fri, 24 Feb 2017 11:29:25 +0100 Subject: [PATCH] Update code for usecase --- .../kb/howtokb/extractor/TaskFrameReader.java | 6 +- src/main/java/kb/howtokb/usecase/Comment.java | 58 +++++++ .../java/kb/howtokb/usecase/YouTubeObj.java | 157 ++++++++++++++++++ .../howtokb/usecase/YoutubeVideoReader.java | 42 +++++ 4 files changed, 260 insertions(+), 3 deletions(-) create mode 100644 src/main/java/kb/howtokb/usecase/Comment.java create mode 100644 src/main/java/kb/howtokb/usecase/YouTubeObj.java create mode 100644 src/main/java/kb/howtokb/usecase/YoutubeVideoReader.java diff --git a/src/main/java/kb/howtokb/extractor/TaskFrameReader.java b/src/main/java/kb/howtokb/extractor/TaskFrameReader.java index 7e827c7..43d8509 100644 --- a/src/main/java/kb/howtokb/extractor/TaskFrameReader.java +++ b/src/main/java/kb/howtokb/extractor/TaskFrameReader.java @@ -22,11 +22,11 @@ public class TaskFrameReader { // TO DO // ====================================================================== /** - * Return list of activity frame extract from file with json format + * Return list of task frame extract from file with json format * * @param directory - * that saves activity frame in json format - * @return a list of activity frame + * that saves task frame in json format + * @return a list of task frame * @throws IOException * @throws ClassNotFoundException * @throws ParseException diff --git a/src/main/java/kb/howtokb/usecase/Comment.java b/src/main/java/kb/howtokb/usecase/Comment.java new file mode 100644 index 0000000..b22fac0 --- /dev/null +++ b/src/main/java/kb/howtokb/usecase/Comment.java @@ -0,0 +1,58 @@ +package kb.howtokb.usecase; + +import org.json.simple.JSONObject; + +public class Comment { + private String author; + private String comment; + private int likeCount; + private int replyCount; + public Comment(String author, String comment, int likeCount, int replyCount) { + super(); + this.author = author; + this.comment = comment; + this.likeCount = likeCount; + this.replyCount = replyCount; + } + public Comment(String author, String comment) { + super(); + this.author = author; + this.comment = comment; + } + @Override + public String toString() { + return "Comment:\nauthor=" + author + "\ncomment=" + comment + "\nlikeCount=" + likeCount + "\nreplyCount=" + + replyCount + "\n"; + } + + @SuppressWarnings("unchecked") + public JSONObject toJson(){ + JSONObject obj = new JSONObject(); + obj.put("author", getAuthor()); + obj.put("comment", getComment()); + obj.put("like", getLikeCount()); + obj.put("reply", getReplyCount()); + return obj; + } + + public static Comment toComment(JSONObject obj){ + String author = (String) obj.get("author"); + String comment = (String) obj.get("comment"); + int like = (int) (long) obj.get("like"); + int reply = (int) (long) obj.get("reply"); + return new Comment(author, comment, like, reply); + } + + public String getAuthor() { + return author; + } + public String getComment() { + return comment; + } + public int getLikeCount() { + return likeCount; + } + public int getReplyCount() { + return replyCount; + } +} diff --git a/src/main/java/kb/howtokb/usecase/YouTubeObj.java b/src/main/java/kb/howtokb/usecase/YouTubeObj.java new file mode 100644 index 0000000..9c3ade0 --- /dev/null +++ b/src/main/java/kb/howtokb/usecase/YouTubeObj.java @@ -0,0 +1,157 @@ +package kb.howtokb.usecase; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +import org.json.simple.JSONArray; +import org.json.simple.JSONObject; + +public class YouTubeObj { + + private String id; + private String title; + private String description; + private String thumbnails; + private List tags; + private List comments; + private String cateID; + + public YouTubeObj(String title, String description, String thumbnails, List tags, + String cateID) { + super(); + this.title = title; + this.description = description; + this.thumbnails = thumbnails; + this.tags = tags; + this.cateID = cateID; + } + + public YouTubeObj(String id, String title, String description, String thumbnails, List tags, + List comments, String cateID) { + super(); + this.id = id; + this.title = title; + this.description = description; + this.thumbnails = thumbnails; + this.tags = tags; + this.comments = comments; + this.cateID = cateID; + } + + public void setId(String id) { + this.id = id; + } + + public YouTubeObj() { + super(); + // TODO Auto-generated constructor stub + } + + public void setComments(List comments) { + this.comments = comments; + } + + public YouTubeObj(String title, String description, String thumbnails, String cateID) { + super(); + this.title = title; + this.description = description; + this.thumbnails = thumbnails; + this.cateID = cateID; + } + + @Override + public String toString() { + return "YouTubeObj\ntitle=" + title + "\ndescription=" + description + "\nthumbnails=" + thumbnails + "\ntags=" + + tags + "\ncomments=" + comments + "\ncateID=" + cateID + "\n"; + } + + @SuppressWarnings("unchecked") + public JSONArray commentToJson(){ + JSONArray comment = new JSONArray(); + if (getComments().size() > 0){ + for (int i=0; i 0){ + for (int i=0; i tags = new ArrayList(); + JSONArray tag = (JSONArray) obj.get("tags"); + Iterator iteratorC = tag.iterator(); + while (iteratorC.hasNext()) { + tags.add(iteratorC.next()); + } + + List comments = new ArrayList<>(); + JSONArray comment = (JSONArray) obj.get("comment"); + Iterator iter = comment.iterator(); + while (iter.hasNext()) { + JSONObject com = iter.next(); + comments.add(Comment.toComment(com)); + } + return new YouTubeObj(id, title, des, thumbnail, tags, comments, cateID); + } + + public String getId() { + return id; + } + + public List getTags() { + return tags; + } + + public String getTitle() { + return title; + } + + public String getDescription() { + return description; + } + + public String getThumbnails() { + return thumbnails; + } + + public List getComments() { + return comments; + } + + public String getCateID() { + return cateID; + } + +} diff --git a/src/main/java/kb/howtokb/usecase/YoutubeVideoReader.java b/src/main/java/kb/howtokb/usecase/YoutubeVideoReader.java new file mode 100644 index 0000000..2c0489a --- /dev/null +++ b/src/main/java/kb/howtokb/usecase/YoutubeVideoReader.java @@ -0,0 +1,42 @@ +package kb.howtokb.usecase; + +import java.io.BufferedReader; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + +import org.json.simple.JSONObject; +import org.json.simple.parser.JSONParser; +import org.json.simple.parser.ParseException; + +public class YoutubeVideoReader { + + /** + * Read youtube video information in json format + * @param input + * @return map: youtube video id -- youtube video object + * @throws FileNotFoundException + * @throws IOException + * @throws ParseException + */ + public static Map tubeReader(String input) throws FileNotFoundException, IOException, ParseException{ + + Map allvideos = new HashMap<>(); + + JSONParser parser = new JSONParser(); + try (BufferedReader br = new BufferedReader(new FileReader(input))) { + String sCurrentLine; + while ((sCurrentLine = br.readLine()) != null) { + Object obj = parser.parse(sCurrentLine); + JSONObject jsonObject = (JSONObject) obj; + YouTubeObj utube = YouTubeObj.toYouTubeObj(jsonObject); + allvideos.put(utube.getId(), utube); + //System.out.println(utube.getTitle()); + System.out.println(utube.getId()); + } + } + return allvideos; + } +}