-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
caodongping
committed
Apr 15, 2016
1 parent
cb3fccd
commit acf4fbf
Showing
6 changed files
with
181 additions
and
98 deletions.
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
11 changes: 11 additions & 0 deletions
11
app/src/main/java/com/github/mzule/androidweekly/api/parser/ArticleParser.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,11 @@ | ||
package com.github.mzule.androidweekly.api.parser; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
/** | ||
* Created by CaoDongping on 4/15/16. | ||
*/ | ||
public interface ArticleParser { | ||
List<Object> parse(String issue) throws IOException; | ||
} |
18 changes: 18 additions & 0 deletions
18
app/src/main/java/com/github/mzule/androidweekly/api/parser/ArticleParsers.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,18 @@ | ||
package com.github.mzule.androidweekly.api.parser; | ||
|
||
import android.support.annotation.WorkerThread; | ||
|
||
/** | ||
* Created by CaoDongping on 4/15/16. | ||
*/ | ||
public class ArticleParsers { | ||
@WorkerThread | ||
public static ArticleParser get(String issue) { | ||
if (issue == null || Integer.parseInt(issue.split("-")[1]) > 102) { | ||
return new FresherArticlesParser(); | ||
} else { | ||
return new OlderArticlesParser(); | ||
} | ||
} | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
app/src/main/java/com/github/mzule/androidweekly/api/parser/DocumentProvider.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,20 @@ | ||
package com.github.mzule.androidweekly.api.parser; | ||
|
||
import org.jsoup.Jsoup; | ||
import org.jsoup.nodes.Document; | ||
|
||
import java.io.IOException; | ||
import java.net.URL; | ||
|
||
/** | ||
* Created by CaoDongping on 4/15/16. | ||
*/ | ||
public class DocumentProvider { | ||
public static Document get(String issue) throws IOException { | ||
String url = "http://androidweekly.net/"; | ||
if (issue != null) { | ||
url += issue; | ||
} | ||
return Jsoup.parse(new URL(url), 30000); | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
app/src/main/java/com/github/mzule/androidweekly/api/parser/FresherArticlesParser.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,57 @@ | ||
package com.github.mzule.androidweekly.api.parser; | ||
|
||
import com.github.mzule.androidweekly.entity.Article; | ||
|
||
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.List; | ||
|
||
/** | ||
* Created by CaoDongping on 4/15/16. | ||
*/ | ||
public class FresherArticlesParser implements ArticleParser { | ||
|
||
@Override | ||
public List<Object> parse(String issue) throws IOException { | ||
Document doc = DocumentProvider.get(issue); | ||
List<Object> articles = new ArrayList<>(); | ||
Elements tables = doc.getElementsByTag("table"); | ||
String currentSection = null; | ||
for (Element e : tables) { | ||
Elements h2 = e.getElementsByTag("h2"); | ||
if (!h2.isEmpty()) { | ||
currentSection = h2.get(0).text(); | ||
articles.add(currentSection); | ||
} else { | ||
Elements tds = e.getElementsByTag("td"); | ||
Element td = tds.get(tds.size() - 2); | ||
String imageUrl = null; | ||
if (tds.size() == 4) { | ||
imageUrl = tds.get(0).getElementsByTag("img").get(0).attr("src"); | ||
} | ||
String title = td.getElementsByClass("article-headline").get(0).text(); | ||
String brief = td.getElementsByTag("p").get(0).text(); | ||
String link = td.getElementsByClass("article-headline").get(0).attr("href"); | ||
String domain = td.getElementsByTag("span").get(0).text().replace("(", "").replace(")", ""); | ||
if (issue == null) { | ||
String number = doc.getElementsByClass("issue-header").get(0).getElementsByTag("span").get(0).text(); | ||
issue = "/issues/issue-" + number.replace("#", ""); | ||
} | ||
Article article = new Article(); | ||
article.setTitle(title); | ||
article.setBrief(brief); | ||
article.setLink(link); | ||
article.setDomain(domain); | ||
article.setIssue(issue); | ||
article.setImageUrl(imageUrl); | ||
article.setSection(currentSection); | ||
articles.add(article); | ||
} | ||
} | ||
return articles; | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
app/src/main/java/com/github/mzule/androidweekly/api/parser/OlderArticlesParser.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,69 @@ | ||
package com.github.mzule.androidweekly.api.parser; | ||
|
||
import com.github.mzule.androidweekly.entity.Article; | ||
|
||
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.List; | ||
|
||
/** | ||
* Created by CaoDongping on 4/15/16. | ||
*/ | ||
public class OlderArticlesParser implements ArticleParser { | ||
|
||
@Override | ||
public List<Object> parse(String issue) throws IOException { | ||
Document doc = DocumentProvider.get(issue); | ||
List<Object> articles = new ArrayList<>(); | ||
Element root = doc.getElementsByClass("issue").get(0); | ||
while (root.children().size() == 1) { | ||
root = root.child(0); | ||
} | ||
String currentSection = null; | ||
for (Element e : root.children()) { | ||
if (e.tagName().equals("h2")) { | ||
currentSection = e.text(); | ||
articles.add(currentSection); | ||
continue; | ||
} | ||
if (e.tagName().equals("div")) { | ||
Elements img = e.getElementsByTag("img"); | ||
if (!img.isEmpty()) { | ||
Article article = new Article(); | ||
article.setImageUrl(img.get(0).attr("src")); | ||
article.setTitle(e.getElementsByTag("a").get(1).text()); | ||
article.setLink(e.getElementsByTag("a").get(1).attr("href")); | ||
article.setBrief(e.getElementsByTag("p").get(0).text()); | ||
Elements span = e.getElementsByTag("span"); | ||
if (!span.isEmpty()) { | ||
article.setDomain(span.get(0).text().replace("(", "").replace(")", "")); | ||
} | ||
article.setIssue(issue); | ||
article.setSection(currentSection); | ||
articles.add(article); | ||
} | ||
} else { | ||
Article article = new Article(); | ||
Elements title = e.getElementsByTag("a"); | ||
if (title.isEmpty()) { | ||
continue; | ||
} | ||
article.setTitle(title.get(0).text()); | ||
Elements span = e.getElementsByTag("span"); | ||
if (!span.isEmpty()) { | ||
article.setDomain(span.get(0).text().replace("(", "").replace(")", "")); | ||
} | ||
article.setLink(e.getElementsByTag("a").get(0).attr("href")); | ||
article.setBrief(e.text()); | ||
article.setIssue(issue); | ||
article.setSection(currentSection); | ||
articles.add(article); | ||
} | ||
} | ||
return articles; | ||
} | ||
} |