import2ledger/import2ledger/importers/patreon.py

68 lines
1.8 KiB
Python

import pathlib
import re
from . import _csv
from .. import util
class IncomeImporter(_csv.CSVImporterBase):
NEEDED_FIELDS = frozenset([
'FirstName',
'LastName',
'Pledge',
'Status',
])
ENTRY_SEED = {
'currency': 'USD',
}
TEMPLATE_KEY = 'template patreon income'
def __init__(self, input_file):
super().__init__(input_file)
match = re.search(r'(?:\b|_)(\d{4}-\d{2}-\d{2})(?:\b|_)',
pathlib.Path(input_file.name).name)
if match:
self.entry_seed['date'] = util.strpdate(match.group(1), '%Y-%m-%d')
def _read_row(self, row):
if row['Status'] != 'Processed':
return None
else:
return {
'amount': row['Pledge'].replace(',', ''),
'payee': '{0[FirstName]} {0[LastName]}'.format(row),
}
class FeeImporterBase(_csv.CSVImporterBase):
ENTRY_SEED = {
'currency': 'USD',
'payee': "Patreon",
}
def _read_row(self, row):
return {
'amount': row[self.AMOUNT_FIELD].lstrip('$'),
'date': util.strpdate(row['Month'], '%Y-%m'),
}
class PatreonFeeImporter(FeeImporterBase):
AMOUNT_FIELD = 'Patreon Fee'
NEEDED_FIELDS = frozenset(['Month', AMOUNT_FIELD])
TEMPLATE_KEY = 'template patreon svcfees'
class CardFeeImporter(FeeImporterBase):
AMOUNT_FIELD = 'Processing Fees'
NEEDED_FIELDS = frozenset(['Month', AMOUNT_FIELD])
TEMPLATE_KEY = 'template patreon cardfees'
class VATImporter(FeeImporterBase):
AMOUNT_FIELD = 'Vat Charged'
NEEDED_FIELDS = frozenset(['Month', AMOUNT_FIELD])
COPIED_FIELDS = {
'Country Code': 'country_code',
'Country Name': 'country_name',
}
TEMPLATE_KEY = 'template patreon vat'