package com.zuoxiaolong.reptile;
/*
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import com.zuoxiaolong.config.Configuration;
import com.zuoxiaolong.dao.*;
import com.zuoxiaolong.model.Status;
import com.zuoxiaolong.model.Type;
import com.zuoxiaolong.orm.DaoFactory;
import com.zuoxiaolong.util.EnrypyUtil;
import com.zuoxiaolong.util.IOUtil;
import com.zuoxiaolong.util.ImageUtil;
import com.zuoxiaolong.util.JsoupUtil;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.Node;
import org.jsoup.nodes.TextNode;
import org.jsoup.select.Elements;
import javax.net.ssl.*;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author 左潇龙
* @since 2015年5月12日 下午9:24:21
*/
public abstract class Cnblogs {
private static final Logger logger = Logger.getLogger(Cnblogs.class);
private static final String loginUrl = "https://passport.cnblogs.com/user/signin";
private static final String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCp0wHYbg/NOPO3nzMD3dndwS0MccuMeXCHgVlGOoYyFwLdS24Im2e7YyhB0wrUsyYf0/nhzCzBK8ZC9eCWqd0aHbdgOQT6CuFQBMjbyGYvlVYU2ZP7kG9Ft6YV6oc9ambuO7nPZh+bvXH0zDKfi02prknrScAKC0XhadTHT3Al0QIDAQAB";
private static final String username = "左潇龙";
private static class DefaultX509TrustManager implements X509TrustManager {
@Override
public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
}
@Override
public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
}
private static class DefaultHostnameVerifier implements HostnameVerifier {
@Override
public boolean verify(String s, SSLSession sslSession) {
return true;
}
}
private static final String loginArticleListUrl = "https://i.cnblogs.com/posts?page=";
public static void fetchArticlesAfterLogin() throws IOException {
String cookie;
try {
cookie = login();
} catch (Exception e) {
logger.error("login failed ...", e);
return;
}
for (int i = 1; ; i++) {
if (logger.isInfoEnabled()) {
logger.info("begin fetch the " + i + " page...");
}
Document document = Jsoup.parse(getHtmlUseCookie(cookie, loginArticleListUrl + i, true));
Element mainElement = document.getElementById("post_list");
Elements elements = mainElement.getElementsByTag("tr");
int pageSize = 0;
for (int j = 1 ; j < elements.size(); j++) {
try {
fetchArticle(elements.get(j),cookie);
if (logger.isInfoEnabled()) {
logger.info("fetch success for pagenumber : " + i + ", pagesize : " + (pageSize + 1));
}
} catch (Throwable e) {
logger.error("fetch failed for pagenumber : " + i + ", pagesize : " + (pageSize + 1), e);
}
pageSize++;
}
if (pageSize < 10) {
break;
}
}
}
private static String login() throws Exception {
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, new TrustManager[]{new DefaultX509TrustManager()}, new SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
HttpsURLConnection.setDefaultHostnameVerifier(new DefaultHostnameVerifier());
HttpsURLConnection loginPageConnection = (HttpsURLConnection) new URL(loginUrl).openConnection();
loginPageConnection.setRequestProperty("Connection","keep-alive");
loginPageConnection.setRequestMethod("GET");
loginPageConnection.connect();
String body = IOUtil.read(loginPageConnection.getInputStream());
String cookie = loginPageConnection.getHeaderField("Set-Cookie");
Pattern pattern = Pattern.compile("'VerificationToken':\\s*?'(.*?)'");
Matcher matcher = pattern.matcher(body);
String token = null;
if (matcher.find()) {
token = matcher.group(1);
}
HttpsURLConnection loginConnection = (HttpsURLConnection) new URL(loginUrl).openConnection();
loginConnection.setDoInput(true);
loginConnection.setDoOutput(true);
loginConnection.setRequestMethod("POST");
loginConnection.setRequestProperty("Connection", "keep-alive");
loginConnection.setRequestProperty("Content-Type", "application/json; charset=utf-8");
loginConnection.setRequestProperty("VerificationToken",token);
loginConnection.setRequestProperty("Cookie",cookie);
loginConnection.setRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 6.3; WOW64; rv:37.0) Gecko/20100101 Firefox/37.0");
loginConnection.setRequestProperty("Referer","http://passport.cnblogs.com/user/signin?ReturnUrl=http%3A%2F%2Fhome.cnblogs.com%2Fu%2Fzuoxiaolong%2F");
loginConnection.setRequestProperty("X-Requested-With","XMLHttpRequest");
loginConnection.connect();
OutputStream outputStream = loginConnection.getOutputStream();
Map<String,Object> params = new HashMap<>();
String username = Configuration.get("cnblogs.username.product");
String password = Configuration.get("cnblogs.password.product");
params.put("input1",EnrypyUtil.publicEnrypy(publicKey, username));
params.put("input2",EnrypyUtil.publicEnrypy(publicKey, password));
params.put("remember", false);
outputStream.write(JSONObject.fromObject(params).toString().getBytes("UTF-8"));
outputStream.flush();
String json = IOUtil.read(loginConnection.getInputStream());
if (JSONObject.fromObject(json).containsKey("success") && JSONObject.fromObject(json).getBoolean("success")) {
List<String> cooks = loginConnection.getHeaderFields().get("Set-Cookie");
return cooks.get(0) + ";" + cooks.get(1);
} else {
throw new RuntimeException("login failed ...");
}
}
private static void fetchArticle(Element element,String cookie) throws IOException {
Elements tdElements = element.getElementsByTag("td");
Element subjectTdElement = tdElements.get(0);
Element subjectElement = subjectTdElement.getElementsByTag("a").first();
String articleUrl = subjectElement.attr("href");
String originArticleHtml = getHtmlUseCookie(cookie, articleUrl, false);
Document articleDocument = Jsoup.parse(originArticleHtml);
Map<String, String> imageMap = saveImage(articleDocument);
//获取标题
String subject = subjectElement.html().trim();
//获取postid
String resourceId = element.attr("id").split("\\-")[2];
Integer postId = Integer.valueOf(resour
没有合适的资源?快使用搜索试试~ 我知道了~
一个简单的个人博客源码
共456个文件
java:132个
js:77个
ftl:60个
4星 · 超过85%的资源 需积分: 47 97 下载量 89 浏览量
2016-10-17
11:40:39
上传
评论 2
收藏 5.71MB ZIP 举报
温馨提示
personal-blog-webapp 该项目是本人个人博客源码,欢迎大家使用,但请仔细阅读Apache License 2.0。 示例网站地址 www.zuoxiaolong.com
资源推荐
资源详情
资源评论
收起资源包目录
一个简单的个人博客源码 (456个子文件)
github_1.css 308KB
github_2.css 227KB
skin.min.css 34KB
skin.ie7.min.css 34KB
skin.ie7.min.css 34KB
skin.min.css 33KB
jquery-ui.min.css 29KB
github_button.css 13KB
shCoreEclipse.css 9KB
shCoreDjango.css 9KB
shCoreFadeToGrey.css 9KB
shCoreDefault.css 9KB
shCoreMidnight.css 8KB
shCoreRDark.css 8KB
shCoreEmacs.css 8KB
shCoreMDUltra.css 8KB
index.css 8KB
base.css 7KB
shCore.css 6KB
visualblocks.css 5KB
article.css 4KB
shThemeEclipse.css 3KB
shThemeDjango.css 3KB
shThemeFadeToGrey.css 3KB
shThemeDefault.css 3KB
shThemeMidnight.css 3KB
shThemeRDark.css 3KB
shThemeEmacs.css 3KB
shThemeMDUltra.css 3KB
content.min.css 1KB
content.min.css 1KB
content.inline.min.css 1KB
content.inline.min.css 1KB
code.css 961B
list.css 85B
tinymce-small.eot 10KB
tinymce.eot 10KB
tinymce-small.eot 9KB
tinymce.eot 9KB
remark.ftl 10KB
user_center_main.ftl 8KB
bottom.ftl 6KB
article_main.ftl 5KB
article_input.ftl 4KB
message_list.ftl 4KB
question_main.ftl 4KB
insert_code.ftl 4KB
right.ftl 4KB
dota_index.ftl 3KB
question.ftl 3KB
article.ftl 2KB
upload_image.ftl 2KB
user_login.ftl 2KB
question_input_main.ftl 2KB
chart.ftl 2KB
record_input.ftl 2KB
about_me_main.ftl 2KB
article_manager.ftl 2KB
match_input.ftl 2KB
record_manager.ftl 2KB
record.ftl 2KB
ask.ftl 1KB
banner.ftl 1KB
statistics.ftl 1KB
match_input_main.ftl 1KB
article_list_main.ftl 1KB
new_comment.ftl 1KB
index_main.ftl 1KB
header.ftl 964B
head.ftl 928B
feed.ftl 894B
login.ftl 833B
new_comments.ftl 812B
question_list_main.ftl 713B
admin_index.ftl 706B
record_list_main.ftl 697B
link.ftl 632B
random_article.ftl 588B
about_me.ftl 555B
right.ftl 550B
record_main.ftl 549B
dota_index_main.ftl 509B
page.ftl 499B
question_list.ftl 497B
record_list.ftl 495B
tag.ftl 487B
follow_me.ftl 477B
article_list.ftl 474B
index.ftl 422B
meta.ftl 415B
question_input.ftl 405B
right.ftl 403B
user_center.ftl 402B
right.ftl 398B
search.ftl 378B
right.ftl 333B
right.ftl 333B
error.ftl 291B
footer.ftl 217B
header_logo.gif 12KB
共 456 条
- 1
- 2
- 3
- 4
- 5
资源评论
- 石头瓜2018-08-27为什么运行不起来呢?
GeekyGuru
- 粉丝: 1594
- 资源: 1099
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功