From 645fe3a8a51e44467fb517b47dc541a11a7e5468 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Phillip=20K=C3=BChne?= Date: Tue, 21 May 2019 01:06:59 +0200 Subject: [PATCH] Initial Commit --- .vscode/launch.json | 71 +++++++++++++++++++++++++++++++++++++++++++ .vscode/settings.json | 3 ++ database.py | 39 ++++++++++++++++++++++++ helpers.py | 16 ++++++++++ main.py | 27 ++++++++++++++++ templates/index.html | 30 ++++++++++++++++++ 6 files changed, 186 insertions(+) create mode 100644 .vscode/launch.json create mode 100644 .vscode/settings.json create mode 100644 database.py create mode 100644 helpers.py create mode 100644 main.py create mode 100644 templates/index.html diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..6c92401 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,71 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + {"name":"Python: Flask","type":"python","request":"launch","module":"flask","env":{"FLASK_APP":"main.py","FLASK_ENV":"development","FLASK_DEBUG":"1"},"args":["run","--no-debugger","--no-reload"],"jinja":true}, + { + "name": "Python: Current File (Integrated Terminal)", + "type": "python", + "request": "launch", + "program": "${file}", + "console": "integratedTerminal" + }, + { + "name": "Python: Remote Attach", + "type": "python", + "request": "attach", + "port": 5678, + "host": "localhost", + "pathMappings": [ + { + "localRoot": "${workspaceFolder}", + "remoteRoot": "." + } + ] + }, + { + "name": "Python: Module", + "type": "python", + "request": "launch", + "module": "enter-your-module-name-here", + "console": "integratedTerminal" + }, + { + "name": "Python: Django", + "type": "python", + "request": "launch", + "program": "${workspaceFolder}/manage.py", + "console": "integratedTerminal", + "args": [ + "runserver", + "--noreload", + "--nothreading" + ], + "django": true + }, + { + "name": "Python: Flask", + "type": "python", + "request": "launch", + "module": "flask", + "env": { + "FLASK_APP": "app.py" + }, + "args": [ + "run", + "--no-debugger", + "--no-reload" + ], + "jinja": true + }, + { + "name": "Python: Current File (External Terminal)", + "type": "python", + "request": "launch", + "program": "${file}", + "console": "externalTerminal" + } + ] +} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..d2a6c12 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "python.pythonPath": "/usr/bin/python" +} \ No newline at end of file diff --git a/database.py b/database.py new file mode 100644 index 0000000..d40846e --- /dev/null +++ b/database.py @@ -0,0 +1,39 @@ +import sqlite3 +import pandas +from io import StringIO + +song_table = "songs" +entry_table = "entries" +index_label = "Id" + +def open_db(): + conn = sqlite3.connect("test.db") + return conn + +def import_songs(song_csv): + df = pandas.read_csv(StringIO(song_csv), sep=';') + conn = open_db() + df.to_sql(song_table, conn, if_exists='replace', + index=False) + conn.close() + return + +def create_entry_table(): + conn = open_db() + t = (entry_table,) + conn.execute('CREATE TABLE IF NOT EXISTS '+entry_table + + ' (ID INTEGER PRIMARY KEY NOT NULL, Song_Id INTEGER NOT NULL, Name VARCHAR(255))') + +def create_list_view(): + conn = open_db() + conn.execute("""CREATE VIEW IF NOT EXISTS [Liste] AS + SELECT Name, Title, Artist + FROM entries, songs + WHERE entries.Song_Id=songs.Id""") + +def get_list(): + conn = open_db() + cur = conn.cursor() + cur.execute("SELECT * FROM Liste") + return cur.fetchall() + diff --git a/helpers.py b/helpers.py new file mode 100644 index 0000000..12b8baf --- /dev/null +++ b/helpers.py @@ -0,0 +1,16 @@ +import requests +from bs4 import BeautifulSoup + +def get_catalog_url(): + r = requests.get('https://www.karafun.de/karaoke-song-list.html') + soup = BeautifulSoup(r.content, 'html.parser') + url = soup.findAll('a', href=True, text='CSV')[0]['href'] + return url + +def get_songs(url): + r = requests.get(url) + return r.text + + +if __name__ == "__main__": + print(get_songs) diff --git a/main.py b/main.py new file mode 100644 index 0000000..1f40a3c --- /dev/null +++ b/main.py @@ -0,0 +1,27 @@ +from flask import Flask, render_template +import helpers +import database +import os, errno +app = Flask(__name__) +@app.route("/") +def home(): + return render_template('index.html', list=database.get_list()) + +@app.route("/list") +def index(): + list = database.get_list() + for entry in list: + print(entry[0]) + return str(database.get_list()) + +if __name__ == "__main__": + """try: + os.remove("test.db") + print("removed database") + except OSError: + print("failed to remove database") + pass""" + database.create_entry_table() + database.create_list_view() + database.import_songs(helpers.get_songs(helpers.get_catalog_url())) + app.run(debug=True) diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..c7b9367 --- /dev/null +++ b/templates/index.html @@ -0,0 +1,30 @@ + + + + + +

Hello!

+ + + + + + + {% for entry in list: %} + + + + + + {% endfor %} +
NameSongKünstler
+ {{ entry[0] }} + + {{ entry[1] }} + + {{ entry[2] }} +
+ + + + \ No newline at end of file