import sqlite3 import csv class AnimeBackend: def __init__(self): self.db = sqlite3.connect('anime_backlog.db') self.create_table() def create_table(self): 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() def get_pre_2010_entries(self): cursor = self.db.cursor() cursor.execute("SELECT * FROM anime WHERE year < 2010 ORDER BY year DESC, name") return cursor.fetchall() def get_years(self): 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()] def get_entries_for_season(self, year, season): cursor = self.db.cursor() cursor.execute("SELECT * FROM anime WHERE year = ? AND season = ? ORDER BY name", (year, season)) return cursor.fetchall() def get_anime_by_id(self, anime_id): cursor = self.db.cursor() cursor.execute("SELECT * FROM anime WHERE id = ?", (anime_id,)) return cursor.fetchone() def add_anime(self, data): 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() def edit_anime(self, anime_id, data): 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() def delete_anime(self, anime_id): cursor = self.db.cursor() cursor.execute("DELETE FROM anime WHERE id = ?", (anime_id,)) self.db.commit() def change_status(self, anime_id, new_status): cursor = self.db.cursor() cursor.execute("UPDATE anime SET status = ? WHERE id = ?", (new_status, anime_id)) self.db.commit() def add_placeholders_for_year(self, year): 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() def import_from_csv(self, file_name): 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() def export_to_csv(self, file_name): 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)