Example #1
0
        private List<DbMovieInfo> GetMoviesByTitle(string title, int year, string director, string imdbid, string tmdbid, bool choose, string language)
        {
            LogMyFilms.Debug("GetMoviesByTitle - title = '" + title + "', year = '" + year + "', imdbid = '" + imdbid + "', tmdbid = '" + tmdbid + "', choose = '" + choose + "', language = '" + language + "'");

              //title = Grabber.GrabUtil.normalizeTitle(title);

              //string apiSearchLanguage = ApiSearchMovie;
              //string apiGetMovieInfoLanguage = ApiGetMovieInfo;
              //if (language.Length == 2)
              //{
              //  apiSearchLanguage = ApiSearchMovie.Replace("/en/", "/" + language + "/");
              //  apiGetMovieInfoLanguage = ApiGetMovieInfo.Replace("/en/", "/" + language + "/");
              //}
              //else
              //{
              //  apiSearchLanguage = CultureInfo.CurrentCulture.Name.Substring(0, 2); // use local language instead
              //  apiGetMovieInfoLanguage = CultureInfo.CurrentCulture.Name.Substring(0, 2); // use local language instead
              //  language = CultureInfo.CurrentCulture.Name.Substring(0, 2); // use local language instead
              //}

              if (language.Length != 2)
            language = CultureInfo.CurrentCulture.Name.Substring(0, 2); // use local language instead

              List<DbMovieInfo> results = new List<DbMovieInfo>();
              List<DbMovieInfo> resultsdet = new List<DbMovieInfo>();
              // XmlNodeList xml = null; // old API2.1 xml structure
              List<TmdbMovie> movies = new List<TmdbMovie>();

              //Tmdb api = new Tmdb(TmdbApiKey, CultureInfo.CurrentCulture.Name.Substring(0, 2)); // language is optional, default is "en"
              //TmdbConfiguration tmdbConf = api.GetConfiguration();
              //TmdbMovieSearch tmdbMovies = api.SearchMovie(searchname, 0, language, 2012);
              //TmdbPersonSearch tmdbPerson = api.SearchPerson(personname, 1);
              //List<MovieResult> persons = tmdbMovies.results;
              //if (persons != null && persons.Count > 0)
              //{
              //  PersonResult pinfo = persons[0];
              //  TmdbPerson singleperson = api.GetPersonInfo(pinfo.id);
              //  // TMDB.TmdbPersonImages images = api.GetPersonImages(pinfo.id);
              //  // TMDB.TmdbPersonCredits personFilmList = api.GetPersonCredits(pinfo.id);
              //}

              Tmdb api = new Tmdb(TmdbApiKey, language); // language is optional, default is "en"
              // TmdbConfiguration tmdbConf = api.GetConfiguration();

              try
              {
            if (!string.IsNullOrEmpty(imdbid) && imdbid.Contains("tt"))
            {
              TmdbMovie movie = api.GetMovieByIMDB(imdbid);
              if (movie.id > 0)
              {
            results.Add(GetMovieInformation(api, movie, language));
            return results;
              }
            }

            TmdbMovieSearch moviesfound;
            if (year > 0)
            {
              moviesfound = api.SearchMovie(title, 1, "", null, year);
              if (moviesfound.results.Count == 0) moviesfound = api.SearchMovie(title, 1, language);
              movies.AddRange(moviesfound.results);
            }
            else
            {
              int ipage = 1;
              while (true)
              {
            moviesfound = api.SearchMovie(title, 1, null);
            movies.AddRange(moviesfound.results);
            ipage++;
            if (ipage > moviesfound.total_pages) break;
              }
              movies = movies.OrderBy(x => x.release_date).ToList(); // .AsEnumerable()
            }

            if (movies.Count == 1)
            {
              results.Add(GetMovieInformation(api, movies[0], language));
              return results;
            }
            else
            {
              foreach (TmdbMovie movieResult in movies)
              {
            DbMovieInfo movie = GetMovieInformation(api, movieResult, language);
            if (movie != null && Grabber.GrabUtil.normalizeTitle(movie.Name.ToLower()).Contains(Grabber.GrabUtil.normalizeTitle(title.ToLower())))
              if (year > 0 && movie.Year > 0 && !choose)
              {
                if ((year >= movie.Year - 2) && (year <= movie.Year + 2))
                  results.Add(movie);
              }
              else
                results.Add(movie);
              }
              return results;
            }

              }
              catch (Exception ex)
              {
            LogMyFilms.Debug(ex.StackTrace);
              }

              #region old TMDB APIv2.1 code
              //if (!string.IsNullOrEmpty(imdbid))
              //  xml = GetXml(ApiSearchMovieByImdb + imdbid);
              //if (xml == null) // if imdb search was unsuccessful use normal search...
              //  xml = GetXml(apiSearchLanguage + Grabber.GrabUtil.RemoveDiacritics(title.Trim().ToLower()).Replace(" ", "+"));

              //if (xml == null)
              //  return results;

              //XmlNodeList movieNodes = xml.Item(0).SelectNodes("//movie");
              //foreach (XmlNode node in movieNodes)
              //{
              //  DbMovieInfo movie = GetMovieInformation(node);
              //  if (movie != null && Grabber.GrabUtil.normalizeTitle(movie.Name.ToLower()).Contains(Grabber.GrabUtil.normalizeTitle(title.ToLower())))
              //    if (year > 0 && movie.Year > 0 && !choose)
              //    {
              //      if ((year >= movie.Year - 2) && (year <= movie.Year + 2))
              //        results.Add(movie);
              //    }
              //    else
              //      results.Add(movie);
              //}

              //if (results.Count > 0)
              //{
              //  // Replace non-descriptive characters with spaces
              //  director = System.Text.RegularExpressions.Regex.Replace(director, "( et | and | & | und )", ",");
              //  if (director.IndexOf(",", System.StringComparison.Ordinal) > 0)
              //    director = director.Substring(0, director.IndexOf(",", System.StringComparison.Ordinal));
              //  foreach (DbMovieInfo movie in results.Where(movie => movie.Identifier != null))
              //  {
              //    try { xml = GetXml(apiGetMovieInfoLanguage + movie.Identifier); }
              //    catch { xml = null; }
              //    if (xml != null)
              //    {
              //      movieNodes = xml.Item(0).SelectNodes("//movie");
              //      foreach (DbMovieInfo movie2 in from XmlNode node in movieNodes select GetMovieInformation(node))
              //      {
              //        if (movie2 != null && Grabber.GrabUtil.normalizeTitle(movie2.Name.ToLower()).Contains(Grabber.GrabUtil.normalizeTitle(title.ToLower())) && movie2.Directors.Contains(director))
              //          if (year > 0 && movie2.Year > 0 && !choose)
              //          {
              //            if ((year >= movie2.Year - 2) && (year <= movie2.Year + 2))
              //              resultsdet.Add(movie2);
              //          }
              //          else
              //            resultsdet.Add(movie2);
              //        else
              //          if (choose)
              //            resultsdet.Add(movie2);
              //      }
              //    }
              //  }
              //}
              #endregion

              return resultsdet.Count > 0 ? resultsdet : results;
        }
Example #2
0
    internal static void LoadCollectionImages(DataRow[] r1, int index, bool interactive, GUIAnimation animation)
    {
      if (!File.Exists(GUIGraphicsContext.Skin + @"\MyFilmsDialogImageSelect.xml"))
      {
        if (interactive) GUIUtils.ShowNotifyDialog("Missing Skin File");
        return;
      }
      // var dlg = (GUIDialogMenu)GUIWindowManager.GetWindow((int)GUIWindow.Window.WINDOW_DIALOG_MENU);
      var dlgMenuOrg = (GUIWindow)GUIWindowManager.GetWindow(2012);
      var dlg = new GUIDialogImageSelect();
      if (dlg == null) return;
      dlg.Init();
      GUIWindowManager.Replace(2009, dlg);

      //var dlgOk = (GUIDialogOK)GUIWindowManager.GetWindow((int)GUIWindow.Window.WINDOW_DIALOG_OK);
      //var dlgYesNo = (GUIDialogYesNo)GUIWindowManager.GetWindow((int)GUIWindow.Window.WINDOW_DIALOG_YES_NO);
      //var dlgPrgrs = (GUIDialogProgress)GUIWindowManager.GetWindow((int)GUIWindow.Window.WINDOW_DIALOG_PROGRESS);

      new Thread(delegate(object o)
      {
        try
        {
          SetProcessAnimationStatus(true, animation);
          #region select and load collection image
          string titlename = Helper.TitleWithoutGroupName(r1[index][MyFilms.conf.StrTitle1].ToString());
          string titlename2 = (Helper.FieldIsSet(MyFilms.conf.StrTitle2)) ? Helper.TitleWithoutGroupName(r1[index][MyFilms.conf.StrTitle2].ToString()) : "";
          string collectionname = Helper.TitleFirstGroupName(r1[index][MyFilms.conf.StrTitle1].ToString());
          string collectionSafeName = GrabUtil.CreateFilename(collectionname);

          string path;
          #region Retrieve original directory of mediafiles
          try
          {
            path = r1[index][MyFilms.conf.StrStorage].ToString();
            if (path.Contains(";")) path = path.Substring(0, path.IndexOf(";", StringComparison.Ordinal));
            //path = Path.GetDirectoryName(path);
            //if (path == null || !Directory.Exists(path))
            //{
            //  LogMyFilms.Warn("Directory of movie '" + titlename + "' doesn't exist anymore - check your DB");
            //  return;
            //}
            if (path.Contains("\\")) path = path.Substring(0, path.LastIndexOf("\\", StringComparison.Ordinal));

            // LogMyFilms.Debug("(SearchAndDownloadTrailerOnlineTMDB) get media directory name: '" + path + "'");
          }
          catch (Exception)
          {
            LogMyFilms.Debug("SearchAndDownloadTrailerOnlineTMDB() error with directory of movie '" + titlename + "' - check your DB");
            return;
          }
          #endregion

          string imdb = "";
          #region get imdb number for better search match
          if (!string.IsNullOrEmpty(r1[index]["IMDB_Id"].ToString()))
            imdb = r1[index]["IMDB_Id"].ToString();
          else if (!string.IsNullOrEmpty(r1[index]["URL"].ToString()))
          {
            string urLstring = r1[index]["URL"].ToString();
            var cutText = new Regex("" + @"tt\d{7}" + "");
            var m = cutText.Match(urLstring);
            if (m.Success) imdb = m.Value;
          }
          #endregion

          int year;

          LogMyFilms.Debug("LoadCollectionImages() - movietitle = '" + titlename + "', collectionname = '" + collectionSafeName + "', collectionSafeName = '" + collectionSafeName + "', interactive = '" + interactive + "'");

          string language = CultureInfo.CurrentCulture.Name.Substring(0, 2);
          var api = new Tmdb(MyFilms.TmdbApiKey, language); // language is optional, default is "en"
          TmdbConfiguration tmdbConf = api.GetConfiguration();
          foreach (string size in tmdbConf.images.poster_sizes) LogMyFilms.Debug("Available TMDB Poster Size: '" + size + "'");
          // foreach (string size in tmdbConf.images.backdrop_sizes) LogMyFilms.Debug("Available TMDB Backdrop Size: '" + size + "'");
          try
          {
            int selectedMovieId = 0;

            #region search matching TMDB movie id
            if (imdb.Contains("tt"))
            {
              TmdbMovie movie = api.GetMovieByIMDB(imdb);
              if (movie.id > 0)
              {
                selectedMovieId = movie.id;
              }
            }

            if (selectedMovieId == 0) // no movie found by tmdb search
            {
              TmdbMovieSearch moviesfound;
              if (int.TryParse(r1[index]["Year"].ToString(), out year))
              {
                moviesfound = api.SearchMovie(titlename, 1, "", null, year);
                if (moviesfound.results.Count == 0) moviesfound = api.SearchMovie(titlename, 1, null);
              }
              else
              {
                moviesfound = api.SearchMovie(r1[index][MyFilms.conf.StrTitle1].ToString(), 1, null);
                if (moviesfound.results.Count == 0 && titlename2.Length > 0)
                {
                  if (int.TryParse(r1[index]["Year"].ToString(), out year))
                  {
                    moviesfound = api.SearchMovie(titlename2, 1, "", null, year);
                    if (moviesfound.results.Count == 0) moviesfound = api.SearchMovie(titlename2, 1, null);
                  }
                }
              }
              SetProcessAnimationStatus(false, animation);

              if (moviesfound.results.Count == 1 && !interactive)
              {
                selectedMovieId = moviesfound.results[0].id;
              }
              else
              {
                LogMyFilms.Debug("LoadCollectionImages() - Movie Search Results: '" + moviesfound.total_results.ToString() + "' for movie '" + titlename + "'");
                if (!interactive) return;
                else
                {
                  if (moviesfound.results.Count == 0)
                  {
                    while (selectedMovieId == 0)
                    {
                      selectedMovieId = SearchTmdbMovie(titlename, titlename, titlename2, year, language, true);
                      if (selectedMovieId == -1) return; // cancel search
                    }
                  }
                  else
                  {
                    var choiceMovies = new List<TmdbMovie>();
                    if (dlg == null) return;
                    dlg.Reset();
                    dlg.SetHeading(GUILocalizeStrings.Get(10798992)); // Select movie ...

                    foreach (TmdbMovie movieResult in moviesfound.results)
                    {
                      dlg.Add(movieResult.title + " (" + movieResult.release_date + ")");
                      choiceMovies.Add(movieResult);
                    }
                    dlg.DoModal(GUIWindowManager.ActiveWindow);
                    if (dlg.SelectedLabel == -1) return;
                    selectedMovieId = choiceMovies[dlg.SelectedLabel].id;
                  }
                }
              }

              // now load the artwork
            }
            #endregion

            if (selectedMovieId == 0)
            {
              LogMyFilms.Debug("SearchAndDownloadTrailerOnlineTMDB - no movie found - no trailers added to DL queue - returning");
              if (interactive) GUIUtils.ShowNotifyDialog(GUILocalizeStrings.Get(10798995)); // No matching movie found !
              return;
            }

            SetProcessAnimationStatus(true, animation);
            #region now load the details and collection images info
            TmdbMovie tmdbMovie = api.GetMovieInfo(selectedMovieId);
            TmdbCollection collection = api.GetCollectionInfo(tmdbMovie.belongs_to_collection.id);
            var collectionPosters = new List<CollectionPoster>();
            var backdrops = new List<CollectionBackdrop>();

            LogMyFilms.Debug("TMDB - Value found - movie   = '" + (tmdbMovie.title ?? "") + "'" + " (" + tmdbMovie.release_date + ")");
            LogMyFilms.Debug("TMDB - Value found - belongs to collection id = '" + collection.id.ToString() + "', name  = '" + (collection.name ?? "") + "'");

            TmdbCollectionImages collectionImages = api.GetCollectionImages(collection.id, language);
            LogMyFilms.Debug("TMDB - Collection Posters found for language = '" + language + "' : '" + collectionImages.posters.Count + "'");
            collectionPosters.AddRange(collectionImages.posters);
            backdrops.AddRange(collectionImages.backdrops);
            collectionImages = api.GetCollectionImages(collection.id, null);
            LogMyFilms.Debug("TMDB - Collection Posters found: '" + collectionImages.posters.Count + "'");
            collectionPosters.AddRange(collectionImages.posters);
            backdrops.AddRange(collectionImages.backdrops);

            collectionPosters.Distinct();
            backdrops.Distinct();

            //foreach (CollectionBackdrop backdrop in backdrops)
            //{
            //  string fanartUrl = tmdbConf.images.base_url + "original" + backdrop.file_path;
            //  LogMyFilms.Debug("TMDB - Backdrop found = '" + fanartUrl + "'");
            //}
            #endregion

            if (collectionPosters.Count == 0)
            {
              SetProcessAnimationStatus(false, animation);
              GUIUtils.ShowNotifyDialog(GUILocalizeStrings.Get(10798760), GUILocalizeStrings.Get(10798625)); // load group covers, no result found
              return;
            }

            #region load collection images into selection menu
            var choicePosters = new List<CollectionPoster>();
            if (dlg == null) return;
            dlg.Reset();
            dlg.SetHeading(GUILocalizeStrings.Get(10798760)); // Load collection cover (Tmdb)
            foreach (CollectionPoster poster in collectionPosters)
            {
              string posterUrlSmall = tmdbConf.images.base_url + "w154" + poster.file_path;
              string posterUrl = tmdbConf.images.base_url + "w500" + poster.file_path;
              LogMyFilms.Debug("TMDB - Collection Poster found = '" + posterUrl + "'");
              var item = new GUIListItem();
              item.Label = poster.width + " x " + poster.height;
              item.IconImage = posterUrlSmall;
              item.ThumbnailImage = posterUrl;
              dlg.Add(item);
              choicePosters.Add(poster);
            }
            #endregion
            SetProcessAnimationStatus(false, animation);

            dlg.DoModal(GUIWindowManager.ActiveWindow);
            if (dlg.SelectedLabel == -1) return;
            CollectionPoster selectedPoster = choicePosters[dlg.SelectedLabel];

            #region load collection cover images and fanart
            SetProcessAnimationStatus(true, animation);
            try
            {
              #region Poster

              string localThumb = MyFilms.conf.StrPicturePrefix.Length > 0
                             ? MyFilms.conf.StrPathImg + "\\" + MyFilms.conf.StrPicturePrefix + collectionSafeName + ".jpg"
                             : Path.Combine(MyFilms.conf.StrPathImg, collectionSafeName + ".jpg");
              string remoteThumb = tmdbConf.images.base_url + "w500" + selectedPoster.file_path;
              LogMyFilms.Debug("GetImagesTMDB() - localThumb = '" + localThumb + "'");
              LogMyFilms.Debug("GetImagesTMDB() - remoteThumb = '" + remoteThumb + "'");

              if (!string.IsNullOrEmpty(remoteThumb) && !string.IsNullOrEmpty(localThumb))
              {
                if (File.Exists(localThumb)) try { File.Delete(localThumb); }
                  catch (Exception) { }
                Thread.Sleep(10);
                if (GrabUtil.DownloadImage(remoteThumb, localThumb))
                {
                  string coverThumbDir = MyFilmsSettings.GetPath(MyFilmsSettings.Path.ThumbsCache) + @"\MyFilms_Movies";
                  string strThumb = MediaPortal.Util.Utils.GetCoverArtName(coverThumbDir, collectionSafeName); // cached cover
                  if (!string.IsNullOrEmpty(strThumb) && strThumb.Contains("."))
                  {
                    string strThumbSmall = strThumb.Substring(0, strThumb.LastIndexOf(".", StringComparison.Ordinal)) + "_s" + Path.GetExtension(strThumb);  // cached cover for Icons - small resolution
                    if (!string.IsNullOrEmpty(localThumb) && localThumb != MyFilms.conf.DefaultCover)
                    {
                      Picture.CreateThumbnail(localThumb, strThumbSmall, 100, 150, 0, Thumbs.SpeedThumbsSmall);
                      Picture.CreateThumbnail(localThumb, strThumb, MyFilms.cacheThumbWith, MyFilms.cacheThumbHeight, 0, Thumbs.SpeedThumbsLarge);
                      LogMyFilms.Debug("Creating thumbimage for collection: '" + collection + "'");
                    }
                  }
                  //// notify that image has been downloaded
                  //item.NotifyPropertyChanged("PosterImageFilename");
                  SetProcessAnimationStatus(false, animation);
                  if (interactive) GUIUtils.ShowNotifyDialog(GUILocalizeStrings.Get(1079846)); // Done !
                }
              }
              #endregion

              #region Fanart
              if (MyFilms.conf.StrFanart)
              {
                //string fanartUrl = tmdbConf.images.base_url + "original" + movie.MovieSearchResult.backdrop_path;
                //string filename;
                //string filename1 = GrabUtil.DownloadBacdropArt(MyFilms.conf.StrPathFanart, fanartUrl, item.Label, true, true, out filename);
                //// LogMyFilms.Debug("Fanart " + filename1.Substring(filename1.LastIndexOf("\\") + 1) + " downloaded for " + item.Label);

                ////movie.MovieImages = api.GetMovieImages(movie.MovieSearchResult.id, language);
                ////if (movie.MovieImages.posters.Count == 0)
                ////{
                ////  movie.MovieImages = api.GetMovieImages(movie.MovieSearchResult.id, null);
                ////  LogMyFilms.Debug("GetImagesTMDB() - no '" + language + "' posters found - used default and found '" + movie.MovieImages.posters.Count + "'");
                ////}
                ////int ii = 0;
                ////foreach (Backdrop fanart in movie.MovieImages.backdrops)
                ////{
                ////  if (ii == 0)
                ////  {
                ////    string fanartUrl = tmdbConf.images.base_url + "original" + fanart.file_path;
                ////    string filename;
                ////    string filename1 = GrabUtil.DownloadBacdropArt(MyFilms.conf.StrPathFanart, fanartUrl, item.Label, true, (i == 0), out filename);
                ////    LogMyFilms.Debug("Fanart " + filename1.Substring(filename1.LastIndexOf("\\") + 1) + " downloaded for " + item.Label);
                ////  }
                ////  ii++;
                ////}
              }
              #endregion

            }
            catch (Exception ex)
            {
              LogMyFilms.Debug("GetImagesForTmdbCollection() - Error: '" + ex.Message + "'");
              LogMyFilms.Debug("GetImagesForTmdbCollection() - Exception: '" + ex.StackTrace + "'");
              SetProcessAnimationStatus(false, animation);
            }
            #endregion
          #endregion
          }
          catch (Exception tex)
          {
            LogMyFilms.DebugException("CollectionArtwork() - error in TMDB grabbing movie '" + titlename + "': " + tex.Message, tex);
            SetProcessAnimationStatus(false, animation);
          }
        }
        catch (Exception ex)
        {
          LogMyFilms.DebugException("Thread 'LoadCollectionImages' - exception! - ", ex);
        }
        finally
        {
          GUIWindowManager.Replace(2009, dlgMenuOrg);
          SetProcessAnimationStatus(false, animation);
        }
        GUIWindowManager.SendThreadCallbackAndWait((p1, p2, data) =>
        {
          // after background thread has finished !
          return 0;
        }, 0, 0, null);
      }) { Name = "LoadCollectionImages", IsBackground = true }.Start();
    }
Example #3
0
    //-------------------------------------------------------------------------------------------
    //  Search and Download Trailerfiles via TMDB (mostly YouTube)
    //-------------------------------------------------------------------------------------------        
    internal static void SearchAndDownloadTrailerOnlineTMDB(DataRow[] r1, int index, bool loadAllTrailers, bool interactive, string overridestoragepath)
    {
      //LogMyFilms.Debug("(SearchAndDownloadTrailerOnlineTMDB) - mastertitle      : '" + MyFilms.r[index][MyFilms.conf.StrTitle1] + "'");
      //if (Helper.FieldIsSet(MyFilms.conf.StrTitle2)) LogMyFilms.Debug("(SearchAndDownloadTrailerOnlineTMDB) - secondary title  : '" + MyFilms.r[index][MyFilms.conf.StrTitle2] + "'");
      //LogMyFilms.Debug("(SearchAndDownloadTrailerOnlineTMDB) - Cleaned Title    : '" + MediaPortal.Util.Utils.FilterFileName(MyFilms.r[index][MyFilms.conf.StrTitle1].ToString().ToLower()) + "'");

      string titlename = Helper.TitleWithoutGroupName(r1[index][MyFilms.conf.StrTitle1].ToString());
      string titlename2 = (Helper.FieldIsSet(MyFilms.conf.StrTitle2)) ? Helper.TitleWithoutGroupName(r1[index][MyFilms.conf.StrTitle2].ToString()) : "";
      string collectionname = Helper.TitleFirstGroupName(r1[index][MyFilms.conf.StrTitle1].ToString());

      string path;
      #region Retrieve original directory of mediafiles
      try
      {
        path = r1[index][MyFilms.conf.StrStorage].ToString();
        if (path.Contains(";")) path = path.Substring(0, path.IndexOf(";", StringComparison.Ordinal));
        //path = Path.GetDirectoryName(path);
        //if (path == null || !Directory.Exists(path))
        //{
        //  LogMyFilms.Warn("Directory of movie '" + titlename + "' doesn't exist anymore - check your DB");
        //  return;
        //}
        if (path.Contains("\\")) path = path.Substring(0, path.LastIndexOf("\\", StringComparison.Ordinal));
        
        // LogMyFilms.Debug("(SearchAndDownloadTrailerOnlineTMDB) get media directory name: '" + path + "'");
      }
      catch (Exception)
      {
        LogMyFilms.Debug("SearchAndDownloadTrailerOnlineTMDB() error with directory of movie '" + titlename + "' - check your DB");
        return;
      }
      #endregion

      string imdb = "";
      #region get imdb number sor better search match
      if (!string.IsNullOrEmpty(r1[index]["IMDB_Id"].ToString()))
        imdb = r1[index]["IMDB_Id"].ToString();
      else if (!string.IsNullOrEmpty(r1[index]["URL"].ToString()))
      {
        string urLstring = r1[index]["URL"].ToString();
        var cutText = new Regex("" + @"tt\d{7}" + "");
        var m = cutText.Match(urLstring);
        if (m.Success) imdb = m.Value;
      }
      #endregion

      int year;

      #region get local language
      string language = CultureInfo.CurrentCulture.Name.Substring(0, 2);
      // LogMyFilms.Debug("SearchAndDownloadTrailerOnlineTMDB - detected language = '" + language + "'");
      #endregion

      LogMyFilms.Debug("SearchAndDownloadTrailerOnlineTMDB() - movie '" + titlename + "', media directory '" + path + "', language '" + language + "'");
      
      var api = new Tmdb(MyFilms.TmdbApiKey, language);
      // var tmdbConf = api.GetConfiguration();

      int selectedMovieId = 0;

      #region search matching TMDB movie id
      if (imdb.Contains("tt"))
      {
        TmdbMovie movie = api.GetMovieByIMDB(imdb);
        if (movie.id > 0)
        {
          selectedMovieId = movie.id;
        }
      }

      if (selectedMovieId == 0) // no movie found by imdb search
      {
        TmdbMovieSearch moviesfound;
        if (int.TryParse(r1[index]["Year"].ToString(), out year))
        {
          moviesfound = api.SearchMovie(titlename, 1, "", null, year);
          if (moviesfound.results.Count == 0) moviesfound = api.SearchMovie(titlename, 1, null);
        }
        else
        {
          moviesfound = api.SearchMovie(r1[index][MyFilms.conf.StrTitle1].ToString(), 1, null);
          if (moviesfound.results.Count == 0 && titlename2.Length > 0)
          {
            if (int.TryParse(r1[index]["Year"].ToString(), out year))
            {
              moviesfound = api.SearchMovie(titlename2, 1, "", null, year);
              if (moviesfound.results.Count == 0) moviesfound = api.SearchMovie(titlename2, 1, null);
            }
          }
        }

        if (moviesfound.results.Count == 1)
        {
          selectedMovieId = moviesfound.results[0].id;
        }
        else
        {
          LogMyFilms.Debug("SearchAndDownloadTrailerOnlineTMDB() - Movie Search Results: '" + moviesfound.total_results.ToString() + "' for movie '" + titlename + "'");
          if (!interactive) return;
          else
          {
            if (moviesfound.results.Count == 0)
            {
              while(selectedMovieId == 0)
              {
                selectedMovieId = SearchTmdbMovie(titlename, titlename, titlename2, year, language, false);
                if (selectedMovieId == -1) return; // cancel search
              }
            }
            else
            {
              var choiceMovies = new List<TmdbMovie>();
              var dlg = (GUIDialogMenu)GUIWindowManager.GetWindow((int)GUIWindow.Window.WINDOW_DIALOG_MENU);
              if (dlg == null) return;
              dlg.Reset();
              dlg.SetHeading(GUILocalizeStrings.Get(10798992)); // Select movie ...

              foreach (TmdbMovie movieResult in moviesfound.results)
              {
                dlg.Add(movieResult.title + " (" + movieResult.release_date + ")");
                choiceMovies.Add(movieResult);
              }
              dlg.DoModal(GUIWindowManager.ActiveWindow);
              if (dlg.SelectedLabel == -1) return;
              selectedMovieId = choiceMovies[dlg.SelectedLabel].id;
            }
          }
        }
      }
      #endregion

      if (selectedMovieId == 0)
      {
        LogMyFilms.Debug("SearchAndDownloadTrailerOnlineTMDB - no movie found - no trailers added to DL queue - returning");
        if (interactive) GUIUtils.ShowNotifyDialog(GUILocalizeStrings.Get(10798995)); // No matching movie found !
        return;
      }

      #region search trailers for movie
      var trailersfound = new List<Youtube>();

      TmdbMovieTrailers trailers = api.GetMovieTrailers(selectedMovieId, language); // trailers in local language
      if (trailers.youtube.Count > 0) trailersfound.AddRange(trailers.youtube);
      LogMyFilms.Debug("SearchAndDownloadTrailerOnlineTMDB() - '" + trailers.youtube.Count + "' trailers found in local language ('" + language + "')");

      if (language != "en")
      {
        trailers = api.GetMovieTrailers(selectedMovieId, "en"); // engish trailers
        if (trailers.youtube.Count > 0) trailersfound.AddRange(trailers.youtube);
        LogMyFilms.Debug("SearchAndDownloadTrailerOnlineTMDB() - '" + trailers.youtube.Count + "' trailers found in language 'en'");
      }

      if (trailersfound.Count == 0)
      {
        LogMyFilms.Debug("SearchAndDownloadTrailerOnlineTMDB() - no trailers found - returning");
        if (interactive) GUIUtils.ShowNotifyDialog(GUILocalizeStrings.Get(10798996)); // no trailers found !
      }
      else
      {
        LogMyFilms.Debug("SearchAndDownloadTrailerOnlineTMDB() - '" + trailersfound.Count + "' trailers found");
        var dlg = (GUIDialogMenu)GUIWindowManager.GetWindow((int)GUIWindow.Window.WINDOW_DIALOG_MENU);
        if (dlg == null) return;

        Youtube selectedTrailer = null;
        string selectedTrailerUrl = "";
        string selectedQuality = "";

        if (interactive)
        {
          #region build menu with available trailers
          var choiceView = new List<Youtube>();
          dlg.Reset();
          dlg.SetHeading(GUILocalizeStrings.Get(10798993)); // Select trailer ...

          dlg.Add("<" + GUILocalizeStrings.Get(10798997) + ">"); // load all 
          choiceView.Add(new Youtube());

          foreach (Youtube trailer in trailersfound)
          {
            dlg.Add(trailer.name + " (" + trailer.size + ")");
            choiceView.Add(trailer);
          }

          //dlg.Add(GUILocalizeStrings.Get(10798711)); //search youtube trailer with onlinevideos
          //choiceView.Add("youtube");

          dlg.DoModal(GUIWindowManager.ActiveWindow);
          if (dlg.SelectedLabel == -1) return;
          if (dlg.SelectedLabel >  0)
          {
            selectedTrailer = choiceView[dlg.SelectedLabel];
            LogMyFilms.Debug("SearchAndDownloadTrailerOnlineTMDB() - selectedTrailer = '" + selectedTrailer.name + " (" + selectedTrailer.size + ")'");
          }
          #endregion
        }

        #region select trailer format and quality in OV player factory
        if (selectedTrailer != null)
        {
          Dictionary<string, string> availableTrailerFiles = MyFilmsPlugin.Utils.OVplayer.GetYoutubeDownloadUrls("http://www.youtube.com/watch?v=" + selectedTrailer.source);
          var choiceView = new List<string>();
          dlg.Reset();
          dlg.SetHeading(GUILocalizeStrings.Get(10798994)); // Select quality ...
          foreach (KeyValuePair<string, string> availableTrailerFile in availableTrailerFiles)
          {
            dlg.Add(availableTrailerFile.Key);
            choiceView.Add(availableTrailerFile.Value); // this is the download URL
          }
          dlg.DoModal(GUIWindowManager.ActiveWindow);
          if (dlg.SelectedLabel == -1) return;
          selectedTrailerUrl = choiceView[dlg.SelectedLabel];
          selectedQuality = dlg.SelectedLabelText;
        }
        #endregion

        #region download trailer

        string destinationDirectory;
        if (overridestoragepath != null)
        {
          string newpath = Path.Combine(overridestoragepath + @"MyFilms\", path.Substring(path.LastIndexOf("\\") + 1));
          newpath = Path.Combine(newpath, "Trailer");
          destinationDirectory = newpath;
        }
        else
        {
          destinationDirectory = Path.Combine(path, "Trailer");
        }

        if (selectedTrailerUrl.Length > 0 && selectedTrailer != null)
        {
          var trailer = new Trailer();
          trailer.MovieTitle = titlename;
          trailer.Trailername = selectedTrailer.name;
          trailer.OriginalUrl = "http://www.youtube.com/watch?v=" + selectedTrailer.source;
          trailer.SourceUrl = selectedTrailerUrl;
          trailer.Quality = selectedQuality;
          trailer.DestinationDirectory = destinationDirectory; // Path.Combine(Path.Combine(path, "Trailer"), (MediaPortal.Util.Utils.FilterFileName(titlename + " (trailer) " + selectedTrailer.name + " (" + dlg.SelectedLabelText.Replace(" ", "") + ")" + extension)));
          MyFilms.AddTrailerToDownloadQueue(trailer);
          LogMyFilms.Debug("SearchAndDownloadTrailerOnlineTMDB() - start loading single trailer '" + selectedTrailer.name + "' from URL: '" + selectedTrailerUrl + "'");
          if (interactive) GUIUtils.ShowNotifyDialog(GUILocalizeStrings.Get(1079703)); //  trailer addd to DL queue
        }
        else
        {
          for (int i = 0; i < trailersfound.Count; i++)
          {
            if (i < 2 || loadAllTrailers)
            {
              Dictionary<string, string> availableTrailerFiles = MyFilmsPlugin.Utils.OVplayer.GetYoutubeDownloadUrls("http://www.youtube.com/watch?v=" + trailersfound[i].source);
              string url = null;
              string quality = null;
              if (availableTrailerFiles != null && availableTrailerFiles.Count > 0)
              {
                //url = availableTrailerFiles.Last().Value;
                //quality = availableTrailerFiles.Last().Key;
                KeyValuePair<string, string> highestQualitySelection = MyFilmsPlugin.Utils.OVplayer.GetPreferredQualityOption(availableTrailerFiles, "FHD");
                url = highestQualitySelection.Value;
                quality = highestQualitySelection.Key;
                LogMyFilms.Debug("SearchAndDownloadTrailerOnlineTMDB() - selected trailer with quality = '" + quality ?? "" + "', url = '" + url ?? "" + "'");
              }
              else
              {
                LogMyFilms.Debug("SearchAndDownloadTrailerOnlineTMDB() - no download Url found - adding trailer without DL links for later processing from queue");
              }
              var trailer = new Trailer();
              trailer.MovieTitle = titlename;
              trailer.Trailername = trailersfound[i].name;
              trailer.OriginalUrl = "http://www.youtube.com/watch?v=" + trailersfound[i].source;
              trailer.SourceUrl = url;
              trailer.Quality = quality;
              trailer.DestinationDirectory = destinationDirectory;
              // filename: (MediaPortal.Util.Utils.FilterFileName(titlename + " (trailer) " + trailersfound[i].name + " (" + quality.Replace(" ", "") + ")" + extension))
              LogMyFilms.Debug("SearchAndDownloadTrailerOnlineTMDB() - add trailer '#" + i + "'");
              MyFilms.AddTrailerToDownloadQueue(trailer);
            }
          }
        }
        #endregion
      }
      #endregion
    }
Example #4
0
        private List<DbMovieInfo> GetMoviesByTitle(string title, int year, string director, string imdbid, string tmdbid, bool choose, string language)
        {
            LogMyFilms.Debug("GetMoviesByTitle - title = '" + title + "', year = '" + year + "', imdbid = '" + imdbid + "', tmdbid = '" + tmdbid + "', choose = '" + choose + "', language = '" + language + "'");

              //title = Grabber.GrabUtil.NormalizeTitle(title);

              //string apiSearchLanguage = ApiSearchMovie;
              //string apiGetMovieInfoLanguage = ApiGetMovieInfo;
              //if (language.Length == 2)
              //{
              //  apiSearchLanguage = ApiSearchMovie.Replace("/en/", "/" + language + "/");
              //  apiGetMovieInfoLanguage = ApiGetMovieInfo.Replace("/en/", "/" + language + "/");
              //}
              //else
              //{
              //  apiSearchLanguage = CultureInfo.CurrentCulture.Name.Substring(0, 2); // use local language instead
              //  apiGetMovieInfoLanguage = CultureInfo.CurrentCulture.Name.Substring(0, 2); // use local language instead
              //  language = CultureInfo.CurrentCulture.Name.Substring(0, 2); // use local language instead
              //}

              if (language.Length != 2) language = CultureInfo.CurrentCulture.Name.Substring(0, 2); // use local language instead

              List<DbMovieInfo> results = new List<DbMovieInfo>();
              List<DbMovieInfo> resultsdet = new List<DbMovieInfo>();
              // XmlNodeList xml = null; // old API2.1 xml structure
              List<TmdbMovie> movies = new List<TmdbMovie>();

              //Tmdb api = new Tmdb(TmdbApiKey, CultureInfo.CurrentCulture.Name.Substring(0, 2)); // language is optional, default is "en"
              //TmdbConfiguration tmdbConf = api.GetConfiguration();
              //TmdbMovieSearch tmdbMovies = api.SearchMovie(searchname, 0, language, 2012);
              //TmdbPersonSearch tmdbPerson = api.SearchPerson(personname, 1);
              //List<MovieResult> persons = tmdbMovies.results;
              //if (persons != null && persons.Count > 0)
              //{
              //  PersonResult pinfo = persons[0];
              //  TmdbPerson singleperson = api.GetPersonInfo(pinfo.id);
              //  // TMDB.TmdbPersonImages images = api.GetPersonImages(pinfo.id);
              //  // TMDB.TmdbPersonCredits personFilmList = api.GetPersonCredits(pinfo.id);
              //}

              Tmdb api = new Tmdb(TmdbApiKey, language); // language is optional, default is "en"
              // TmdbConfiguration tmdbConf = api.GetConfiguration();

              try
              {
            if (!string.IsNullOrEmpty(imdbid) && imdbid.Contains("tt"))
            {
              TmdbMovie movie = api.GetMovieByIMDB(imdbid);
              if (movie.id > 0)
              {
            results.Add(GetMovieInformation(api, movie, language));
            return results;
              }
            }

            TmdbMovieSearch moviesfound;
            if (year > 0)
            {
              moviesfound = api.SearchMovie(title, 1, "", null, year);
              if (moviesfound.results.Count == 0) moviesfound = api.SearchMovie(title, 1, language);
              movies.AddRange(moviesfound.results);
            }
            else
            {
              int ipage = 1;
              while (true)
              {
            moviesfound = api.SearchMovie(title, 1, null);
            movies.AddRange(moviesfound.results);
            ipage++;
            if (ipage > moviesfound.total_pages) break;
              }
              movies = movies.OrderBy(x => x.release_date).ToList(); // .AsEnumerable()
            }

            if (movies.Count == 1)
            {
              results.Add(GetMovieInformation(api, movies[0], language));
              return results;
            }
            else
            {
              foreach (TmdbMovie movieResult in movies)
              {
            DbMovieInfo movie = GetMovieInformation(api, movieResult, language);
            if (movie != null && GrabUtil.NormalizeTitle(movie.Name.ToLower()).Contains(GrabUtil.NormalizeTitle(title.ToLower())))
              if (year > 0 && movie.Year > 0 && !choose)
              {
                if ((year >= movie.Year - 2) && (year <= movie.Year + 2))
                  results.Add(movie);
              }
              else
                results.Add(movie);
              }
              return results;
            }

              }
              catch (Exception ex)
              {
            LogMyFilms.Debug(ex.StackTrace);
              }
              return resultsdet.Count > 0 ? resultsdet : results;
        }