Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Created class for database managing - files for database creation ada…
…pted to Cassandra model
- Loading branch information
1 parent
abc06a8
commit 0ba1de1
Showing
6 changed files
with
200 additions
and
124 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,49 @@ | ||
class DatabaseOperations: | ||
def __init__(self): | ||
|
||
from cassandra.cluster import Cluster | ||
from cassandra.auth import PlainTextAuthProvider | ||
|
||
|
||
# Singleton class | ||
class DatabaseOperations(object): | ||
__instance = None | ||
|
||
def __new__(cls, username: str, password: str): | ||
if DatabaseOperations.__instance is None: | ||
DatabaseOperations.__instance = object.__new__(cls) | ||
auth_provider = PlainTextAuthProvider(username=username, password=password) | ||
cluster = Cluster(auth_provider=auth_provider) | ||
DatabaseOperations.__instance.__session = cluster.connect() | ||
DatabaseOperations.__instance.__createTables() | ||
return DatabaseOperations.__instance | ||
|
||
def __createTables(self): | ||
with open("../../Design/db_script.cql", "r") as sql_script: | ||
queries = sql_script.read().splitlines() | ||
for query in queries: | ||
self.__session.execute(query) | ||
|
||
def registerNewUser(self, user_id: int, username: str, name: str): | ||
query = """INSERT INTO YouTubeMDApp.User(user_id, username, name) VALUES (%s, %s, %s);""" | ||
self.__session.execute(query, (user_id, username, name)) | ||
|
||
def registerPreferences(self, user_id: int, audio_quality: str, audio_format: str, os: str, | ||
should_ask_metadata: bool): | ||
query = """ | ||
INSERT INTO YouTubeMDApp.preferences(audio_quality, audio_format, os, should_ask_metadata, user_id) | ||
VALUES (%s, %s, %s, %s, %s); | ||
""" | ||
self.__session.execute(query, (audio_quality, audio_format, os, should_ask_metadata, user_id)) | ||
|
||
def registerStatistics(self, user_id: int, lang: str, downloads: int, last_time_active): | ||
query = """ | ||
INSERT INTO YouTubeMDApp.Statistics(lang, downloads, last_time_active, user_id) VALUES (%s, %s, %s, %s); | ||
""" | ||
self.__session.execute(query, (lang, downloads, last_time_active, user_id)) | ||
|
||
def registerNewSong(self, file_id: str, video_id: str, audio_quality: int, audio_format: int, times_requested: int, | ||
is_metadata_by_user: bool): | ||
query = """ | ||
INSERT INTO YouTubeMDApp.music (file_id, video_id, audio_quality, audio_format, times_requested, | ||
is_metadata_by_user) VALUES (%s, %s, %s, %s, %s, %s) | ||
""" | ||
self.__session.execute(query, (file_id, video_id, audio_quality, audio_format, times_requested, | ||
is_metadata_by_user)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
CREATE KEYSPACE IF NOT EXISTS YouTubeMDApp WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 3}; | ||
USE YouTubeMDApp; | ||
CREATE TABLE IF NOT EXISTS history (user_id int NOT NULL, file_id varchar NOT NULL); | ||
CREATE TABLE IF NOT EXISTS metadata (title varchar, artist varchar, cover text, duration varchar, music_file_id varchar NOT NULL, PRIMARY KEY (music_file_id)); | ||
CREATE TABLE IF NOT EXISTS music (file_id varchar NOT NULL,video_id varchar, audio_quality int, audio_format int, times_requested int, is_metadata_by_user boolean, PRIMARY KEY (file_id)); | ||
CREATE TABLE IF NOT EXISTS playlist (playlist_id varchar NOT NULL, number_elements int, times_requested int, PRIMARY KEY (playlist_id)); | ||
CREATE TABLE IF NOT EXISTS playlist_has_music (playlist_playlist_id varchar NOT NULL, music_file_id varchar NOT NULL); | ||
CREATE TABLE IF NOT EXISTS preferences (audio_quality varchar, audio_format varchar, os varchar, should_ask_metadata boolean, user_id int NOT NULL, PRIMARY KEY (user_id)); | ||
CREATE TABLE IF NOT EXISTS "User" (user_id int NOT NULL, username varchar, name text, PRIMARY KEY (user_id)); | ||
CREATE TABLE IF NOT EXISTS Statistics (lang varchar, downloads int, last_time_active datetime, user_id int NOT NULL, PRIMARY KEY(user_id)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
cassandra-driver | ||
telegram | ||
pip | ||
python-telegram-bot |
Oops, something went wrong.