From 2674ed9329f0e065225db7268df2e820c69bef12 Mon Sep 17 00:00:00 2001 From: KKlochko Date: Fri, 8 Jul 2022 20:42:09 +0300 Subject: [PATCH] (new small feature) 0.1.3 - add an anime entry if --- CHANGELOG.org | 2 ++ src/db.py | 24 +++++++++++++++++++++++- src/main.py | 10 ++++++++-- 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.org b/CHANGELOG.org index b0f3f18..caa90f8 100644 --- a/CHANGELOG.org +++ b/CHANGELOG.org @@ -5,3 +5,5 @@ New small feature: update anime status ** 0.1.2 <2022-07-08> New small feature: get an anime entry +** 0.1.3 <2022-07-08> + New small feature: add an anime entry if diff --git a/src/db.py b/src/db.py index 9c4887d..1a80172 100644 --- a/src/db.py +++ b/src/db.py @@ -74,7 +74,10 @@ class DataBase: connect.close() def get_entry(self, url): - """Returns the entry if exists. Otherwise, return [].""" + """ + Returns the enties as an list of tuples if exists. + Otherwise, return []. + """ connect = sqlite3.connect(self.PATH) cursor = connect.cursor() cursor.execute(f"SELECT * FROM {self.TABLE} WHERE Url = '{url}';") @@ -82,3 +85,22 @@ class DataBase: cursor.close() connect.close() return data + + def add_anime_if(self, url, title, status): + """ + Return 0 if not exists. + Return 1 if the same as the entry in the DB. + Return -1 if the status is newer than the entry status. + """ + data = self.get_entry(url) + # If not exists + if data == []: + self.add_anime(url, title, status) + return 0 + # If the same + if data[0][1:] == (url, title, status): + return 1 + # If the status is newer than the entry status. + if data[0][3] != status: + self.update_anime_status(url, status) + return -1 diff --git a/src/main.py b/src/main.py index ce929d4..e35704c 100644 --- a/src/main.py +++ b/src/main.py @@ -24,8 +24,14 @@ def main(): # Test data #db.add_anime("test.url", "The anime", "1") #db.update_anime_status("test.url", "2") - data = db.get_entry("test.url") - print(f"{data=}") + #data = db.get_entry("test.url") + #print(f"{data=}") + r = db.add_anime_if("test.url", "The anime", "1") + print(f"1{r=}") + r = db.add_anime_if("test.url", "The anime", "1") + print(f"2{r=}") + r = db.add_anime_if("test.url", "The anime", "2") + print(f"3{r=}") if __name__ == "__main__": main()