Chinaunix首页 | 论坛 | 博客
  • 博客访问: 193730
  • 博文数量: 26
  • 博客积分: 1121
  • 博客等级: 少尉
  • 技术积分: 499
  • 用 户 组: 普通用户
  • 注册时间: 2009-10-12 14:24
文章分类
文章存档

2011年(3)

2010年(10)

2009年(13)

我的朋友

分类:

2009-10-14 11:58:53

特此声明:此版本校内开心农场外挂由python语言编写,运行平台为linux。本代码是修改网络上流传的源码而成,本人贴出此代码没有任何利益想法,只当学习交流之用,并感谢源码开发者!
#!/usr/bin/env python
#encoding: utf-8
#2009-8-21 升级后
import urllib,urllib2,cookielib
import time,zlib,re,md5
import cStringIO,gzip
import json
import pickle
import thread,sys,os
class Farm:
        debug=False
        status={} #self's farm status
        friends={} #list of friends
        shop={} #information of seeds
        bag={} #背包
        uid=0 #user id
        autoweed=True #自动除草
        autonorm=True #自动杀虫
        autowater=True #自动浇水
        autofert=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))
                while True:
                        try:
                                raw=urllib2.urlopen(req)
                                break
                        except:
                                print 'ReqError % url
                                pass
           
                if self.debug: #输出调试信息
                        print url
                        print body
                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\:\/\/renren\.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('?','/?').replace('&','&') # fuck url, it lacks a '/'
                raw=self.req(url)
                # read current status
                url='%s&farmTime=%s&inuId=' % self.param()
                self.status=json.read(self.req(url))
        # 获取所有好友
        def listFriends(self):
                url="=" % self.param()
                self.friends=json.read(self.req(url))
        # 获取某好友信息
        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,'tId':0,'place':place}
                return json.read(self.req(url,body))
        # 获取商店列表
        def scanShop(self):
                url='%s&farmTime=%s&inuId=' % self.param()
                print url
                self.shop=json.read(self.req(url))
                return self.shop
        # 查看自己购买的物品
        def getBag(self):
                url='%s&farmTime=%s&inuId=' % self.param()
                self.bag=json.read(self.req(url))
                return self.bag
        # 翻土
        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 fert(self,place):
                url='%s&farmTime=%s&inuId=' % self.param()
                body={'ownerId':self.uid,'tId':1,'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
                for land in lands:
                        seeds=[x for x in self.shop['1'] if x['cId']==land['a']]
                        if len(seeds) == 1:
                                durtime=int(seeds[0]['growthCycle'])+land['q']
                                if durtime > time.time():#尚未成熟
                                        result[i]='%s:\t%s' % (seeds[0]['cName'],str(durtime));
                                else:#已经成熟
                                        if 1==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 harvest(self,place):
                url='%s&farmTime=%s&inuId=' % self.param()
                body={'ownerId':self.uid,'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 moneyGo(self,n=1):
                for i in range(n):
                        print i
                        self.scarity(self.uid,0)
                        self.plant(2,self.uid,0)
        # 自动运行 默认最小间隔20分钟
        def autorun(self,interval=20*60):
                if len(self.friends)==0:
                        self.listFriends()
                if len(self.shop)==0:
                        self.scanShop()
                self.bag=self.getBag() #背包
                ret={} #下次运行时间
  print self.friends['data']
                for friend in self.friends['data']:
                        print (time.ctime()+friend['userName']).encode('utf-8')
                        lands=self.showFriend(friend['userId'])['farmlandStatus']
                        i=-1
                        for land in lands:#每块地
                                i+=1
                                s=[x for x in self.shop['1'] if x['cId']==land['a']]
                                if len(s)==0:#空地
                                        print u'\t%d\t空地'.encode('utf-8') % i
                                if len(s)==1:#不是空地
                                        durtime=land['q']+int(s[0]['growthCycle'])
                                        if durtime < time.time(): #已经成熟
                                                if friend['userId']==self.uid: #自己的地,收获
                                                        if land['b']==6: #成熟
                                                                self.harvest(i)
                                                                stealed=u'收获'
                                                        elif land['b']==7: #已经收获
                                                                stealed=u'已收'
                                                        else:
                                                                stealed=u'未知'
                                                        if self.autoplant: #自动种植
                                                                if land['b'] == 6 and land['j']==s[0]['maturingTime'] or land['b']==7:
                                                                        self.scarity(self.uid,i) #翻地
                                                                        for seed in self.bag['1']: #种值 if seed['type']==1:
                                                                                        self.plant(seed['cId'],self.uid,i)
                                                                                        stealed+=u'已种'
                                                                                        self.getBag()
                                                                                        break
                                                else:#别人的地
                                                        if 1==land['n']:
                                                                stealed=u'偷过'
                                                        elif land['b']==7:
                                                                stealed=u'已收'
                                                        elif land['m']==land['l']:
                                                                stealed=u'偷光'
                                                        elif self.autosteal: #偷
                                                                temp=self.steal(friend['userId'],i)
                                                                if temp.has_key('harvest'):
                                                                    stealed=u'偷得%d' % temp['harvest']
                                                                elif temp['direction']=='':
                                                                    print temp
                                                                else:
                                                                    print temp
                                                                    raise Exception
                                        else: #未成熟
                                                if (durtime-int(time.time())) < interval:#开新线程偷
                                                        print 'set Thread %s' % time.ctime(durtime)
                                                        if friend['userId']==self.uid:
                                                                temp = -1
                                                        else:
                                                                temp = friend['userId']
                                                        thread.start_new_thread(threadSteal,
                                                                        (self,durtime,temp,i))
                                                if not ret.has_key(friend['userId']) or ret[friend['userId']] > durtime:
                                                        ret[friend['userId']]=durtime
                                                stealed=time.ctime(durtime)
                                                if friend['userId']==self.uid and self.autofert: #自动施肥
                                                        temp=(int(time.time())-land['q'])/(int(s[0]['growthCycle'])/5)-land['o']
                                                        if not temp < 0:
                                                                temp=self.fert(i)
                                                                if temp.has_key('direction'):
                                                                        stealed+=temp['direction']
                                                                else:
                                                                        stealed+=u'施肥'
                                        if land['f'] > 0:
                                                weed=str(land['f'])+u'草,已除'
                                                while self.autoweed and land['f']>0:
                                                        self.clearWeed(friend['userId'],i)
                                                        land['f']-=1
                                        else:
                                                weed=''
                                        if land['h']==0:
                                                han=u'干旱,已浇水'
                                                if self.autowater:
                                                        self.water(friend['userId'],i)
                                        else:
                                                han=''
                                        if land['t']>0 or land['g']>0:
                                                if self.autonorm:
                                                        while land['g']>0: #杀小虫
                                                                self.spraying(friend['userId'],i)
                                                                land['g']-=1
                                                                norm=u'捉虫1'
                                                        if land['t'] > 0:  #杀大虫
                                                                self.spraying(friend['userId'],i)
                                                                norm=u'杀虫1/%d' % land['t']
                                        else:
                                                norm=''
                                        temp=u'\t%d\t%s\t%s\t%s\t%s\t%s' % (
                                                        i,
                                                        s[0]['cName'],
                                                        stealed,
                                                        weed,
                                                        han,
                                                        norm)
                                        print temp.encode('utf-8')
                return ret.values()
#在时间T自动去偷
def threadSteal(farm, T, fid, place):
        time.sleep(T-int(time.time())+2)
        if fid==-1:
                temp=farm.harvest(place)
        else:
                temp=farm.steal(fid,place)
        if temp.has_key('harvest'):
                print u'Thread 偷得 %d at %s:%d'.encode('utf-8') % (temp['harvest'],fid,place)
        else:
                print u'Thread Failed %s at %s:%d'.encode('utf-8') % (temp,fid,place)
if __name__=='__main__':
        farm = Farm()
        farm.debug= True
        running  = True
        interval=20*60
       
        print u'请输入校内网用户邮箱:'.encode('utf-8')
        email=sys.stdin.readline()
        print u'请输入密码:'.encode('utf-8')
        password=sys.stdin.readline()
        farm.login(email[0:len(email)-1],password[0:len(password)-1])
        while running:
                farm.autorun(interval=interval) #自动运行
                print u'进入自动运行模式(Y/N)?'
                autorun = sys.stdin.read(1);
                print autorun
                if autorun == 'Y' or autorun == 'y':
                    print u'%s等待下次运行:%d分钟后'.encode('utf-8') % (time.ctime(),interval/60)
                    sys.stdout.flush()
                    time.sleep(interval)
                elif autorun == 'N' or autorun == 'n':
                    running = False
阅读(3251) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~