1212
1313import com .brian .csdnblog .Env ;
1414import com .brian .csdnblog .manager .Constants ;
15+ import com .brian .csdnblog .manager .TypeManager ;
1516import com .brian .csdnblog .model .BlogInfo ;
1617import com .brian .csdnblog .datacenter .preference .CommonPreference ;
18+ import com .brian .csdnblog .model .Bloger ;
1719import com .brian .csdnblog .util .JsoupUtil ;
1820import com .brian .csdnblog .util .LogUtil ;
1921import com .brian .csdnblog .util .Md5 ;
@@ -62,13 +64,54 @@ public static ITEyeHtmlParser getInstance() {
6264 @ Override
6365 public List <BlogInfo > getBlogList (int type , String strHtml ) {
6466 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+ }
6672 } catch (Exception e ) {
67- e . printStackTrace ( );
73+ LogUtil . printError ( e );
6874 MobclickAgent .reportError (Env .getContext (), e );
6975 return null ;
7076 }
7177 }
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+ }
72115
73116 private List <BlogInfo > doGetBlogList (int type , String str ) {
74117 List <BlogInfo > list = new ArrayList <>();
@@ -89,6 +132,9 @@ private List<BlogInfo> doGetBlogList(int type, String str) {
89132 BlogInfo item = new BlogInfo ();
90133 item .title = blogItem .select ("h3" ).select ("a" ).text (); // 得到标题
91134 item .link = blogItem .select ("h3" ).select ("a" ).attr ("href" );
135+ if (item .link .startsWith ("/" )) {
136+ item .link = getBlogBaseUrl () + item .link ;
137+ }
92138 item .summary = blogItem .getElementsByIndexEquals (1 ).text ();
93139 item .blogId = Md5 .getMD5ofStr (item .link );
94140
@@ -99,6 +145,20 @@ private List<BlogInfo> doGetBlogList(int type, String str) {
99145 item .extraMsg = msgElement .text ();
100146 item .type = type ;
101147
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+
102162 list .add (item );
103163 }
104164 return list ;
0 commit comments