{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Imports \n",
    "import os\n",
    "import sys\n",
    "import subprocess\n",
    "import time\n",
    "import dateutil.parser\n",
    "from ids import *\n",
    "#from hallen import *\n",
    "import locale\n",
    "import re\n",
    "import requests\n",
    "from bs4 import BeautifulSoup\n",
    "import tabulate\n",
    "#NummerDJugend=10\n",
    "#%run ./QR_parser.ipynb"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "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.01in]{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}{-2.5cm}\n",
    "\\begin{document}\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'spiele' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[31m---------------------------------------------------------------------------\u001b[39m",
      "\u001b[31mNameError\u001b[39m                                 Traceback (most recent call last)",
      "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[8]\u001b[39m\u001b[32m, line 104\u001b[39m\n\u001b[32m    102\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m team[y][\u001b[32m0\u001b[39m] == verein:\n\u001b[32m    103\u001b[39m     verein=\u001b[33m'\u001b[39m\u001b[38;5;130;01m\\\\\u001b[39;00m\u001b[33mtextbf\u001b[39m\u001b[33m{\u001b[39m\u001b[33m'\u001b[39m+verein+\u001b[33m'\u001b[39m\u001b[33m}\u001b[39m\u001b[33m'\u001b[39m\n\u001b[32m--> \u001b[39m\u001b[32m104\u001b[39m     spiele = \u001b[33m'\u001b[39m\u001b[38;5;130;01m\\\\\u001b[39;00m\u001b[33mtextbf\u001b[39m\u001b[33m{\u001b[39m\u001b[33m'\u001b[39m + \u001b[43mspiele\u001b[49m + \u001b[33m'\u001b[39m\u001b[33m}\u001b[39m\u001b[33m'\u001b[39m\n\u001b[32m    105\u001b[39m     siege = \u001b[33m'\u001b[39m\u001b[38;5;130;01m\\\\\u001b[39;00m\u001b[33mtextbf\u001b[39m\u001b[33m{\u001b[39m\u001b[33m'\u001b[39m + siege + \u001b[33m'\u001b[39m\u001b[33m}\u001b[39m\u001b[33m'\u001b[39m\n\u001b[32m    106\u001b[39m     unentschieden = \u001b[33m'\u001b[39m\u001b[38;5;130;01m\\\\\u001b[39;00m\u001b[33mtextbf\u001b[39m\u001b[33m{\u001b[39m\u001b[33m'\u001b[39m + unentschieden + \u001b[33m'\u001b[39m\u001b[33m}\u001b[39m\u001b[33m'\u001b[39m\n",
      "\u001b[31mNameError\u001b[39m: name 'spiele' is not defined"
     ]
    }
   ],
   "source": [
    "for y in range(len(team)):\n",
    "#for y in range(10):\n",
    "    #print('Y=',y)\n",
    "    #print(team[y])\n",
    "    #print(y)\n",
    "    #print(team)\n",
    "    try: \n",
    "        table=tabelle(team[y])\n",
    "    except: \n",
    "        print('Table ', team[y][1], ' failed')\n",
    "    try:\n",
    "        Teamspielplan=teamspielplan(team[y])\n",
    "    except:\n",
    "        print('Teamspielplan ', team[y][1], 'failed')\n",
    "    parse=re.compile(r\"([K|L]\\w*\\s\\d?)\")\n",
    "    Liga=getliga(team[y])\n",
    "    #print(y, team[y])\n",
    "    tex_code += r\"\"\"\n",
    "    \\begin{textblock}{3}(0,13.2)\n",
    "    \\begin{figure}\n",
    "    \\includegraphics[width=3cm]{\"\"\"+team[y][Shortname]\n",
    "    tex_code += r\"\"\"}\\\\\\centering\n",
    "    Link zur liga\\end{figure}\\end{textblock}\n",
    "    \\begin{center}\n",
    "    \\vspace{0cm}\n",
    "    {\\scalefont{3}\"\"\"\n",
    "    tex_code +=r\"{}\".format(team[y][Alias])\n",
    "    tex_code +=r\"\"\"}\\\\\\scalefont{2}\"\"\"\n",
    "    if y<14:\n",
    "        tex_code +=r\"{}\".format(Liga)\n",
    "        tex_code +=r\"\"\"\\\\\"\"\"\n",
    "    tex_code +=r\"\"\"\n",
    "    \\vspace{0cm}\n",
    "    \\end{center}\n",
    "    \\rowcolors{2}{gray!25}{white}\n",
    "    \\begin{table}[h]\"\"\"\n",
    "    tex_code +=r\"\"\"\\small\n",
    "   \"\"\"\n",
    "    tex_code +=r\"\"\"\\centering\"\"\"\n",
    "    if len(table[0]) == 9:\n",
    "        tex_code +=r\"\"\"\\begin{tabular}{|L{0.6cm}|L{4.3cm}|C{1.2cm}|C{0.5cm}|C{0.4cm}|C{0.5cm}|C{1.5cm}|C{1.2cm}|C{1.3cm}|}\"\"\"\n",
    "        tex_code +=r\"\"\"\\hline\n",
    "        \\rowcolor{gray!50}\"\"\"\n",
    "        tex_code +=r\"\"\"Nr. & Mannschaft &Spiele& S & U &N & Tore & diff. & Punkte\\\\\"\"\"\n",
    "    if len(table[0]) == 7:\n",
    "        tex_code +=r\"\"\"\\begin{tabular}{|L{0.6cm}|L{5.5cm}|C{1.3cm}|C{0.5cm}|C{0.4cm}|C{0.5cm}|C{1.5cm}|}\"\"\"\n",
    "        tex_code +=r\"\"\"\\hline\n",
    "        \\rowcolor{gray!50}\"\"\"\n",
    "        tex_code +=r\"\"\"Nr. & Mannschaft &Spiele& S & U &N & Punkte\\\\\"\"\"\n",
    "    if len(table[0]) == 3:\n",
    "        tex_code +=r\"\"\"\\begin{tabular}{|L{5.5cm}|}\"\"\"\n",
    "        tex_code +=r\"\"\"\\hline\n",
    "        \\rowcolor{gray!50}\"\"\"\n",
    "        tex_code +=r\"\"\"Mannschaft\\\\\"\"\"\n",
    "    tex_code +=r\"\"\"\\hline \"\"\"\n",
    "    for x in range(len(table)):\n",
    "        verein = Teamspielplan_kuerzen(table[x][1])\n",
    "        rank = table[x][0]\n",
    "\n",
    "        if len(table[x]) > 3:\n",
    "            if len(table[x][3]) < 3:\n",
    "                spiele =table[x][2]\n",
    "            else:\n",
    "                spiele = '-'\n",
    "            try: \n",
    "                if len(table[x][3]) < 3:\n",
    "                    siege = table[x][3]\n",
    "                else: \n",
    "                    siege = '-'\n",
    "            except:\n",
    "                siege = '0'\n",
    "        if table[x][3].startswith('zur') or table[x][2].startswith('zur'):\n",
    "            break\n",
    "        if len(table[0]) > 3:\n",
    "            try: \n",
    "                unentschieden = table[x][4]\n",
    "                niederlagen = table[x][5]\n",
    "            except:\n",
    "                unentschieden ='-'\n",
    "                niederlagen = '-'\n",
    "        if len(table[0]) > 7:\n",
    "            try:\n",
    "                tore = table[x][6]\n",
    "                diff = table[x][7]\n",
    "                punkte = table[x][8]\n",
    "            except:\n",
    "                tore = '-'\n",
    "                diff = '-'\n",
    "                punkte = '-'\n",
    "        if len(table[0]) == 7: \n",
    "            try:\n",
    "                punkte = table[x][6]\n",
    "                tore = '-'\n",
    "                diff = '-'\n",
    "            except:\n",
    "                punkte = '-'\n",
    "                print('Tabellenlänge falsch: '+team[y][1])\n",
    "                print(table)\n",
    "                #spiele=\"\\multicolumn{4}{|c|}{\"+table[x][2]+\"}\"\n",
    "        rank = '\\\\textbf{' + rank + '}'\n",
    "        #if \"Eschweiler\" in verein:\n",
    "        if team[y][0] == verein:\n",
    "            verein='\\\\textbf{'+verein+'}'\n",
    "            spiele = '\\\\textbf{' + spiele + '}'\n",
    "            siege = '\\\\textbf{' + siege + '}'\n",
    "            unentschieden = '\\\\textbf{' + unentschieden + '}'\n",
    "            niederlagen = '\\\\textbf{' + niederlagen + '}'\n",
    "            tore = '\\\\textbf{' + tore + '}'\n",
    "            diff = '\\\\textbf{' + diff + '}'\n",
    "            punkte = '\\\\textbf{' + punkte + '}'\n",
    "        if len(table[0]) == 9:\n",
    "            tex_code += r\"\"\"{}&{}&{}&{}&{}&{}&{}&{}&{}\\\\\"\"\".format(rank,verein, spiele,siege,unentschieden,niederlagen,tore,diff,punkte)\n",
    "        if len(table[0]) == 7:\n",
    "            tex_code += r\"\"\"{}&{}&{}&{}&{}&{}&{}\\\\\"\"\".format(rank,verein, spiele,siege,unentschieden,niederlagen,punkte)\n",
    "        if len(table[0]) == 3:\n",
    "            tex_code += r\"\"\"{}\\\\\"\"\".format(verein)\n",
    "        tex_code += os.linesep\n",
    "    tex_code+=r\"\"\"\n",
    "        \\hline\n",
    "        \\end{tabular}\"\"\"\n",
    "    tex_code+=r\"\"\"\\hspace*{0.3cm}\"\"\"\n",
    "    if len(table[0]) > 3:\n",
    "        tex_code+=r\"\"\"\n",
    "        \\begin{tabular}{|C{0.65cm}|C{2cm}|C{1.0cm}|C{4.1cm}|C{4.1cm}|C{1.2cm}|C{1.4cm}|}\n",
    "        \\hline\n",
    "        \\rowcolor{gray!50}\n",
    "        Tag & Datum & Zeit& Heim & Gast & Erg.& Halle\\\\\n",
    "        \"\"\"\n",
    "    if len(table[0]) == 3:\n",
    "        tex_code+=r\"\"\"\n",
    "        \\begin{tabular}{|C{0.65cm}|C{2cm}|C{1.0cm}|C{4.1cm}|C{4.1cm}|C{1.4cm}|}\n",
    "        \\hline\n",
    "        \\rowcolor{gray!50}\n",
    "        Tag & Datum & Zeit& Heim & Gast & Halle\\\\\n",
    "        \"\"\"\n",
    "        \n",
    "    for x in range(len(Teamspielplan)):\n",
    "        tag = Teamspielplan[x][0]\n",
    "        if tag == 'Termin offen':\n",
    "            tag = ' '\n",
    "            Teamspielplan[x][0]='offen'\n",
    "            Teamspielplan[x].insert(0,'')\n",
    "        zeit = Teamspielplan[x][2].lstrip()\n",
    "        #try:\n",
    "        #    zeit = Teamspielplan[x][2]\n",
    "        #except: \n",
    "        #    zeit = Teamspielplan[x][2].replace('v', '')    \n",
    "        if zeit == '00:00':\n",
    "            zeit = ''\n",
    "        datum = Teamspielplan[x][1]\n",
    "        heim = Teamspielplan_kuerzen(Teamspielplan[x][5])\n",
    "        gast = Teamspielplan_kuerzen(Teamspielplan[x][6])\n",
    "        if team[y][0] == heim:\n",
    "            heim='\\\\textbf{'+team[y][0]+'}'\n",
    "        if team[y][0] == gast:\n",
    "            gast='\\\\textbf{'+team[y][0]+'}'\n",
    "        turnhalle = Teamspielplan[x][3]\n",
    "        ergebnis = Teamspielplan[x][7]\n",
    "        #print(ergebnis)\n",
    "        if \"spielfrei\" in gast or \"spielfrei\" in heim:\n",
    "            continue\n",
    "        if len(table[0]) > 3:\n",
    "            tex_code += r\"{}&{}&{}&{}&{}&{}&{}\\\\\".format(tag,datum,zeit[0:5],heim,gast,ergebnis,halle(turnhalle))\n",
    "            tex_code += os.linesep\n",
    "        if len(table[0]) == 3:\n",
    "            tex_code += r\"{}&{}&{}&{}&{}&{}\\\\\".format(tag,datum,zeit[0:5],heim,gast,halle(turnhalle))\n",
    "            tex_code += os.linesep\n",
    "    tex_code+=r\"\"\"\n",
    "    \\hline\n",
    "    \\end{tabular}\"\"\"\n",
    "    tex_code+=r\"\"\"\n",
    "    \\end{table}\"\"\"\n",
    "    erklaerung =''\n",
    "    if any('NG' in sublist for sublist in Teamspielplan):\n",
    "        erklaerung += '\\\\textbf{NG} = Gastmannschaft nicht angetreten' \n",
    "    if any('NH' in sublist for sublist in Teamspielplan):\n",
    "        if erklaerung != '':\n",
    "            erklaerung += '; '\n",
    "        erklaerung += '\\\\textbf{NH} = Heimmannschaft nicht angetreten' \n",
    "    if any('WG' in sublist for sublist in Teamspielplan):\n",
    "        if erklaerung != '':\n",
    "            erklaerung += '; '\n",
    "        erklaerung += '\\\\textbf{WG} = Wertung für Heimmannschaft' \n",
    "    if any('WH' in sublist for sublist in Teamspielplan):\n",
    "        if erklaerung != '':\n",
    "            erklaerung += '; '\n",
    "        erklaerung += '\\\\textbf{WH} = Wertung für Auswärtsmannschaft' \n",
    "    if any('ZH' in sublist for sublist in Teamspielplan):\n",
    "        if erklaerung != '':\n",
    "            erklaerung += '; '\n",
    "        erklaerung += '\\\\textbf{ZH} = Heimmannschaft zurückgezogen'\n",
    "    if any('ZG' in sublist for sublist in Teamspielplan):\n",
    "        if erklaerung != '':\n",
    "            erklaerung += '; '\n",
    "        erklaerung += '\\\\textbf{ZG} = Gastmannschaft zurückgezogen'\n",
    "    if erklaerung != '':\n",
    "        tex_code+=r\"\"\"\\begin{textblock}{9}(7,15.5)\"\"\"\n",
    "        tex_code+=erklaerung\n",
    "        tex_code+=r\"\"\"\\end{textblock}\"\"\"\n",
    "    tex_code+=r\"\"\"\n",
    "    \\newpage\"\"\"\n",
    "\n",
    "tex_code += r\"\"\"\n",
    "\\centering\n",
    "\\scalefont{5}Die nächsten Spiele\\\\[0.5cm]\n",
    "\\begin{table}[h]\n",
    "\\centering\n",
    "\\begin{tabular}{|p{3cm} R{10cm} p{0.5cm} p{10cm} p{4cm}|}\n",
    "\\rowcolor{gray!50}\n",
    "\"\"\"\n",
    "minis=False\n",
    "Spiele=naechsteSpiele(4)\n",
    "Tage=0\n",
    "spiele=0\n",
    "for spiel in Spiele:\n",
    "    tag = spiel[0]\n",
    "    if tag == '' :\n",
    "        spiele=spiele+1\n",
    "    else: \n",
    "        Tage=Tage+1\n",
    "#print(Tage)\n",
    "#print(spiele)\n",
    "if spiele > 18:\n",
    "    limit=Tage-1\n",
    "    #print(\"begrenzt\")\n",
    "else:\n",
    "    limit = 8\n",
    "#print(limit)\n",
    "ctr=0\n",
    "for spiel in Spiele:\n",
    "    tag = spiel[0]\n",
    "    datum = spiel[1]\n",
    "    uhrzeit = spiel[2]\n",
    "    if tag != '' :\n",
    "        ctr=ctr+1\n",
    "        if ctr>limit:\n",
    "            break\n",
    "        tex_code += r\"\"\"\\rowcolor{darkgray!50}\\hline\\multicolumn{5}{|l|}{{\\scalefont{2.5}\\textbf{\\textcolor{white}{\"\"\"\n",
    "        tex_code += r\"{}\".format(tag + '' + datum)\n",
    "        tex_code += r\"}}}}\\\\\"\n",
    "        tex_code += os.linesep\n",
    "    halle=spiel[3]\n",
    "    heim=spiel[6]\n",
    "    gast=spiel[7]\n",
    "    if (heim==\"spielfrei\") or (gast == \"spielfrei\"):\n",
    "        continue \n",
    "    if (heim==\"\\\\textbf{Minis}\") or (gast == \"\\\\textbf{Minis}\"):\n",
    "        if minis == True:\n",
    "            continue\n",
    "        if minis == False:\n",
    "            minis = True\n",
    "            #TEXT=\"Mini Turnier\"\n",
    "            tex_code += r'\\LARGE {'+uhrzeit[0:5]+r'}&\\multicolumn{3}{c}{\\LARGE \\\\textbf{ MINI Turnier}}&\\LARGE {'+ halle +r'}'  \n",
    "            tex_code += r\"\\\\\"\n",
    "            tex_code += os.linesep\n",
    "    # 18 Lines\n",
    "    else:\n",
    "        # print bla.strftime(\"%d.%m.%Y %H:%M\"), heim,' - ', gast, halle, strasse, ort, gespann\n",
    "        tex_code += r\"\\LARGE {}&\\LARGE {} &\\LARGE :& \\LARGE {}&\\LARGE {}\\\\\".format(uhrzeit[0:5],heim,gast,halle)\n",
    "        tex_code += os.linesep\n",
    "tex_code += r\"\"\"\n",
    "\\hline\n",
    "\\end{tabular}\n",
    "\\end{table}\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "tex_code+=r\"\"\"\n",
    "\\end{document}\"\"\"\n",
    "#print(tex_code)\n",
    "tex_file = \"tabelle.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/2025/bin/universal-darwin/pdflatex tabelle.tex\")\n",
    "os.system(\"rm -f *.log *.aux\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "@webio": {
   "lastCommId": null,
   "lastKernelId": null
  },
  "kernel_info": {
   "name": "ldap3-ipython"
  },
  "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.13.6"
  },
  "nteract": {
   "version": "0.15.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
