Chinaunix首页 | 论坛 | 博客
  • 博客访问: 579569
  • 博文数量: 190
  • 博客积分: 10937
  • 博客等级: 上将
  • 技术积分: 2205
  • 用 户 组: 普通用户
  • 注册时间: 2009-04-07 11:28
文章分类

全部博文(190)

文章存档

2012年(1)

2011年(27)

2010年(20)

2009年(142)

我的朋友

分类: 系统运维

2009-09-19 10:52:17

开心农场外挂,自动登录,自动杀虫、除草、浇水、翻地、种植、偷窃,每小时自动运行一次(可设置)
需要安装json包,然后填写自己的校内网用户名和密码即可
sudo apt-get install python-json

#!/usr/bin/env python
#farm.py encoding: utf-8
import urllib,urllib2,cookielib
import time,zlib,re,md5
import cStringIO,gzip
import json
class Farm:
       debug=False
       status={} #self's farm status
       friends={} #list of friends
       shop={} #information of seeds
       uid=0 #user id
       autoweed=True #自动除草
       autonorm=True #自动杀虫
       autowater=True #自动浇水
       autoplant=True #自动种植
       autosteal=True #自动收获
       #创建Cookie
       def __init__(self):
               cj=cookielib.LWPCookieJar()
               opener=urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
               opener.addheaders=[('User-agent','Opera/9.23')]
               urllib2.install_opener(opener)
       # 请求网页的工具函数,直接返回网页内容
       def req(self,url,body=()):
               if len(body)==0:
                       req=urllib2.Request(url)
               else:
                       req=urllib2.Request(url,urllib.urlencode(body))
               raw=urllib2.urlopen(req)
               if self.debug: #输出调试信息
                       print url
                       print raw.headers
               return raw.read()

       def param(self): # build params farmKey and farmTime
               farmTime=str(time.time())[0:10]
               farmKey=md5.md5(farmTime+'15l3h4kh').hexdigest()
               return (farmKey, farmTime)
       # 登录校内网 获取校内网到开心网的链接地址,并取得Cookie
       def login(self, email, password):
               url=""
               body=(("email",email),("password",password))
               ret=self.req(url,body)
               uids=re.findall('http\:\/\/xiaonei\.com\/profile\.do\?id\=(\d+)',ret)
               if len(uids)>0:
                       self.uid=uids[0]
               else:
                       print ret
               # build cookie to faminutes
               url=''
               url=re.findall('iframe_canvas" src="([^"]+)"',self.req(url))[0]
               url=url.replace('?','/?') # fuck url, it lacks a '/'
               req=urllib2.Request(url)
               raw=urllib2.urlopen(req)
               # read current status
               url='%s&farmTime=%s&inuId=' % self.param()
               self.status=json.read(self.req(url))
       # 获取所有好友
       def listFriends(self):
               url="=" % self.param()
               body=({"refresh":"true"})
               self.friends=json.read(self.req(url,body))
       # 获取某好友信息
       def showFriend(self,fid):
               url='%s&farmTime=%s&inuId=' % self.param()
               body={'ownerId':fid}
               return json.read(self.req(url,body))
       # 浇水 fid: 用户ID place:土地编号
       def water(self,fid,place):
               url='%s&farmTime=%s&inuId=' % self.param()
               body={'ownerId':fid,'place':place}
               return json.read(self.req(url,body))
       # 除草 fid:用户ID place:土地编号
       def clearWeed(self,fid,place):
               url='%s&farmTime=%s&inuId=' % self.param()
               body={'ownerId':fid,'place':place}
               return json.read(self.req(url,body))
       # 杀虫 fid:用户ID place: 土地编号
       def spraying(self,fid,place):
               url='%s&farmTime=%s&inuId=' % self.param()
               body={'ownerId':fid,'place':place,'tId':0}
               return json.read(self.req(url,body))
       # 获取商店列表
       def scanShop(self):
               url='%s&farmTime=%s&inuId=' % self.param()
               self.shop=json.read(self.req(url))
               return self.shop
       # 查看自己购买的物品
       def getBag(self):
               url='%s&farmTime=%s&inuId=' % self.param()
               return json.read(self.req(url))
       # 翻土
       def scarity(self,fid,place):
               url='%s&farmTime=%s&inuId=' % self.param()
               body={'ownerId':fid,'place':place}
               return json.read(self.req(url,body))

       # 种植 cid: 种子编号 uid: 用户编号 place: 土地编号
       def plant(self,cid,uid,place):
               url='%s&farmTime=%s&inuId=' % self.param()
               body={'cId':cid,'ownerId':uid,'place':place}
               return json.read(self.req(url,body))
       # 某好友的果实成熟时间列表
       def find(self,fid):
               self.scanShop()
               url='%s&farmTime=%s&inuId=' % self.param()
               body={'ownerId':fid}
               ret=json.read(self.req(url,body))
               lands=ret['farmlandStatus']
               result={}
               i=0
               now=int(time.time())
               for land in lands:
                       seeds=[x for x in self.shop if x['cId']==land['a']]
                       if len(seeds) == 1:
                               durtime=int(seeds[0]['growthCycle'])+land['q']
                               if durtime > now:#尚未成熟
                                       result[i]='%s:\t%s' % (seeds[0]['cName'],str(durtime));
                               else:#已经成熟
                                       if self.uid in land['n']: #已经偷过
                                               pass
                                       else:#
                                               result[i]='%s:\tReady' % seeds[0]['cName']
                       i+=1
               return result

       # 偷好友的果子 fid:好友编号 place:土地编号
       def steal(self,fid,place):
               url='%s&farmTime=%s&inuId=' % self.param()
               body={"ownerId":fid,"place":place}
               return json.read(self.req(url,body))
  
       # 查找果实列表
       def autofind(self):
               self.listFriends()
               result={}
               for f in farm.friends: #输出所有好友
                       result[f['userId']]=farm.find(f['userId'])
               return result
       # 自动偷果子
       def autosteal(self):
               goods=self.autofind()
               nexttime=time.time() #下次偷果子的时间
               for friend in goods:
                       for place in goods[friend]:
                               # 偷果子
                               if goods[friend][place].endswith('Ready'):
                                       self.steal(friend,place)
                               else:
                                       t=int(re.findall('(\d+)',goods[friend][place])[0])
                                       if t < nexttime:
                                               nexttime = t
               return nexttime
       # 自动运行
       def autorun(self):
               if len(self.friends)==0:
                       self.listFriends()
               if len(self.shop)==0:
                       self.scanShop()
               data={}
               now=int(time.time())
               friendsid={}
               bag=self.getBag() #背包
              
               for friend in self.friends:
                       data[friend['userName']]=self.showFriend(friend['userId'])
                       friendsid[friend['userName']]=friend['userId']
               for friend in data:#每个好友
                       print friend
                       lands=data[friend]['farmlandStatus']
                       i=-1

                       for land in lands:#每块地
                               i+=1
                               s=[x for x in self.shop if x['cId']==land['a']]
                               if len(s)==1:#不是空地
                                       durtime=land['q']+int(s[0]['growthCycle'])
                                       if durtime < now: #已经成熟
                                               if self.uid in land['n']:
                                                       status=u'偷过'
                                               else:
                                                       status=u'可偷'
                                                       if self.autosteal:
                                                               self.steal(friendsid[friend],i)
                                       else: #未成熟
                                               status=str((durtime-now)/3600)

                                       if land['f'] > 0:
                                               weed=str(land['f'])+u'草'
                                               while self.autoweed and land['f']>0:
                                                       self.clearWeed(friendsid[friend],i)
                                                       land['f']-=1
                                       else:
                                               weed=''
                                       if land['h']==0:
                                               han=u'干旱'
                                               if self.autowater:
                                                       self.water(friendsid[friend],i)
                                       else:
                                               han=''
                                       if land['g']==1:
                                               norm=str(land['g'])+u'虫子'
                                               while self.autonorm and land['g']>0:
                                                       self.spraying(friendsid[friend],i)
                                                       land['g']-=1
                                       else:
                                               norm=''

                                       print '\t%s\t%s\t%s\t%s\t%s' % (
                                                       s[0]['cName'],
                                                       status,
                                                       weed,
                                                       han,
                                                       norm)
                               elif friendsid[friend]==self.uid and self.autoplant: #自己的空地
                                       self.scarity(self.uid,i) #翻地
                                       for seed in bag:
                                               if seed['type']==1:
                                                       farm.plant(seed['cId'],1842469710,i)
                                                       break
                               else:
                                       print u'\t空地'


if __name__=='__main__':
       farm = Farm()
       farm.login("校内网用户名","校内网密码") #登录
       interval=1*3600 #每隔一小时运行一次
       while True:
               goods=farm.autorun() #自动运行
               time.sleep(interval)

阅读(2199) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~