Karan Adhikari commited on
Fix Duplicate Issue
Browse files- Backend/helper/database.py +99 -13
Backend/helper/database.py
CHANGED
|
@@ -966,6 +966,12 @@ class Database:
|
|
| 966 |
|
| 967 |
# ---------------- UPDATE MOVIE ----------------
|
| 968 |
movie_id = existing_movie["_id"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 969 |
existing_qualities = existing_movie.get("telegram", [])
|
| 970 |
|
| 971 |
existing_qualities = await self._apply_quality_update(existing_qualities, quality_to_update)
|
|
@@ -1044,6 +1050,11 @@ class Database:
|
|
| 1044 |
# ---------------- UPDATE TV ----------------
|
| 1045 |
tv_id = existing_tv["_id"]
|
| 1046 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1047 |
for season in tv_show_dict["seasons"]:
|
| 1048 |
existing_season = next(
|
| 1049 |
(s for s in existing_tv["seasons"]
|
|
@@ -1861,6 +1872,49 @@ class Database:
|
|
| 1861 |
|
| 1862 |
|
| 1863 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1864 |
async def replace_media_metadata(
|
| 1865 |
self,
|
| 1866 |
media_type: str,
|
|
@@ -1876,7 +1930,7 @@ class Database:
|
|
| 1876 |
if not current_doc:
|
| 1877 |
return None
|
| 1878 |
|
| 1879 |
-
current_doc
|
| 1880 |
|
| 1881 |
def _pick(key: str):
|
| 1882 |
new_value = metadata.get(key)
|
|
@@ -1884,11 +1938,13 @@ class Database:
|
|
| 1884 |
return current_doc.get(key)
|
| 1885 |
return new_value
|
| 1886 |
|
|
|
|
|
|
|
|
|
|
| 1887 |
if collection_name == "movie":
|
| 1888 |
-
preserved_telegram = current_doc.get("telegram", [])
|
| 1889 |
current_doc.update({
|
| 1890 |
-
"tmdb_id":
|
| 1891 |
-
"imdb_id":
|
| 1892 |
"title": _pick("title"),
|
| 1893 |
"release_year": _pick("release_year"),
|
| 1894 |
"rating": _pick("rating"),
|
|
@@ -1900,14 +1956,13 @@ class Database:
|
|
| 1900 |
"cast": _pick("cast"),
|
| 1901 |
"runtime": _pick("runtime"),
|
| 1902 |
"media_type": "movie",
|
| 1903 |
-
"telegram":
|
| 1904 |
"updated_on": datetime.utcnow(),
|
| 1905 |
})
|
| 1906 |
else:
|
| 1907 |
-
preserved_seasons = current_doc.get("seasons", [])
|
| 1908 |
current_doc.update({
|
| 1909 |
-
"tmdb_id":
|
| 1910 |
-
"imdb_id":
|
| 1911 |
"title": _pick("title"),
|
| 1912 |
"release_year": _pick("release_year"),
|
| 1913 |
"rating": _pick("rating"),
|
|
@@ -1919,13 +1974,44 @@ class Database:
|
|
| 1919 |
"cast": _pick("cast"),
|
| 1920 |
"runtime": _pick("runtime"),
|
| 1921 |
"media_type": "tv",
|
| 1922 |
-
"seasons":
|
| 1923 |
"updated_on": datetime.utcnow(),
|
| 1924 |
})
|
| 1925 |
|
| 1926 |
-
|
| 1927 |
-
|
| 1928 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1929 |
|
| 1930 |
-
updated_doc = await collection.find_one({"
|
| 1931 |
return convert_objectid_to_str(updated_doc) if updated_doc else None
|
|
|
|
| 966 |
|
| 967 |
# ---------------- UPDATE MOVIE ----------------
|
| 968 |
movie_id = existing_movie["_id"]
|
| 969 |
+
|
| 970 |
+
if imdb_id and not existing_movie.get("imdb_id"):
|
| 971 |
+
existing_movie["imdb_id"] = imdb_id
|
| 972 |
+
if tmdb_id and not existing_movie.get("tmdb_id"):
|
| 973 |
+
existing_movie["tmdb_id"] = tmdb_id
|
| 974 |
+
|
| 975 |
existing_qualities = existing_movie.get("telegram", [])
|
| 976 |
|
| 977 |
existing_qualities = await self._apply_quality_update(existing_qualities, quality_to_update)
|
|
|
|
| 1050 |
# ---------------- UPDATE TV ----------------
|
| 1051 |
tv_id = existing_tv["_id"]
|
| 1052 |
|
| 1053 |
+
if imdb_id and not existing_tv.get("imdb_id"):
|
| 1054 |
+
existing_tv["imdb_id"] = imdb_id
|
| 1055 |
+
if tmdb_id and not existing_tv.get("tmdb_id"):
|
| 1056 |
+
existing_tv["tmdb_id"] = tmdb_id
|
| 1057 |
+
|
| 1058 |
for season in tv_show_dict["seasons"]:
|
| 1059 |
existing_season = next(
|
| 1060 |
(s for s in existing_tv["seasons"]
|
|
|
|
| 1872 |
|
| 1873 |
|
| 1874 |
|
| 1875 |
+
@staticmethod
|
| 1876 |
+
def _merge_telegram_lists(primary: List[dict], secondary: List[dict]) -> List[dict]:
|
| 1877 |
+
merged = list(primary or [])
|
| 1878 |
+
existing_ids = {q.get("id") for q in merged if q.get("id")}
|
| 1879 |
+
existing_groups = {q.get("group_key") for q in merged if q.get("group_key")}
|
| 1880 |
+
for q in (secondary or []):
|
| 1881 |
+
group_key = q.get("group_key")
|
| 1882 |
+
if group_key and group_key in existing_groups:
|
| 1883 |
+
continue
|
| 1884 |
+
if q.get("id") and q.get("id") in existing_ids:
|
| 1885 |
+
continue
|
| 1886 |
+
merged.append(q)
|
| 1887 |
+
if q.get("id"):
|
| 1888 |
+
existing_ids.add(q.get("id"))
|
| 1889 |
+
if group_key:
|
| 1890 |
+
existing_groups.add(group_key)
|
| 1891 |
+
return merged
|
| 1892 |
+
|
| 1893 |
+
def _merge_season_lists(self, primary: List[dict], secondary: List[dict]) -> List[dict]:
|
| 1894 |
+
merged = list(primary or [])
|
| 1895 |
+
season_map = {s.get("season_number"): s for s in merged}
|
| 1896 |
+
for season in (secondary or []):
|
| 1897 |
+
season_number = season.get("season_number")
|
| 1898 |
+
target_season = season_map.get(season_number)
|
| 1899 |
+
if not target_season:
|
| 1900 |
+
merged.append(season)
|
| 1901 |
+
season_map[season_number] = season
|
| 1902 |
+
continue
|
| 1903 |
+
|
| 1904 |
+
target_season.setdefault("episodes", [])
|
| 1905 |
+
episode_map = {e.get("episode_number"): e for e in target_season["episodes"]}
|
| 1906 |
+
for episode in season.get("episodes", []):
|
| 1907 |
+
episode_number = episode.get("episode_number")
|
| 1908 |
+
target_episode = episode_map.get(episode_number)
|
| 1909 |
+
if not target_episode:
|
| 1910 |
+
target_season["episodes"].append(episode)
|
| 1911 |
+
episode_map[episode_number] = episode
|
| 1912 |
+
continue
|
| 1913 |
+
target_episode["telegram"] = self._merge_telegram_lists(
|
| 1914 |
+
target_episode.get("telegram", []), episode.get("telegram", [])
|
| 1915 |
+
)
|
| 1916 |
+
return merged
|
| 1917 |
+
|
| 1918 |
async def replace_media_metadata(
|
| 1919 |
self,
|
| 1920 |
media_type: str,
|
|
|
|
| 1930 |
if not current_doc:
|
| 1931 |
return None
|
| 1932 |
|
| 1933 |
+
source_id = current_doc["_id"]
|
| 1934 |
|
| 1935 |
def _pick(key: str):
|
| 1936 |
new_value = metadata.get(key)
|
|
|
|
| 1938 |
return current_doc.get(key)
|
| 1939 |
return new_value
|
| 1940 |
|
| 1941 |
+
new_tmdb_id = int(metadata.get("tmdb_id") or tmdb_id)
|
| 1942 |
+
new_imdb_id = _pick("imdb_id")
|
| 1943 |
+
|
| 1944 |
if collection_name == "movie":
|
|
|
|
| 1945 |
current_doc.update({
|
| 1946 |
+
"tmdb_id": new_tmdb_id,
|
| 1947 |
+
"imdb_id": new_imdb_id,
|
| 1948 |
"title": _pick("title"),
|
| 1949 |
"release_year": _pick("release_year"),
|
| 1950 |
"rating": _pick("rating"),
|
|
|
|
| 1956 |
"cast": _pick("cast"),
|
| 1957 |
"runtime": _pick("runtime"),
|
| 1958 |
"media_type": "movie",
|
| 1959 |
+
"telegram": current_doc.get("telegram", []),
|
| 1960 |
"updated_on": datetime.utcnow(),
|
| 1961 |
})
|
| 1962 |
else:
|
|
|
|
| 1963 |
current_doc.update({
|
| 1964 |
+
"tmdb_id": new_tmdb_id,
|
| 1965 |
+
"imdb_id": new_imdb_id,
|
| 1966 |
"title": _pick("title"),
|
| 1967 |
"release_year": _pick("release_year"),
|
| 1968 |
"rating": _pick("rating"),
|
|
|
|
| 1974 |
"cast": _pick("cast"),
|
| 1975 |
"runtime": _pick("runtime"),
|
| 1976 |
"media_type": "tv",
|
| 1977 |
+
"seasons": current_doc.get("seasons", []),
|
| 1978 |
"updated_on": datetime.utcnow(),
|
| 1979 |
})
|
| 1980 |
|
| 1981 |
+
identity_filters = []
|
| 1982 |
+
if new_imdb_id:
|
| 1983 |
+
identity_filters.append({"imdb_id": new_imdb_id})
|
| 1984 |
+
identity_filters.append({"tmdb_id": new_tmdb_id})
|
| 1985 |
+
|
| 1986 |
+
existing_other = await collection.find_one({
|
| 1987 |
+
"$and": [{"$or": identity_filters}, {"_id": {"$ne": source_id}}]
|
| 1988 |
+
})
|
| 1989 |
+
|
| 1990 |
+
if existing_other:
|
| 1991 |
+
if collection_name == "movie":
|
| 1992 |
+
existing_other["telegram"] = self._merge_telegram_lists(
|
| 1993 |
+
existing_other.get("telegram", []), current_doc.get("telegram", [])
|
| 1994 |
+
)
|
| 1995 |
+
else:
|
| 1996 |
+
existing_other["seasons"] = self._merge_season_lists(
|
| 1997 |
+
existing_other.get("seasons", []), current_doc.get("seasons", [])
|
| 1998 |
+
)
|
| 1999 |
+
|
| 2000 |
+
for field in (
|
| 2001 |
+
"tmdb_id", "imdb_id", "title", "release_year", "rating",
|
| 2002 |
+
"description", "poster", "backdrop", "logo", "genres",
|
| 2003 |
+
"cast", "runtime", "media_type",
|
| 2004 |
+
):
|
| 2005 |
+
if field in current_doc:
|
| 2006 |
+
existing_other[field] = current_doc[field]
|
| 2007 |
+
existing_other["updated_on"] = datetime.utcnow()
|
| 2008 |
+
|
| 2009 |
+
await collection.delete_one({"_id": source_id})
|
| 2010 |
+
await collection.replace_one({"_id": existing_other["_id"]}, existing_other)
|
| 2011 |
+
|
| 2012 |
+
updated_doc = await collection.find_one({"_id": existing_other["_id"]})
|
| 2013 |
+
return convert_objectid_to_str(updated_doc) if updated_doc else None
|
| 2014 |
+
await collection.replace_one({"_id": source_id}, current_doc)
|
| 2015 |
|
| 2016 |
+
updated_doc = await collection.find_one({"_id": source_id})
|
| 2017 |
return convert_objectid_to_str(updated_doc) if updated_doc else None
|