Skip to content

Instantly share code, notes, and snippets.

@skienteca
Last active February 9, 2016 00:12
Show Gist options
  • Save skienteca/280021f8ed386e5c7c90 to your computer and use it in GitHub Desktop.
Save skienteca/280021f8ed386e5c7c90 to your computer and use it in GitHub Desktop.
# coding: utf-8
import datetime
from collections import OrderedDict
from io import BytesIO
import requests
import rows
from bs4 import BeautifulSoup
import functions as f
DOMAIN = 'http://www.curto.coffee'
URL_CONTADOR = DOMAIN + '/contador'
def rows_to_dict(rows):
'''Return an `OrderedDict` based on rows
The returned `OrderedDict` will have row's `produto` as key and row's
`quantidade` as value.
'''
return OrderedDict([(row.produto, row.quantidade)
for row in rows])
# retorna um dicionario com os dados que o rows pegou do html
def get_ordered_row(html, table_ndex):
table = rows.import_from_html(BytesIO(html), enconding='utf-8', index=table_ndex)
return rows_to_dict(table)
#capura o ultimo dia trabalhado e retorna a data convertida para dd/mm/aaaa
def last_worked_day(data):
return datetime.datetime.strptime(data, "%Y-%m-%d").strftime("%d/%m/%Y")
def pega_dados_ultimo_dia_trabalhado():
# abre o arquivo html com a lista de dias trabalhados
html_contador = requests.get(URL_CONTADOR).text
# cria um objeto BeautifulSoup da pagina URL_CONTADOR
soup_contador = BeautifulSoup(html_contador, 'html.parser')
# pega a data do ultimo dia trabalhado
date = last_worked_day(soup_contador.a.get_text())
# pega a url do ultimo dia trabalhado (primeiro item da lista)
url_last_workday = DOMAIN + soup_contador.a.get('href')
# pega o conteudo HTML do consumo do ultimo dia
html_content_last_workday = requests.get(url_last_workday).content
return html_content_last_workday, date
def main():
html_content_last_workday, date = pega_dados_ultimo_dia_trabalhado()
consumo = get_ordered_row(html_content_last_workday, 0)
consumo_avulso = get_ordered_row(html_content_last_workday, 1)
tabela = rows.import_from_html(BytesIO(html_content_last_workday),enconding='utf-8', index=1)
recargas = tabela[-1].valor
f.update_gspread_cells(date, consumo, consumo_avulso, recargas)
if __name__ == '__main__':
main()
# coding: utf-8
import json
import gspread
from oauth2client.client import SignedJwtAssertionCredentials
#indice da coluna onde estao esses dados na google spreadsheet
ESPRESSO = 4
CAPPUCCINO = 5
QTD_PACOTE_100g = 6
CONTRIBUICAO_025 = 7
CONTRIBUICAO_1 = 8
CONTRIBUICAO_MARIO = 9
QTD_EMBALAGEM = 10
RECARGAS = 11
columns = ((ESPRESSO,u'Materia Prima Espresso'),
(CAPPUCCINO,u'Materia Prima Cappuccino'),
(QTD_PACOTE_100g,u'Materia Prima Pacote (a cada 100g)'),
(CONTRIBUICAO_025,u'Arrecadação pra pagar contas em Geral 0,25'),
(CONTRIBUICAO_1,u'Arrecadação pra pagar contas em Geral'),
(CONTRIBUICAO_MARIO,u'Livre Contribuição Mario Zardo (Torra de Café)'),
(QTD_EMBALAGEM,u'Custo Embalagem por Pacote'))
#pega as credenciais no arquivo.json de autorização para o google
def get_credentials():
json_key = json.load(open('APIProject-b26e61efa31c.json'))
scope = ['https://spreadsheets.google.com/feeds']
return SignedJwtAssertionCredentials(json_key['client_email'], json_key['private_key'], scope)
#retorna o usuario autorizado no google
def get_account():
return gspread.authorize(get_credentials())
# descobre a celula com a data desejada
def update_gspread_cells(date, consumo, consumo_avulso, recargas):
account = get_account()
sheet = account.open('Curto Cafe - Planilha Diaria 2016')
worksheet = sheet.worksheet('Entendendo "Consumo"')
worksheet2 = sheet.worksheet('Ententendo "Consumo" A vulso')
cell = worksheet.find(date)
cell2 = worksheet2.find(date)
for col, name in columns:
worksheet.update_cell(cell.row, col, consumo.get(name, 0))
worksheet2.update_cell(cell2.row, col, consumo_avulso.get(name, 0))
worksheet2.update_cell(cell.row, 11, recargas)
alabaster==0.7.6
Babel==1.3
backports.ssl-match-hostname==3.4.0.2
beautifulsoup4==4.4.0
cached-property==1.2.0
certifi==2015.4.28
click==6.2
coloredlogs==1.0.1
docutils==0.12
filemagic==1.6
functools32==3.2.3.post2
gspread==0.2.5
httplib2==0.9.1
humanfriendly==1.32
ipython==3.2.1
Jinja2==2.7.3
jsonschema==2.5.1
lxml==3.4.4
MarkupSafe==0.23
mistune==0.7
nose==1.3.7
numpydoc==0.5
oauth2client==1.4.12
pip-accel==0.31
ptyprocess==0.5
pyasn1==0.1.8
pyasn1-modules==0.0.6
pycrypto==2.6.1
Pygments==2.0.2
pytz==2015.4
pyzmq==14.7.0
requests==2.9.1
rows==0.1.1
rsa==3.1.4
six==1.9.0
snowballstemmer==1.2.0
Sphinx==1.3.1
sphinx-rtd-theme==0.1.8
terminado==0.5
tornado==4.2.1
unicodecsv==0.14.1
xlrd==0.9.4
xlwt==1.0.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment