Skip to content

Instantly share code, notes, and snippets.

@lacucaracha-jp
Last active February 13, 2016 13:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lacucaracha-jp/270b10bc6e4deedff851 to your computer and use it in GitHub Desktop.
Save lacucaracha-jp/270b10bc6e4deedff851 to your computer and use it in GitHub Desktop.
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.sql.DriverManager;
import java.sql.PreparedStatement;
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 hconn = null;
static String url = null;
static InputStream is = null;
static String str = null;
static org.jsoup.nodes.Document document;
static PreparedStatement pstmt;
static java.sql.Connection sconn = null;
public static void main(String[] args) {
try {
String dbUrl = "jdbc:sqlite:" + args[2];
String driver = "org.sqlite.JDBC";
DriverManager.setLoginTimeout(10000);
Class.forName(driver).newInstance();
sconn = DriverManager.getConnection(dbUrl);
sconn.setAutoCommit(false);
pstmt = sconn.prepareStatement("delete from BOOKMARKS");
pstmt.executeUpdate();
} catch (Exception e) {
System.out.println(e);
}
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");;
hconn = (HttpURLConnection) new URL(url).openConnection();
hconn.setRequestMethod("GET");
hconn.setConnectTimeout(100000);
is = hconn.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());
pstmt = sconn.prepareStatement("insert into BOOKMARKS(URL,EID,BOOKMARKUSER,STARCOUNT,TIMESTAMP,COMMENT,TAG) values(?,?,?,?,?,?,?) ");
pstmt.setString(1, page.getUrl());
pstmt.setString(2, page.getEid());
pstmt.setString(3, bookmark.getUser());
pstmt.setInt(4, 0);
pstmt.setDate(5, new java.sql.Date(bookmark.getTimestamp().getTime()));
pstmt.setString(6, bookmark.getComment());
pstmt.setString(7, bookmark.getTags());
pstmt.executeUpdate();
}
} catch (Exception e) {
System.out.println(e);
}
}
sconn.commit();
} 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