java实现QQ空间日志列表获取

可以下载任意QQ用户的日志,以html文档形势保存。(该用户必须开放所有用户访问权限)

效果如图:

 

java实现QQ空间日志列表获取_第1张图片

/**
 * 日志下载修正版核心功能类
 * 
 * @author wensefu.jerry.Ling
* wrote on 2011-1-26 */ public class Kernel { private static final String baseUri = "http://b.qzone.qq.com/cgi-bin/blognew/blog_get_titlelist?"; private static final String blogdetailUri = "http://b.qzone.qq.com/cgi-bin/blognew/blog_output_data?bdm=b.qzone.qq.com&"; private static final String charset = "gb2312"; public Kernel() { } /** * 获取用户日志数量 * * @param qq * @return */ public int getBlogNo(String qq) { HttpClient client = null; HttpMethod method = null; int result = 0; String getUri = baseUri + "uin=" + qq + "&vuin=0&property=GoRE&category=&numperpage=1&sorttype=0&arch=0&pos=0&direct=1"; method = new GetMethod(getUri); client = new HttpClient(); int status = 0; try { status = client.executeMethod(method); } catch (HttpException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } if (status != HttpStatus.SC_OK) { return 0; } InputStream is = null; InputStreamReader isr = null; BufferedReader br = null; try { is = method.getResponseBodyAsStream(); if (is == null) return 0; isr = new InputStreamReader(is); br = new BufferedReader(isr); String temp = null; while (true) { temp = br.readLine(); if (temp == null) return 0; if (temp.contains("title_num")) { break; } } String cnt = temp.substring(12, temp.length() - 1); result = Integer.parseInt(cnt); } catch (IOException e) { return 0; } finally { method.releaseConnection(); try { br.close(); isr.close(); is.close(); } catch (IOException e) { e.printStackTrace(); } } return result; } /** * 获取所有日志的ID,标题信息 * * @param qq * @return */ public List getAllBlog(String qq) { HttpClient client = new HttpClient(); HttpMethod method = null; List result = new ArrayList(); List blogIds = new ArrayList(); List blogTitles = new ArrayList(); int cnt = 0; int pos = 0; // String getUri = null; int status = 0; int blogNo = getBlogNo(qq); while (cnt < blogNo) { getUri = baseUri + "uin=" + qq + "&vuin=0&property=GoRE&category=&numperpage=100&pos=" + pos + "&direct=1"; method = new GetMethod(getUri); try { status = client.executeMethod(method); } catch (HttpException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } if (status != HttpStatus.SC_OK) { break; } InputStream is = null; InputStreamReader isr = null; BufferedReader br = null; try { is = method.getResponseBodyAsStream(); isr = new InputStreamReader(is); br = new BufferedReader(isr); String temp = null; while ((temp = br.readLine()) != null) { if (temp.contains("/"title/":")) { int index = temp.lastIndexOf("/"title/":/""); String title = temp.substring(index + 9, temp.length() - 3); blogTitles.add(title); } if (temp.contains("{/"blogid/":")) { int index = temp.lastIndexOf("{/"blogid/":"); String blogid = temp.substring(index + 10, temp.length() - 1); blogIds.add(blogid); cnt++; } } } catch (IOException e) { e.printStackTrace(); } finally { method.releaseConnection(); try { br.close(); isr.close(); is.close(); } catch (IOException e) { e.printStackTrace(); } } pos += 100; } for (int i = 0; i < blogIds.size(); i++) { Blog blog = new Blog(blogIds.get(i), blogTitles.get(i)); result.add(blog); } return result; } /** * 以html格式保存日志到本地 * * @param qq */ public void saveBlogtoLocal(String basepath, String qq, int index) { List blogs = getAllBlog(qq); if (blogs == null) { System.err.println("获取日志信息失败或用户没有日志"); return; } HttpClient client = new HttpClient(); HttpMethod method = null; String getUri = null; getUri = blogdetailUri + "blogid=" + blogs.get(index).getBlogid() + "&imgdm=ctc.qzs.qq.com&mode=2&numperpage=15&property=GoRE&uin=" + qq; method = new GetMethod(getUri); method.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, charset); int status = 0; try { status = client.executeMethod(method); } catch (HttpException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } if (status != HttpStatus.SC_OK) { return; } BufferedWriter bw = null; try { String toWrite = method.getResponseBodyAsString(); if (toWrite != null) { String title = blogs.get(index).getTitle(); File path = new File(basepath + "//" + qq + "//"); if (!path.exists()) { path.mkdir(); } File file = new File(path, (index + 1) + "-" + title + ".html"); if (!file.exists()) { file.createNewFile(); } bw = new BufferedWriter(new FileWriter(file)); bw.write(toWrite); } } catch (IOException e) { e.printStackTrace(); } finally { method.releaseConnection(); if (bw != null) { try { bw.flush(); bw.close(); } catch (IOException e) { e.printStackTrace(); } } } } public static void main(String[] args) throws IOException { new Kernel().saveBlogtoLocal("e:/blogs", "123456", 0); } }
 
 
 
  

程序下载地址:http://code.google.com/p/qhelperx/downloads/list

源码已经放到google svn上托管,有兴趣的朋友可以一起研究

刷人气功能之一。

你可能感兴趣的:(个人作品)