Commit abb4ae56c3

abb4ae56c36d90e572afcbd8d2b6d5e6b545129b

parent: e03f98c4c8

Unsigned

Wraient <wraient.911@gmail.com> · 2024-09-03 19:08 UTC

File contains functions to:
add anime to database
remove anime from database
update anime in database
get all the anime in database as dictionary

Layout: unified · split

track_anime.py added +96
@@ -0,0 +1,96 @@
1import csv
2
3# Path to your text database file
4# database_file = 'curd_history.txt'
5
6# Function to add a new anime entry
7def add_anime(database_file, anilist_id, allanime_id, episode, time, name):
8 with open(database_file, mode='a', newline='') as file:
9 writer = csv.writer(file)
10 writer.writerow([anilist_id, allanime_id, episode, time, name])
11 print("Written to file")
12
13# Function to delete an anime entry by Anilist ID and Allanime ID
14def delete_anime(database_file, anilist_id, allanime_id):
15 anime_list = []
16 with open(database_file, mode='r', newline='') as file:
17 reader = csv.reader(file)
18 for row in reader:
19 # Keep only entries that don't match the Anilist ID and Allanime ID
20 if row[0] != anilist_id or row[1] != allanime_id:
21 anime_list.append(row)
22
23 # Rewrite the database without the deleted entry
24 with open(database_file, mode='w', newline='') as file:
25 writer = csv.writer(file)
26 writer.writerows(anime_list)
27
28# Function to retrieve all anime entries
29def get_all_anime(database_file):
30 anime_list = []
31 with open(database_file, mode='r', newline='') as file:
32 reader = csv.reader(file)
33 for row in reader:
34 anime_list.append({
35 'anilist_id': row[0],
36 'allanime_id': row[1],
37 'episode': row[2],
38 'time': row[3],
39 'name': row[4]
40 })
41 return anime_list
42
43# Function to update or add a new anime entry
44def update_anime(database_file, anilist_id, allanime_id, episode, time, name):
45 anime_list = get_all_anime(database_file)
46 updated = False
47
48 # Create a new list to store updated entries
49 updated_anime_list = []
50
51 # Check if the anime entry exists and update it
52 for anime in anime_list:
53 if anime['anilist_id'] == anilist_id and anime['allanime_id'] == allanime_id:
54 updated_anime_list.append({
55 'anilist_id': anilist_id,
56 'allanime_id': allanime_id,
57 'episode': episode,
58 'time': time,
59 'name': name
60 })
61 updated = True
62 else:
63 updated_anime_list.append(anime)
64
65 # If no entry was updated, add new entry
66 if not updated:
67 updated_anime_list.append({
68 'anilist_id': anilist_id,
69 'allanime_id': allanime_id,
70 'episode': episode,
71 'time': time,
72 'name': name
73 })
74
75 # Write all entries back to the file
76 with open(database_file, mode='w', newline='') as file:
77 writer = csv.writer(file)
78 for anime in updated_anime_list:
79 writer.writerow([anime['anilist_id'], anime['allanime_id'], anime['episode'], anime['time'], anime['name']])
80
81 print("Updated the file")
82
83# Example usage
84# database_name = "/home/wraient/.local/share/curd/curd_history.txt"
85# add_anime(database_name, '54321', '09876', '3', '100', 'My Hero Academia')
86
87# # Print all anime entries
88# update_anime(database_name, '21', 'ReooPAxPMsHM4KPMY', '1110', '10', 'ONE PIECE')
89# print(get_all_anime(database_name))
90# update_anime(database_name, '21', 'ReooPAxPMsHM4KPMY', '1110', '101', 'ONE PIECE')
91
92# # Update an entry
93# update_anime(database_name, '12345', '67890', '13', '120', 'Attack on Titan')
94
95# # Print all anime entries after update
96# print(get_all_anime(database_name))