forked from fblanke/tvstud-serial-mail
Initial commit
This commit is contained in:
commit
3973986011
103
seriesmail.py
Normal file
103
seriesmail.py
Normal file
@ -0,0 +1,103 @@
|
|||||||
|
import argparse
|
||||||
|
import subprocess
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
import pandas as pd
|
||||||
|
from jinja2 import Template
|
||||||
|
|
||||||
|
|
||||||
|
def read_data(
|
||||||
|
data_file: str,
|
||||||
|
sheet_name: str,
|
||||||
|
full_area_name: str,
|
||||||
|
subject: str,
|
||||||
|
dry_run: bool = False,
|
||||||
|
mail_idx: int = 6,
|
||||||
|
name_idx: int = 0,
|
||||||
|
template_file: str = "template.txt",
|
||||||
|
from_addr: str = "bonn@tvstud.de",
|
||||||
|
) -> None:
|
||||||
|
df = pd.read_excel(data_file, sheet_name=sheet_name)
|
||||||
|
today = pd.to_datetime("today").normalize()
|
||||||
|
for idx, row in df.iterrows():
|
||||||
|
name = row[name_idx]
|
||||||
|
mail = row[mail_idx]
|
||||||
|
|
||||||
|
if pd.isna(mail):
|
||||||
|
continue
|
||||||
|
|
||||||
|
with open(template_file) as f:
|
||||||
|
template = Template(f.read())
|
||||||
|
|
||||||
|
msg = template.render(
|
||||||
|
name=name,
|
||||||
|
full_area_name=full_area_name,
|
||||||
|
)
|
||||||
|
|
||||||
|
payload = [
|
||||||
|
"thunderbird",
|
||||||
|
"-compose",
|
||||||
|
f"to={mail},subject={subject},from={from_addr},body='{msg}'",
|
||||||
|
]
|
||||||
|
if dry_run:
|
||||||
|
print(payload)
|
||||||
|
else:
|
||||||
|
subprocess.run(payload)
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
parser = argparse.ArgumentParser("TVStud Bonn Mail")
|
||||||
|
parser.add_argument(
|
||||||
|
"--data-file",
|
||||||
|
type=Path,
|
||||||
|
default=Path("Strukturaufbau.xlsx"),
|
||||||
|
help="Path of the ods/Excel file containing the mapping data",
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"--fromname", type=str, default="TVStud Bonn", help="Sender name to use"
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"--sheet",
|
||||||
|
type=str,
|
||||||
|
default=None,
|
||||||
|
help="The name of the sheet to run on",
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"--full-area-name",
|
||||||
|
type=str,
|
||||||
|
default=None,
|
||||||
|
help="The name of the area to use in the mail (e.g. 'Psychologie' for the sheet 'Psycho')",
|
||||||
|
)
|
||||||
|
|
||||||
|
parser.add_argument(
|
||||||
|
"--print-sheets",
|
||||||
|
action="store_true",
|
||||||
|
help="Print available sheets and exit",
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"--dry-run",
|
||||||
|
action="store_true",
|
||||||
|
help="Do a dry run and only print the generated emails to stdout",
|
||||||
|
)
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
if args.print_sheets or args.sheet is None:
|
||||||
|
xl = pd.ExcelFile(args.data_file)
|
||||||
|
print("Available sheets (specify via '--sheet'):")
|
||||||
|
print(xl.sheet_names)
|
||||||
|
return
|
||||||
|
|
||||||
|
if args.full_area_name is None:
|
||||||
|
args.full_area_name = args.sheet
|
||||||
|
|
||||||
|
read_data(
|
||||||
|
args.data_file,
|
||||||
|
args.sheet,
|
||||||
|
args.full_area_name,
|
||||||
|
subject="TVStud TODO",
|
||||||
|
dry_run=args.dry_run,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
Loading…
x
Reference in New Issue
Block a user