def get(self): """Popula os objetos Hash para os pontos de uma linha""" self.response.headers['Content-Type'] = 'text/plain' key = self.request.get("key") linha = Linha.get(db.Key(key)) self.response.out.write('Linha: %s\n' % linha.nome) # Percorre os pontos dessa linha, processando os hashes dos segmentos nearhashAnterior = "" for ponto in linha.pontos: if ponto.nearhash: if ponto.nearhash == nearhashAnterior: self.response.out.write('hash repetido, pulando %s\n' % ponto.nearhash) continue nearhashAnterior = ponto.nearhash # if len(ponto.nearhash) != 6: # self.response.out.write('hash curto, pulando %s\n' % ponto.nearhash) # Se ja existe objeto para o hash, pega ele, senao cria um novo hashLista = Hash.all().filter("hash =", ponto.nearhash).fetch(1) if hashLista: self.response.out.write('Hash: %s ja existia \n' % ponto.nearhash) hash = hashLista[0] else: self.response.out.write('Hash: %s criado \n' % ponto.nearhash) hash = Hash(hash=ponto.nearhash) # Se a linha ainda nao esta associada ao objeto-hash, associa if str(linha.key()) not in hash.linhas: self.response.out.write('Linha adicionada a lista\n') hash.linhas.append(str(linha.key())) hash.put()
def get(self): self.response.headers['Content-Type'] = 'text/html' if self.request.get("hashes"): for hash in Hash.all(): self.response.out.write('<a href="/cachehash?hash=%s">%s</a><br/>' % (hash.hash, hash.hash)) else: for linha in Linha.all(): self.response.out.write('<a href="/cachelinha?key=%s">%s</a><br/>' % (str(linha.key()), linha.nome))
def get_info_hashes_linhas(self, lat, lng): """Retorna array JSON com as infos e hashes das linhas que passam num ponto""" hash = str(Geohash((lng, lat)))[0:6] chave_memcache = "linhas_por_hash_%s" % hash; linhas_ids = self.cache.get(chave_memcache) if linhas_ids is None: result = Hash.all().filter("hash =", hash).fetch(1) linhas_ids = json.loads(result[0].linhas) if result else [] self.cache.add(chave_memcache, linhas_ids) return json.dumps([json.loads(self.get_info_hashes_linha(linha)) for linha in linhas_ids], separators=(',',':'))
def get(self): hash = self.request.get("hash") chave_memcache = "hash_linhas_keys_" + hash client = memcache.Client() linhas_keys = client.get(chave_memcache) if linhas_keys is None: result = Hash.all().filter("hash =", hash).fetch(999) self.response.out.write("count %d " % len(result)) linhas_keys = result[0].linhas if result else [] client.add(chave_memcache, linhas_keys) self.response.out.write("gerou cache de %s " % chave_memcache) else: self.response.out.write("ja tinha cache de %s " % chave_memcache)
def get_info_hashes_linhas(self, lat, lng): """Retorna array JSON com as infos e hashes das linhas que passam num ponto""" hash = str(Geohash((lng, lat)))[0:6] chave_memcache = "linhas_por_hash_%s" % hash linhas_ids = self.cache.get(chave_memcache) if linhas_ids is None: result = Hash.all().filter("hash =", hash).fetch(1) linhas_ids = json.loads(result[0].linhas) if result else [] self.cache.add(chave_memcache, linhas_ids) return json.dumps([ json.loads(self.get_info_hashes_linha(linha)) for linha in linhas_ids ], separators=(',', ':'))
def put_hash(self, hash, linhas): """Atualiza um hash no banco (mesmo vazio) e anula seu cache. Retorna mensagem consumível pelo sptscraper""" try: self.cache.delete("linhas_por_hash_" + hash) hash_obj = Hash.all().filter("hash =", hash).fetch(1) if hash_obj: hash_obj = hash_obj[0] hash_obj.linhas = linhas else: hash_obj = Hash(hash = hash, linhas = linhas) hash_obj.put() return "OK HASH UPLOAD %s " % id except: return "ERRO HASH: %s" % sys.exc_info()[1]
def put_hash(self, hash, linhas): """Atualiza um hash no banco (mesmo vazio) e anula seu cache. Retorna mensagem consumível pelo sptscraper""" try: self.cache.delete("linhas_por_hash_" + hash) hash_obj = Hash.all().filter("hash =", hash).fetch(1) if hash_obj: hash_obj = hash_obj[0] hash_obj.linhas = linhas else: hash_obj = Hash(hash=hash, linhas=linhas) hash_obj.put() return "OK HASH UPLOAD %s " % id except: return "ERRO HASH: %s" % sys.exc_info()[1]
def get(self): self.response.headers['Content-Type'] = 'application/json' lat = float(self.request.get('lat')) lng = float(self.request.get('lng')) # Recupera as chaves das linhas que passam pelo geohash do ponto hash = models.calculaNearhash(lng, lat) chave_memcache = "hash_linhas_keys_" + hash; client = memcache.Client() linhas_keys = client.get(chave_memcache) if linhas_keys is None: result = Hash.all().filter("hash =", hash).fetch(1) linhas_keys = result[0].linhas if result else [] client.add(chave_memcache, linhas_keys) # Converte elas para objetos no formato da resposta e devolve como JSON linhas_obj = [self._linha_obj(key) for key in linhas_keys] linhas_json = simplejson.dumps(linhas_obj) callback = self.request.get("callback"); if callback: linhas_json = callback + "(" + linhas_json + ");" self.response.out.write(linhas_json)