-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Expand file tree
/
Copy pathdatabase.py
More file actions
49 lines (41 loc) · 1.35 KB
/
database.py
File metadata and controls
49 lines (41 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import sqlite3
from dataclasses import astuple
from pathlib import Path
from bandcamp.storage.models import Track
DATABASE_PATH = Path.home() / "bandcamp.db"
SQL_CREATE = """\
CREATE TABLE IF NOT EXISTS history (
id TEXT PRIMARY KEY,
title TEXT,
artist TEXT,
artist_url TEXT,
album TEXT,
album_url TEXT,
genre TEXT,
timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
"""
SQL_INSERT = """\
INSERT INTO history (id, title, artist, artist_url, album, album_url, genre)
VALUES (?, ?, ?, ?, ?, ?, ?)
"""
SQL_SELECT_ALL = "SELECT * FROM history"
SQL_SELECT_ONE = "SELECT id FROM history WHERE id=?"
class Database:
def __init__(self):
self.connection = sqlite3.connect(DATABASE_PATH)
self.cursor = self.connection.cursor()
self.create_table()
def create_table(self):
self.cursor.execute(SQL_CREATE)
self.connection.commit()
def persist(self, track: Track):
self.cursor.execute(SQL_SELECT_ONE, (track.id,))
if not self.cursor.fetchone():
self.cursor.execute(SQL_INSERT, (track.id, *astuple(track)))
self.connection.commit()
def find_all(self):
self.cursor.execute(SQL_SELECT_ALL)
return [Track(*row[1:-1]) for row in self.cursor.fetchall()]
def close(self):
self.connection.close()