用python写的开心网收菜偷菜的小工具

        其实原理很简单,首先用FirFox的插件HttpFox来获取登录、查看菜地和偷菜的http请求,再使用python的urllib来模拟发送HTTP请求,对回应数据进行分析,就OK了。

        第一步:登录

        第二部:进入花园,获取verify代码

        第三步:查看有成熟菜的好友列表

        第四步:查看某个好友的菜地信息

        第五步:开偷

 

 

        代码如下:

 

#-*- coding:utf-8 -*-
import urllib
import socket
import http.cookiejar 
import random
import time
import sys


class Farm:
    
    cj = http.cookiejar.CookieJar()
    is_login = False
    user = ""    
    password = ""
    verify = ""
    steal_list = []
    
    def __init__(self,user,password,steal_list):
        self.user = user
        self.password = password
        self.steal_list = steal_list.split(",")
        
    def login(self):
        url = "http://www.kaixin001.com/login/login.php";
        opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(self.cj))
        print("正在登陆开心网....")
        f = opener.open(url,"url=/home/&email=" + self.user + "&password=" + self.password)
        f.read()
        url = "http://www.kaixin001.com/!house/garden/index.php"
        print("正在进入农场....")
        f = opener.open(url)
        data = f.read()        
        idx = str(data).find("var g_verify = \"");        
        if idx == -1:
            return
        idx2 = str(data).find("\"",idx + 18)
        if idx2 == -1:
            return
        is_login = True
        self.verify = str(data)[idx+16:idx2]
        print("获取Key=" + self.verify)

    def _havest_one(self,farm_num,fuid):
        if self.verify == "":
            print("用户尚未登录!")
            return
        opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(self.cj))
        url = "http://www.kaixin001.com/!house/!garden/havest.php?farmnum=" + farm_num + "&seedid=0&fuid=" + fuid + "&r=" + str(random.uniform(0,1))
        f = opener.open(url)
        data = str(f.read().decode("utf-8"))
        
        if not self.validate(data) :
            self.login()
            return
        
        index1 = data.find("<ret>")
        index2 = data.find("</ret>")
        result = data[index1+5:index2]
        
        if result == "succ":            
            name = data[data.find("<seedname>")+10:data.find("</seedname>")]
            if fuid != "0":
                print("★★☆☆成功偷了ID(" + fuid + ")家的" + name + "!★★☆☆")
            else:
                print("★★☆☆成功收获了自己的" + name + "!★★☆☆")
        else:
            index1 = data.find("<reason>")
            index2 = data.find("</reason>")
            reason = data[index1+8:index2]
            if fuid != "0":
                print("!!!!偷ID(" + fuid + ")家的菜失败,失败原因:" + reason + "!")
            else:
                print("!!!!收获自己家的菜失败,失败原因:" + reason + "!")
        time.sleep(3)

    def validate(self,str):
        if str.find("您需要登录后才能继续浏览或操作") != -1:
            return False
        return True

    
    def havest(self,fuid):
        if self.verify == "":
            print("用户尚未登录!")
            return
        
        opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(self.cj))
        url = "http://www.kaixin001.com/!house/!garden/getconf.php?verify=" + self.verify + "&fuid=" + fuid + "&r=" + str(random.uniform(0,1))
        f = opener.open(url)
        data = f.read()
        result = str(data.decode("utf-8"))
        if not self.validate(result) :
            self.login()
            return
            
        index = result.find("<farmnum>")
        name = result[result.find("<name>")+6:result.find("</name>")]
        while index != -1:
            index2 = result.find("</farmnum>",index)
            num = result[index+9:index2]
            index3 = result.find("<cropsstatus>",index)
            index4 = result.find("</cropsstatus>",index)
            
            if index3 == -1 or index4 == -1:
                index = result.find("<farmnum>",index+10)
                continue
            status = result[index3+13:index4]
            status_str = "状态未知(" + status + ")";
            if status == "1":
                status_str = "未成熟"
            if status == "2":
                status_str = "已成熟"
            if status == "3":
                status_str = "没东西"

            index3 = result.find("<name>",index)
            index4 = result.find("</name>",index)
            fname = "未知"
            if index3 != -1 and index4 != -1:
                fname = result[index3+6:index4]
            print(name + "的第" + num + "块地[" + fname + "]" + status_str)
            #成熟的
            if status == "2":
                flag = False
                for f in self.steal_list:
                    if f == fname :
                        self._havest_one(num,fuid)
                        flag = True
                if not flag:
                    print("●●○○" + fname + "太便宜了,不偷!!!!")
            index = result.find("<farmnum>",index+10)

    def friend_list(self):
        opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(self.cj))
        url = "http://www.kaixin001.com/!house/!garden/getfriendmature.php?verify=" + self.verify + "&r=" + str(random.uniform(0,1))
        f = opener.open(url)
        data = f.read()
        result = str(data.decode("utf-8"))
        index = result.find("\"uid\":")
        while index != -1:
            index2 = result.find(",",index)
            uid = result[index+6:index2]
            index3 = result.find("\"realname\":",index)
            index4 = result.find(",",index3)
            if index3 == -1 or index4 == -1:
                index = result.find("\"uid\":",index+3)
                continue
            name = result[index3+11:index4]

            print("uid=" + uid + ",name=" + name)

            self.havest(uid)

            time.sleep(5)
            
            index = result.find("\"uid\":",index+3)

 

f = Farm(sys.argv[1],sys.argv[2],sys.argv[3])
f.login()
while True:
    f.havest("0")
    time.sleep(10)
    f.friend_list()
    time.sleep(10)

  

 

 

 

使用方法:

1、下载安装python3.1

2、保存上边的文本为farm.py

3、命令行下敲入

;farm.py 开心网用户名 开心网密码 要偷的菜的列表(半角逗号分割)

你可能感兴趣的:(PHP,python,socket,F#)