def building(self, planet, build, wait=False, timeout=10):
        def post_request(url):
            page = self.fetch_valid_response(url).read()
            token = self.REGEXPS['token'].search(page)

            token = token.group(1)

            if '<li id="%s" class="on">' % (build_entry['slot']) in page:
                params = {'modus': '1', 'token': token, 'type':build_entry['type']}
                if planet.code:
                    params['cp'] = planet.code
                if build_entry.get('build_params'):
                        params.update(build_entry.get('build_params'))
                request = urllib2.Request(url, urllib.urlencode(params))
                resp = self.fetch_valid_response(request)
                self.logger.info('Upgrading %s on %s%s' % (build_entry['name'], planet.name, planet.coord))
                my_sleep(2)
                return True
            else:
                return False

        build_entry = self.master.config.building[build]
        url = self.base_urls[build_entry['section']]

        if planet.code:
            url = url + '&cp=%s' % planet.code

        if wait:
            while not post_request(url):
                my_sleep(10)
        else:
            return post_request(url)
    def spy(self, target):

        while not (self.get_free_fleet_slots > 0):
            my_sleep(2)

        request = urllib2.Request(self.base_urls['espionage'],
                                  urllib.urlencode({'galaxy':target[0], 'system':target[1], 'position':target[2], 'shipCount':3, 'type':1, 'mission':6}))
        resp = self.fetch_valid_response(request)
        result = resp.read()
        if result.endswith("[%s]" % format_coord(target)):
            return True
        else:
            return False
    def fetch_valid_response(self, request, skip_validate_check=False):
        self._mutex.acquire()

        if isinstance(request, str):
            request = urllib2.Request(request)
        if self.last_fetched_url:
            request.add_header('Referer', self.last_fetched_url)

        valid = False
        while not valid:
            valid = True
            try:
                response = self.opener.open(request)
                self.last_fetched_url = response.geturl()
                
                cached_response = StringIO.StringIO(response.read())
                
                p = cached_response.getvalue()
                cached_response.seek(0)

                self.last_fetched_page = p

#                if skip_validate_check:
#                    return cached_response
#                elif 'You attempted to log in' in p:
#                    raise BotFatalError("Invalid username and/or password.")
#                elif not p or 'errorcode=8' in self.last_fetched_url:
#                    valid = False
#		#NOTE \ FIX THIS if this fails then just comment it ???
                if 'DB problem' in p:
                    my_sleep(120)
                    old_session = self.session
                    self.do_login()
                    print self.session
                    print request.get_full_url()
                    url = request.get_full_url().replace(old_session, self.session)
                    data = request.get_data()
                    if data: data = data.replace(old_session, self.session)
                    request = urllib2.Request(url, data)
                    valid = False
            except urllib2.HTTPError, e:
                if e.code == 302: # happens once in a while when user and bot are playing simultaneusly.
                    raise BotError()
                else: raise e
            except (urllib2.URLError, httplib.IncompleteRead, httplib.BadStatusLine), e:
                valid = False
        def post_request(url):
            page = self.fetch_valid_response(url).read()
            token = self.REGEXPS['token'].search(page)

            token = token.group(1)

            if '<li id="%s" class="on">' % (build_entry['slot']) in page:
                params = {'modus': '1', 'token': token, 'type':build_entry['type']}
                if planet.code:
                    params['cp'] = planet.code
                if build_entry.get('build_params'):
                        params.update(build_entry.get('build_params'))
                request = urllib2.Request(url, urllib.urlencode(params))
                resp = self.fetch_valid_response(request)
                self.logger.info('Upgrading %s on %s%s' % (build_entry['name'], planet.name, planet.coord))
                my_sleep(2)
                return True
            else:
                return False
    def statistic(self):
        self.fetch_page('statistic')
        my_sleep(2)
        request = urllib2.Request(self.base_urls['statistic_content'],
                    urllib.urlencode({'sort_per_member':0, 'start_at':-1, 'type':'ressources', 'who':'player'}))
        self.fetch_page('events')

        page = self.fetch_valid_response(request).read()

        soup = BeautifulSoup(page)

        values = [int(op['value']) for op in soup.find("select").findAll("option")[1:]]

        count = 0
        records = []
        for v in values:
            records = records + self.get_statistic(v)
            count = count + 1
            print v
            if count % 10 == 0:
                print 'relogin'
                my_sleep(120)
                self.relogin()
        return records
                    data = request.get_data()
                    if data: data = data.replace(old_session, self.session)
                    request = urllib2.Request(url, data)
                    valid = False
            except urllib2.HTTPError, e:
                if e.code == 302: # happens once in a while when user and bot are playing simultaneusly.
                    raise BotError()
                else: raise e
            except (urllib2.URLError, httplib.IncompleteRead, httplib.BadStatusLine), e:
                valid = False
            except Exception, e:
                if "timed out" in str(e):
                    valid = False
                else: raise e
            if not valid:
                my_sleep(120)

        self._mutex.release()
        return cached_response

    def generate_base_urls(self):
        self.base_urls = {
            'overview': 'http://%s.ogame.org/game/index.php?page=overview&session=%s&lgn=1' % (self.account.universe, self.session),
            'resource': 'http://%s.ogame.org/game/index.php?page=resources&session=%s' % (self.account.universe, self.session),
            'station': 'http://%s.ogame.org/game/index.php?page=station&session=%s' % (self.account.universe, self.session),
            'fleet': 'http://%s.ogame.org/game/index.php?page=fleet1&session=%s' % (self.account.universe, self.session),
            'galaxy': 'http://%s.ogame.org/game/index.php?page=galaxyContent&session=%s&ajax=1' % (self.account.universe, self.session),
            'message': 'http://%s.ogame.org/game/index.php?page=messages&session=%s' % (self.account.universe, self.session),
            'msg' : 'http://%s.ogame.org/game/index.php?page=showmessage&session=%s&ajax=1' % (self.account.universe, self.session),
            'espionage': 'http://%s.ogame.org/game/index.php?page=minifleet&session=%s&ajax=1' % (self.account.universe, self.session),
            'movement': 'http://%s.ogame.org/game/index.php?page=movement&session=%s' % (self.account.universe, self.session),