Last active
March 14, 2017 10:41
-
-
Save murilobsd/21cbd6c11b7d3d0f668c4a37ccf09b8e to your computer and use it in GitHub Desktop.
tjsp download captcha - https://drive.google.com/file/d/0B5M9euvZPDWkejJlckFzM2FSZ0U/view?usp=sharing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Simples script para baixar imagens do captcha do portal TJSP. | |
Copyright (c) 2016, Murilo Ijanc | |
All rights reserved. | |
Redistribution and use in source and binary forms, with or without | |
modification, are permitted provided that the following conditions are met: | |
* Redistributions of source code must retain the above copyright notice, this | |
list of conditions and the following disclaimer. | |
* Redistributions in binary form must reproduce the above copyright notice, | |
this list of conditions and the following disclaimer in the documentation | |
and/or other materials provided with the distribution. | |
* Neither the name of crawlcps nor the names of its | |
contributors may be used to endorse or promote products derived from | |
this software without specific prior written permission. | |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | |
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | |
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | |
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
""" | |
import time | |
import socket | |
import logging | |
import argparse | |
import urllib.error | |
import urllib.request | |
# url do captcha "precisamos" adicionar timestamp | |
captcha_url = ("http://esaj.tjsp.jus.br/sco/" | |
"imagemCaptcha.do?timestamp={timestamp}") | |
# nome final do arquivo | |
filename = "{file_name}.png" | |
def timestamp_now(): | |
"""gera timestamp""" | |
return int(time.time()) | |
def download(num, interval, _timeout, max_retry=3): | |
"""funcao para download das imagens do captcha | |
o timestamp gerado sera o nome utilizado para | |
salvar as imagens. | |
""" | |
logging.info("Baixando %d imagens." % num) | |
count = 1 | |
retry = 1 | |
while count <= num and retry <= max_retry: | |
logging.debug("Faltam [%d] imagens de um total: [%d]" % ((num - count), | |
num)) | |
timestamp = timestamp_now() | |
url = captcha_url.format(timestamp=timestamp) # url final | |
_file = filename.format(file_name=timestamp) | |
try: | |
with urllib.request.urlopen(url, timeout=_timeout) \ | |
as response, open(_file, 'wb') as out_file: | |
data = response.read() | |
out_file.write(data) | |
logging.debug("Salvando %s" % _file) | |
except (urllib.error.HTTPError, urllib.error.URLError) as e: | |
logging.error("Erro: %s, tentativa [%d] de [%d]" % (e, retry, | |
max_retry)) | |
retry += 1 | |
except socket.timeout: | |
logging.warning("Timeout, faltam [%d] tentativas de um total: [%d]" | |
% ((max_retry - retry), max_retry)) | |
retry += 1 | |
count -= 1 # subtraimos para nova tentativa | |
count += 1 | |
time.sleep(interval) | |
def main(): | |
ap = argparse.ArgumentParser() | |
ap.add_argument("-n", "--num", type=int, default=10, | |
help="numero de imagens para download, padrao = 10") | |
ap.add_argument("-i", "--interval", type=float, default=5.0, | |
help="intervalo para baixar imagem") | |
ap.add_argument("-t", "--timeout", type=float, default=10.0, | |
help="timeout, padrao = 10 s") | |
ap.add_argument("-r", "--retry", type=int, default=3, | |
help="tentativas no caso do timeout, padrao = 0") | |
ap.add_argument("-v", "--verbose", action="count", default=0, | |
help=("depuracao: 0 = avisos, " | |
"1 = info, 2 = debug. " | |
"padrao = avisos.")) | |
args = ap.parse_args() | |
# nivel de debug | |
levels = [logging.WARNING, logging.INFO, logging.DEBUG] | |
level = levels[min(len(levels) - 1, args.verbose)] | |
logging.basicConfig(level=level, | |
format="%(asctime)s %(levelname)s %(message)s") | |
download(args.num, args.interval, args.timeout, args.retry) | |
if __name__ == '__main__': | |
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ python3 tjsp.py --help | |
usage: tjsp.py [-h] [-n NUM] [-i INTERVAL] [-t TIMEOUT] [-r RETRY] [-v] | |
optional arguments: | |
-h, --help show this help message and exit | |
-n NUM, --num NUM numero de imagens para download, padrao = 10 | |
-i INTERVAL, --interval INTERVAL | |
intervalo para baixar imagem | |
-t TIMEOUT, --timeout TIMEOUT | |
timeout, padrao = 10 s | |
-r RETRY, --retry RETRY | |
tentativas no caso do timeout, padrao = 0 | |
-v, --verbose depuracao: 0 = avisos, 1 = info, 2 = debug. padrao = | |
avisos. | |
$ python3 tjsp.py -vvv | |
2017-03-13 21:45:12,855 INFO Baixando 10 imagens. | |
2017-03-13 21:45:12,855 DEBUG Faltam [9] imagens de um total: [10] | |
2017-03-13 21:45:12,971 DEBUG Salvando 1489452312.png | |
2017-03-13 21:45:17,977 DEBUG Faltam [8] imagens de um total: [10] | |
2017-03-13 21:45:18,100 DEBUG Salvando 1489452317.png | |
2017-03-13 21:45:23,103 DEBUG Faltam [7] imagens de um total: [10] | |
2017-03-13 21:45:23,207 DEBUG Salvando 1489452323.png | |
2017-03-13 21:45:28,210 DEBUG Faltam [6] imagens de um total: [10] | |
2017-03-13 21:45:28,330 DEBUG Salvando 1489452328.png | |
2017-03-13 21:45:33,330 DEBUG Faltam [5] imagens de um total: [10] | |
2017-03-13 21:45:33,454 DEBUG Salvando 1489452333.png | |
2017-03-13 21:45:38,457 DEBUG Faltam [4] imagens de um total: [10] | |
2017-03-13 21:45:38,560 DEBUG Salvando 1489452338.png | |
2017-03-13 21:45:43,566 DEBUG Faltam [3] imagens de um total: [10] | |
2017-03-13 21:45:43,638 DEBUG Salvando 1489452343.png | |
2017-03-13 21:45:48,641 DEBUG Faltam [2] imagens de um total: [10] | |
2017-03-13 21:45:48,737 DEBUG Salvando 1489452348.png | |
2017-03-13 21:45:53,739 DEBUG Faltam [1] imagens de um total: [10] | |
2017-03-13 21:45:53,845 DEBUG Salvando 1489452353.png | |
2017-03-13 21:45:58,847 DEBUG Faltam [0] imagens de um total: [10] | |
2017-03-13 21:45:58,951 DEBUG Salvando 1489452358.png | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment