Skip to content

Instantly share code, notes, and snippets.

@amarczew
Created August 4, 2019 18:08
Show Gist options
  • Save amarczew/2b446ffd06ba96fac80a2b151696d329 to your computer and use it in GitHub Desktop.
Save amarczew/2b446ffd06ba96fac80a2b151696d329 to your computer and use it in GitHub Desktop.
Baixar suas transações do guiabolso para um único arquivo csv ou xlsx.
'''
Requirements:
guiabolso2csv -- code below based on __main__.py file in this library
unicodecsv
click
requests
openpyxl
Example:
python guiabolso2csv.py --email seuemail@mail.com --year 2015 --month 01 --last-year 2019 --last-month 07 --unique-file
'''
import click
import datetime
import unicodecsv as csv
import openpyxl
from guiabolso2csv.guia_bolso import GuiaBolso
def month_iterator(initial_date, finish_date):
current_date = initial_date.replace(day=1)
while current_date <= finish_date:
yield current_date
current_date += datetime.timedelta(days=32)
current_date = current_date.replace(day=1)
@click.command()
@click.option('--email', prompt=True, help="Email used in your GuiaBolso accou"
"nt")
@click.option('--password', prompt=True, hide_input=True)
@click.option('--year', prompt=True, type=int,
help="Year from the transactions you are interested. It will be "
"used as the first year if LAST_YEAR is specified")
@click.option('--month', prompt=True, type=click.IntRange(1, 12),
help="Month from the transactions you are interested. It will be"
" used as the first month if LAST_MONTH is specified")
@click.option('--last-year', type=int, default=None,
help="If you specify last year it will be used as the last year "
"to get a range of years, starting in YEAR and ending in LA"
"ST_YEAR")
@click.option('--last-month', type=click.IntRange(1, 12), default=None,
help="If you specify last month it will be used as the last mont"
"h to get a range of months, starting in MONTH and ending i"
"n LAST_MONTH")
@click.option('--excel', is_flag=True, help="Save transactions as xlsx instead"
" of csv")
@click.option('--unique-file', is_flag=True, help='Save all transactions in a unique file with yearmon field')
def main(email, password, year, month, last_year, last_month, excel, unique_file):
gb = GuiaBolso(email, password)
initial_date = datetime.date(year, month, 1)
finish_date = datetime.date(last_year or year, last_month or month, 1)
transactions = list()
for date in month_iterator(initial_date, finish_date):
year = date.year
month = date.month
if unique_file:
_trans = gb.transactions(year, month)
for t in _trans:
t['yearmon'] = datetime.date(year, month, 1)
transactions.extend(_trans)
else:
filename = "%i-%i" % (year, month)
if excel:
filename += '.xlsx'
gb.xlsx_transactions(year, month, filename)
else:
filename += '.csv'
gb.csv_transactions(year, month, filename)
print(filename)
if unique_file:
if len(transactions) == 0:
print('No transactions for the period')
return
filename = 'gb_%i-%i' % (year, month)
if excel:
filename += '.xlsx'
wb = openpyxl.Workbook()
ws = wb.active
ws.append([u'yearmon'] + gb.fieldnames)
for trans in transactions:
if u'date' in trans:
trans[u'date'] = datetime.datetime.fromtimestamp(
trans[u'date']/1000).date()
row = [trans[k] for k in self.fieldnames]
ws.append(row)
wb.save(filename)
else:
filename += '.csv'
with open(filename, 'wb') as f:
csv_writer = csv.DictWriter(f, fieldnames=[u'yearmon'] + gb.fieldnames,
encoding='utf-8-sig') # add BOM to csv
csv_writer.writeheader()
csv_writer.writerows(transactions)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment