Skip to content

Instantly share code, notes, and snippets.

View sergiolucero's full-sized avatar
馃挱
coding the days away

Sergio Lucero sergiolucero

馃挱
coding the days away
View GitHub Profile
@sergiolucero
sergiolucero / jumbito.py
Created May 13, 2018 00:02
minijumboscraper
import requests, pandas as pd
from bs4 import BeautifulSoup
url_base = 'https://nuevo.jumbo.cl/%s'
def parse(what):
print(what)
bs = BeautifulSoup(requests.get(url_base %what).text, 'lxml')
producto = [pt.text.split(chr(10))[1] for pt in bs.find_all('div',attrs={'class':'product-item__info'})]
marca = [pt.text.split(chr(10))[2] for pt in bs.find_all('div',attrs={'class':'product-item__info'})]
@sergiolucero
sergiolucero / async_scraper.py
Last active October 6, 2019 17:15
async scraping
import aiohttp
import asyncio
import time, pandas as pd
def async_http_get(urls, extractor=None, json_response=True):
tasks = []
sem = asyncio.Semaphore(32)
async def fetch(session, url):
async with session.get(url) as response:
@sergiolucero
sergiolucero / localtime.py
Created May 18, 2018 14:06
time in Chile
from datetime import datetime
from pytz import timezone
def chile_time():
scl = timezone('America/Santiago')
scl_time = datetime.now(scl)
return scl_time.strftime('%Y-%m-%d %H-%M-%S')
@sergiolucero
sergiolucero / cne_api.py
Last active July 27, 2023 17:41
Precios combustibles
import pandas as pd
from token import TOKEN # token personal: http://www.energiaabierta.cne.cl/
TIPOS = ['calefaccion','vehicular']
SOURCE = 'http://api.cne.cl/v3/combustibles/{}/estaciones?token={}'
COPY_VARS = ['nombre_comuna', 'id_region', 'direccion_calle', 'fecha_hora_actualizacion']
for tipo in TIPOS:
print(tipo)
df = pd.read_json(SOURCE.format(tipo, TOKEN))
out = pd.DataFrame()
@sergiolucero
sergiolucero / isa.py
Last active November 24, 2020 03:40
here isolines
from creds import ID,CODE # isolines: driving from my house
import folium
import requests
home =[-33.406654,-70.572701] # 085 LOS MILITARES / ALONSO DE CORDOVA
head = 'https://isoline.route.cit.api.here.com/routing/7.2/calculateisoline.json?'
URL_BASE = '{}app_id={}&app_code={}&mode=shortest;car;traffic:disabled&start=geo!{},{}&range={}&rangetype={}'
def isodata(home, range, type):
@sergiolucero
sergiolucero / colab_tpot_totalcheck.py
Last active June 14, 2018 17:09
Training cars on Google colab
#from google.colab import files;uploaded = files.upload()
#!pip install TPOT
from tpot import TPOTRegressor
from sklearn.model_selection import train_test_split
df=pd.read_csv('32928_chile_autos.csv')
df=df[(df.A帽o>1980)&(df.Kilometraje>1000)&(df.Precio<100000000)&(df.Precio>1000000)]
data_columns = df.columns[2:-1] # add some more!
target_columns = df.columns[-1] # use logs!
X,y = df[data_columns], df[target_columns]
@sergiolucero
sergiolucero / tgr_heatmap.py
Created May 23, 2018 19:34
Evoluci贸n semanal atenci贸n TGR
gdf=pysqldf('SELECT semana,Oficina,AVG(Tiempo_Ate_Min) AS TiempoAtencion, \
AVG(Tiempo_Esp_Min) AS TiempoEspera,\
COUNT(DISTINCT(Ejecutivo)) AS Personal FROM df GROUP BY Oficina, semana')
atencion = gdf.pivot("Oficina","semana", "TiempoAtencion")
espera = gdf.pivot("Oficina","semana", "TiempoEspera")
ejecutivos = gdf.pivot("Oficina","semana", "Personal") # on same plot?
f, (ax1, ax2, ax3) = plt.subplots(1,3,figsize=(20, 8),sharey=True)
sns.heatmap(atencion, cmap="rainbow",fmt="d",ax=ax1)
sns.heatmap(espera, cmap="rainbow",fmt="d",ax=ax2)
@sergiolucero
sergiolucero / metropolitana.py
Created June 3, 2018 17:30
Comunas de Chile
#!wget https://www.bcn.cl/siit/obtienearchivo?id=repositorio/10221/10396/1/division_comunal.zip
#!mv obtienearchivo\?id\=repositorio%2F10221%2F10396%2F1%2Fdivision_comunal.zip comunal.zip
#!unzip comunal
#!pip install geopandas
import geopandas as gp
df = gp.read_file('division_comunal.shp')
#df.NOM_REG.unique()
rmdf = df[df.NOM_REG=='Regi贸n Metropolitana de Santiago']
print('Procesamos %d comunas en la Regi贸n Metropolitana' %len(rmdf))
rmdf.to_pickle('rm.pk') # 780K! upload to quant.cl for reference (just repeat the above)
@sergiolucero
sergiolucero / bikelib.py
Last active June 4, 2018 21:09
Citybik.es + here.com APIs
BIKE_URL = 'https://api.citybik.es/v2/networks?fields=id,location,href'
HERE_URL = '''https://route.cit.api.here.com/routing/7.2/calculateroute.json?
app_id=arVngf4Gl3gIU145UGVB&app_code=Pas2aQELd9IynApEQ4m8jg&
waypoint0=geo!52.5,13.4&waypoint1=geo!52.5,13.45&
mode=fastest;bicycle''' # or car
BACKUP = 'https://github.com/sergiolucero/hereapi/blob/master/DATA/bikesystems.csv'
@sergiolucero
sergiolucero / serverless.yml
Created June 5, 2018 16:44
Primer deployment serverless en Amazon Lambda
'use strict';
module.exports.hello = (event, context, callback) => {
const response = {
statusCode: 200,
body: JSON.stringify({
message: 'hola Sergio!',
}),
};
callback(null, response);
};