44
55import com .brian .csdnblog .Env ;
66import com .brian .csdnblog .datacenter .preference .CommonPreference ;
7+ import com .brian .csdnblog .manager .TypeManager ;
78import com .brian .csdnblog .model .BlogInfo ;
9+ import com .brian .csdnblog .model .Bloger ;
810import com .brian .csdnblog .util .JsoupUtil ;
911import com .brian .csdnblog .util .LogUtil ;
1012import com .brian .csdnblog .util .Md5 ;
@@ -59,14 +61,48 @@ public static OsChinaHtmlParser getInstance() {
5961 @ Override
6062 public List <BlogInfo > getBlogList (int type , String strHtml ) {
6163 try {
62- return doGetBlogList (type , strHtml );
64+ if (TypeManager .getCateType (type ) == TypeManager .TYPE_CAT_BLOGER ) {
65+ return doGetBlogerItemList (type , strHtml );
66+ } else {
67+ return doGetBlogList (type , strHtml );
68+ }
6369 } catch (Exception e ) {
6470 e .printStackTrace ();
6571 MobclickAgent .reportError (Env .getContext (), e );
6672 return null ;
6773 }
6874 }
6975
76+ private List <BlogInfo > doGetBlogerItemList (int type , String str ) {
77+ List <BlogInfo > list = new ArrayList <>();
78+ if (TextUtils .isEmpty (str )) {
79+ return list ;
80+ }
81+ // LogUtil.d("str=" + str);
82+ // 获取文档对象
83+ Document doc = Jsoup .parse (str );
84+ // 获取class="article_item"的所有元素
85+ Element blogs = doc .getElementById ("list" );
86+ if (blogs == null ) {
87+ return list ;
88+ }
89+ Elements blogList = blogs .getElementsByClass ("list-item" );
90+
91+ for (Element blogItem : blogList ) {
92+ BlogInfo item = new BlogInfo ();
93+ item .title = blogItem .getElementsByClass ("title" ).get (0 ).select ("a" ).text (); // 得到标题
94+ item .link = blogItem .getElementsByClass ("title" ).get (0 ).select ("a" ).attr ("href" );
95+ item .summary = "" ;
96+ item .extraMsg = blogItem .getElementsByClass ("time" ).get (0 ).text ();
97+ item .blogId = Md5 .getMD5ofStr (item .link );
98+
99+ item .type = type ;
100+
101+ list .add (item );
102+ }
103+ return list ;
104+ }
105+
70106 private List <BlogInfo > doGetBlogList (int type , String str ) {
71107 List <BlogInfo > list = new ArrayList <>();
72108 if (TextUtils .isEmpty (str )) {
@@ -92,6 +128,20 @@ private List<BlogInfo> doGetBlogList(int type, String str) {
92128
93129 item .type = type ;
94130
131+ String homePageUrl = blogItem .select ("a" ).get (0 ).attr ("href" );
132+ if (!TextUtils .isEmpty (homePageUrl )) {
133+ Bloger bloger = new Bloger ();
134+ bloger .blogerType = type ;
135+ bloger .nickName = blogItem .select ("a" ).get (0 ).select ("img" ).attr ("title" );
136+ bloger .headUrl = blogItem .select ("a" ).get (0 ).select ("img" ).attr ("src" );
137+ bloger .homePageUrl = homePageUrl ;
138+ bloger .blogerID = Bloger .getBlogerId (bloger .homePageUrl );
139+
140+ // BlogerTable.getInstance().insert(bloger);//保存用户信息
141+ item .blogerJson = bloger .toJson ();
142+ item .blogerID = bloger .blogerID ;
143+ }
144+
95145 list .add (item );
96146 }
97147 return list ;
@@ -101,6 +151,7 @@ public String getBlogContent(int type, String contentSrc) {
101151 try {
102152 return doGetBlogContent (contentSrc );
103153 } catch (Exception e ) {
154+ LogUtil .printError (e );
104155 MobclickAgent .reportError (Env .getContext (), e );
105156 return "" ;
106157 }
0 commit comments