morningpaper/morningpaper.py

143 lines
4.8 KiB
Python

from datetime import datetime
from dateutil.relativedelta import relativedelta
import os
import requests
import re
import shutil
import csv
from tabulate import tabulate
import random
import time
import webbrowser
from bs4 import BeautifulSoup
# url = 'https://www.kuriose-feiertage.de/'
# webbrowser.open(url)
#Dateimanagement
os.chdir(os.path.dirname(__file__))
path =os.path.join(os.path.join(os.environ['USERPROFILE']), 'Desktop')
new_file=os.path.join(path ,"goodmorning.html")
shutil.copyfile('template.html', 'template_1.html')
#Datumsaufgaben
date_format = "%d.%m.%Y"
Now = datetime.now()
#Berechnung der Differenz von Einstiegsdatum bis jetzt
def datecalc(employee_row):
date = datetime.strptime(employee_row, date_format)
delta = Now - date
difference = relativedelta(Now, date)
return str(delta.days),str(difference.years),str(difference.months),str(difference.days)
employedata=list(csv.reader((open('input.csv')), delimiter=';'))
employedata[0].extend(['Tagegesamt', 'Jahre', 'Monate', 'Tage'])
for i in range(1, len(employedata)):
a=datecalc(employedata[i][2])
employedata[i].extend([a[0], a[1], a[2], a[3]])
table = tabulate(employedata, tablefmt='html')
def getdateinfo():
response = requests.get('https://welcher-tag-ist-heute.org/')
title= re.sub(r'.*<title>(.*)</title>.*','\g<1>', response.text, flags=re.S)
name_dat= re.sub('.*<a href="#Namenstag" class="box color4" id="name">(.*?</p>).*','\g<1>', response.text, flags=re.S)
spec_day = re.sub('.*<div class="inner">(.*)</div> <!-- .inner -->.*','\g<1>', response.text, flags=re.S)
spec_day = re.sub('</div>.*','', spec_day, flags=re.S)
spec_day = re.sub('<a href=".*?">','', spec_day, flags=re.S)
spec_day = re.sub('</a>','', spec_day, flags=re.S)
return title,name_dat,spec_day
def templator(templatorid, templatorresult):
with open('template_1.html', 'r', encoding="utf-8") as file:
string = file.read()
string = string.replace(templatorid, templatorresult)
with open('template_1.html', 'w', encoding="utf-8") as file:
file.write(string)
# title,name_dat,spec_day = getdateinfo()
def getdateinfo_new():
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36'}
response = requests.get('https://www.kuriose-feiertage.de/', headers=headers)
html = BeautifulSoup(response.text, 'html.parser')
titles = html.find_all(class_='entry-title')
spec_day=''
for title in titles:
#print (str(title))
spec_day+=str(title)
return spec_day
#print ('---->'+str(title_element.text)+'<-----')
spec_day = getdateinfo_new()
#Test:
#Rebecca immer mo-donnerstag ganz oben und Tatjana di-do direkt hinter rebecca, sonst random
tablehead = employedata[0]
tablebody = employedata[1:]
shuffled_tablebody = tablebody[:]
random.shuffle(shuffled_tablebody)
#HTML Tabelle erstellen
tablehead_html = """
<table style='border-collapse: collapse; width: 40%;'>
<thead>
<tr><th>""" + "</th><th>".join(tablehead) + "</th></tr></thead>"
tablebody_html = tabulate(shuffled_tablebody, headers=tablehead, tablefmt='html')
html_table = tablebody_html
def makeitpretty():
with open('template_1.html', 'r', encoding="utf-8") as file:
string = file.read()
string = string.replace('</td></tr></tbody></table><table><tbody>', '')
string= string.replace('h2', 'h3')
with open('template_1.html', 'w', encoding="utf-8") as file:
file.write(string)
def birthdaychecker():
check = Now.strftime('%d.%m')
geburtstagskind = [row[0] for row in employedata if row[1][:5] == check]
return geburtstagskind
def birthdaymonthchecker():
check = Now.strftime('%m')
geburtstagskinddesmonats = [row[0] for row in employedata if row[1][3:5] == check]
return geburtstagskinddesmonats
geburtstagskinddesmonats=str(birthdaymonthchecker())
def anniversarychecker():
return [row[0] for row in employedata if (row[5] == '0' and row[6] == '0') or len(set(str(row[3]).split(" test")[0])) <= 1]
anniversary=anniversarychecker()
geburtstagskind=str(birthdaychecker())
templator('$geburtstagskind$', geburtstagskind)
templator('$geburtstagskinddesmonats$', geburtstagskinddesmonats)
templator('$specialday$', spec_day)
templator('$namenstage$', ' ')
templator('$specialday_d$', ' ')
templator('$tablehead$', tablehead_html)
templator('$table$', html_table)
templator('$anniversary$', str(anniversary))
makeitpretty()
now_file_name = time.strftime('%Y-%m-%d')
print(now_file_name)
archivepath = os.path.join(os.path.dirname(__file__), 'archive')
os.makedirs(archivepath, exist_ok=True)
shutil.copyfile('template_1.html', os.path.join(archivepath, now_file_name + '.html'))
shutil.copyfile('template_1.html', new_file)
os.system(new_file)