# Path: src/moviesapp/exceptions.py
# class OmdbError(Exception):
#     """OMDb error."""
# 
# class OmdbLimitReached(Exception):
#     """OMDb limit reached."""
# 
# class OmdbRequestError(Exception):
#     """OMDb request error."""
# 
# Path: src/moviesapp/models.py
# class Movie(models.Model):
#     title = models.CharField(max_length=255)
#     title_original = models.CharField(max_length=255)
#     country = models.CharField(max_length=255, null=True, blank=True)
#     description = models.TextField(null=True, blank=True)
#     director = models.CharField(max_length=255, null=True, blank=True)
#     writer = models.CharField(max_length=255, null=True, blank=True)
#     genre = models.CharField(max_length=255, null=True, blank=True)
#     actors = models.CharField(max_length=255, null=True, blank=True)
#     imdb_id = models.CharField(max_length=15, unique=True, db_index=True)
#     tmdb_id = models.IntegerField(unique=True)
#     imdb_rating = models.DecimalField(max_digits=2, decimal_places=1, null=True)
#     poster = models.CharField(max_length=255, null=True)
#     release_date = models.DateField(null=True)
#     runtime = models.TimeField(null=True, blank=True)
#     homepage = models.URLField(null=True, blank=True)
#     trailers = JSONField(null=True, blank=True)
# 
#     class Meta:
#         ordering = ["pk"]
# 
#     def __str__(self):
#         return str(self.title)
# 
#     def imdb_url(self):
#         return settings.IMDB_BASE_URL + self.imdb_id + "/"
# 
#     def get_trailers(self):
#         if self.trailers:
#             return self.trailers
#         return self.trailers_en
# 
#     def has_trailers(self):
#         return bool(self.get_trailers())
# 
#     def _get_poster(self, size):
#         return get_poster_url(size, self.poster)
# 
#     @property
#     def poster_normal(self):
#         return self._get_poster("normal")
# 
#     @property
#     def poster_small(self):
#         return self._get_poster("small")
# 
#     @property
#     def poster_big(self):
#         return self._get_poster("big")
# 
#     @property
#     def id_title(self):
#         return f"{self.pk} - {self}"
# 
#     def cli_string(self, last_movie_id):
#         """
#         Return string version for CLI.
# 
#         We need last_movie_id because we want to know how big is the number (in characters) to be able to make
#         perfect formatting.
#         We need to keep last_movie_id as a parameter because otherwise we hit the database every time we run the
#         function.
#         """
#         MAX_CHARS = 40
#         ENDING = ".."
#         n = len(str(last_movie_id)) + 1
#         id_format = f"{{0: < {n}}}"
#         title = str(self)
#         title = (title[:MAX_CHARS] + ENDING) if len(title) > MAX_CHARS else title
#         id_ = id_format.format(self.pk)
#         title_max_length = MAX_CHARS + len(ENDING)
#         title_format = f"{{:{title_max_length}s}}"
#         title = title_format.format(title)  # pylint: disable=consider-using-f-string
#         return f"{id_} - {title}"[1:]
# 
# Path: src/moviesapp/tmdb.py
# def get_tmdb_movie_data(tmdb_id):
#     def get_release_date(release_date):
#         if release_date:
#             return release_date
#         return None
# 
#     def get_trailers(movie_data):
#         trailers = []
#         for trailer in movie_data.videos()["results"]:
#             trailer_ = {"name": trailer["name"], "source": trailer["key"]}
#             trailers.append(trailer_)
#         return trailers
# 
#     def get_movie_data(tmdb_id, lang):
#         tmdb = get_tmdb(lang=lang)
#         return tmdb.Movies(tmdb_id)
# 
#     movie_data_en = get_movie_data(tmdb_id, "en")
#     movie_info_en = movie_data_en.info()
#     # We have to get all info in english first before we switch to russian or everything breaks.
#     trailers_en = get_trailers(movie_data_en)
#     movie_data_ru = get_movie_data(tmdb_id, "ru")
#     movie_info_ru = movie_data_ru.info()
#     trailers_ru = get_trailers(movie_data_ru)
#     imdb_id = movie_info_en["imdb_id"]
#     if imdb_id:
#         return {
#             "tmdb_id": tmdb_id,
#             "imdb_id": imdb_id,
#             "release_date": get_release_date(movie_info_en["release_date"]),
#             "title_original": movie_info_en["original_title"],
#             "poster_ru": get_poster_from_tmdb(movie_info_ru["poster_path"]),
#             "poster_en": get_poster_from_tmdb(movie_info_en["poster_path"]),
#             "homepage": movie_info_en["homepage"],
#             "trailers_en": trailers_en,
#             "trailers_ru": trailers_ru,
#             "title_en": movie_info_en["title"],
#             "title_ru": movie_info_ru["title"],
#             "description_en": movie_info_en["overview"],
#             "description_ru": movie_info_ru["overview"],
#         }
#     raise MovieNotInDb(tmdb_id)


import re
import requests
from datetime import datetime
from django.conf import settings
from requests.exceptions import RequestException
from sentry_sdk import capture_exception
from .exceptions import OmdbError, OmdbLimitReached, OmdbRequestError
from .models import Movie
from .tmdb import get_tmdb_movie_data




def load_omdb_movie_data(imdb_id):
    try:
        response = requests.get(f"http://www.omdbapi.com/?i={imdb_id}&apikey={settings.OMDB_KEY}")
    except RequestException as e:

# Path: src/moviesapp/exceptions.py
# class OmdbError(Exception):
#     """OMDb error."""
# 
# class OmdbLimitReached(Exception):
#     """OMDb limit reached."""
# 
# class OmdbRequestError(Exception):
#     """OMDb request error."""
# 
# Path: src/moviesapp/models.py
# class Movie(models.Model):
#     title = models.CharField(max_length=255)
#     title_original = models.CharField(max_length=255)
#     country = models.CharField(max_length=255, null=True, blank=True)
#     description = models.TextField(null=True, blank=True)
#     director = models.CharField(max_length=255, null=True, blank=True)
#     writer = models.CharField(max_length=255, null=True, blank=True)
#     genre = models.CharField(max_length=255, null=True, blank=True)
#     actors = models.CharField(max_length=255, null=True, blank=True)
#     imdb_id = models.CharField(max_length=15, unique=True, db_index=True)
#     tmdb_id = models.IntegerField(unique=True)
#     imdb_rating = models.DecimalField(max_digits=2, decimal_places=1, null=True)
#     poster = models.CharField(max_length=255, null=True)
#     release_date = models.DateField(null=True)
#     runtime = models.TimeField(null=True, blank=True)
#     homepage = models.URLField(null=True, blank=True)
#     trailers = JSONField(null=True, blank=True)
# 
#     class Meta:
#         ordering = ["pk"]
# 
#     def __str__(self):
#         return str(self.title)
# 
#     def imdb_url(self):
#         return settings.IMDB_BASE_URL + self.imdb_id + "/"
# 
#     def get_trailers(self):
#         if self.trailers:
#             return self.trailers
#         return self.trailers_en
# 
#     def has_trailers(self):
#         return bool(self.get_trailers())
# 
#     def _get_poster(self, size):
#         return get_poster_url(size, self.poster)
# 
#     @property
#     def poster_normal(self):
#         return self._get_poster("normal")
# 
#     @property
#     def poster_small(self):
#         return self._get_poster("small")
# 
#     @property
#     def poster_big(self):
#         return self._get_poster("big")
# 
#     @property
#     def id_title(self):
#         return f"{self.pk} - {self}"
# 
#     def cli_string(self, last_movie_id):
#         """
#         Return string version for CLI.
# 
#         We need last_movie_id because we want to know how big is the number (in characters) to be able to make
#         perfect formatting.
#         We need to keep last_movie_id as a parameter because otherwise we hit the database every time we run the
#         function.
#         """
#         MAX_CHARS = 40
#         ENDING = ".."
#         n = len(str(last_movie_id)) + 1
#         id_format = f"{{0: < {n}}}"
#         title = str(self)
#         title = (title[:MAX_CHARS] + ENDING) if len(title) > MAX_CHARS else title
#         id_ = id_format.format(self.pk)
#         title_max_length = MAX_CHARS + len(ENDING)
#         title_format = f"{{:{title_max_length}s}}"
#         title = title_format.format(title)  # pylint: disable=consider-using-f-string
#         return f"{id_} - {title}"[1:]
# 
# Path: src/moviesapp/tmdb.py
# def get_tmdb_movie_data(tmdb_id):
#     def get_release_date(release_date):
#         if release_date:
#             return release_date
#         return None
# 
#     def get_trailers(movie_data):
#         trailers = []
#         for trailer in movie_data.videos()["results"]:
#             trailer_ = {"name": trailer["name"], "source": trailer["key"]}
#             trailers.append(trailer_)
#         return trailers
# 
#     def get_movie_data(tmdb_id, lang):
#         tmdb = get_tmdb(lang=lang)
#         return tmdb.Movies(tmdb_id)
# 
#     movie_data_en = get_movie_data(tmdb_id, "en")
#     movie_info_en = movie_data_en.info()
#     # We have to get all info in english first before we switch to russian or everything breaks.
#     trailers_en = get_trailers(movie_data_en)
#     movie_data_ru = get_movie_data(tmdb_id, "ru")
#     movie_info_ru = movie_data_ru.info()
#     trailers_ru = get_trailers(movie_data_ru)
#     imdb_id = movie_info_en["imdb_id"]
#     if imdb_id:
#         return {
#             "tmdb_id": tmdb_id,
#             "imdb_id": imdb_id,
#             "release_date": get_release_date(movie_info_en["release_date"]),
#             "title_original": movie_info_en["original_title"],
#             "poster_ru": get_poster_from_tmdb(movie_info_ru["poster_path"]),
#             "poster_en": get_poster_from_tmdb(movie_info_en["poster_path"]),
#             "homepage": movie_info_en["homepage"],
#             "trailers_en": trailers_en,
#             "trailers_ru": trailers_ru,
#             "title_en": movie_info_en["title"],
#             "title_ru": movie_info_ru["title"],
#             "description_en": movie_info_en["overview"],
#             "description_ru": movie_info_ru["overview"],
#         }
#     raise MovieNotInDb(tmdb_id)


# Path: src/moviesapp/utils.py
import re
import requests
from datetime import datetime
from django.conf import settings
from requests.exceptions import RequestException
from sentry_sdk import capture_exception
from .exceptions import OmdbError, OmdbLimitReached, OmdbRequestError
from .models import Movie
from .tmdb import get_tmdb_movie_data





def load_omdb_movie_data(imdb_id):
    try:
        response = requests.get(f"http://www.omdbapi.com/?i={imdb_id}&apikey={settings.OMDB_KEY}")
    except RequestException as e:

        if settings.DEBUG: