fortianwei 发表于 2013-1-15 02:57:42

校内网狗狗刷骨机的java实现

上次为了图个方便,用ruby写了,貌似很多人不会ruby,那这次用ruby写了,而且上次要求知道狗狗的id,人的id和cookie,不是人人都会的,这次用java写的只需要输入用户名和密码就行了。代码大部分参考了:http://hi.baidu.com/superyhao/blog/item/2ab2dd1626d19f1f962b43b4.html
 
注意! 我的思想是先刷狗粮,因为玩耍可以获得骨头,每当骨头超过5个了就去买狗粮,所以最好现将你的骨头花玩。当积累了很多的狗粮之后,我们可以通过喂别人挨饿的狗狗来赚取骨头了。
用法:1.在main函数中将用户名和密码填上
         2.运行,得到狗粮
         3.狗粮很多了,将
GouGou.earnFoods(pid, fid, cookie);    //GouGou.earnBones(pid,fid,cookie);变成
   //GouGou.earnFoods(pid, fid, cookie);    GouGou.earnBones(pid,fid,cookie);就行了。
 
import java.io.BufferedReader;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStream;import java.io.PrintWriter;import java.io.StringReader;import java.net.HttpURLConnection;import java.net.URL;import java.util.HashSet;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.Set;import java.util.regex.Matcher;import java.util.regex.Pattern;import org.jdom.Document;import org.jdom.Element;import org.jdom.input.SAXBuilder;import org.xml.sax.InputSource;public class GouGou {private static String FLASHADDRESS="http://dog.xnimg.cn/dog/xn_swf/dogs_v0.14.swf";private static String MD5_KEY="%$T&*jkhjksdhfjk$%^&*BJHhh";private static String propsCont ="";// 初始化的页面public static String initPage( String pid, String fid, String cookie ) throws Exception {URL url = new URL("http://dog.xiaonei.com/pet-profile.do?method=dogXML&pid="+pid);//&t=1240806646390");//http://dog.xiaonei.com/pet-profile.do?method=dogXML&actor_id=" + fid + "&pid=" + pid );   String method = "GET";   String referer = FLASHADDRESS;   HttpURLConnection conn = getConn( url, method, cookie, referer );   return readContent( conn.getInputStream() );}// 玩耍public static String playDog( String pid, String fid, String cookie, String key ) throws Exception {   URL url = new URL("http://dog.xiaonei.com/pet-profile.do");   String method = "POST";   String referer = FLASHADDRESS;   HttpURLConnection conn = getConn( url, method, cookie, referer );   String code = key + "#" + pid + "#" + fid+"#"+MD5_KEY;   writeContent( conn.getOutputStream(), "actor%5Fid=" + fid + "&method=pelt&code="   + getMD5(code.getBytes()) + "&pid=" + pid );   return readContent( conn.getInputStream() );}// 喂狗, type = 1 是喂食, 2是喂水public static String feedDog( String pid, String fid, String cookie, String key, String type ) throws Exception {   URL url = new URL("http://dog.xiaonei.com/pet-profile.do");   String method = "POST";   String referer = FLASHADDRESS;   HttpURLConnection conn = getConn( url, method, cookie, referer );   String code = key + "#"+ pid +"#" + fid+"#"+MD5_KEY;   writeContent( conn.getOutputStream(), "actor%5Fid="+ fid +   "&active%5Fid=" + type + "&method=doing&code=" + getMD5(code.getBytes()) + "&pid=" + pid );   return readContent( conn.getInputStream() );}//进入商店,获取tscpublic static String getTsc( String cookie ) throws Exception {   URL url = new URL( "http://dog.xiaonei.com/store.do?method=food" );   String method = "GET";   String referer = "http://i.static.xiaonei.com/pet/swf/dog_v3.swf?ver=081111c";   HttpURLConnection conn = getConn( url, method, cookie, referer );   String cont = readContent( conn.getInputStream() );String tsc=cont.substring(cont.indexOf("tsc:'")+5, cont.indexOf("',urlDog"));return tsc; }// 买东西public static void buySomething( String cookie) throws Exception {   URL url = new URL("http://dog.xiaonei.com/store.do");   String method = "POST";   String referer = "http://dog.xiaonei.com/store.do?method=food";   HttpURLConnection conn = getConn( url, method, cookie, referer );   String cont = "method=buyfood&buy_id=1&buy_count=1&tsc=" + getTsc(cookie).trim();   writeContent( conn.getOutputStream(), cont );   String property=readContent( conn.getInputStream() );   System.out.println("====buy 5 foods========");   System.out.println(property);   //printProps(property);   conn.disconnect();}// 取得饥饿狗狗列表public static Set<String> getHungryList( String cookie ) throws Exception {   URL url = new URL( "http://dog.xiaonei.com/hall.do?method=hungry" );   String method = "GET";   String referer = FLASHADDRESS;   HttpURLConnection conn = getConn( url, method, cookie, referer );   String cont = readContent( conn.getInputStream() );   Set<String> list = new HashSet<String>();   Pattern pt = Pattern.compile( "[<]a[ ]href=[\"][/]pet-profile.do[?]pid=(.+?)[\"][>]" );   Matcher mh = pt.matcher( cont );   while( mh.find() )    list.add( mh.group(1) );   return list;}// 取得链接private static HttpURLConnection getConn( URL url, String method, String cookie, String referer ) throws Exception {   HttpURLConnection conn = (HttpURLConnection) url.openConnection();   conn.setDoInput(true);   conn.setDoOutput(true);   conn.setRequestMethod(method);   conn.addRequestProperty( "Cookie", cookie);   conn.addRequestProperty("Accept", "*/*");   conn.addRequestProperty("Accept-Language", "zh-CN");   conn.addRequestProperty( "Referer", referer );   conn.addRequestProperty("x-flash-version", "9,0,124,0"); //conn.setInstanceFollowRedirects(true);   conn.connect();   return conn;}public static String getCookie(String userName,String password)throws Exception {URL url=null;HttpURLConnection conn=null;url=new URL("http://login.xiaonei.com/Login.do");conn=(HttpURLConnection)url.openConnection();conn.setDoInput(true);conn.setDoOutput(true);conn.setRequestMethod("POST");conn.setRequestProperty("Host", "login.xiaonei.com");conn.setRequestProperty("User-Agent", "Internet Explorer");//conn.setRequestProperty("Cookie", cookie0);conn.setInstanceFollowRedirects(false);//conn.setRequestProperty("Cookie", cookie0);if(userName.contains("@"))userName.replace("@", "%40");String cont="email="+userName+"&password="+password+"&origURL=http%3A%2F%2Fwww.xiaonei.com%2FSysHome.do";writeContent( conn.getOutputStream(), cont );conn.connect();Map<String,List<String>> hm2=conn.getHeaderFields();List<String> cookieList2=hm2.get(new String("Set-Cookie"));Iterator it2=cookieList2.iterator();StringBuffer sb2=new StringBuffer("");while(it2.hasNext()){String str=(String)it2.next();String s=str.substring(0,str.indexOf("domain"));sb2.append(s);}String cookie=sb2.toString();System.out.println(cookie);//System.out.println("return is :"+readContent(conn.getInputStream()));conn.disconnect();return cookie;}private static String[] getIds(String userName,String password,String cookie) {URL url=null;HttpURLConnection conn=null;String[] strings=null;try{url=new URL("http://dog.xiaonei.com/pet-profile.do");conn=(HttpURLConnection)url.openConnection();conn.setDoInput(true);conn.setDoOutput(true);conn.setRequestMethod("GET");conn.setRequestProperty("Cookie", cookie);conn.connect();String cont=readContent(conn.getInputStream());Pattern pt = Pattern.compile( "PET.profile.init(.*)" );Matcher mh = pt.matcher(cont);mh.find();strings=mh.group(1).split(",");}catch(Exception e){System.out.println("用户名或密码不正确!");}    return strings;}public static String getFid(String userName,String password,String cookie){String fid="";try{    fid=GouGou.getIds(userName, password,cookie);}catch(Exception e){return "wrong";}return fid;}public static String getPid(String userName,String password,String cookie)throws Exception{String pid=GouGou.getIds(userName, password,cookie);return pid;}private static void writeContent( OutputStream os, String content ) throws Exception {   PrintWriter pw = new PrintWriter(os);   pw.write( content );   pw.flush();   pw.close();}private static String readContent( InputStream is ) throws Exception {   BufferedReader br = new BufferedReader(new InputStreamReader(is));   String line = "";   StringBuilder sb = new StringBuilder();   while ((line = br.readLine()) != null)    sb.append(line + "\n");   br.close();   return new String(sb.toString().getBytes(), "utf-8");}public static String getMD5(byte[] source) {   String s = null;   char hexDigits[] = { // 用来将字节转换成 16 进制表示的字符   '0', '1', '2', '3', '4', '5', '6', '7',   '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };   try {    java.security.MessageDigest md =   java.security.MessageDigest.getInstance("MD5");    md.update(source);    byte tmp[] = md.digest(); // MD5 的计算结果是一个 128 位的长整数,    // 用字节表示就是 16 个字节    char str[] = new char; // 每个字节用 16 进制表示的话,使用两个字符,    // 所以表示成 16 进制需要 32 个字符    int k = 0; // 表示转换结果中对应的字符位置    for (int i = 0; i < 16; i++) { // 从第一个字节开始,对 MD5 的每一个字节   // 转换成 16 进制字符的转换   byte byte0 = tmp; // 取第 i 个字节   str = hexDigits; // 取字节中高 4 位的数字转换,   // >>> 为逻辑右移,将符号位一起右移   str = hexDigits; // 取字节中低 4 位的数字转换    }    s = new String(str); // 换后的结果转换为字符串   } catch (Exception e) {    e.printStackTrace();   }   return s;}private static String getInitKeyText( String str, String key ) throws Exception {   SAXBuilder sb=new SAXBuilder();   StringReader sr = new StringReader( str );   InputSource isu = new InputSource( sr );   Document doc=sb.build( isu ); //构造文档对象   Element root=doc.getRootElement(); //获取根元素         return root.getChild( key ).getText();}public static String getFoods() throws Exception{return getPropKey( propsCont, "user_foods" ).toString();}public static String getBones() throws Exception{return getPropKey( propsCont, "user_bones" ).toString();}private static String getPropKey( String str, String key ) throws Exception {   SAXBuilder sb=new SAXBuilder();   StringReader sr = new StringReader( str );   InputSource isu = new InputSource( sr );   Document doc=sb.build( isu ); //构造文档对象   Element root=doc.getRootElement(); //获取根元素         return root.getAttributeValue( key );}// 锻炼经验,增加狗粮方案public static void earnFoods( String pid, String fid, String cookie ) throws Exception {   //int forceNum = (int) (25 * Math.random()) + 20;// System.out.println( forceNum );   String initCont = initPage( pid, fid, cookie ); // System.out.print(initCont);   String propsKey = getInitKeyText( initCont, "key" );   Thread.sleep( (long)(Math.random() * 3) * 1000 );   propsCont = playDog( pid, fid, cookie, propsKey );   propsKey = getPropKey( propsCont, "key" );   Thread.sleep( (long)(Math.random() * 3 + 3) * 1000 );   while( true ) {    double maxForce = Double.parseDouble( getPropKey( propsCont, "max_force" ) );    double force = Double.parseDouble( getPropKey( propsCont, "force" ) );    int bones = Integer.parseInt( getPropKey( propsCont, "user_bones" ) );    String thirsty=getPropKey(propsCont,"is_pet_thirsty");    if( bones >= 5 ) break;    if(thirsty.equals("true"))    {    propsCont = feedDog( pid, fid, cookie, propsKey, "2" );      propsKey = getPropKey( propsCont, "key" );      System.out.println( "============feed dog with water==========" );    }    if( force/maxForce<=0.8) {   propsCont = feedDog( pid, fid, cookie, propsKey, "1" );   propsKey = getPropKey( propsCont, "key" );   System.out.println( "============feed dog with food==========" );    } else {   propsCont = playDog( pid, fid, cookie, propsKey );   propsKey = getPropKey( propsCont, "key" );   System.out.println( "============dog play==========" );    }    printProps( propsCont );    Thread.sleep( (long)(Math.random() * 3 + 5) * 1000 );   }   buySomething( cookie );   earnFoods( pid, fid, cookie );}// 增加爱心, 提高骨头public static void earnBones( String pid, String fid, String cookie ) throws Exception {   Set<String> set = getHungryList( cookie );   for( String str : set ) {    System.out.println( "for:" + str );       String initCont = initPage( str, fid, cookie );    String propsKey = getInitKeyText( initCont, "key" );    Thread.sleep( 5000 );       propsCont = feedDog( str, fid, cookie, propsKey, "2" );    propsKey = getPropKey( propsCont, "key" );    Thread.sleep( 5000 );       propsCont = feedDog( str, fid, cookie, propsKey, "1" );    printProps( propsCont );    propsKey = getPropKey( propsCont, "key" );   }   earnBones(pid, fid, cookie);}private static void printProps( String cont ) throws Exception {   System.out.println( "user_bones:" + getPropKey( cont, "user_bones" ) );   System.out.println( "user_foods:" + getPropKey( cont, "user_foods" ) );}public static void main(String[] args) throws Exception {    String userName="XXXX";    String password="XXXX";String pid = "";   String fid = "";   String cookie=GouGou.getCookie(userName, password);    fid=GouGou.getFid(userName, password,cookie).trim();    if(fid.equals("wrong")){}    else{    pid=GouGou.getPid(userName,password,cookie).trim();    System.out.println("pid:"+pid);    System.out.println("fid:"+fid);    System.out.println("tsc:"+GouGou.getTsc(cookie));    GouGou.earnFoods(pid, fid, cookie);    //GouGou.earnBones(pid,fid,cookie);    }}}/*返回的狗狗配置单示例<data get_bone="-1" key="HNtql6ux1g" user_bones="4" user_foods="29" expe="11.0" level="0" force="74" max_force="100" heart="38" max_heart="100"charm="0" is_force_available="true" is_pet_thirsty ="false" is_pet_hungry="false" heart_level="1"heart_poke=""charm_level="0"></data>*/
页: [1]
查看完整版本: 校内网狗狗刷骨机的java实现