Skip to content

Commit

Permalink
persistencia ok
Browse files Browse the repository at this point in the history
  • Loading branch information
Robson committed Jun 6, 2017
1 parent 8c53b5c commit fd122c3
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 10 deletions.
13 changes: 12 additions & 1 deletion app/contador.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import app.texto as texto
import app.twitter as twitter
import app.mongo_database as mongo
import app.redis_database as rd
from collections import Counter


Expand All @@ -15,7 +17,16 @@ def buscarTermo(termo):

for item in count:
listagem.append({'text':item,"size":count[item]})
id = mongo.salvar(termo,listagem)
rd.salvar(termo,id)
return listagem


def verificar(termo):
id = rd.checar(termo)
print(id)
if(id):
dados = mongo.recuperar(id)
return dados['resultados']
else:
return buscarTermo(termo)

15 changes: 10 additions & 5 deletions app/mongo_database.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import pymongo
from pymongo import MongoClient
import pprint
from bson.objectid import ObjectId

cliente = MongoClient('***REMOVED***')
db = cliente.nuvempalavras #define database used

teste = db.teste
def salvar(termo,resultados):
buscas = db.buscas
busca = {'termo':termo,'resultados':resultados}
return buscas.insert_one(busca).inserted_id

#teste
import pprint
for item in teste.find():
pprint.pprint(item)
def recuperar(identificador):
buscas = db.buscas
dados = buscas.find_one({'_id': ObjectId(identificador)})
return dados
7 changes: 5 additions & 2 deletions app/redis_database.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import redis

#substituir por variaveis de ambiente
r = redis.StrictRedis(host='***REMOVED***', port=18605, password='***REMOVED***')
r = redis.StrictRedis(host='***REMOVED***', port=18605, password='***REMOVED***',charset="utf-8", decode_responses=True)

def checar(termo):
if r.exists(termo):
Expand All @@ -10,4 +10,7 @@ def checar(termo):
return False

def salvar(termo,valor):
r.set(termo,valor)
r.set(termo,valor,ex=60)

def recentes():
return r.keys()
3 changes: 1 addition & 2 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from flask import Flask
from flask import request
from flask import render_template
import app.redis_database as rd
import app.contador as cont

app = Flask(__name__)
Expand All @@ -18,7 +17,7 @@ def sobre():

@app.route('/busca/<termo>')
def busca(termo=''):
palavras = cont.buscarTermo(termo)
palavras = cont.verificar(termo)
return render_template('index.html',valores=palavras)


Expand Down

0 comments on commit fd122c3

Please sign in to comment.