{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "import re\n",
    "from bs4 import BeautifulSoup\n",
    "#import qrcode\n",
    "import os\n",
    "import sys\n",
    "import subprocess\n",
    "import time\n",
    "response = requests.get('https://www.esg-handball.de/training/index')\n",
    "Fi =str(response.content.decode('utf-8', 'ignore'))\n",
    "Fi_cleaned = re.sub('(\\s{2,})',' ',str(Fi))\n",
    "soup = BeautifulSoup(Fi_cleaned, 'html.parser')\n",
    " \n",
    "tabelle=soup.find_all(class_='content-box m-bot-15')\n",
    "\n",
    "rows = soup.find_all('table')\n",
    "#print(rows[0])\n",
    "header = soup.find_all('h2')\n",
    "#print(header)\n",
    "parse=re.compile(r\"\\<h2\\>(.*)\\<\\/h2\\>\")\n",
    "headers =[]\n",
    "for head in header:\n",
    "    name=parse.findall(str(head))\n",
    "    try:\n",
    "        headers.append(name[0])\n",
    "    except:\n",
    "        continue\n",
    "dates = []\n",
    "for row in rows:\n",
    "    training = row.find_all('p')\n",
    "    parse=re.compile(r\"\\<p\\>(.*)\\<\\/p\\>\")\n",
    "    date = []\n",
    "    for ele in training:\n",
    "        name=parse.findall(str(ele))\n",
    "        date.append(name[0])\n",
    "    dates.append(date)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#print(dates)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "done.\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "sh: /usr/local/TeX/bin/x86_64-darwin/pdflatex: No such file or directory\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "32512"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tex_code = r\"\"\"\n",
    "\\documentclass[12pt,a4paper,landscape]{extarticle}\n",
    "\\usepackage[T1]{fontenc}\n",
    "\\usepackage[default]{opensans}\n",
    "\\usepackage[utf8]{inputenc}\n",
    "\\usepackage[table]{xcolor}\n",
    "\\usepackage[margin=0.1in]{geometry}\n",
    "\\usepackage{array}\n",
    "\\usepackage{arev}\n",
    "%\\usepackage[T1]{fontenc}\n",
    "\\usepackage{scalefnt}\n",
    "\\usepackage{caption}\n",
    "\\usepackage{array}\n",
    "\\usepackage{graphicx}\n",
    "\\usepackage[absolute]{textpos}\n",
    "\\usepackage{threeparttable}\n",
    "\\newcolumntype{L}[1]{>{\\raggedright\\let\\newline\\\\\\arraybackslash\\hspace{0pt}}m{#1}}\n",
    "\\newcolumntype{C}[1]{>{\\centering\\let\\newline\\\\\\arraybackslash\\hspace{0pt}}m{#1}}\n",
    "\\newcolumntype{R}[1]{>{\\raggedleft\\let\\newline\\\\\\arraybackslash\\hspace{0pt}}m{#1}}\n",
    "\\makeatletter\n",
    "\\newcommand{\\thickhline}{%\n",
    "    \\noalign {\\ifnum 0=`}\\fi \\hrule height 2pt\n",
    "    \\futurelet \\reserved@a \\@xhline\n",
    "}\n",
    "\\setlength{\\tabcolsep}{.16667em}\n",
    "\\newcolumntype{\"}{@{\\hskip\\tabcolsep\\vrule width 2pt\\hskip\\tabcolsep}}\n",
    "\\makeatother\n",
    "\\usepackage{color, colortbl}\n",
    "\\definecolor{LRed}{rgb}{1,.8,.8}\n",
    "\\definecolor{LGreen}{rgb}{.8,1,.8}\n",
    "\\definecolor{LBlue}{rgb}{8,39,142}\n",
    "\\renewcommand{\\rmdefault}{ptm}\n",
    "\\setlength{\\topmargin}{-3.5cm}\n",
    "\\begin{document}\n",
    "\\begin{center}\n",
    "\\vspace{-2cm}\n",
    "{\\scalefont{3}Trainingszeiten\"\"\"\n",
    "tex_code +=r\"\"\"}\\\\\\scalefont{2}\\end{center}\"\"\"\n",
    "#tex_code +=r\"\"\"\\begin{table}[h]\"\"\"\n",
    "tex_code +=r\"\"\"\\centering\"\"\"\n",
    "tex_code +=r\"\"\"\\begin{tabular}{|L{6cm}|L{5.5cm}|C{4cm}|L{13cm}|}\"\"\"\n",
    "tex_code +=r\"\"\"\\hline \"\"\"\n",
    "tex_code +=r\"\"\"Mannschaft. & Tag &Uhrzeit& Halle\\\\\"\"\"\n",
    "tex_code +=r\"\"\"\\hline \"\"\"\n",
    "for x in range(len(headers)):\n",
    "\n",
    "    #print(headers[x])\n",
    "    for y in range(3):\n",
    "        if (x % 2) == 0:\n",
    "            tex_code +=r\"\"\"\\rowcolor{gray!25}\"\"\"\n",
    "        else:\n",
    "            tex_code +=r\"\"\"\\rowcolor{white}\"\"\"\n",
    "        if y == 0:\n",
    "            tex_code +=r\"\"\"{}&{}&{}&{}\\\\\"\"\".format(headers[x],dates[x][y*4], dates[x][(y*4)+1], (dates[x][(y*4)+2])[7:])\n",
    "        else:\n",
    "            try: \n",
    "                tex_code +=r\"\"\"{}&{}&{}&{}\\\\\"\"\".format(' ',dates[x][y*4], dates[x][(y*4)+1], (dates[x][(y*4)+2])[7:])\n",
    "            except:\n",
    "                continue\n",
    "    tex_code +=r\"\"\"\\hline \"\"\"\n",
    "tex_code+=r\"\"\"\\end{tabular}\"\"\"    \n",
    "#tex_code+=r\"\"\"\\end{table}\"\"\" \n",
    "tex_code+=r\"\"\"\\end{document}\"\"\"\n",
    "#print(tex_code)\n",
    "tex_file = \"training.tex\"\n",
    "with open(tex_file, \"w\") as out_file:\n",
    "    out_file.write(tex_code)\n",
    "print(\"done.\")\n",
    "os.system(\"/usr/local/texlive/2023/bin/universal-darwin/pdflatex training.tex\")\n",
    "os.system(\"rm -f *.log *.aux\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#print(headers)\n",
    "parse=re.compile(r\"([mw])\\S*\\s(\\w\\-\\w*)\\s(\\d?)|(\\d\\.)\\s(\\w*)|([mw])\\S*\\s(\\w\\-\\w*)|^(\\w\\-\\w*)|^(\\w*)\\Z\")\n",
    "headers2 =[]\n",
    "for head in headers:\n",
    "    name=parse.findall(head)\n",
    "    headers2.append(name)\n",
    "#print(headers2) \n",
    "mails=[]\n",
    "for head in headers2:\n",
    "    res=''\n",
    "    try:\n",
    "        for x in head[0]:\n",
    "            res+=str(x)\n",
    "    except:\n",
    "        continue\n",
    "    if res=='Damen':\n",
    "        res='1.Damen'\n",
    "    #res+=\"@esg-handball.de\"\n",
    "    mails.append(res)\n",
    "print(mails)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from datetime import datetime\n",
    "day_of_year = datetime.now().timetuple().tm_yday\n",
    "year=datetime.now().timetuple().tm_year\n",
    "#print(day_of_year)\n",
    "#print(year)\n",
    "if day_of_year<182:\n",
    "    add_off=-1\n",
    "else:\n",
    "    add_off=0\n",
    "Offsets_2nd=[18, 16,14,12,10,8,6]\n",
    "Shift=[-1,-1,-1,-1,-1,-1,-2]\n",
    "Jahrgänge=[]\n",
    "for x in range(len(Offsets_2nd)):\n",
    "    Jahrgänge.append(str(year-Offsets_2nd[x]+add_off)+'-'+str(year-(Offsets_2nd[x]+Shift[x]+add_off)))\n",
    "Jugenden=['A-Jugend','B-Jugend', 'C-Jugend', 'D-Jugend', 'E-Jugend', 'F-Jugend','Minis']\n",
    "Mannschaften = [[0 for x in range(2)] for y in range(7)]\n",
    "for i in range(len(Jugenden)):\n",
    "    Mannschaften[i][1]=Jahrgänge[i]\n",
    "    Mannschaften[i][0]=Jugenden[i]\n",
    "#print(Mannschaften)\n",
    "Teams = [[0 for x in range(3)] for y in range(len(mails))]\n",
    "for i in range(len(mails)):\n",
    "    Teams[i][0]=mails[i] \n",
    "    for x in range(len(Jugenden)):\n",
    "        if Jugenden[x][0] in str(Teams[i][0]):\n",
    "            Teams[i][1] = Mannschaften[x][1]\n",
    "            Teams[i][2] = Mannschaften[x][0]\n",
    "            break\n",
    "print(Teams)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for x in range(len(headers)):\n",
    "    for y in range(3):\n",
    "        try:\n",
    "            Teams[x][2].append(dates[x][y*4], dates[x][(y*4)+1], dates[x][(y*4)+2])\n",
    "        except:\n",
    "            continue\n",
    "print(Teams)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
