-
Notifications
You must be signed in to change notification settings - Fork 0
/
benzin.py
90 lines (77 loc) · 3.22 KB
/
benzin.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import requests
from bs4 import BeautifulSoup
import csv
from datetime import datetime
import subprocess
def git_push(remote_repo, branch="main"):
try:
# Adding the file to the staging area
subprocess.run(["git", "add", "."], check=True)
# Committing the changes
commit_message = f"Add update"
subprocess.run(["git", "commit", "-m", commit_message], check=True)
# Pushing the changes to the remote repository
subprocess.run(["git", "push", remote_repo, branch], check=False)
print("File successfully pushed to the remote repository.")
except subprocess.CalledProcessError as e:
print(f"An error occurred: {str(e)}")
print(
"Please make sure you have configured your Git credentials and have the necessary permissions."
)
hour = int(datetime.now().strftime('%H'))
url = 'https://www.benzinpreis.de/aktuell/super_e5/deutschland/baden-wuerttemberg/karlsruhe/landkreis_heidelberg/heidelberg'
response = requests.get(
url,
headers={
"Accept-Language":
"de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7",
"User-Agent":
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36",
"Accept-Encoding":
"gzip, deflate",
"Upgrade-Insecure-Requests":
"1",
"Accept":
"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"
})
soup = BeautifulSoup(response.text, "lxml")
prices_list = soup.find_all('td', 'center')
prices = [i.text for i in prices_list]
with open('Files/test.csv', 'a+') as tempLog:
csv.writer(tempLog).writerow([
f"Kategorie (Datum: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}",
prices[0], prices[1], prices[2]
])
csv.writer(tempLog).writerow(
["Günstigster Preis:", prices[3], prices[4], prices[5]])
csv.writer(tempLog).writerow(
["Teuerster Preis:", prices[6], prices[7], prices[8]])
csv.writer(tempLog).writerow(
["Durchschnittspreis:", prices[9], prices[10], prices[11]])
with open('Files/guenstigster_preis.csv', 'a+') as tempLog:
csv.writer(tempLog).writerow([
f"{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}", prices[3],
prices[4], prices[5]
])
with open('Files/teuerster_preis.csv', 'a+') as tempLog:
csv.writer(tempLog).writerow([
f"{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}", prices[6],
prices[7], prices[8]
])
with open('Files/durchschnitts_preis.csv', 'a+') as tempLog:
csv.writer(tempLog).writerow([
f"{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}", prices[9],
prices[10], prices[11]
])
with open("Files/git_log.txt", "r") as f:
todays_day = datetime.now().weekday()
if todays_day in [0,2,4,6]:
if f.read() == "False":
remote_repo = "origin"
branch = "main" # Change this to your branch name if different
git_push(remote_repo, branch)
with open("Files/git_log.txt", "w") as file:
file.write("True")
else:
with open("Files/git_log.txt", "w") as file:
file.write("False")