-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add getting BlogFeed from HTML source
- Loading branch information
Showing
4 changed files
with
330 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
199 changes: 199 additions & 0 deletions
199
app/src/main/java/shts/jp/android/nogifeed/api/AsyncBlogFeedClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,199 @@ | ||
package shts.jp.android.nogifeed.api; | ||
|
||
import android.content.Context; | ||
import android.os.Handler; | ||
import android.os.HandlerThread; | ||
import android.os.Looper; | ||
|
||
import org.jsoup.Jsoup; | ||
import org.jsoup.nodes.Document; | ||
import org.jsoup.nodes.Element; | ||
import org.jsoup.select.Elements; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.Calendar; | ||
|
||
import shts.jp.android.nogifeed.common.Logger; | ||
import shts.jp.android.nogifeed.models.BlogEntry; | ||
import shts.jp.android.nogifeed.utils.NetworkUtils; | ||
|
||
// TODO: implement pagination | ||
public class AsyncBlogFeedClient { | ||
|
||
private static final String TAG = AsyncBlogFeedClient.class.getSimpleName(); | ||
private static final String URL = "http://blog.nogizaka46.com/"; | ||
private static final String USER_AGENT | ||
= "Opera/9.80 (Macintosh; Intel Mac OS X 10.6.8; U; fr) Presto/2.9.168 Version/11.52"; | ||
|
||
private static final HandlerThread WORKER_THREAD = new HandlerThread("AsyncBlogFeedClient"); | ||
private static Handler HANDLER; | ||
static { | ||
WORKER_THREAD.start(); | ||
HANDLER = new Handler(WORKER_THREAD.getLooper()); | ||
} | ||
|
||
public interface Callbacks { | ||
public void onFinish(ArrayList<BlogEntry> blogEntries); | ||
} | ||
|
||
public static class Target { | ||
public int year; | ||
public int month; | ||
public Page page; | ||
public Target(int from, int to) { | ||
Calendar calendar = Calendar.getInstance(); | ||
this.year = calendar.get(Calendar.YEAR); | ||
this.month = calendar.get(Calendar.MONTH) + 1; | ||
this.page = new Page(from, to); | ||
} | ||
public Target(int year, int month, int from, int to) { | ||
this.year = year; | ||
this.month = month; | ||
this.page = new Page(from, to); | ||
} | ||
static class Page { | ||
public int from; | ||
public int to; | ||
Page(int from, int to) { | ||
this.from = from; | ||
this.to = to; | ||
} | ||
} | ||
|
||
public void nextPage() { | ||
Logger.v(TAG, "next before(" + toString() + ")"); | ||
// page.from += 8; | ||
// page.to += 8; | ||
Logger.v(TAG, "next after(" + toString() + ")"); | ||
} | ||
|
||
void nextMonth() { | ||
Logger.v(TAG, "next before(" + toString() + ")"); | ||
if (month == 1) { | ||
month = 12; | ||
year--; | ||
} else { | ||
month--; | ||
} | ||
page.from = 1; | ||
page.to = 8; | ||
Logger.v(TAG, "next after(" + toString() + ")"); | ||
} | ||
|
||
String getDateParameter() { | ||
return String.valueOf(year) + (month < 10 ? String.valueOf("0") + month : month); | ||
} | ||
@Override | ||
public String toString() { | ||
StringBuilder sb = new StringBuilder(); | ||
sb.append("year(").append(year).append(") "); | ||
sb.append("month(").append(month).append(") "); | ||
sb.append("from(").append(page.from).append(") "); | ||
sb.append("to(").append(page.to).append(") "); | ||
return sb.toString(); | ||
} | ||
} | ||
|
||
public static boolean getBlogEntry(final Context context, | ||
final Target target, | ||
final Callbacks callbacks) { | ||
if (callbacks == null) { | ||
Logger.w(TAG, "cannot connection because of callback is null."); | ||
return false; | ||
} | ||
|
||
if (target == null) { | ||
Logger.w(TAG, "cannot connection because of entryTarget is null."); | ||
return false; | ||
} | ||
|
||
if (!NetworkUtils.enableNetwork(context)) { | ||
Logger.w(TAG, "cannot connection because of network disconnected."); | ||
return false; | ||
} | ||
|
||
HANDLER.post(new Runnable() { | ||
@Override | ||
public void run() { | ||
try { | ||
final ArrayList<BlogEntry> blogEntries | ||
= getBlogEntry(target); | ||
|
||
new Handler(Looper.getMainLooper()).post(new Runnable() { | ||
@Override | ||
public void run() { | ||
callbacks.onFinish(blogEntries); | ||
} | ||
}); | ||
|
||
} catch (IOException e) { | ||
Logger.e(TAG, "failed to parse blog feed", e); | ||
} | ||
} | ||
}); | ||
|
||
return true; | ||
} | ||
|
||
private static ArrayList<BlogEntry> getBlogEntry( | ||
final Target target) throws IOException { | ||
|
||
final ArrayList<BlogEntry> blogEntries = new ArrayList<BlogEntry>(); | ||
|
||
// decriment read page to max page size | ||
final int maxSize = getPaginate(target.getDateParameter()); | ||
Logger.v(TAG, "getBlogEntry(Target) in : maxSize(" + maxSize | ||
+ ") to(" + target.page.to + ")"); | ||
final boolean needToNextMonth = (maxSize < (target.page.to + 1)); | ||
while (maxSize < (target.page.to + 1)) { | ||
target.page.to--; | ||
} | ||
Logger.v(TAG, "getBlogEntry(Target) in : target(" + target.toString() + ")"); | ||
|
||
for (int i = target.page.from; i < (target.page.to + 1); i++) { | ||
|
||
String pageUrl = URL + "?p=" + i + "&d=" + target.getDateParameter(); | ||
Logger.d(TAG, "getBlogEntry() : pageUrl(" + pageUrl + ")"); | ||
|
||
Document document = Jsoup.connect(pageUrl).userAgent(USER_AGENT).get(); | ||
|
||
Element body = document.body(); | ||
Element sheet = body.getElementById("sheet"); | ||
Elements clearfix = sheet.getElementsByClass("clearfix"); | ||
|
||
final int size = clearfix.size(); | ||
for (int j = 0; j < size; j++) { | ||
Element e = clearfix.get(j); | ||
|
||
String yearmonth = e.getElementsByClass("yearmonth").get(0).text(); | ||
String dd1 = e.getElementsByClass("dd1").get(0).text(); | ||
String dd2 = e.getElementsByClass("dd2").get(0).text(); | ||
String date = yearmonth + "/" + dd1 + " " + dd2; | ||
|
||
String title = e.getElementsByClass("entrytitle").get(0).text(); | ||
String author = e.getElementsByClass("author").get(0).text(); | ||
String url = e.getElementsByTag("a").get(0).attr("href"); | ||
|
||
String comment = body.getElementsByClass("entrybottom") | ||
.get(j).getElementsByTag("a").get(1) | ||
.text().replace("コメント(", "").replace(")", ""); | ||
|
||
blogEntries.add(new BlogEntry(date, title, url, author, comment)); | ||
} | ||
} | ||
|
||
if (needToNextMonth) { | ||
target.nextMonth(); | ||
} | ||
|
||
return blogEntries; | ||
} | ||
|
||
private static int getPaginate(String param) throws IOException { | ||
Document document = Jsoup.connect(URL + "?d=" + param).userAgent(USER_AGENT).get(); | ||
Element paginate = document.body() | ||
.getElementsByClass("paginate").get(0); | ||
return paginate.getElementsByTag("a").size(); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
app/src/main/java/shts/jp/android/nogifeed/models/BlogEntries.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package shts.jp.android.nogifeed.models; | ||
|
||
import android.text.TextUtils; | ||
|
||
import java.util.ArrayList; | ||
|
||
/** | ||
* Created by saitoushouta on 15/07/28. | ||
*/ | ||
public class BlogEntries extends ArrayList<BlogEntry> { | ||
|
||
public BlogEntry getEntryFrom(String article) { | ||
for (BlogEntry e : this) { | ||
if (TextUtils.isEmpty(e.url)) { | ||
continue; | ||
} | ||
if (e.url.equals(article)) { | ||
return e; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
StringBuilder sb = new StringBuilder(); | ||
sb.append("BlogEntries : ["); | ||
for (BlogEntry e : this) { | ||
if (e == null) { | ||
continue; | ||
} | ||
sb.append(e.toString()).append("\n"); | ||
} | ||
sb.append("]"); | ||
return sb.toString(); | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
app/src/main/java/shts/jp/android/nogifeed/models/BlogEntry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package shts.jp.android.nogifeed.models; | ||
|
||
import android.os.Parcel; | ||
import android.os.Parcelable; | ||
|
||
import shts.jp.android.nogifeed.utils.UrlUtils; | ||
|
||
public class BlogEntry implements Parcelable { | ||
|
||
final String date; | ||
final String title; | ||
final String url; | ||
final String author; | ||
final String comment; | ||
|
||
public BlogEntry(String date, String title, String url, String author, String comment) { | ||
this.date = date; | ||
this.title = title; | ||
this.url = url; | ||
this.author = author; | ||
this.comment = comment; | ||
} | ||
|
||
public String getProfileImageUrl() { | ||
return UrlUtils.getImageUrlFromArticleUrl(url); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
StringBuilder sb = new StringBuilder(); | ||
sb.append("date(").append(date).append(") "); | ||
sb.append("title(").append(title).append(") "); | ||
sb.append("url(").append(url).append(") "); | ||
sb.append("author(").append(author).append(") "); | ||
sb.append("comment(").append(comment).append(") "); | ||
return sb.toString(); | ||
} | ||
|
||
protected BlogEntry(Parcel in) { | ||
date = in.readString(); | ||
title = in.readString(); | ||
url = in.readString(); | ||
author = in.readString(); | ||
comment = in.readString(); | ||
} | ||
|
||
@Override | ||
public int describeContents() { | ||
return 0; | ||
} | ||
|
||
@Override | ||
public void writeToParcel(Parcel dest, int flags) { | ||
dest.writeString(date); | ||
dest.writeString(title); | ||
dest.writeString(url); | ||
dest.writeString(author); | ||
dest.writeString(comment); | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
public static final Parcelable.Creator<BlogEntry> CREATOR = new Parcelable.Creator<BlogEntry>() { | ||
@Override | ||
public BlogEntry createFromParcel(Parcel in) { | ||
return new BlogEntry(in); | ||
} | ||
|
||
@Override | ||
public BlogEntry[] newArray(int size) { | ||
return new BlogEntry[size]; | ||
} | ||
}; | ||
} |