Skip to content

Instantly share code, notes, and snippets.

@mvallebr
Created February 9, 2018 00:29
Show Gist options
  • Select an option

  • Save mvallebr/b31cf3d6d9c44ca255a2eb62bffa9d34 to your computer and use it in GitHub Desktop.

Select an option

Save mvallebr/b31cf3d6d9c44ca255a2eb62bffa9d34 to your computer and use it in GitHub Desktop.
import json
from flask import make_response, jsonify, request
from api import app, mysql
@app.errorhandler(404)
def not_found(error):
return make_response(jsonify({'error': 'Not found'}), 404)
def all_results(keywords, num_pagina, tamanho_pagina):
connection = mysql.connect()
try:
with connection.cursor() as cursor:
# Read a single record
sql = "SELECT * FROM CAMPANHA WHERE PALAVRAS_CHAVE=%s"
cursor.execute(sql, (keywords))
campaign = cursor.fetchone()
if campaign is None:
return []
campaign_id = campaign[0]
with connection.cursor() as cursor:
# Read a single record
sql = """SELECT ID, ID_CAMPANHA, URL, DT_CRIACAO, DT_ALTERACAO, TITULO,
SUMARIO, CONTEUDO FROM RESULTADO_CAMPANHA WHERE ID_CAMPANHA=%s"""
cursor.execute(sql, (campaign_id))
yield from cursor.fetchall()
# for x in cursor.fetchall():
# yield x
finally:
connection.close()
# {
# "Operation": "11ac5f0d-199c-4427-8b36-658b9e3ce6d2",
# "Parameters": [
# {
# "Name": "TermoPesquisa",
# "Value": "JBS,Microsoft"
# },
# {
# "Name": "TermoPesquisa2",
# "Value": "JBS,Microsoft22"
# }
# ]
# }
def parse_parameters(req):
return {param["Name"]: param["Value"] for param in req["Parameters"]}
# {"TermoPesquisa": "JBS,Microsoft", "TermoPesquisa2": "JBS,Microsoft22"}
@app.route('/api/BDE', methods=['POST'])
def index():
print(request.content_type)
params = parse_parameters(request.get_json(force=True))
keywords = params["TermoPesquisa"].split(",")
num_pagina = params.get("NumeroPagina", 1)
tamanho_pagina = params.get("TamanhoPagina", 20)
results = all_results(" ".join(keywords), num_pagina, tamanho_pagina)
json_value = {
"News": [
{
"NaiveBayesClassification": "",
"TermoPesquisa": " ".join(keywords),
"RelevanceScore": 0,
"PolarityPositiveWords": [],
"NaiveBayesPositive": 0.0,
"Veiculo": "engine1",
"Imagem": "",
"PolarityNegativeWords": [],
"NaiveBayesScore": 0,
"PolarityClassification": "",
"NaiveBayesNegative": 0.0,
"Titulo": result[5],
"Data": result[3].strftime("%Y-%m-%dT%H:%M:%S"),
"Idioma": "",
"PolarityWeight": 0,
"PolarityScore": 1,
"Url": result[2],
"Descricao": result[6],
"Texto": ""
} for result in results
],
"ResultsCount": 1,
"Filter": {
"Sentiment": None,
"Sources": [],
"EndDate": None,
"PageSize": tamanho_pagina,
"PageNumber": num_pagina,
"Sort": 0,
"SearchTerms": keywords,
"InitialDate": None
}
}
result = {
"RequestDateTime": "2018-01-18T23:36:29.002861+00:00",
"ResponseId": "4180e645-9faf-499e-8095-29b0a4a7ae02",
"OK": True,
"Message": None,
"Results": [
{
"DataServiceFriendlyName": "MaisQClipping",
"MethodName": "IndexarNoticias",
"Data": [
{
"Key": "",
"JsonValue": json.dumps(json_value)
}
]
}
]
}
return json.dumps(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment