Skip to content

Instantly share code, notes, and snippets.

@dunossauro
Created December 5, 2016 01:35
Show Gist options
  • Save dunossauro/fb1d6183d1eaeb414a5571759da13d7d to your computer and use it in GitHub Desktop.
Save dunossauro/fb1d6183d1eaeb414a5571759da13d7d to your computer and use it in GitHub Desktop.
Script que gera cpfs aleatórios e traz dados do SUS
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from random import randint
from requests import get
from json import loads
url = 'http://dabsistemas.saude.gov.br/sistemas/sadab/js/buscar_cpf_dbpessoa.json.php?cpf='
saida = 'N: {5}\nNome: {0}\nMãe: {1}\nSexo: {2}\nNacimento: {3}\nCPF: {4}'
def cpf_gen():
"""
Função que gera CPFs retirada de um post no VOL:
https://www.vivaolinux.com.br/script/Gerador-de-CPF-em-Python
Créditos: danilosampaio
"""
num = []
for i in range(9):
num.append(randint(0, 9))
somaJ = sum([(num[0] * 10),
(num[1] * 9),
(num[2] * 8),
(num[3] * 7),
(num[4] * 6),
(num[5] * 5),
(num[6] * 4),
(num[7] * 3),
(num[8] * 2)])
restoJ = somaJ % 11
if (restoJ == 0 or restoJ == 1):
j = 0
else:
j = 11 - restoJ
num.append(j)
somaK = sum([(num[0] * 11),
(num[1] * 10),
(num[2] * 9),
(num[3] * 8),
(num[4] * 7),
(num[5] * 6),
(num[6] * 5),
(num[7] * 4),
(num[8] * 3),
(j * 2)])
restoK = somaK % 11
if (restoK == 0 or restoK == 1):
k = 0
else:
k = 11 - restoK
num.append(k)
return ''.join(str(x) for x in num)
def get_sus(num):
"""
Função que faz chamada na API do SUS:
https://gist.github.com/jh00nbr/9d713872f0e972d4fc9af4725a8a77aa
Créditos: Jhonathan Davi A.K.A jh00nbr
"""
config = {'api_sus': url,
'cpf': cpf_gen()}
try:
req = get(config['api_sus']+config['cpf'])
d = loads(req.content.decode('utf-8'))
if d['CO_SEXO']:
print(saida.format(d['NO_PESSOA_FISICA'],
d['NO_MAE'],
d['CO_SEXO'],
d['DT_NASCIMENTO'],
d['NU_CPF'],
num))
except KeyError:
pass
# Fazendo 100 tentativas
for x in range(100):
get_sus(x)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment