12
12
13
13
import com .brian .csdnblog .Env ;
14
14
import com .brian .csdnblog .manager .Constants ;
15
+ import com .brian .csdnblog .manager .TypeManager ;
15
16
import com .brian .csdnblog .model .BlogInfo ;
16
17
import com .brian .csdnblog .datacenter .preference .CommonPreference ;
18
+ import com .brian .csdnblog .model .Bloger ;
17
19
import com .brian .csdnblog .util .JsoupUtil ;
18
20
import com .brian .csdnblog .util .LogUtil ;
19
21
import com .brian .csdnblog .util .Md5 ;
@@ -62,13 +64,54 @@ public static ITEyeHtmlParser getInstance() {
62
64
@ Override
63
65
public List <BlogInfo > getBlogList (int type , String strHtml ) {
64
66
try {
65
- return doGetBlogList (type , strHtml );
67
+ if (TypeManager .getCateType (type ) == TypeManager .TYPE_CAT_BLOGER ) {
68
+ return doGetBlogerItemList (type , strHtml );
69
+ } else {
70
+ return doGetBlogList (type , strHtml );
71
+ }
66
72
} catch (Exception e ) {
67
- e . printStackTrace ( );
73
+ LogUtil . printError ( e );
68
74
MobclickAgent .reportError (Env .getContext (), e );
69
75
return null ;
70
76
}
71
77
}
78
+
79
+
80
+ private List <BlogInfo > doGetBlogerItemList (int type , String str ) {
81
+ List <BlogInfo > list = new ArrayList <>();
82
+ if (TextUtils .isEmpty (str )) {
83
+ return list ;
84
+ }
85
+ // LogUtil.d("str=" + str);
86
+ // 获取文档对象
87
+ Document doc = Jsoup .parse (str );
88
+ // 获取class="article_item"的所有元素
89
+ Elements blogList = doc .getElementsByClass ("blog_main" );
90
+ if (blogList == null ) {
91
+ return list ;
92
+ }
93
+
94
+ for (Element blogItem : blogList ) {
95
+ BlogInfo item = new BlogInfo ();
96
+ item .title = blogItem .select ("h3" ).select ("a" ).text (); // 得到标题
97
+ item .link = blogItem .select ("h3" ).select ("a" ).attr ("href" );
98
+ if (item .link .startsWith ("/" )) {
99
+ item .link = getBlogBaseUrl () + item .link ;
100
+ }
101
+ item .summary = blogItem .getElementsByClass ("blog_content" ).first ().text ();
102
+ item .blogId = Md5 .getMD5ofStr (item .link );
103
+
104
+ Element msgElement = blogItem .getElementsByClass ("blog_bottom" ).get (0 );
105
+ msgElement .getElementsByClass ("comment" ).remove ();
106
+ msgElement .getElementsByClass ("view" ).remove ();
107
+ msgElement .getElementsByClass ("digged" ).remove ();
108
+ item .extraMsg = msgElement .text ();
109
+ item .type = type ;
110
+
111
+ list .add (item );
112
+ }
113
+ return list ;
114
+ }
72
115
73
116
private List <BlogInfo > doGetBlogList (int type , String str ) {
74
117
List <BlogInfo > list = new ArrayList <>();
@@ -89,6 +132,9 @@ private List<BlogInfo> doGetBlogList(int type, String str) {
89
132
BlogInfo item = new BlogInfo ();
90
133
item .title = blogItem .select ("h3" ).select ("a" ).text (); // 得到标题
91
134
item .link = blogItem .select ("h3" ).select ("a" ).attr ("href" );
135
+ if (item .link .startsWith ("/" )) {
136
+ item .link = getBlogBaseUrl () + item .link ;
137
+ }
92
138
item .summary = blogItem .getElementsByIndexEquals (1 ).text ();
93
139
item .blogId = Md5 .getMD5ofStr (item .link );
94
140
@@ -99,6 +145,20 @@ private List<BlogInfo> doGetBlogList(int type, String str) {
99
145
item .extraMsg = msgElement .text ();
100
146
item .type = type ;
101
147
148
+ String homePageUrl = blogItem .getElementsByClass ("blog_info" ).get (0 ).select ("a" ).attr ("href" );
149
+ if (!TextUtils .isEmpty (homePageUrl )) {
150
+ Bloger bloger = new Bloger ();
151
+ bloger .blogerType = type ;
152
+ bloger .nickName = blogItem .getElementsByClass ("blog_info" ).get (0 ).select ("a" ).text ();
153
+ bloger .headUrl = blogItem .getElementsByClass ("logo" ).get (0 ).select ("img" ).attr ("src" );
154
+ bloger .homePageUrl = homePageUrl ;
155
+ bloger .blogerID = Bloger .getBlogerId (bloger .homePageUrl );
156
+
157
+ // BlogerTable.getInstance().insert(bloger);//保存用户信息
158
+ item .blogerJson = bloger .toJson ();
159
+ item .blogerID = bloger .blogerID ;
160
+ }
161
+
102
162
list .add (item );
103
163
}
104
164
return list ;
0 commit comments