Skip to content

Instantly share code, notes, and snippets.

View guilehm's full-sized avatar

Guilherme Luis guilehm

  • São Paulo
View GitHub Profile
@guilehm
guilehm / cep.py
Created February 16, 2018 00:56
Busca CEP
import requests
from lxml import etree
URL = 'http://www.buscacep.correios.com.br/sistemas/buscacep/resultadoBuscaCepEndereco.cfm'
def busca_cep(cep):
r = requests.post(URL, data=dict(relaxation=cep, tipoCEP='ALL', semelhante='N'))
tabela = etree.HTML(r.text).find(".//table[@class='tmptabela']")
logradouro, bairro, cidade_uf, cep = (x.text.strip() for x in tabela.findall('.//td'))
cidade, _, uf = cidade_uf.rpartition('/')
@guilehm
guilehm / rename_database_shell_script.sh
Created March 28, 2019 21:13
rename MySQL database with shell script
mysql -u root -p"root" old_db_name -sNe 'show tables' | while read table;
do mysql -u root -p"root" -sNe "RENAME TABLE old_db_name.$table TO new_db_name.$table";
done
@guilehm
guilehm / asyncio_aiohttp.py
Created October 7, 2019 00:14
multiple requests per minute with asyncio + aiohttp
import asyncio
import aiohttp
from pymongo import MongoClient
MONGO_URL = 'mongodb://localhost:27017/'
db_name = 'marvelCrawler'
db = MongoClient(MONGO_URL)[db_name]
from django.db.models import Prefetch
def get_queryset(self):
if self.action_map.get('get') == 'list':
return CuttingLine.objects.all().select_related(
'line', 'line__machine_type',
).defer('description', 'price')
return CuttingLine.objects.all().select_related(
'line', 'line__machine_type', 'picture_primary', 'picture_pack_360'
import django
django.setup()
from apscheduler.schedulers.background import BackgroundScheduler
scheduler = BackgroundScheduler()
@scheduler.scheduled_job('interval', minutes=1)
{
"by":"symbol",
"valid_key":true,
"results":{
"MGLU3":{
"symbol":"MGLU3",
"name":"Magaz Luiza",
"company_name":"Magazine Luiza S.A.",
"document":"47.960.950/0001-21",
"description":"Somos Uma Rede Varejista E Atuamos Com Foco Na Comercialização de Bens Duráveis No Brasil.",