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

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

效果如图:

 

/**
 * 日志下载修正版核心功能类
 * 
 * @author wensefu.jerry.Ling<br/>
 *         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<Blog> getAllBlog(String qq) {
		HttpClient client = new HttpClient();
		HttpMethod method = null;
		List<Blog> result = new ArrayList<Blog>();
		List<String> blogIds = new ArrayList<String>();
		List<String> blogTitles = new ArrayList<String>();
		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<Blog> 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上托管,有兴趣的朋友可以一起研究

刷人气功能之一。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值