Skip to content
This repository has been archived by the owner on Sep 5, 2022. It is now read-only.

Commit

Permalink
Add command 'contacts' for extracting contact information
Browse files Browse the repository at this point in the history
  • Loading branch information
S1SYPHOS committed May 8, 2021
1 parent 6557a99 commit f4b7c91
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ data/*
build
imports
backup*
blocklist.txt
2 changes: 2 additions & 0 deletions blocklist.example.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[email protected]
[email protected]
1 change: 1 addition & 0 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ output:
invoice_file: invoices.pdf
match_dir: build/matches
rank_dir: build/rankings
contacts_dir: build/contacts
52 changes: 52 additions & 0 deletions lib/inspector.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from operator import itemgetter
from os.path import basename, join

import pendulum
from pandas import DataFrame
from PyPDF2 import PdfFileReader, PdfFileMerger

Expand Down Expand Up @@ -217,3 +218,54 @@ def rank(self, year, quarter) -> None:
ranking_file = join(self.config['rank_dir'], file_name + '.csv')

self.export_csv(ranking, ranking_file)


def contacts(self, cutoff_date):
today = pendulum.today()

# Set default date
if cutoff_date is None:
cutoff_date = today.subtract(years=2).to_datetime_string()[:10]

# Select order files to be analyzed
order_files = build_path(self.config['order_dir'])

# Fetch their content
orders = load_json(order_files)

# Load blacklisted mail addresses
with open('blocklist.txt', 'r') as file:
blocklist = file.read().splitlines()

codes = set()
contacts = []

for order in sorted(orders, key=itemgetter('Datum'), reverse=True):
mail_address = order['Email']

if mail_address in blocklist:
continue

# Throw out everything before cutoff date
if order['Datum'] < cutoff_date:
continue

# Prepare dictionary
contact = {}

contact['Anrede'] = order['Anrede']
contact['Vorname'] = order['Vorname']
contact['Nachname'] = order['Nachname']
contact['Name'] = order['Name']
contact['Email'] = order['Email']
contact['Letzte Bestelltung'] = order['Datum']

if mail_address not in codes:
codes.add(mail_address)
contacts.append(contact)

# Write ranking to CSV file
file_name = cutoff_date + '_' + today.to_datetime_string()[:10]
contacts_file = join(self.config['contacts_dir'], file_name + '.csv')

self.export_csv(contacts, contacts_file)
18 changes: 18 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,5 +132,23 @@ def rank(
click.echo(' done!')


@click.option('-c', '--config', help='Path to configuration file.')
@click.option('-d', '--date', help='Cutoff date in ISO date format, eg \'YYYY-MM-DD\'. Default: today two years ago')
@cli.command()
def contacts(config: str, date: str = None):
"""Generates mailmerge-ready contact list"""

# Load config
config = load_config(config)

# Initialize object
handler = Inspector(config)

# Load & match data sources
click.echo('Generating contact list ..', nl=False)
handler.contacts(date)
click.echo(' done!')


if __name__ == '__main__':
cli()
11 changes: 10 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
knv-paypal-matcher @ git+https://github.com/fundevogel/knv-pypal@96480054d480c932da3e890d87053c6fc2047ebd
astroid==2.5.6
click==7.1.2
isort==5.8.0
lazy-object-proxy==1.6.0
mccabe==0.6.1
numpy==1.20.2
pandas==1.2.4
pendulum==2.1.2
pylint==2.8.2
PyPDF2==1.26.0
python-dateutil==2.8.1
pytz==2021.1
pytzdata==2020.1
PyYAML==5.4.1
six==1.15.0
toml==0.10.2
wrapt==1.12.1

0 comments on commit f4b7c91

Please sign in to comment.