Karan Adhikari commited on
Support REPLACE_MODE across split and non-split files
Browse files- Backend/helper/database.py +89 -58
Backend/helper/database.py
CHANGED
|
@@ -843,6 +843,91 @@ class Database:
|
|
| 843 |
result.append(quality_to_update)
|
| 844 |
return result
|
| 845 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 846 |
async def update_movie(self, movie_data: MovieSchema) -> Optional[ObjectId]:
|
| 847 |
try:
|
| 848 |
movie_dict = movie_data.dict()
|
|
@@ -856,7 +941,6 @@ class Database:
|
|
| 856 |
release_year = movie_dict["release_year"]
|
| 857 |
|
| 858 |
quality_to_update = movie_dict["telegram"][0]
|
| 859 |
-
target_quality = quality_to_update["quality"]
|
| 860 |
|
| 861 |
current_db_key = f"storage_{self.current_db_index}"
|
| 862 |
total_storage_dbs = len(self.dbs) - 1
|
|
@@ -901,31 +985,7 @@ class Database:
|
|
| 901 |
movie_id = existing_movie["_id"]
|
| 902 |
existing_qualities = existing_movie.get("telegram", [])
|
| 903 |
|
| 904 |
-
|
| 905 |
-
existing_qualities = await self._merge_split_part(existing_qualities, quality_to_update)
|
| 906 |
-
|
| 907 |
-
elif SettingsManager.current().replace_mode:
|
| 908 |
-
to_delete = [q for q in existing_qualities if q.get("quality") == target_quality]
|
| 909 |
-
|
| 910 |
-
for q in to_delete:
|
| 911 |
-
try:
|
| 912 |
-
old_id = q.get("id")
|
| 913 |
-
if old_id:
|
| 914 |
-
decoded = await decode_string(old_id)
|
| 915 |
-
chat_id = int(f"-100{decoded['chat_id']}")
|
| 916 |
-
msg_id = int(decoded['msg_id'])
|
| 917 |
-
create_task(delete_message(chat_id, msg_id))
|
| 918 |
-
except Exception as e:
|
| 919 |
-
LOGGER.error(f"Failed to delete old quality: {e}")
|
| 920 |
-
|
| 921 |
-
existing_qualities = [
|
| 922 |
-
q for q in existing_qualities if q.get("quality") != target_quality
|
| 923 |
-
]
|
| 924 |
-
existing_qualities.append(quality_to_update)
|
| 925 |
-
|
| 926 |
-
else:
|
| 927 |
-
# allow duplicate qualities
|
| 928 |
-
existing_qualities.append(quality_to_update)
|
| 929 |
|
| 930 |
existing_movie["telegram"] = existing_qualities
|
| 931 |
existing_movie["updated_on"] = datetime.utcnow()
|
|
@@ -1026,38 +1086,9 @@ class Database:
|
|
| 1026 |
existing_episode.setdefault("telegram", [])
|
| 1027 |
|
| 1028 |
for quality in episode["telegram"]:
|
| 1029 |
-
|
| 1030 |
-
|
| 1031 |
-
|
| 1032 |
-
existing_episode["telegram"] = await self._merge_split_part(
|
| 1033 |
-
existing_episode["telegram"], quality
|
| 1034 |
-
)
|
| 1035 |
-
|
| 1036 |
-
elif SettingsManager.current().replace_mode:
|
| 1037 |
-
to_delete = [
|
| 1038 |
-
q for q in existing_episode["telegram"]
|
| 1039 |
-
if q.get("quality") == target_quality
|
| 1040 |
-
]
|
| 1041 |
-
|
| 1042 |
-
for q in to_delete:
|
| 1043 |
-
try:
|
| 1044 |
-
old_id = q.get("id")
|
| 1045 |
-
if old_id:
|
| 1046 |
-
decoded = await decode_string(old_id)
|
| 1047 |
-
chat_id = int(f"-100{decoded['chat_id']}")
|
| 1048 |
-
msg_id = int(decoded['msg_id'])
|
| 1049 |
-
create_task(delete_message(chat_id, msg_id))
|
| 1050 |
-
except Exception as e:
|
| 1051 |
-
LOGGER.error(f"Failed to delete old quality: {e}")
|
| 1052 |
-
|
| 1053 |
-
existing_episode["telegram"] = [
|
| 1054 |
-
q for q in existing_episode["telegram"]
|
| 1055 |
-
if q.get("quality") != target_quality
|
| 1056 |
-
]
|
| 1057 |
-
existing_episode["telegram"].append(quality)
|
| 1058 |
-
|
| 1059 |
-
else:
|
| 1060 |
-
existing_episode["telegram"].append(quality)
|
| 1061 |
|
| 1062 |
existing_tv["updated_on"] = datetime.utcnow()
|
| 1063 |
|
|
|
|
| 843 |
result.append(quality_to_update)
|
| 844 |
return result
|
| 845 |
|
| 846 |
+
async def _delete_quality_media(self, quality: dict) -> None:
|
| 847 |
+
"""Delete the underlying Telegram message(s) backing a single quality entry.
|
| 848 |
+
|
| 849 |
+
Works for both normal (single message) entries and split groups (one
|
| 850 |
+
message per part). Failures are logged but never raised so a failed
|
| 851 |
+
Telegram delete can't abort the database update.
|
| 852 |
+
"""
|
| 853 |
+
parts = quality.get("parts")
|
| 854 |
+
if parts:
|
| 855 |
+
for part in parts:
|
| 856 |
+
try:
|
| 857 |
+
chat_id = int(f"-100{part['chat_id']}")
|
| 858 |
+
msg_id = int(part["msg_id"])
|
| 859 |
+
create_task(delete_message(chat_id, msg_id))
|
| 860 |
+
except Exception as e:
|
| 861 |
+
LOGGER.error(f"Failed to delete split part message: {e}")
|
| 862 |
+
return
|
| 863 |
+
|
| 864 |
+
old_id = quality.get("id")
|
| 865 |
+
if not old_id:
|
| 866 |
+
return
|
| 867 |
+
try:
|
| 868 |
+
decoded = await decode_string(old_id)
|
| 869 |
+
chat_id = int(f"-100{decoded['chat_id']}")
|
| 870 |
+
msg_id = int(decoded["msg_id"])
|
| 871 |
+
create_task(delete_message(chat_id, msg_id))
|
| 872 |
+
except Exception as e:
|
| 873 |
+
LOGGER.error(f"Failed to delete old quality message: {e}")
|
| 874 |
+
|
| 875 |
+
async def _apply_quality_update(
|
| 876 |
+
self, existing_qualities: List[dict], quality_to_update: dict
|
| 877 |
+
) -> List[dict]:
|
| 878 |
+
"""Merge an incoming quality entry into an existing list of qualities.
|
| 879 |
+
|
| 880 |
+
Handles three scenarios:
|
| 881 |
+
* Incoming split part: merge with the matching split group. When
|
| 882 |
+
REPLACE_MODE is on, any other version of the same quality (a normal
|
| 883 |
+
file or a different split group) is deleted first so only the new
|
| 884 |
+
split group survives.
|
| 885 |
+
* Incoming normal file with REPLACE_MODE on: delete every existing
|
| 886 |
+
entry of the same quality (normal file or split group, including all
|
| 887 |
+
split parts) and keep only the new normal file.
|
| 888 |
+
* REPLACE_MODE off: keep existing entries and append the new one.
|
| 889 |
+
"""
|
| 890 |
+
target_quality = quality_to_update.get("quality")
|
| 891 |
+
incoming_group_key = quality_to_update.get("group_key")
|
| 892 |
+
replace_mode = SettingsManager.current().replace_mode
|
| 893 |
+
|
| 894 |
+
if incoming_group_key:
|
| 895 |
+
# Incoming is a split part.
|
| 896 |
+
if replace_mode:
|
| 897 |
+
# Remove any other version of this quality (a normal file or a
|
| 898 |
+
# different split group); the matching split group is preserved
|
| 899 |
+
# so its parts can be merged below.
|
| 900 |
+
stale = [
|
| 901 |
+
q for q in existing_qualities
|
| 902 |
+
if q.get("quality") == target_quality
|
| 903 |
+
and q.get("group_key") != incoming_group_key
|
| 904 |
+
]
|
| 905 |
+
for q in stale:
|
| 906 |
+
await self._delete_quality_media(q)
|
| 907 |
+
existing_qualities = [
|
| 908 |
+
q for q in existing_qualities
|
| 909 |
+
if not (
|
| 910 |
+
q.get("quality") == target_quality
|
| 911 |
+
and q.get("group_key") != incoming_group_key
|
| 912 |
+
)
|
| 913 |
+
]
|
| 914 |
+
return await self._merge_split_part(existing_qualities, quality_to_update)
|
| 915 |
+
|
| 916 |
+
# Incoming is a normal (non-split) file.
|
| 917 |
+
if replace_mode:
|
| 918 |
+
stale = [q for q in existing_qualities if q.get("quality") == target_quality]
|
| 919 |
+
for q in stale:
|
| 920 |
+
await self._delete_quality_media(q)
|
| 921 |
+
existing_qualities = [
|
| 922 |
+
q for q in existing_qualities if q.get("quality") != target_quality
|
| 923 |
+
]
|
| 924 |
+
existing_qualities.append(quality_to_update)
|
| 925 |
+
return existing_qualities
|
| 926 |
+
|
| 927 |
+
# REPLACE_MODE off: allow duplicate qualities.
|
| 928 |
+
existing_qualities.append(quality_to_update)
|
| 929 |
+
return existing_qualities
|
| 930 |
+
|
| 931 |
async def update_movie(self, movie_data: MovieSchema) -> Optional[ObjectId]:
|
| 932 |
try:
|
| 933 |
movie_dict = movie_data.dict()
|
|
|
|
| 941 |
release_year = movie_dict["release_year"]
|
| 942 |
|
| 943 |
quality_to_update = movie_dict["telegram"][0]
|
|
|
|
| 944 |
|
| 945 |
current_db_key = f"storage_{self.current_db_index}"
|
| 946 |
total_storage_dbs = len(self.dbs) - 1
|
|
|
|
| 985 |
movie_id = existing_movie["_id"]
|
| 986 |
existing_qualities = existing_movie.get("telegram", [])
|
| 987 |
|
| 988 |
+
existing_qualities = await self._apply_quality_update(existing_qualities, quality_to_update)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 989 |
|
| 990 |
existing_movie["telegram"] = existing_qualities
|
| 991 |
existing_movie["updated_on"] = datetime.utcnow()
|
|
|
|
| 1086 |
existing_episode.setdefault("telegram", [])
|
| 1087 |
|
| 1088 |
for quality in episode["telegram"]:
|
| 1089 |
+
existing_episode["telegram"] = await self._apply_quality_update(
|
| 1090 |
+
existing_episode["telegram"], quality
|
| 1091 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1092 |
|
| 1093 |
existing_tv["updated_on"] = datetime.utcnow()
|
| 1094 |
|