Skip to content

Commit 6aea2cd

Browse files
committed
Added sql_insert functions in python.
1 parent bdbd91f commit 6aea2cd

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

sql_insert.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import sqlite3
2+
3+
LIST_DATA_INSERT = '''INSERT or IGNORE
4+
INTO "main"."ListDataHistorical"
5+
("ID", "Title", "TitleRomaji", "MediaType", "Format",
6+
"Score", "Status", "Progress", "Episodes", "Repeat",
7+
"StartedAt", "CompletedAt", "UpdatedAt", "Notes", "HiddenFromStatusList")
8+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, date(?), date(?), datetime(?, 'unixepoch'), ?, ?);'''
9+
10+
CUSTOM_LISTS_INSERT = '''INSERT or IGNORE
11+
INTO "main"."CustomListMembershipHistorical"
12+
("ID", "UpdatedAt", "CustomList", "IsMember")
13+
VALUES (?, datetime(?, 'unixepoch'), ?, ?);'''
14+
15+
def insert_custom_lists(cursor: sqlite3.Connection,
16+
id: int,
17+
updated_at: int,
18+
custom_lists_membership: dict[str, bool]) -> None:
19+
"""Inserts an entry into the CustomListMembershipHistorical table for each pair in the given dictionary."""
20+
for list, is_memeber in custom_lists_membership.items():
21+
cursor.execute(CUSTOM_LISTS_INSERT, (
22+
id,
23+
updated_at,
24+
list,
25+
is_memeber
26+
))
27+
28+
def insert_media(cursor: sqlite3.Connection, media_info: dict):
29+
"""Inserts an entry into the ListDataHistorical table."""
30+
date_started = (f"{media_info['startedAt']['year']}"
31+
f"-{media_info['startedAt']['month']}"
32+
f"-{media_info['startedAt']['day']}")
33+
date_completed = (f"{media_info['completedAt']['year']}"
34+
f"-{media_info['completedAt']['month']}"
35+
f"-{media_info['completedAt']['day']}")
36+
37+
cursor.execute(LIST_DATA_INSERT, (
38+
media_info['media']['id'],
39+
media_info['media']['title']['english'],
40+
media_info['media']['title']['romaji'],
41+
media_info['media']['type'],
42+
media_info['media']['format'],
43+
media_info['score'],
44+
media_info['status'],
45+
media_info['progress'],
46+
media_info['media']['episodes'],
47+
media_info['repeat'],
48+
date_started,
49+
date_completed,
50+
media_info['updatedAt'],
51+
media_info['notes'],
52+
media_info['hiddenFromStatusLists'],
53+
))
54+
55+
if media_info['customLists'] is not None:
56+
insert_custom_lists(cursor, media_info['media']['id'], media_info['updatedAt'], media_info['customLists'])

0 commit comments

Comments
 (0)