-
-
Save turicas/e90f6d0f7b313b412cb2 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# 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 | |
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 row pegou do html | |
def get_ordered_row(html, tableIndex): | |
print 'importando a tabela rows do html' | |
table = rows.import_from_html(BytesIO(html), enconding='utf-8', | |
index=tableIndex) | |
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 main(): | |
DOMAIN = 'http://www.curto.coffee' | |
URL_CONTADOR = DOMAIN + '/contador' | |
# abre o arquivo html com a lista de dias trabalhados | |
HTML_CONTADOR = requests.get(URL_CONTADOR).text | |
print 'acessando a pagina com os dias trabalhados' | |
# cria um objeto BeautifulSoup da pagina URL_CONTADOR | |
SOUP = BeautifulSoup(HTML_CONTADOR, 'html.parser') | |
print 'pega o ultimo dia trabalhado' | |
# pega a url do ultimo dia trabalhado (primeiro item da lista) | |
URL_LAST_WORKDAY = DOMAIN + SOUP.a.get('href') | |
print 'preparando para pegar o html dos dados do ultimo dia trabalhado' | |
# pega o conteudo HTML do consumo do ultimo dia | |
summaryHTML = requests.get(URL_LAST_WORKDAY).content | |
print len(summaryHTML) | |
#summaryHTML = summaryHTML[0:15000] | |
# pega um dicionario com os dados que | |
# o rows pegou na pagina de consumo de hoje | |
consumo = get_ordered_row(summaryHTML, 0) | |
consumo_avulso = get_ordered_row(summaryHTML, 1) | |
tabela = rows.import_from_html(BytesIO(summaryHTML), encoding='utf-8', index=1) | |
recargas = tabela[-1].valor | |
# pega a data do ultimo dia trabalhado | |
date = last_worked_day(SOUP.a.get_text()) | |
# atualiza os dados do consumo de hoje no ultimo dia trabalhado | |
f.update_gspread_cells(date, consumo, consumo_avulso, recargas) | |
#if __name__ == '__main__': | |
# main() |
This file contains hidden or 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
# 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) |
This file contains hidden or 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
BeautifulSoup==3.2.1 | |
beautifulsoup4==4.4.1 | |
click==6.2 | |
gspread==0.3.0 | |
httplib2==0.9.2 | |
lxml==3.5.0 | |
oauth2client==1.5.2 | |
pyasn1==0.1.9 | |
pyasn1-modules==0.0.8 | |
pycrypto==2.6.1 | |
requests==2.9.1 | |
rows==0.1.1 | |
rsa==3.3 | |
six==1.10.0 | |
unicodecsv==0.14.1 | |
wheel==0.24.0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment