Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;
import net.arnx.jsonic.JSON;
import org.jsoup.Jsoup;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Amano
*/
public class HatenaBookmark {
//
static InputStream in = null;
static HttpURLConnection connect = null;
static String url = null;
static InputStream is = null;
static String str = null;
static org.jsoup.nodes.Document document;
public static void main(String[] args) {
for (int i = 0; i <= Integer.parseInt(args[1]); i++) {
try {
url = "http://b.hatena.ne.jp/entrylist?sort=hot&layout=headline&url=" + URLEncoder.encode(args[0], "utf-8") + "&of=" + i * 20;
document = Jsoup.connect(url).timeout(100000).get();
for (org.jsoup.nodes.Element element : document.select("li.entrylist-unit")) {
try {
url = element.getElementsByTag("a").attr("href");
url = "http://b.hatena.ne.jp/entry/jsonlite/?url=" + URLEncoder.encode(url, "utf-8");;
connect = (HttpURLConnection) new URL(url).openConnection();
connect.setRequestMethod("GET");
connect.setConnectTimeout(100000);
is = connect.getInputStream();
str = convertString(is);
Page page = JSON.decode(str, Page.class);
for (Bookmark bookmark : page.getBookmarks()) {
System.out.println(page.getTitle()+","+bookmark.getUser() + "," + bookmark.getComment());
}
} catch (Exception e) {
System.out.println(e);
}
}
} catch (Exception e) {
System.out.println(e);
}
}
}
static String convertString(InputStream is) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
StringBuilder sb = new StringBuilder();
char[] b = new char[1024];
int line;
while (0 <= (line = reader.read(b))) {
sb.append(b, 0, line);
}
return sb.toString();
}
}
class Bookmark {
private Date timestamp;
private String comment;
private String user;
private String tag;
public Date getTimestamp() {
return timestamp;
}
public String getComment() {
return comment;
}
public String getUser() {
return user;
}
public void setTimestamp(Date timestamp) {
this.timestamp = timestamp;
}
public void setComment(String comment) {
this.comment = comment;
}
public void setUser(String user) {
this.user = user;
}
public String getTags() {
return tag;
}
public void setTags(String tag) {
this.tag = tag;
}
}
class Page {
public int count;
public String url;
public String eid;
public String title;
public String screenshot;
public String entry_url;
private List<Bookmark> bookmarkList;
public void setBookmarks(List<Bookmark> bookmarkList) {
this.bookmarkList = bookmarkList;
}
public List<Bookmark> getBookmarks() {
return bookmarkList;
}
public int getCount() {
return count;
}
public String getUrl() {
return url;
}
public String getEid() {
return eid;
}
public String getTitle() {
return title;
}
public String getScreenshot() {
return screenshot;
}
public String getEntry_url() {
return entry_url;
}
public void setCount(int count) {
this.count = count;
}
public void setUrl(String url) {
this.url = url;
}
public void setEid(String eid) {
this.eid = eid;
}
public void setTitle(String title) {
this.title = title;
}
public void setScreenshot(String screenshot) {
this.screenshot = screenshot;
}
public void setEntry_url(String entry_url) {
this.entry_url = entry_url;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment