From a3ac0efb1333facc92087a520e1ee01691332ab1 Mon Sep 17 00:00:00 2001 From: Anorak_1 Date: Fri, 5 Jan 2024 18:20:30 +0000 Subject: [PATCH] Upload files to "templates" --- templates/search.py | 31 +++++++++++++++++++++++++++++++ templates/webserver.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 templates/search.py create mode 100644 templates/webserver.py diff --git a/templates/search.py b/templates/search.py new file mode 100644 index 0000000..c8586a1 --- /dev/null +++ b/templates/search.py @@ -0,0 +1,31 @@ +import requests +import re +from lxml import html, etree + + +def search_site(search_string: str): + search = search_string + search = search.replace(" ", "%20") + headers = { + "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36" +} + search_res = requests.get(f"https://prehrajto.cz/hledej/{search}", headers=headers) + tree = html.fromstring(search_res.content) + links = tree.xpath('.//a[contains(@class, "video video--small video--link")]') + if not links: + print("EMPTYYY") + x = [] + for link in links: + title = link.get("title").replace("+", " ").replace("-", " ").replace(".", " ") # title of video + href = link.get("href") # link to video + thumbnail = link.xpath('.//img[contains(concat(" ", normalize-space(@class), " "), " thumb thumb1 ")]')[0].get("src") # link to thumbnail + x.append({'title': title, + 'href': href, + 'thumbnail': thumbnail}) + return x + +def get_video_url(href: str): + video_site = requests.get(f"https://prehrajto.cz/{href}") + x = re.search('(https:.{0,6}?storage.+?)\"', video_site.content.decode("utf-8")).groups() + + return x[0] diff --git a/templates/webserver.py b/templates/webserver.py new file mode 100644 index 0000000..e00f25f --- /dev/null +++ b/templates/webserver.py @@ -0,0 +1,29 @@ +from flask import Flask, render_template, request, redirect, url_for +from search import search_site, get_video_url + +app = Flask(__name__) +app.config['SECRET_KEY'] = 'hjshjhdjah kjshkjdhjs' + +@app.route('/') +def index(): + return render_template('index.html', results=None) + +@app.route('/search', methods=['GET', 'POST']) +def search(): + if request.method == 'POST': + search_query = request.form['search_query'] + print("Search Query:", search_query) + elif request.method == 'GET' and 'search_query' in request.args: + search_query = request.args['search_query'] + results = search_site(search_query) + return render_template('index.html', results=results, query = search_query) + else: + return redirect(url_for('index')) + +@app.route('/video/') +def video(href): + return redirect(get_video_url(href)) + + +if __name__ == "__main__": + app.run(host="0.0.0.0") \ No newline at end of file