Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Update code for usecase
  • Loading branch information
cxchu committed Feb 24, 2017
1 parent d956641 commit ad8a8d2
Show file tree
Hide file tree
Showing 4 changed files with 260 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/main/java/kb/howtokb/extractor/TaskFrameReader.java
Expand Up @@ -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
Expand Down
58 changes: 58 additions & 0 deletions 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;
}
}
157 changes: 157 additions & 0 deletions 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<String> tags;
private List<Comment> comments;
private String cateID;

public YouTubeObj(String title, String description, String thumbnails, List<String> 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<String> tags,
List<Comment> 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<Comment> 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<getComments().size(); i++){
comment.add(getComments().get(i).toJson());
}
}
return comment;
}

@SuppressWarnings("unchecked")
public JSONArray tagToJson(){
JSONArray tags = new JSONArray();
if (getTags().size() > 0){
for (int i=0; i<getTags().size(); i++){
tags.add(getTags().get(i));
}
}
return tags;
}

@SuppressWarnings("unchecked")
public JSONObject toJson(){
JSONObject obj = new JSONObject();
obj.put("id", getId());
obj.put("title", getTitle());
obj.put("description", getDescription());
obj.put("thumbnail", getThumbnails());
obj.put("tags", tagToJson());
obj.put("cateID", getCateID());
obj.put("comment", commentToJson());
return obj;
}

@SuppressWarnings("unchecked")
public static YouTubeObj toYouTubeObj(JSONObject obj){
String id = (String) obj.get("id");
String title = (String) obj.get("title");
String des = (String) obj.get("description");
String thumbnail = (String) obj.get("thumbnail");
String cateID = (String) obj.get("cateID");

ArrayList<String> tags = new ArrayList<String>();
JSONArray tag = (JSONArray) obj.get("tags");
Iterator<String> iteratorC = tag.iterator();
while (iteratorC.hasNext()) {
tags.add(iteratorC.next());
}

List<Comment> comments = new ArrayList<>();
JSONArray comment = (JSONArray) obj.get("comment");
Iterator<JSONObject> 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<String> getTags() {
return tags;
}

public String getTitle() {
return title;
}

public String getDescription() {
return description;
}

public String getThumbnails() {
return thumbnails;
}

public List<Comment> getComments() {
return comments;
}

public String getCateID() {
return cateID;
}

}
42 changes: 42 additions & 0 deletions 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<String, YouTubeObj> tubeReader(String input) throws FileNotFoundException, IOException, ParseException{

Map<String, YouTubeObj> 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;
}
}

0 comments on commit ad8a8d2

Please sign in to comment.