diff --git a/backend/get_data.py b/backend/get_data.py index 05c4b654..4813af22 100644 --- a/backend/get_data.py +++ b/backend/get_data.py @@ -4,11 +4,11 @@ import codecs from bs4 import BeautifulSoup +FILENAME = "Zastępstwa.html" + def main(): - with codecs.open( - "Zastępstwa.html", "r", "utf-8" - ) as f: # Zastępstwa.html in main folder + with codecs.open(FILENAME, "r", "utf-8") as f: # Zastępstwa.html in main folder soup = BeautifulSoup(f.read(), "html.parser") rows = soup.find_all("tr") @@ -29,3 +29,14 @@ def main(): else: replacements[teacher].append(lesson_info) return replacements + + +def date(): + with codecs.open(FILENAME, "r", "utf-8") as f: # Zastępstwa.html in main folder + soup = BeautifulSoup(f.read(), "html.parser") + + # it looks for

Okres: 26.11.2019 (wt.) - 26.11.2019 (wt.)

and takes date from it + data = soup.find("h2") + date = str(data.text).split(" ")[1] + + return date diff --git a/backend/main.py b/backend/main.py index 2c4951b6..a6eff14a 100644 --- a/backend/main.py +++ b/backend/main.py @@ -38,6 +38,12 @@ def get_all(): return jsonify({"data": all_substitutions}) +@app.route("/api/date", methods=["GET"]) +def get_date(): + date = get_data.date() + return jsonify({"date": date}) + + @app.errorhandler(404) def not_found(error): return make_response(jsonify({"error": "Not found"}), 404) diff --git a/frontend/src/components/Header.jsx b/frontend/src/components/Header.jsx index a8476737..51c066db 100644 --- a/frontend/src/components/Header.jsx +++ b/frontend/src/components/Header.jsx @@ -3,6 +3,7 @@ import { Link } from 'react-router-dom'; import styled from 'styled-components'; import logo from '../assets/logo-zsk.svg'; +import SubstitutionDate from "./SubstitutionDate" const HeaderLink = styled(Link)` display: flex; @@ -27,6 +28,7 @@ const Header = () => (

Zastępstwa

+ ); diff --git a/frontend/src/components/SubstitutionDate.jsx b/frontend/src/components/SubstitutionDate.jsx new file mode 100644 index 00000000..e5f787cb --- /dev/null +++ b/frontend/src/components/SubstitutionDate.jsx @@ -0,0 +1,43 @@ +import React, { useState, useEffect } from 'react'; +import axios from 'axios'; +import styled from 'styled-components'; + +const SubstitutionDateWrapper = styled.div` + position: absolute; + top: 0; + right: 0; + margin: 30px; + font-size: 30px; +`; + +const SubstitutionDate = () => { + const [date, setDate] = useState(""); + const [error, setError] = useState(""); + + const getData = () => + axios + .get(`/api/date`) + .then(({ data }) => setDate(data.date)) + .catch((err) => setError(String(err))); + + useEffect(() => { + getData(); + const refreshId = setInterval(getData, 60000); + return () => clearInterval(refreshId); + }, []); + + return ( + <> + + {error ? ( +

Nie udało się pobrać daty

+ ) : ( + date + )} +
+ + ); + +}; + +export default SubstitutionDate;