201 lines
8.2 KiB
Python
201 lines
8.2 KiB
Python
import sqlite3
|
|
import csv
|
|
import logging
|
|
|
|
# Set up logging
|
|
logging.basicConfig(filename='anime_tracker.log', level=logging.ERROR,
|
|
format='%(asctime)s - %(levelname)s - %(message)s')
|
|
|
|
class AnimeBackend:
|
|
def __init__(self):
|
|
self.db = sqlite3.connect('anime_backlog.db')
|
|
self.create_table()
|
|
|
|
def create_table(self):
|
|
try:
|
|
cursor = self.db.cursor()
|
|
cursor.execute("""
|
|
CREATE TABLE IF NOT EXISTS anime (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
name TEXT NOT NULL,
|
|
year INTEGER NOT NULL,
|
|
season TEXT,
|
|
status TEXT NOT NULL DEFAULT 'unwatched',
|
|
type TEXT,
|
|
comment TEXT,
|
|
url TEXT
|
|
)
|
|
""")
|
|
self.db.commit()
|
|
except Exception as e:
|
|
logging.error(f"Error creating table: {e}")
|
|
|
|
def get_pre_2010_entries(self, status_filter=None, type_filter=None, search=None, year_filter=None):
|
|
try:
|
|
cursor = self.db.cursor()
|
|
sql = "SELECT * FROM anime WHERE year < 2010"
|
|
add_where = []
|
|
params = []
|
|
if year_filter is not None:
|
|
add_where.append("year = ?")
|
|
params.append(year_filter)
|
|
if status_filter is not None:
|
|
add_where.append("status = ?")
|
|
params.append(status_filter)
|
|
if type_filter is not None:
|
|
add_where.append("type = ?")
|
|
params.append(type_filter)
|
|
if search is not None:
|
|
add_where.append("(name LIKE ? OR comment LIKE ?)")
|
|
params.append(f"%{search}%")
|
|
params.append(f"%{search}%")
|
|
where_clause = " AND " + " AND ".join(add_where) if add_where else ""
|
|
sql += where_clause + " ORDER BY year DESC, name"
|
|
cursor.execute(sql, params)
|
|
return cursor.fetchall()
|
|
except Exception as e:
|
|
logging.error(f"Error getting pre-2010 entries: {e}")
|
|
return []
|
|
|
|
def get_years(self):
|
|
try:
|
|
cursor = self.db.cursor()
|
|
cursor.execute("SELECT DISTINCT year FROM anime WHERE year >= 2010 ORDER BY year DESC")
|
|
return [row[0] for row in cursor.fetchall()]
|
|
except Exception as e:
|
|
logging.error(f"Error getting years: {e}")
|
|
return []
|
|
|
|
def get_entries_for_season(self, year, season, status_filter=None, type_filter=None, search=None):
|
|
try:
|
|
cursor = self.db.cursor()
|
|
sql = "SELECT * FROM anime WHERE year = ? AND season = ?"
|
|
params = [year, season]
|
|
add_where = []
|
|
if status_filter is not None:
|
|
add_where.append("status = ?")
|
|
params.append(status_filter)
|
|
if type_filter is not None:
|
|
add_where.append("type = ?")
|
|
params.append(type_filter)
|
|
if search is not None:
|
|
add_where.append("(name LIKE ? OR comment LIKE ?)")
|
|
params.append(f"%{search}%")
|
|
params.append(f"%{search}%")
|
|
where_clause = " AND " + " AND ".join(add_where) if add_where else ""
|
|
sql += where_clause + " ORDER BY name"
|
|
cursor.execute(sql, params)
|
|
return cursor.fetchall()
|
|
except Exception as e:
|
|
logging.error(f"Error getting entries for season {season} in year {year}: {e}")
|
|
return []
|
|
|
|
def get_anime_by_id(self, anime_id):
|
|
try:
|
|
cursor = self.db.cursor()
|
|
cursor.execute("SELECT * FROM anime WHERE id = ?", (anime_id,))
|
|
return cursor.fetchone()
|
|
except Exception as e:
|
|
logging.error(f"Error getting anime by id {anime_id}: {e}")
|
|
return None
|
|
|
|
def add_anime(self, data):
|
|
try:
|
|
cursor = self.db.cursor()
|
|
cursor.execute(
|
|
"INSERT INTO anime (name, year, season, status, type, comment, url) VALUES (?, ?, ?, ?, ?, ?, ?)",
|
|
(data['name'], data['year'], data['season'], data['status'], data['type'], data['comment'], data['url'])
|
|
)
|
|
self.db.commit()
|
|
except Exception as e:
|
|
logging.error(f"Error adding anime: {e}")
|
|
|
|
def edit_anime(self, anime_id, data):
|
|
try:
|
|
cursor = self.db.cursor()
|
|
cursor.execute(
|
|
"UPDATE anime SET name=?, year=?, season=?, status=?, type=?, comment=?, url=? WHERE id=?",
|
|
(data['name'], data['year'], data['season'], data['status'], data['type'], data['comment'], data['url'], anime_id)
|
|
)
|
|
self.db.commit()
|
|
except Exception as e:
|
|
logging.error(f"Error editing anime id {anime_id}: {e}")
|
|
|
|
def delete_anime(self, anime_id):
|
|
try:
|
|
cursor = self.db.cursor()
|
|
cursor.execute("DELETE FROM anime WHERE id = ?", (anime_id,))
|
|
self.db.commit()
|
|
except Exception as e:
|
|
logging.error(f"Error deleting anime id {anime_id}: {e}")
|
|
|
|
def change_status(self, anime_id, new_status):
|
|
try:
|
|
cursor = self.db.cursor()
|
|
cursor.execute("UPDATE anime SET status = ? WHERE id = ?", (new_status, anime_id))
|
|
self.db.commit()
|
|
except Exception as e:
|
|
logging.error(f"Error changing status for anime id {anime_id}: {e}")
|
|
|
|
def add_placeholders_for_year(self, year):
|
|
try:
|
|
cursor = self.db.cursor()
|
|
for season in ['winter', 'spring', 'summer', 'fall', '']:
|
|
cursor.execute(
|
|
"INSERT INTO anime (name, year, season, status, type, comment, url) VALUES (?, ?, ?, ?, ?, ?, ?)",
|
|
('Placeholder', year, season, 'unwatched', '', 'Delete or edit me', '')
|
|
)
|
|
self.db.commit()
|
|
except Exception as e:
|
|
logging.error(f"Error adding placeholders for year {year}: {e}")
|
|
|
|
def import_from_csv(self, file_name):
|
|
try:
|
|
with open(file_name, 'r', newline='') as f:
|
|
reader = csv.reader(f)
|
|
header = next(reader, None)
|
|
if header:
|
|
cursor = self.db.cursor()
|
|
for row in reader:
|
|
if len(row) == 7:
|
|
name, year_str, season, status, type_, comment, url = row
|
|
elif len(row) == 8:
|
|
_, name, year_str, season, status, type_, comment, url = row
|
|
else:
|
|
continue
|
|
try:
|
|
year = int(year_str)
|
|
except ValueError:
|
|
continue
|
|
cursor.execute(
|
|
"SELECT id FROM anime WHERE name = ? AND year = ? AND season = ?",
|
|
(name, year, season)
|
|
)
|
|
if not cursor.fetchone():
|
|
cursor.execute(
|
|
"INSERT INTO anime (name, year, season, status, type, comment, url) VALUES (?, ?, ?, ?, ?, ?, ?)",
|
|
(name, year, season, status, type_, comment, url)
|
|
)
|
|
self.db.commit()
|
|
except Exception as e:
|
|
logging.error(f"Error importing from CSV {file_name}: {e}")
|
|
|
|
def export_to_csv(self, file_name):
|
|
try:
|
|
cursor = self.db.cursor()
|
|
cursor.execute("SELECT * FROM anime")
|
|
rows = cursor.fetchall()
|
|
with open(file_name, 'w', newline='') as f:
|
|
writer = csv.writer(f)
|
|
writer.writerow(['id', 'name', 'year', 'season', 'status', 'type', 'comment', 'url'])
|
|
writer.writerows(rows)
|
|
except Exception as e:
|
|
logging.error(f"Error exporting to CSV {file_name}: {e}")
|
|
|
|
def delete_year(self, year):
|
|
try:
|
|
cursor = self.db.cursor()
|
|
cursor.execute("DELETE FROM anime WHERE year = ?", (year,))
|
|
self.db.commit()
|
|
except Exception as e:
|
|
logging.error(f"Error deleting year {year}: {e}") |