自动评论优快云下载资源
在优快云游荡多年,帐号上传的部分资源,编写了部分博客,同时下载了大量的资源,其中只下载为评论的剩余仅二百个,
如果手动进行评论耗时(优快云评论下载资源时间间隔要求60秒以上,填写评论大约5秒)大约 200 * (60s+5s) = 1300s = 216min = 3.6 hour,
此事相当的无聊啊,特用httpclient编写一个自动评论的小程序,直接修改优快云Pingjia.java中的的用户名和密码即可使用,支持cookie登录,本地文件保存登录的cookie。
优快云Pingjia.java
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import net.sf.json.JSONObject;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import com.teleframe.park.ImportParsFrom122Park;
/*
* @author : BlackShadowWalker http://blog.youkuaiyun.com/lanmo555
* @creation : 2014-7-26 下午10:25:40
* @title: 自动评论优快云下载资源
* @description :
* 登录优快云,打开我下载的资源列表,并自动评价资源(sleep 60s)
* 经常在优快云下载文件,但是大部分都没有进行评价,
* 并且每个资源的评价时间间隔需要在60s以上,手动进行评价费时、费力,
* 特写了一个自动评价的小程序
*
* 登录支持:
* 1. 用户名+密码登录
* 2. cookie登录
*/
public class 优快云Pingjia {
public Logger log = Logger.getLogger(ImportParsFrom122Park.class);
//使用之前修改下面的个人信息哦
private String username = "username";
private String password = "password";
private String loginurl = "https://passport.youkuaiyun.com/account/login";
private String loginouturl = "https://passport.youkuaiyun.com/account/logout?ref=toolbar";
private String BASE_DONWLOAD_URL = "http://download.youkuaiyun.com";
private String mydownloadurl = BASE_DONWLOAD_URL + "/my/downloads/1";
private String Referer = "https://passport.youkuaiyun.com";
//COOKIE 登录, 上次的cookie记录
private String LAST_COOKIE;
private String COOKIE_FILE = "csdn.cookie";
private int LOGIN_WIAT_TIME = 60 ; //s
long lastLoginTime = 0;
long thisLoginTime = 0;
Header[] cookieHeaders;
String cookies;
public static void main(String[] args) {
优快云Pingjia csdn = new 优快云Pingjia();
boolean done = false;
while(!done){
try {
csdn.AuotPingjia优快云();
done = true;
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
csdn.logout();
} catch (ClientProtocolException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
System.out.println("-------------Done----------");
}
public void setCookie(Header[] cookieHeaders){
if(cookieHeaders==null || cookieHeaders.length<1){
log.info("cookieHeaders is empty");
return;
}
cookies = "";
for(Header s : cookieHeaders){
String str = s.toString();
cookies += str.split(":")[1].split(";")[0]+";";
}
log.info("cookies: " +cookies);
}
public void sleep(int seconds){
for(int i=0; i<seconds; i++){
try {
Thread.sleep( 1000 );
System.out.print(".");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("");
}
public HttpClient getHttpClient(){
HttpClient httpClient = HttpClients.createDefault(); // new DefaultHttpClient(pccm, params);
return httpClient;
}
public void readCookieFromFile() throws IOException{
File file = new File(COOKIE_FILE);
if(!file.exists()){
log.info("cookie file is not exist : "+file.getAbsolutePath());
return ;
}
byte [] b = new byte[4096];
FileInputStream fis = new FileInputStream(file);
fis.read(b);
fis.close();
this.LAST_COOKIE = new String(b);
log.info("readed cookie from file : "+file.getAbsolutePath());
}
public void saveCookie2File(){
File file = new File(COOKIE_FILE);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
if(fos!=null){
fos.write(this.cookies.getBytes());
fos.close();
log.info("saved cookie 2 file : "+file.getAbsolutePath());
return ;
}
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
log.info("failed save cookie 2 file : "+file.getAbsolutePath());
}
public boolean login() throws ClientProtocolException, IOException{
this.logout();
if(lastLoginTime>0 && (System.currentTimeMillis()-lastLoginTime)<( LOGIN_WIAT_TIME*1000 ) ){
log.info("sleep "+LOGIN_WIAT_TIME+" s");
this.sleep(LOGIN_WIAT_TIME);
}
HttpClient httpclient = new DefaultHttpClient();
//设置 HttpClient 接收 Cookie,用与浏览器一样的策略
//httpclient.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
//让服务器知道访问源为浏览器
httpclient.getParams().setParameter(HttpMethodParams.USER_AGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36");
//get cookie
HttpGet get = new HttpGet(loginurl);
HttpResponse httpResponse = httpclient.execute(get);
String html = EntityUtils.toString(httpResponse.getEntity());
// log.info(html);
cookieHeaders = httpResponse.getHeaders("Set-Cookie");
this.setCookie(cookieHeaders);
get.releaseConnection();
List<NameValuePair> params = new ArrayList<NameValuePair>();
Document htmljsoup = Jsoup.parse(html);
Element form = htmljsoup.getElementById("fm1");
Elements hiddens = form.select("input[type=hidden]");
for(Element input : hiddens){
params.add(new BasicNameValuePair( input.attr("name"), input.attr("value")));
}
//login
HttpPost post = new HttpPost(loginurl);
params.add(new BasicNameValuePair( "rememberMe", "true"));
params.add(new BasicNameValuePair( "username", username));
params.add(new BasicNameValuePair( "password", password));
post.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
post.addHeader("Referer", Referer);
post.setHeader("Cookie", cookies);
httpResponse = httpclient.execute(post);
cookieHeaders = httpResponse.getHeaders("Set-Cookie");
this.setCookie(cookieHeaders);
html = EntityUtils.toString(httpResponse.getEntity());
htmljsoup = Jsoup.parse(html);
post.releaseConnection();
if(html.indexOf("redirect_back")>0){
this.saveCookie2File();//save cookie
log.info("----------------登录成功----------------");
return true;
}else{
Element errorMsg = htmljsoup.getElementById("error-message");
if(errorMsg!=null){
log.info("登录失败 : "+errorMsg.html());
if(errorMsg.html().indexOf("登录太频繁")>=0){
log.info("sleep "+LOGIN_WIAT_TIME+" s ...");
this.sleep(LOGIN_WIAT_TIME);
}
}else{
log.info("\n"+html);
log.info("登录失败");
}
return false;
}
}
/**
*
* <p>Title: getMyDownloadList</p>
* <p>Description: </p>
* @param mydownloadurls
* @return 返回包含 共 ? 页 的html
* @throws ClientProtocolException
* @throws IOException
*/
public String getMyDownloadList(List<String> mydownloadurls) throws ClientProtocolException, IOException{
log.info("query "+mydownloadurl);
//打开我的下载资源列表
HttpClient httpclient = new DefaultHttpClient();
HttpGet get = null;
get = new HttpGet(this.mydownloadurl);
get.addHeader("Referer", Referer);
get.setHeader("Cookie", cookies);
// 请求超时
httpclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 60000);
// 读取超时
httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 60000);
HttpResponse httpResponse = httpclient.execute(get);
cookieHeaders = httpResponse.getHeaders("Set-Cookie");
this.setCookie(cookieHeaders);
String html = EntityUtils.toString(httpResponse.getEntity());
get.releaseConnection();
// log.info(html);
if(html.indexOf("请您先登录")>0){
while( !this.login() ){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return null;
}
//get my downloads
Document htmljsoup = Jsoup.parse(html);
Elements btn_comments = htmljsoup.select("div.list-container dl dt div.btns a.btn-comment");
if(btn_comments!=null){
log.info("btn_comments " + btn_comments.size());
for(Element btn: btn_comments){
String url = BASE_DONWLOAD_URL + btn.attr("href");
log.info( url );
mydownloadurls.add(url);
}
}else{
log.info("btn_comments 0" );
}
//page_nav
Elements pagenav = htmljsoup.select("div.page_nav");
if(pagenav!=null){
return pagenav.html();
}
return null;
}
public void AuotPingjia优快云() throws ClientProtocolException, IOException{
this.readCookieFromFile(); //read cookie;
if(LAST_COOKIE!=null && !LAST_COOKIE.trim().isEmpty()){
//cookie login
this.cookies = LAST_COOKIE ;
log.info("cookie login ... @ " +this.cookies);
}else{
while(!login()){
log.info("try relogin");
}
}
List<String> mydownloadurls = new ArrayList<String>();
String pagenavhtml = getMyDownloadList(mydownloadurls);
// log.info("pagenavhtml="+pagenavhtml);//"共258个 共43页";
if(pagenavhtml!=null){
String regex = "共(\\d+)页";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(pagenavhtml);//"共258个 共43页";
int pagesum = 0;
if (matcher.find()) {
String group = matcher.group(1);
pagesum = Integer.parseInt( group);
System.out.println("pagesum = "+pagesum);
for(int i=2; i<pagesum; i++){
this.mydownloadurl = BASE_DONWLOAD_URL + "/my/downloads/"+i;
if(getMyDownloadList(mydownloadurls)==null)
i--;
}
}else {
System.out.println("no matches!!");
}
}
log.info("--------------- mydownloadurls -----------");
for(String url : mydownloadurls){
log.info(url);
}
//auto commit my downloads
this.autoCommitMyDownloads(mydownloadurls);
}
public void logout() throws ClientProtocolException, IOException{
HttpClient httpclient = new DefaultHttpClient();
HttpGet get = null;
get = new HttpGet(this.loginouturl);
get.setHeader("Cookie", cookies);
HttpResponse httpResponse = httpclient.execute(get);
String html = EntityUtils.toString(httpResponse.getEntity());
get.releaseConnection();
log.info("logout");
}
public void autoCommitMyDownloads(List<String> mydownloadurls) throws ClientProtocolException, IOException{
log.info("--------------- auto commit my downloads -----------");
log.info("mydownloadurls.size = "+mydownloadurls.size());
String BaseCommitUrl = "http://download.youkuaiyun.com/index.php/comment/post_comment?";
String notifyurl = "http://s3-im-notify.youkuaiyun.com/socket.io/1/xhr-polling/hwq6_sBm3oL2VmHbR7C7?t=1406445067806";
HttpClient httpclient = new DefaultHttpClient();
HttpGet get = null;
int nubmer = 0;
for(String url : mydownloadurls){
log.info(nubmer + ": commitparse from "+ url);
//String commiturl = "http://download.youkuaiyun.com/detail/kaile8324/681084#comment";
Pattern pattern = Pattern.compile("/(\\d+)#comment$");
Matcher matcher = pattern.matcher(url);
if (matcher.find()) {
String sourceid = matcher.group(1);
String content = "%E5%8F%AF%E4%BB%A5%E4%BD%BF%E7%94%A8%EF%BC%8C%E8%BF%98%E4%B8%8D%E9%94%99"; //UTF8
String commitUrl = BaseCommitUrl + "jsonpcallback=jsonp1406444925577" +
"&content="+content+
"&sourceid="+sourceid+"&rating=3&t="+System.currentTimeMillis();
log.info(nubmer++ + ": commiting... @ "+ commitUrl);
get = new HttpGet( commitUrl );
get.addHeader("Referer", Referer);
get.setHeader("Cookie", cookies);
//请求超时
httpclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 60000);
//读取超时
httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 60000);
boolean bposted = false;
while(bposted==false){
HttpResponse httpResponse = httpclient.execute(get);
cookieHeaders = httpResponse.getHeaders("Set-Cookie");
this.setCookie(cookieHeaders);
String html = EntityUtils.toString(httpResponse.getEntity());
get.releaseConnection();
log.info(html);
String jsonret = html; //"jsonp1406444925577({\"succ\":-1,\"msg\":\"\u60a8\u8fd8\u6ca1\u6709\u767b\u5f55,\u8bf7\u767b\u5f55\u53d1\u8868\u8bc4\u8bba\"})";
pattern = Pattern.compile("\\((.+?)\\)$");
matcher = pattern.matcher(jsonret);
if (matcher.find()) {
String group = matcher.group(1);
System.out.println(group);
try{
JSONObject json = JSONObject.fromObject(group);
if(json.getInt("succ")==-1){
while( !this.login()){
log.info("try relogin");
}
bposted = false;
}else{
if(json.getInt("succ")!=1)
log.info(json.getString("msg"));
this.sleep(61);
bposted = true;
}
}catch(Exception e){
e.printStackTrace();
}
}else {
bposted = true;
System.out.println("no matches! @ "+jsonret);
}
}
}else {
System.out.println("no matches!! @ "+url);
}
}//end of for
}//end of autoCommitMyDownloads()
}
运行截图