Commit 8656350d165d6e27e1f233bb413892065acd98f5
1 parent
eb4e16dc
Exists in
master
Merging WSCBot
Showing
6 changed files
with
43 additions
and
21 deletions
Show diff stats
LICENSE
development.ini
| 1 | [WSCBot] | 1 | [WSCBot] |
| 2 | ###Url de acesso ao modulo wscserver (terminar com '/') | 2 | ###Url de acesso ao modulo wscserver (terminar com '/') |
| 3 | -url_wscserver: http://http://10.209.8.39/wscserver/ | 3 | +url_wscserver: http://localhost/wscserver/ |
| 4 | 4 | ||
| 5 | ###Url de acesso ao Super Gerente | 5 | ###Url de acesso ao Super Gerente |
| 6 | -ip_superg: http://10.209.8.39/lbbulk/reg | 6 | +ip_superg: http://localhost/lbbulk/reg |
| 7 | 7 | ||
| 8 | ###Local do disco onde o arquivo será salvo temporariamente | 8 | ###Local do disco onde o arquivo será salvo temporariamente |
| 9 | filepath: /tmp/extract/ | 9 | filepath: /tmp/extract/ |
| 10 | 10 | ||
| 11 | +###Nome da fonte dos dados (precisa ser um nome já existente na base do super-gerente) | ||
| 12 | +source_name: brlight | ||
| 13 | + | ||
| 14 | +###Local temporario para download de arquivos, comente para usar o diretorio temporario padrão do sistema | ||
| 15 | +tmp_dir: /home/tmp | ||
| 16 | + | ||
| 11 | ###Nome de usuário | 17 | ###Nome de usuário |
| 12 | username: rest | 18 | username: rest |
| 13 | 19 | ||
| @@ -18,7 +24,7 @@ senha: rest | @@ -18,7 +24,7 @@ senha: rest | ||
| 18 | data: 01-01-1970 | 24 | data: 01-01-1970 |
| 19 | 25 | ||
| 20 | ###Tempo entre as leituras em busca de registros (em segundos) | 26 | ###Tempo entre as leituras em busca de registros (em segundos) |
| 21 | -sleep_time: 86400 | 27 | +sleep_time: 60 |
| 22 | 28 | ||
| 23 | [Daemon] | 29 | [Daemon] |
| 24 | stdin_path = /dev/null | 30 | stdin_path = /dev/null |
production.ini
| 1 | [WSCBot] | 1 | [WSCBot] |
| 2 | ###Url de acesso ao modulo wscserver (terminar com '/') | 2 | ###Url de acesso ao modulo wscserver (terminar com '/') |
| 3 | -url_wscserver: http://10.209.8.39/wscserver/ | 3 | +url_wscserver: http://localhost/wscserver/ |
| 4 | 4 | ||
| 5 | ###Url de acesso ao Super Gerente | 5 | ###Url de acesso ao Super Gerente |
| 6 | -ip_superg: http://10.209.8.39/lbbulk/reg | 6 | +ip_superg: http://localhost/lbbulk/reg |
| 7 | 7 | ||
| 8 | ###Local do disco onde o arquivo será salvo temporariamente | 8 | ###Local do disco onde o arquivo será salvo temporariamente |
| 9 | -filepath: /tmp/extract/ | 9 | +filepath: /home/extract |
| 10 | + | ||
| 11 | +###Nome da fonte dos dados (precisa ser um nome já existente na base do super-gerente) | ||
| 12 | +source_name: cacic_mpog | ||
| 13 | + | ||
| 14 | +###Local temporario para download de arquivos, comente para usar o diretorio temporario padrão do sistema | ||
| 15 | +tmp_dir: /home/tmp | ||
| 10 | 16 | ||
| 11 | ###Nome de usuário | 17 | ###Nome de usuário |
| 12 | username: rest | 18 | username: rest |
| @@ -18,7 +24,7 @@ senha: rest | @@ -18,7 +24,7 @@ senha: rest | ||
| 18 | data: 01-01-1970 | 24 | data: 01-01-1970 |
| 19 | 25 | ||
| 20 | ###Tempo entre as leituras em busca de registros (em segundos) | 26 | ###Tempo entre as leituras em busca de registros (em segundos) |
| 21 | -sleep_time: 3600 | 27 | +sleep_time: 86400 |
| 22 | 28 | ||
| 23 | [Daemon] | 29 | [Daemon] |
| 24 | stdin_path = /dev/null | 30 | stdin_path = /dev/null |
wscbot/__main__.py
| @@ -14,9 +14,11 @@ from app import main | @@ -14,9 +14,11 @@ from app import main | ||
| 14 | from requests.exceptions import * | 14 | from requests.exceptions import * |
| 15 | import zipfile | 15 | import zipfile |
| 16 | import lockfile | 16 | import lockfile |
| 17 | +import tempfile | ||
| 17 | 18 | ||
| 19 | +# absolute path to .ini file | ||
| 20 | +#ini_path='/srv/super-gerente/WSCBot/development.ini' | ||
| 18 | ini_path='/srv/bot-super-gerente/WSCBot/production.ini' | 21 | ini_path='/srv/bot-super-gerente/WSCBot/production.ini' |
| 19 | -#absolute path to .ini file | ||
| 20 | 22 | ||
| 21 | 23 | ||
| 22 | 24 | ||
| @@ -29,10 +31,15 @@ def setconfig(config_file): | @@ -29,10 +31,15 @@ def setconfig(config_file): | ||
| 29 | user = {} | 31 | user = {} |
| 30 | user['url_wscserver'] = config.get('WSCBot', 'url_wscserver') | 32 | user['url_wscserver'] = config.get('WSCBot', 'url_wscserver') |
| 31 | user['filepath'] = config.get('WSCBot', 'filepath') | 33 | user['filepath'] = config.get('WSCBot', 'filepath') |
| 34 | + if config.has_option('WSCBot', 'tmp_dir'): | ||
| 35 | + user['tmp_dir'] = config.get('WSCBot', 'tmp_dir') | ||
| 36 | + else: | ||
| 37 | + user['tmp_dir'] = tempfile.mkdtemp() | ||
| 32 | user['username'] = config.get('WSCBot', 'username') | 38 | user['username'] = config.get('WSCBot', 'username') |
| 33 | user['senha'] = config.get('WSCBot', 'senha') | 39 | user['senha'] = config.get('WSCBot', 'senha') |
| 34 | user['data'] = config.get('WSCBot', 'data') | 40 | user['data'] = config.get('WSCBot', 'data') |
| 35 | user['ip_superg'] = config.get('WSCBot', 'ip_superg') | 41 | user['ip_superg'] = config.get('WSCBot', 'ip_superg') |
| 42 | + user['source_name'] = config.get('WSCBot', 'source_name') | ||
| 36 | user['sleep_time'] = int(config.get('WSCBot', 'sleep_time')) | 43 | user['sleep_time'] = int(config.get('WSCBot', 'sleep_time')) |
| 37 | user['stdin_path'] = config.get('Daemon', 'stdin_path') | 44 | user['stdin_path'] = config.get('Daemon', 'stdin_path') |
| 38 | user['stdout_path'] = config.get('Daemon', 'stdout_path') | 45 | user['stdout_path'] = config.get('Daemon', 'stdout_path') |
| @@ -57,7 +64,7 @@ class App(): | @@ -57,7 +64,7 @@ class App(): | ||
| 57 | timeron = time.time() | 64 | timeron = time.time() |
| 58 | logger.info ('Iniciando execução') | 65 | logger.info ('Iniciando execução') |
| 59 | try: | 66 | try: |
| 60 | - main(ip_servidor,username,senha,ip_superg,filepath,data) | 67 | + main(ip_servidor,username,senha,ip_superg,filepath,data,tmp_dir,source_name) |
| 61 | except (ConnectionError, Timeout): | 68 | except (ConnectionError, Timeout): |
| 62 | logger.error ('Não foi possivel estabelecer conexão com o servidor! ' + domain) | 69 | logger.error ('Não foi possivel estabelecer conexão com o servidor! ' + domain) |
| 63 | except zipfile.BadZipfile: | 70 | except zipfile.BadZipfile: |
| @@ -78,8 +85,10 @@ ip_servidor = user['url_wscserver'] | @@ -78,8 +85,10 @@ ip_servidor = user['url_wscserver'] | ||
| 78 | senha = user['senha'] | 85 | senha = user['senha'] |
| 79 | ip_superg = user['ip_superg'] | 86 | ip_superg = user['ip_superg'] |
| 80 | sleep_time = user['sleep_time'] | 87 | sleep_time = user['sleep_time'] |
| 88 | +source_name = user['source_name'] | ||
| 81 | username = user['username'] | 89 | username = user['username'] |
| 82 | filepath = user['filepath'] | 90 | filepath = user['filepath'] |
| 91 | +tmp_dir = user['tmp_dir'] | ||
| 83 | data = user['data'] | 92 | data = user['data'] |
| 84 | app = App() | 93 | app = App() |
| 85 | logger = logging.getLogger("WSCBot") | 94 | logger = logging.getLogger("WSCBot") |
wscbot/app.py
| @@ -11,26 +11,29 @@ from wscbot.scripts import erro | @@ -11,26 +11,29 @@ from wscbot.scripts import erro | ||
| 11 | from wscbot.scripts import rotinas | 11 | from wscbot.scripts import rotinas |
| 12 | from wscbot.scripts.zipparser import ziphandler | 12 | from wscbot.scripts.zipparser import ziphandler |
| 13 | 13 | ||
| 14 | -def main(ip_servidor,username,senha,ip_superg,filepath,data): | 14 | + |
| 15 | + | ||
| 16 | +def main(ip_servidor,username,senha,ip_superg,filepath,data,tmp_dir, | ||
| 17 | + source_name): | ||
| 15 | """ | 18 | """ |
| 16 | - Envia uma requisição GET HTTP para o endereço cadastrado, | ||
| 17 | - fornecendo como parâmetros da requisição o usuário e senha informados; | ||
| 18 | - Pega a resposta da requisição e escreve num arquivo; | ||
| 19 | - Criptografa o conteúdo do arquivo com base em uma chave compartilhada com o Super Gerente; | ||
| 20 | - Envia o arquivo criptografado para o Super Gerente; | ||
| 21 | - Registra a data do envio e o resultado da comunicação. | 19 | + Envia uma requisição GET HTTP para o endereço cadastrado fornecendo |
| 20 | + como parâmetros da requisição o usuário e senha informados, pega a | ||
| 21 | + resposta da requisição e escreve num arquivo. Criptografa o conteúdo | ||
| 22 | + do arquivo com base em uma chave compartilhada com o Super Gerente. | ||
| 23 | + Envia o arquivo criptografado para o Super Gerente e registra a data | ||
| 24 | + do envio e o resultado da comunicação. | ||
| 22 | """ | 25 | """ |
| 23 | 26 | ||
| 24 | # Faz a requisição do arquivo zip | 27 | # Faz a requisição do arquivo zip |
| 25 | zip_path = req.req(ip_servidor, filepath) | 28 | zip_path = req.req(ip_servidor, filepath) |
| 26 | 29 | ||
| 27 | - dicionario = ziphandler(zip_path) | 30 | + dicionario = ziphandler(zip_path, tmp_dir) |
| 28 | for arquivo in dicionario: | 31 | for arquivo in dicionario: |
| 29 | logger.debug('Inicio do envio de dados') | 32 | logger.debug('Inicio do envio de dados') |
| 30 | coleta = json.loads(dicionario[arquivo])['results'] | 33 | coleta = json.loads(dicionario[arquivo])['results'] |
| 31 | for registro in coleta: | 34 | for registro in coleta: |
| 35 | + registro['name_source'] = source_name | ||
| 32 | registro = json.dumps(registro) | 36 | registro = json.dumps(registro) |
| 33 | - | ||
| 34 | f = post(ip_superg,data={'json_reg':registro}) | 37 | f = post(ip_superg,data={'json_reg':registro}) |
| 35 | erro.is_error_p(f) | 38 | erro.is_error_p(f) |
| 36 | 39 |
wscbot/scripts/zipparser.py
| @@ -3,13 +3,12 @@ | @@ -3,13 +3,12 @@ | ||
| 3 | 3 | ||
| 4 | import zipfile | 4 | import zipfile |
| 5 | import logging | 5 | import logging |
| 6 | -import tempfile | ||
| 7 | import json | 6 | import json |
| 8 | import os | 7 | import os |
| 9 | 8 | ||
| 10 | from wscbot.scripts.tratamento import t_dados | 9 | from wscbot.scripts.tratamento import t_dados |
| 11 | 10 | ||
| 12 | -def ziphandler(filename): | 11 | +def ziphandler(filename, tmp_dir): |
| 13 | """ | 12 | """ |
| 14 | Receive a zip file and parse one file at a time, returning a collection of parsed files. | 13 | Receive a zip file and parse one file at a time, returning a collection of parsed files. |
| 15 | """ | 14 | """ |
| @@ -17,7 +16,6 @@ def ziphandler(filename): | @@ -17,7 +16,6 @@ def ziphandler(filename): | ||
| 17 | z_root = zipfile.ZipFile(filename) | 16 | z_root = zipfile.ZipFile(filename) |
| 18 | 17 | ||
| 19 | # Create a temp dir and unzip file contents | 18 | # Create a temp dir and unzip file contents |
| 20 | - tmp_dir = tempfile.mkdtemp() | ||
| 21 | 19 | ||
| 22 | # registros = { | 20 | # registros = { |
| 23 | # 'import_error' : [], | 21 | # 'import_error' : [], |