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 | 1 | [WSCBot] |
2 | 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 | 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 | 8 | ###Local do disco onde o arquivo será salvo temporariamente |
9 | 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 | 17 | ###Nome de usuário |
12 | 18 | username: rest |
13 | 19 | |
... | ... | @@ -18,7 +24,7 @@ senha: rest |
18 | 24 | data: 01-01-1970 |
19 | 25 | |
20 | 26 | ###Tempo entre as leituras em busca de registros (em segundos) |
21 | -sleep_time: 86400 | |
27 | +sleep_time: 60 | |
22 | 28 | |
23 | 29 | [Daemon] |
24 | 30 | stdin_path = /dev/null | ... | ... |
production.ini
1 | 1 | [WSCBot] |
2 | 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 | 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 | 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 | 17 | ###Nome de usuário |
12 | 18 | username: rest |
... | ... | @@ -18,7 +24,7 @@ senha: rest |
18 | 24 | data: 01-01-1970 |
19 | 25 | |
20 | 26 | ###Tempo entre as leituras em busca de registros (em segundos) |
21 | -sleep_time: 3600 | |
27 | +sleep_time: 86400 | |
22 | 28 | |
23 | 29 | [Daemon] |
24 | 30 | stdin_path = /dev/null | ... | ... |
wscbot/__main__.py
... | ... | @@ -14,9 +14,11 @@ from app import main |
14 | 14 | from requests.exceptions import * |
15 | 15 | import zipfile |
16 | 16 | import lockfile |
17 | +import tempfile | |
17 | 18 | |
19 | +# absolute path to .ini file | |
20 | +#ini_path='/srv/super-gerente/WSCBot/development.ini' | |
18 | 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 | 31 | user = {} |
30 | 32 | user['url_wscserver'] = config.get('WSCBot', 'url_wscserver') |
31 | 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 | 38 | user['username'] = config.get('WSCBot', 'username') |
33 | 39 | user['senha'] = config.get('WSCBot', 'senha') |
34 | 40 | user['data'] = config.get('WSCBot', 'data') |
35 | 41 | user['ip_superg'] = config.get('WSCBot', 'ip_superg') |
42 | + user['source_name'] = config.get('WSCBot', 'source_name') | |
36 | 43 | user['sleep_time'] = int(config.get('WSCBot', 'sleep_time')) |
37 | 44 | user['stdin_path'] = config.get('Daemon', 'stdin_path') |
38 | 45 | user['stdout_path'] = config.get('Daemon', 'stdout_path') |
... | ... | @@ -57,7 +64,7 @@ class App(): |
57 | 64 | timeron = time.time() |
58 | 65 | logger.info ('Iniciando execução') |
59 | 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 | 68 | except (ConnectionError, Timeout): |
62 | 69 | logger.error ('Não foi possivel estabelecer conexão com o servidor! ' + domain) |
63 | 70 | except zipfile.BadZipfile: |
... | ... | @@ -78,8 +85,10 @@ ip_servidor = user['url_wscserver'] |
78 | 85 | senha = user['senha'] |
79 | 86 | ip_superg = user['ip_superg'] |
80 | 87 | sleep_time = user['sleep_time'] |
88 | +source_name = user['source_name'] | |
81 | 89 | username = user['username'] |
82 | 90 | filepath = user['filepath'] |
91 | +tmp_dir = user['tmp_dir'] | |
83 | 92 | data = user['data'] |
84 | 93 | app = App() |
85 | 94 | logger = logging.getLogger("WSCBot") | ... | ... |
wscbot/app.py
... | ... | @@ -11,26 +11,29 @@ from wscbot.scripts import erro |
11 | 11 | from wscbot.scripts import rotinas |
12 | 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 | 27 | # Faz a requisição do arquivo zip |
25 | 28 | zip_path = req.req(ip_servidor, filepath) |
26 | 29 | |
27 | - dicionario = ziphandler(zip_path) | |
30 | + dicionario = ziphandler(zip_path, tmp_dir) | |
28 | 31 | for arquivo in dicionario: |
29 | 32 | logger.debug('Inicio do envio de dados') |
30 | 33 | coleta = json.loads(dicionario[arquivo])['results'] |
31 | 34 | for registro in coleta: |
35 | + registro['name_source'] = source_name | |
32 | 36 | registro = json.dumps(registro) |
33 | - | |
34 | 37 | f = post(ip_superg,data={'json_reg':registro}) |
35 | 38 | erro.is_error_p(f) |
36 | 39 | ... | ... |
wscbot/scripts/zipparser.py
... | ... | @@ -3,13 +3,12 @@ |
3 | 3 | |
4 | 4 | import zipfile |
5 | 5 | import logging |
6 | -import tempfile | |
7 | 6 | import json |
8 | 7 | import os |
9 | 8 | |
10 | 9 | from wscbot.scripts.tratamento import t_dados |
11 | 10 | |
12 | -def ziphandler(filename): | |
11 | +def ziphandler(filename, tmp_dir): | |
13 | 12 | """ |
14 | 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 | 16 | z_root = zipfile.ZipFile(filename) |
18 | 17 | |
19 | 18 | # Create a temp dir and unzip file contents |
20 | - tmp_dir = tempfile.mkdtemp() | |
21 | 19 | |
22 | 20 | # registros = { |
23 | 21 | # 'import_error' : [], | ... | ... |