diff --git a/.vs/slnx.sqlite b/.vs/slnx.sqlite new file mode 100644 index 0000000000000000000000000000000000000000..3a98caecf56759ad85d16df7167f60c79268e8b0 Binary files /dev/null and b/.vs/slnx.sqlite differ diff --git a/.vs/v0/FileContentIndex/1aaf5d53-ea2d-4258-98d2-b44ed08ded91.vsidx b/.vs/v0/FileContentIndex/1aaf5d53-ea2d-4258-98d2-b44ed08ded91.vsidx new file mode 100644 index 0000000000000000000000000000000000000000..c7aff72b34940565a24c3c937366619c9deb595c Binary files /dev/null and b/.vs/v0/FileContentIndex/1aaf5d53-ea2d-4258-98d2-b44ed08ded91.vsidx differ diff --git a/.vs/v0/FileContentIndex/read.lock b/.vs/v0/FileContentIndex/read.lock new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/.vs/v0/v17/.wsuo b/.vs/v0/v17/.wsuo new file mode 100644 index 0000000000000000000000000000000000000000..96a438bd2104dbab6782d0a082003950470ab8e9 Binary files /dev/null and b/.vs/v0/v17/.wsuo differ diff --git a/Best_Listens.db b/Best_Listens.db new file mode 100644 index 0000000000000000000000000000000000000000..068b99e8de336b1e3dc364c3af9857f3a4e5a990 Binary files /dev/null and b/Best_Listens.db differ diff --git a/Database_testing.ipynb b/Database_testing.ipynb new file mode 100644 index 0000000000000000000000000000000000000000..646a8eb4d3d1574b2cca71752e8d6fd5c79ea708 --- /dev/null +++ b/Database_testing.ipynb @@ -0,0 +1,380 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 5, + "id": "90e29bcf", + "metadata": {}, + "outputs": [], + "source": [ + "import sqlite3\n", + "from sqlite3 import Error" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "id": "b76f11a0", + "metadata": {}, + "outputs": [], + "source": [ + "def search_tracks_by_name(name):\n", + "\n", + " conn = sqlite3.connect('Best_Listens.db')\n", + " cursor = conn.cursor()\n", + " cursor.execute(\"SELECT * FROM tracks WHERE name LIKE ?\", ('%' + name + '%',))\n", + " tracks = cursor.fetchall()\n", + " conn.close()\n", + " return tracks\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "id": "770b3dfa", + "metadata": {}, + "outputs": [], + "source": [ + "def search_tracks_by_artist(artist):\n", + "\n", + " conn = sqlite3.connect('Best_Listens.db')\n", + " cursor = conn.cursor()\n", + " cursor.execute(\"SELECT * FROM tracks WHERE Artist LIKE ?\", ('%' + artist + '%',))\n", + " tracks = cursor.fetchall()\n", + " conn.close()\n", + " return tracks" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "id": "18381449", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[(1, 'Flowers', 'Miley Cyrus', 'Endless Summer Vacation', \"['pop']\")]" + ] + }, + "execution_count": 33, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "search_tracks_by_name('Flowers')" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "id": "5622616a", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[(1, 'Flowers', 'Miley Cyrus', 'Endless Summer Vacation', \"['pop']\"),\n", + " (18, 'River', 'Miley Cyrus', 'Endless Summer Vacation', \"['pop']\")]" + ] + }, + "execution_count": 37, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "search_tracks_by_artist('Miley')" + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "id": "aaadfe32", + "metadata": {}, + "outputs": [], + "source": [ + "def search_tracks(query):\n", + "\n", + " conn = sqlite3.connect('Best_Listens.db')\n", + " cursor = conn.cursor()\n", + "\n", + " cursor.execute(\"SELECT * FROM tracks WHERE name LIKE ? OR artist LIKE ? OR album LIKE ?\",('%' + query + '%', '%' + query + '%', '%' + query + '%'))\n", + " tracks = cursor.fetchall()\n", + "\n", + " conn.close()\n", + " return tracks" + ] + }, + { + "cell_type": "code", + "execution_count": 46, + "id": "e49e6419", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[(15,\n", + " 'Last Night',\n", + " 'Morgan Wallen',\n", + " 'One Thing At A Time',\n", + " \"['contemporary country']\"),\n", + " (35,\n", + " \"I Ain't Worried\",\n", + " 'OneRepublic',\n", + " 'OneRepublic (Japan Paradise Tour Edition)',\n", + " \"['piano rock', 'pop']\"),\n", + " (40,\n", + " 'Bones',\n", + " 'Imagine Dragons',\n", + " 'Mercury - Acts 1 & 2',\n", + " \"['modern rock', 'rock']\")]" + ] + }, + "execution_count": 46, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "search_tracks('one')" + ] + }, + { + "cell_type": "code", + "execution_count": 66, + "id": "97eda0d3", + "metadata": {}, + "outputs": [], + "source": [ + "def search_tracks_by_id(track_id):\n", + "\n", + " conn = sqlite3.connect('Best_Listens.db')\n", + " cursor = conn.cursor()\n", + " cursor.execute(\"SELECT * FROM tracks WHERE Track_id=?\", (track_id ,))\n", + " tracks = cursor.fetchall()\n", + " conn.close()\n", + " return tracks" + ] + }, + { + "cell_type": "code", + "execution_count": 68, + "id": "af496ca6", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[(3,\n", + " 'Die For You (with Ariana Grande) - Remix',\n", + " 'The Weeknd',\n", + " 'Starboy (Deluxe)',\n", + " \"['canadian contemporary r&b', 'canadian pop', 'pop']\")]" + ] + }, + "execution_count": 68, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "search_tracks_by_id(3)" + ] + }, + { + "cell_type": "code", + "execution_count": 76, + "id": "48b40c01", + "metadata": {}, + "outputs": [], + "source": [ + "def get_average_rating(track_id):\n", + "\n", + " conn = sqlite3.connect('Best_Listens.db')\n", + " cursor = conn.cursor()\n", + " cursor.execute(\"SELECT AVG(rating) FROM ratings WHERE track_id=?\", (track_id,))\n", + " average_rating = cursor.fetchone()[0]\n", + " conn.close()\n", + " if average_rating == None:\n", + " average_rating = 'Not rated yet'\n", + "\n", + " return average_rating" + ] + }, + { + "cell_type": "code", + "execution_count": 100, + "id": "938f4b94", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Not rated yet'" + ] + }, + "execution_count": 100, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "get_average_rating(3)" + ] + }, + { + "cell_type": "code", + "execution_count": 98, + "id": "00b27865", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "4.0" + ] + }, + "execution_count": 98, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "get_average_rating(1)" + ] + }, + { + "cell_type": "code", + "execution_count": 82, + "id": "2a018420", + "metadata": {}, + "outputs": [], + "source": [ + "def search_ratings_by_user(user_id):\n", + "\n", + " conn = sqlite3.connect('Best_Listens.db')\n", + " cursor = conn.cursor()\n", + " cursor.execute(\"SELECT track_id, rating FROM ratings WHERE user_id=?\", (user_id ,))\n", + " ratings = cursor.fetchall()\n", + " conn.close()\n", + " return ratings" + ] + }, + { + "cell_type": "code", + "execution_count": 95, + "id": "ecec1f2a", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[(1, 3)]" + ] + }, + "execution_count": 95, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "search_ratings_by_user(1)" + ] + }, + { + "cell_type": "code", + "execution_count": 89, + "id": "efa5ea7b", + "metadata": {}, + "outputs": [], + "source": [ + "def add_rating(user_id, track_id, rating):\n", + " \n", + " conn = sqlite3.connect('Best_Listens.db')\n", + " cursor = conn.cursor()\n", + " cursor.execute(\"SELECT * FROM ratings WHERE user_id=? AND track_id=?\", (user_id, track_id))\n", + " existing_rating = cursor.fetchone()\n", + " if existing_rating is None:\n", + " cursor.execute(\"INSERT INTO ratings (user_id, track_id, rating) VALUES (?, ?, ?)\", (user_id, track_id, rating))\n", + " else:\n", + " cursor.execute(\"UPDATE ratings SET rating=? WHERE id=?\", (rating, existing_rating[0]))\n", + "\n", + " conn.commit()\n", + " conn.close()" + ] + }, + { + "cell_type": "code", + "execution_count": 92, + "id": "cf5e64e8", + "metadata": {}, + "outputs": [], + "source": [ + "add_rating(1,1,4)" + ] + }, + { + "cell_type": "code", + "execution_count": 94, + "id": "9e6de28b", + "metadata": {}, + "outputs": [], + "source": [ + "add_rating(1,1,3)" + ] + }, + { + "cell_type": "code", + "execution_count": 96, + "id": "09163857", + "metadata": {}, + "outputs": [], + "source": [ + "add_rating(1,2,5)" + ] + }, + { + "cell_type": "code", + "execution_count": 97, + "id": "a8dc4827", + "metadata": {}, + "outputs": [], + "source": [ + "add_rating(2,1,5)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "10d08df2", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.10.6" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/track_app.py b/track_app.py new file mode 100644 index 0000000000000000000000000000000000000000..52fd92c92526fefbfaa2d56a263daf1046a97e02 --- /dev/null +++ b/track_app.py @@ -0,0 +1,96 @@ +from flask import Flask, request +import sqlite3 +from sqlite3 import Error +app = Flask(__name__) + +@app.route('/') +def hello_world(): + return 'Hello World' + +#@app.route('/search/<query>') +#def search_tracks(query): + +# conn = sqlite3.connect('Best_Listens.db') +# cursor = conn.cursor() +# +# cursor.execute("SELECT * FROM tracks WHERE name LIKE ? OR artist LIKE ? OR album LIKE ?",('%' + query + '%', '%' + query + '%', '%' + query + '%')) +# tracks = cursor.fetchall() +# +# conn.close() +# return tracks +@app.route('/search/<query>') +def search_tracks(query): + + + conn = sqlite3.connect('Best_Listens.db') + cursor = conn.cursor() + cursor.execute("SELECT * FROM tracks WHERE name LIKE ?", ('%' + query + '%',)) + tracks = cursor.fetchall() + cursor.execute("SELECT * FROM tracks WHERE artist LIKE ?", ('%' + query + '%',)) + artists = cursor.fetchall() + cursor.execute("SELECT * FROM tracks WHERE album LIKE ?", ('%' + query + '%',)) + albums = cursor.fetchall() + + conn.close() + return [tracks, artists, albums] + +@app.route('/search2/', methods=['POST', 'GET']) +def search_tracks2(): + + if request.method == 'GET': + query = request.args.get('search') + conn = sqlite3.connect('Best_Listens.db') + cursor = conn.cursor() + cursor.execute("SELECT * FROM tracks WHERE name LIKE ?", ('%' + query + '%',)) + tracks = cursor.fetchall() + cursor.execute("SELECT * FROM tracks WHERE artist LIKE ?", ('%' + query + '%',)) + artists = cursor.fetchall() + cursor.execute("SELECT * FROM tracks WHERE album LIKE ?", ('%' + query + '%',)) + albums = cursor.fetchall() + + conn.close() + return [tracks, artists, albums] + + + +@app.route('/search_id/<track_id>') +def search_tracks_by_id(track_id): + + conn = sqlite3.connect('Best_Listens.db') + cursor = conn.cursor() + cursor.execute("SELECT * FROM tracks WHERE Track_id=?", (track_id ,)) + tracks = cursor.fetchall() + conn.close() + return tracks + +@app.route('/av_rating/<track_id>') +def get_average_rating(track_id): + + conn = sqlite3.connect('Best_Listens.db') + cursor = conn.cursor() + cursor.execute("SELECT AVG(rating) FROM ratings WHERE track_id=?", (track_id,)) + average_rating = cursor.fetchone()[0] + conn.close() + if average_rating == None: + average_rating = 'Not rated yet' + + return average_rating + + +@app.route('/add_rating') +def add_rating(user_id, track_id, rating): + + conn = sqlite3.connect('Best_Listens.db') + cursor = conn.cursor() + cursor.execute("SELECT * FROM ratings WHERE user_id=? AND track_id=?", (user_id, track_id)) + existing_rating = cursor.fetchone() + if existing_rating is None: + cursor.execute("INSERT INTO ratings (user_id, track_id, rating) VALUES (?, ?, ?)", (user_id, track_id, rating)) + else: + cursor.execute("UPDATE ratings SET rating=? WHERE id=?", (rating, existing_rating[0])) + + conn.commit() + conn.close() + +if __name__ == '__main__': + app.run() \ No newline at end of file