context: // Type: FanartHandler.DatabaseManager
  // Assembly: FanartHandler, Version=4.0.3.0, Culture=neutral, PublicKeyToken=null
  // MVID: 073E8D78-B6AE-4F86-BDE9-3E09A337833B
  
  extern alias FHNLog;
  
  using MediaPortal.Configuration;
  using MediaPortal.Database;
  using MediaPortal.Music.Database;
  using MediaPortal.Video.Database;
  
  using FHNLog.NLog;
  
  using SQLite.NET;
  
  using System;
  using System.Collections;
  using System.Collections.Generic;
  using System.Diagnostics;
  using System.Globalization;
  using System.IO;
  using System.Windows.Forms;
  using System.Linq;
  
  
  namespace FanartHandler
  {
    internal class DatabaseManager
    {
      private static readonly Logger logger = LogManager.GetCurrentClassLogger();
      private readonly object lockObject = new object();
      private const string dbFilename = "FanartHandler.db3";
      private const string dbDateFormat = "yyyy-MM-dd";
      private const string dbDateTimeFormat = "yyyyMMddHHmmss";
      private SQLiteClient dbClient;
  
      private Hashtable htAnyFanart;
      private Hashtable htAnyLatestsFanart;
      // private bool HtAnyFanartUpdate;
  
      private MusicDatabase m_db;
      // private VideoDatabase v_db;
  
      private Scraper scraper;
      private bool DBIsInit = false;
  
      public bool IsScraping { get; set; }
      public bool StopScraper { get; set; }
      public bool StopScraperInfo { get; set; }
      public bool StopScraperMovieInfo { get; set; }
  
      public double CurrArtistsBeingScraped { get; set; }
      public double TotArtistsBeingScraped { get; set; }
  
      public string CurrTextBeingScraped { get; set; }
  
      public Hashtable HtAnyFanart
      {
        get { return htAnyFanart; }
        set { htAnyFanart = value; }
      }
  
      public Hashtable HtAnyLatestsFanart
      {
        get { return htAnyLatestsFanart; }
        set { htAnyLatestsFanart = value; }
      }
  
      public bool IsDBInit
      {
        get { return DBIsInit; }
        set { DBIsInit = value; }
      }
  
      static DatabaseManager()
      {
      }
  
      public DatabaseManager()
      {
        // HtAnyFanartUpdate = false;
      }
  
      #region DoScrape
      public int DoScrapeArtist(FanartArtist fa)
      {
        return DoScrapeArtist(fa, true, false, false);
      }
  
      public int DoScrapeArtist(FanartArtist fa, bool reportProgress, bool triggerRefresh, bool externalAccess)
      {
        if (fa.IsEmpty)
        {
          return 0;
        }
  
        if (Utils.UseArtistException && Utils.ArtistExceptionList.Contains(fa.Artist, StringComparer.OrdinalIgnoreCase))
        {
          logger.Debug("Artist: " + fa.Artist + " in Exception list, skipped...");
          return Utils.iScraperMaxImages;
        }
  
        if (!StopScraper)
        {
          try
          {
            var GetImages = 0;
            var MaxImages = Utils.iScraperMaxImages;
            var numberOfFanartImages = GetNumberOfFanartImages(Utils.Category.MusicFanart, fa.DBArtist);
            var doScrapeFanart = (numberOfFanartImages < MaxImages);
  
            logger.Debug("Artist: " + fa.Artist + " images: " + numberOfFanartImages + " from: " + MaxImages);
  
            if (!doScrapeFanart)
            {
              GetImages = Utils.iScraperMaxImages;
            }
            else
            {
              scraper = new Scraper();
              lock (lockObject)
                dbClient.Execute("BEGIN TRANSACTION;");
              GetImages = scraper.GetArtistFanart(fa, MaxImages, reportProgress, triggerRefresh, externalAccess, doScrapeFanart);
              lock (lockObject)
                dbClient.Execute("COMMIT;");
              scraper = null;
            }
            if (GetImages > 0)
            {
              UpdateTimeStamp(fa.DBArtist, null, Utils.Category.MusicFanart, Utils.SubCategory.MusicFanartScraped);
            }
  
            var dbImages = GetNumberOfFanartImages(Utils.Category.MusicFanart, fa.DBArtist);
            if ((GetImages == 0) && (dbImages == 0))
            {
              logger.Info("No fanart found for Artist: {0}.", fa.Artist);
            }
            else if (dbImages >= Utils.iScraperMaxImages)
            {
              if (doScrapeFanart)
              {
                logger.Info("Artist: {0} has already maximum number of images. Will not download anymore images for this artist.", fa.Artist);
              }
              else
              {
                logger.Debug("Artist: {0} has already maximum number of images. Will not download anymore images for this artist.", fa.Artist);
            }
            }
            else if (dbImages > 0)
            {
              if (doScrapeFanart)
              {
                logger.Info("Artist: {0} has already {1} images. No more images were found for this artist.", fa.Artist, dbImages);
              }
              else
              {
                logger.Debug("Artist: {0} has already {1} images. No more images were found for this artist.", fa.Artist, dbImages);
              }
            }
  
            if (StopScraper)
            {
              return GetImages;
            }
  
            DoScrapeFanartTV(fa, externalAccess, triggerRefresh, Utils.FanartTV.MusicClearArt, Utils.FanartTV.MusicBanner);
  
            return GetImages;
          }
          catch (Exception ex)
          {
            scraper = null;
            logger.Error("DoScrapeArtist: " + ex);
            lock (lockObject)
              dbClient.Execute("ROLLBACK;");
          }
        }
        return 0;
      }
  
      public int DoScrapeArtistThumbs(FanartArtist fa, bool onlyMissing)
      {
        return DoScrapeArtistThumbs(fa, onlyMissing, false);
      }
  
      public int DoScrapeArtistThumbs(FanartArtist fa, bool onlyMissing, bool externalAccess)
      {
        if (!Utils.ScrapeThumbnails)
        {
          return 0;
        }
        if (fa.IsEmpty)
        {
          return 0;
        }
  
        if (!StopScraper)
        {
          try
          {
            var num = 0;
            var hasThumb = HasArtistThumb(fa.DBArtist);
            if (!hasThumb || !onlyMissing)
            {
              scraper = new Scraper();
              lock (lockObject)
                dbClient.Execute("BEGIN TRANSACTION;");
              num = scraper.GetArtistThumbs(fa, onlyMissing);
              lock (lockObject)
                dbClient.Execute("COMMIT;");
              scraper = null;
              if (num == 0)
                logger.Info("No Thumbs found for Artist: " + fa.Artist + ".");
              hasThumb = HasArtistThumb(fa.DBArtist);
            }
  
            if (hasThumb)
            {
              UpdateTimeStamp(fa.DBArtist, null, Utils.Category.MusicArtist, Utils.SubCategory.MusicArtistThumbScraped);
            }
  
            if (StopScraper)
            {
              return num;
            }
  
            DoScrapeFanartTV(fa, externalAccess, false, Utils.FanartTV.MusicClearArt, Utils.FanartTV.MusicBanner);
  
            return num;
          }
          catch (Exception ex)
          {
            scraper = null;
            logger.Error("DoScrapeArtistThumbs: " + ex);
            lock (lockObject)
              dbClient.Execute("ROLLBACK;");
          }
        }
        return 0;
      }
  
      public int DoScrapeAlbumThumbs(FanartAlbum fa)
      {
        return DoScrapeAlbumThumbs(fa, true, false);
      }
  
      public int DoScrapeAlbumThumbs(FanartAlbum fa, bool onlyMissing, bool externalAccess)
      {
        if (!Utils.ScrapeThumbnailsAlbum)
        {
          return 0;
        }
        if (fa.IsEmpty)
        {
          return 0;
        }
  
        if (!StopScraper)
        {
          try
          {
            // logger.Debug("*** Artist: " + fa.Artist + " Album: " + fa.Album);
            var num = 0;
            var hasThumb = HasAlbumThumb(fa.DBArtist, fa.DBAlbum);
            if (!hasThumb || !onlyMissing)
            {
              scraper = new Scraper();
              lock (lockObject)
                dbClient.Execute("BEGIN TRANSACTION;");
              num = scraper.GetArtistAlbumThumbs(fa, onlyMissing, externalAccess);
              lock (lockObject)
                dbClient.Execute("COMMIT;");
              scraper = null;
              if (num == 0)
              {
                logger.Info("No Thumbs found for Artist: " + fa.Artist + " Album: " + fa.Album);
              }
              hasThumb = HasAlbumThumb(fa.DBArtist, fa.DBAlbum);
            }
  
            if (hasThumb)
            {
              UpdateTimeStamp(fa.DBArtist, fa.DBAlbum, Utils.Category.MusicAlbum, Utils.SubCategory.MusicAlbumThumbScraped);
            }
  
            if (StopScraper)
            {
              return num;
            }
  
            DoScrapeFanartTV(fa, externalAccess, false, Utils.FanartTV.MusicCDArt);
  
            return num;
          }
          catch (Exception ex)
          {
            scraper = null;
            logger.Error("DoScrapeAlbumThumbs: " + ex);
            lock (lockObject)
              dbClient.Execute("ROLLBACK;");
          }
        }
        return 0;
      }
  
      public int DoScrapeArtistAlbum(string artist, string album)
      {
        return DoScrapeArtistAlbum(artist, album, 0, false);
      }
  
      public int DoScrapeArtistAlbum(string artist, string album, int discs)
      {
        return DoScrapeArtistAlbum(artist, album, discs, false);
      }
  
      public int DoScrapeArtistAlbum(string artist, string album, bool externalAccess)
      {
        return DoScrapeArtistAlbum(artist, album, 0, externalAccess);
      }
  
      public int DoScrapeArtistAlbum(string artist, string album, int discs, bool externalAccess)
      {
        if (!StopScraper)
        {
          try
          {
            var GetImages = 0;
            if (!string.IsNullOrWhiteSpace(artist))
            {
              FanartArtist fa = new FanartArtist(artist);
  
              var numberOfFanartImages = GetNumberOfFanartImages(Utils.Category.MusicFanart, fa.DBArtist);
              var doTriggerRefresh = (numberOfFanartImages < Utils.iScraperMaxImages) && !externalAccess;
  
              // logger.Debug("*** DoScrapeArtistAlbum: Trigger {0} - {1} - {2}", numberOfFanartImages, Utils.iScraperMaxImages, doTriggerRefresh);
  
              #region Artist
              GetImages = DoScrapeArtist(fa, false, doTriggerRefresh, externalAccess);
              UpdateLastAccess(fa.DBArtist, null, Utils.Category.MusicFanart, Utils.SubCategory.MusicFanartScraped);
  
              if (Utils.GetArtistInfo)
              {
                DoScrapeArtistInfo(fa);
              }
  
              if (StopScraper)
              {
                return GetImages;
              }
              #endregion
  
              #region Artist Thumb
              DoScrapeArtistThumbs(fa, true);
              UpdateLastAccess(fa.DBArtist, null, Utils.Category.MusicArtist, Utils.SubCategory.MusicArtistThumbScraped);
  
              if (StopScraper)
              {
                return GetImages;
              }
              #endregion
  
              #region Album Thumb
              if (!string.IsNullOrWhiteSpace(album))
              {
                FanartAlbum faa = new FanartAlbum(artist, album, discs);
                DoScrapeAlbumThumbs(faa, true, externalAccess);
                UpdateLastAccess(faa.DBArtist, faa.DBAlbum, Utils.Category.MusicAlbum, Utils.SubCategory.MusicAlbumThumbScraped);
  
                if (Utils.MusicLabelDownload)
                {
                  DoScrapeMusicLabels(faa);
                }
  
                if (Utils.GetAlbumInfo)
                {
                  DoScrapeAlbumInfo(faa);
                }
              }
  
              if (StopScraper)
              {
                return GetImages;
              }
              #endregion
            } // if (!string.IsNullOrWhiteSpace(artist))
            return GetImages;
          }
          catch (Exception ex)
          {
            logger.Error("DoScrapeArtistAlbum: " + ex);
          }
        }
        return 0;
      }
  
      public int DoScrapeMovies(FanartMovie fm)
      {
        return DoScrapeMovies(fm, false);
      }
  
      public int DoScrapeMovies(FanartMovie fm, bool externalAccess)
      {
        if (fm.IsEmpty || !fm.HasIMDBID)
        {
          return 0;
        }
  
        if (!StopScraper)
        {
          try
          {
            int num = 0;
            scraper = new Scraper();
            lock (lockObject)
              dbClient.Execute("BEGIN TRANSACTION;");
            num = scraper.GetMoviesFanart(fm);
            lock (lockObject)
              dbClient.Execute("COMMIT;");
            scraper = null;
            if (StopScraper)
            {
              return num;
            }
            DoScrapeFanartTV(fm, externalAccess, false, Utils.FanartTV.MoviesPoster, Utils.FanartTV.MoviesBanner, Utils.FanartTV.MoviesCDArt, Utils.FanartTV.MoviesClearArt, Utils.FanartTV.MoviesClearLogo);
            DoScrapeAnimated(fm, externalAccess, false, Utils.Animated.MoviesPoster, Utils.Animated.MoviesBackground);
            return num;
          }
          catch (Exception ex)
          {
            scraper = null;
            logger.Error("DoScrapeMovies: " + ex);
            lock (lockObject)
              dbClient.Execute("ROLLBACK;");
          }
        }
        return 0;
      }
  
      public int DoScrapeMovieCollections(FanartMovieCollection fmc)
      {
        return DoScrapeMovieCollections(fmc, false);
      }
  
      public int DoScrapeMovieCollections(FanartMovieCollection fmc, bool externalAccess)
      {
        if (!StopScraper)
        {
          try
          {
            int num = 0;
            scraper = new Scraper();
            lock (lockObject)
              dbClient.Execute("BEGIN TRANSACTION;");
            num = scraper.GetMovieCollectionsFanart(fmc);
            lock (lockObject)
              dbClient.Execute("COMMIT;");
            scraper = null;
            if (StopScraper)
            {
              return num;
            }
            DoScrapeFanartTV(fmc, externalAccess, false, Utils.FanartTV.MoviesCollectionBackground, Utils.FanartTV.MoviesCollectionPoster, Utils.FanartTV.MoviesCollectionBanner, Utils.FanartTV.MoviesCollectionCDArt, Utils.FanartTV.MoviesCollectionClearArt, Utils.FanartTV.MoviesCollectionClearLogo);
            return num;
          }
          catch (Exception ex)
          {
            scraper = null;
            logger.Error("DoScrapeMovieCollections: " + ex);
            lock (lockObject)
              dbClient.Execute("ROLLBACK;");
          }
        }
        return 0;
      }
  
      public int DoScrapeSeries(string id, string tvdbid, string title, bool externalAccess)
      {
        FanartTVSeries fs = new FanartTVSeries(tvdbid, title);
        return DoScrapeSeries(fs, externalAccess);
      }
  
      public int DoScrapeSeries(FanartTVSeries fs, bool externalAccess)
      {
        if (!fs.HasTVDBID)
        {
          return 0;
        }
  
        if (!StopScraper)
        {
          try
          {
            int num = 0;
            scraper = new Scraper();
            lock (lockObject)
              dbClient.Execute("BEGIN TRANSACTION;");
            num = scraper.GetSeriesFanart(fs);
            lock (lockObject)
              dbClient.Execute("COMMIT;");
            scraper = null;
            if (StopScraper)
            {
              return num;
            }
            DoScrapeFanartTV(fs, externalAccess, false, Utils.FanartTV.SeriesBanner, Utils.FanartTV.SeriesClearArt, Utils.FanartTV.SeriesClearLogo, Utils.FanartTV.SeriesCDArt, Utils.FanartTV.SeriesSeasonBanner, Utils.FanartTV.SeriesSeasonCDArt);
            return num;
          }
          catch (Exception ex)
          {
            scraper = null;
            logger.Error("DoScrapeSeries: " + ex);
            lock (lockObject)
              dbClient.Execute("ROLLBACK;");
          }
        }
        return 0;
      }
  
      public int DoScrapeMusicLabels(FanartAlbum fa)
      {
        if (!Utils.MusicLabelDownload)
        {
          return 0;
        }
  
        if (!StopScraper)
        {
          try
          {
            return DoScrapeFanartTV(fa, Utils.FanartTV.MusicLabel);
          }
          catch (Exception ex)
          {
            logger.Error("DoScrapeMusicLabels: " + ex);
          }
        }
        return 0;
      }
  
      public int DoScrapeFanartTV(FanartClass key, params Utils.FanartTV[] fanartTypes)
      {
        return DoScrapeFanartTV(key, false, false, fanartTypes);
      }
  
      public int DoScrapeFanartTV(FanartClass key, bool externalAccess, bool triggerRefresh, params Utils.FanartTV[] fanartTypes)
      {
        if (fanartTypes == null || !fanartTypes.Any())
        {
          return 0;
        }
  
        if (!StopScraper)
        {
          try
          {
            int GetImages = 0;
  
            #region Check for Download is needed
            bool bFlagMusic = false;
            bool bFlagMusicAlbum = false;
            bool bFlagMusicLabel = false;
            bool bFlagMovie = false;
            bool bFlagCollection = false;
            bool bFlagSeries = false;
  
            foreach (Utils.FanartTV type in fanartTypes)
            {
              if (type == Utils.FanartTV.None)
              {
                continue;
              }
              // Music
              bFlagMusic = bFlagMusic || (type == Utils.FanartTV.MusicClearArt && Utils.MusicClearArtDownload && !Utils.FanartTVFileExists(((FanartArtist)key).Artist, null, null, type));
              bFlagMusic = bFlagMusic || (type == Utils.FanartTV.MusicBanner && Utils.MusicBannerDownload && !Utils.FanartTVFileExists(((FanartArtist)key).Artist, null, null, type));
              // Music Album
              if (type == Utils.FanartTV.MusicCDArt && Utils.MusicCDArtDownload)
              {
                FanartAlbum fa = (FanartAlbum)key;
                bFlagMusicAlbum = bFlagMusicAlbum || !Utils.FanartTVFileExists(fa.Artist, fa.Album, null, type);
                if (fa.CDs > 1)
                {
                  for (int i = 1; i <= fa.CDs; i++)
                  {
                    bFlagMusicAlbum = bFlagMusicAlbum || !Utils.FanartTVFileExists(fa.Artist, fa.Album, i.ToString(), type);
                  }
                }
              }
              // Music Label
              if (type == Utils.FanartTV.MusicLabel && Utils.MusicLabelDownload)
              {
                FanartAlbum fa = (FanartAlbum)key;
                if (fa.RecordLabel.IsEmpty)
                {
                  ((FanartAlbum)key).RecordLabel.SetRecordLabelFromDB(GetLabelIdNameForAlbum(fa.DBArtist, fa.DBAlbum));
                }
                bFlagMusicLabel = bFlagMusicLabel || !Utils.FanartTVFileExists(((FanartAlbum)key).RecordLabel.RecordLabel, null, null, type);
              }
              // Movie
              // bFlagMovie = bFlagMovie || (type == Utils.FanartTV.MoviesBackground && Utils.MoviesBackgroundDownload && !Utils.FanartTVFileExists(((FanartMovie)key).Id, null, null, type));
              bFlagMovie = bFlagMovie || (type == Utils.FanartTV.MoviesPoster && Utils.MoviesPosterDownload && !Utils.FanartTVFileExists(((FanartMovie)key).Title, ((FanartMovie)key).Id, null, type));
              bFlagMovie = bFlagMovie || (type == Utils.FanartTV.MoviesClearArt && Utils.MoviesClearArtDownload && !Utils.FanartTVFileExists(((FanartMovie)key).IMDBId, null, null, type));
              bFlagMovie = bFlagMovie || (type == Utils.FanartTV.MoviesBanner && Utils.MoviesBannerDownload && !Utils.FanartTVFileExists(((FanartMovie)key).IMDBId, null, null, type));
              bFlagMovie = bFlagMovie || (type == Utils.FanartTV.MoviesClearLogo && Utils.MoviesClearLogoDownload && !Utils.FanartTVFileExists(((FanartMovie)key).IMDBId, null, null, type));
              bFlagMovie = bFlagMovie || (type == Utils.FanartTV.MoviesCDArt && Utils.MoviesCDArtDownload && !Utils.FanartTVFileExists(((FanartMovie)key).IMDBId, null, null, type));
              // Movie Collection
              // bFlagCollection = bFlagCollection || (type == Utils.FanartTV.MoviesCollectionsBackground && Utils.MoviesCollectionsBackgroundDownload && !Utils.FanartTVFileExists(((FanartMovieCollection)key).Title, null, null, type));
              bFlagCollection = bFlagCollection || (type == Utils.FanartTV.MoviesCollectionPoster && Utils.MoviesCollectionPosterDownload && !Utils.FanartTVFileExists(((FanartMovieCollection)key).Title, null, null, type));
              bFlagCollection = bFlagCollection || (type == Utils.FanartTV.MoviesCollectionClearArt && Utils.MoviesCollectionClearArtDownload && !Utils.FanartTVFileExists(((FanartMovieCollection)key).Title, null, null, type));
              bFlagCollection = bFlagCollection || (type == Utils.FanartTV.MoviesCollectionBanner && Utils.MoviesCollectionBannerDownload && !Utils.FanartTVFileExists(((FanartMovieCollection)key).Title, null, null, type));
              bFlagCollection = bFlagCollection || (type == Utils.FanartTV.MoviesCollectionClearLogo && Utils.MoviesCollectionClearLogoDownload && !Utils.FanartTVFileExists(((FanartMovieCollection)key).Title, null, null, type));
              bFlagCollection = bFlagCollection || (type == Utils.FanartTV.MoviesCollectionCDArt && Utils.MoviesCollectionCDArtDownload && !Utils.FanartTVFileExists(((FanartMovieCollection)key).Title, null, null, type));
              // Series
              bFlagSeries = bFlagSeries || (type == Utils.FanartTV.SeriesClearArt && Utils.SeriesClearArtDownload && !Utils.FanartTVFileExists(((FanartTVSeries)key).Id, null, null, type));
              bFlagSeries = bFlagSeries || (type == Utils.FanartTV.SeriesBanner && Utils.SeriesBannerDownload && !Utils.FanartTVFileExists(((FanartTVSeries)key).Id, null, null, type));
              bFlagSeries = bFlagSeries || (type == Utils.FanartTV.SeriesClearLogo && Utils.SeriesClearLogoDownload && !Utils.FanartTVFileExists(((FanartTVSeries)key).Id, null, null, type));
              bFlagSeries = bFlagSeries || (type == Utils.FanartTV.SeriesCDArt && Utils.SeriesCDArtDownload && !Utils.FanartTVFileExists(((FanartTVSeries)key).Id, null, null, type));
              // Series.Season
              if (type == Utils.FanartTV.SeriesSeasonBanner || type == Utils.FanartTV.SeriesSeasonCDArt)
              {
                FanartTVSeries fs = (FanartTVSeries)key;
                if (!string.IsNullOrEmpty(fs.Seasons))
                {
                  string[] seasons = Utils.MultipleKeysToDistinctArray(fs.Seasons, true);
                  foreach (string season in seasons)
                  {
                    bFlagSeries = bFlagSeries || (type == Utils.FanartTV.SeriesSeasonBanner && Utils.SeriesSeasonBannerDownload && !Utils.FanartTVFileExists(fs.Id, null, season, type));
                    bFlagSeries = bFlagSeries || (type == Utils.FanartTV.SeriesSeasonCDArt && Utils.SeriesSeasonCDArtDownload && !Utils.FanartTVFileExists(fs.Id, null, season, type));
                  }
                }
              }
            }
            #endregion
  
            #region ClearArt Banner CDArt etc
            if (bFlagMusic)
            {
              scraper = new Scraper();
              GetImages = GetImages + scraper.GetArtistFanart((FanartArtist)key, false, triggerRefresh, externalAccess, true, Utils.WhatDownload.OnlyFanart);
              scraper = null;
            }
            if (StopScraper)
            {
              return GetImages;
            }
            if (bFlagMusicAlbum)
            {
              scraper = new Scraper();
              GetImages = GetImages + scraper.GetArtistAlbumThumbs((FanartAlbum)key, false, externalAccess, Utils.WhatDownload.OnlyFanart);
              scraper = null;
            }
            if (StopScraper)
            {
              return GetImages;
            }
            if (bFlagMusicLabel)
            {
              scraper = new Scraper();
              GetImages = GetImages + scraper.GetArtistAlbumLabels((FanartAlbum)key);
              scraper = null;
            }
            if (StopScraper)
            {
              return GetImages;
            }
            if (bFlagMovie)
            {
              scraper = new Scraper();
              GetImages = GetImages + scraper.GetMoviesFanart((FanartMovie)key, Utils.WhatDownload.OnlyFanart);
              scraper = null;
            }
            if (bFlagCollection)
            {
              scraper = new Scraper();
              GetImages = GetImages + scraper.GetMovieCollectionsFanart((FanartMovieCollection)key, Utils.WhatDownload.OnlyFanart);
              scraper = null;
            }
            if (StopScraper)
            {
              return GetImages;
            }
            if (bFlagSeries)
            {
              scraper = new Scraper();
              GetImages = GetImages + scraper.GetSeriesFanart((FanartTVSeries)key, Utils.WhatDownload.OnlyFanart);
              scraper = null;
            }
            #endregion
            return GetImages;
          }
          catch (Exception ex)
          {
            scraper = null;
            logger.Error("DoScrapeFanartTV: " + ex);
          }
        }
        return 0;
      }
  
      public int DoScrapeAnimated(FanartClass key, params Utils.Animated[] aniTypes)
      {
        return DoScrapeAnimated(key, false, false, aniTypes);
      }
  
      public int DoScrapeAnimated(FanartClass key, bool externalAccess, bool triggerRefresh, params Utils.Animated[] aniTypes)
      {
        if (aniTypes == null || !aniTypes.Any())
        {
          return 0;
        }
  
        if (!StopScraper)
        {
          try
          {
            int GetImages = 0;
  
            #region Check for Download is needed
            bool bFlagMovie = false;
  
            foreach (Utils.Animated type in aniTypes)
            {
              if (type == Utils.Animated.None)
              {
                continue;
              }
              // Movie
              bFlagMovie = bFlagMovie || (type == Utils.Animated.MoviesPoster && Utils.AnimatedMoviesPosterDownload && !Utils.AnimatedFileExists(((FanartMovie)key).IMDBId, null, null, type));
              bFlagMovie = bFlagMovie || (type == Utils.Animated.MoviesBackground && Utils.AnimatedMoviesBackgroundDownload && !Utils.AnimatedFileExists(((FanartMovie)key).IMDBId, null, null, type));
            }
            #endregion
  
            #region Animated
            if (bFlagMovie)
            {
              scraper = new Scraper();
              GetImages = GetImages + scraper.GetMoviesAnimated((FanartMovie)key);
              scraper = null;
            }
            if (StopScraper)
            {
              return GetImages;
            }
            #endregion
            return GetImages;
          }
          catch (Exception ex)
          {
            scraper = null;
            logger.Error("DoScrapeAnimated: " + ex);
          }
        }
        return 0;
      }
  
      public bool DoScrapeArtistInfo(FanartArtist fa)
      {
        if (!Utils.GetArtistInfo)
        {
          return false;
        }
  
        if (fa.IsEmpty)
        {
          return false;
        }
  
        if (!StopScraperInfo)
        {
          try
          {
            ArtistInfo aArtistInfo = new ArtistInfo();
            if (!m_db.GetArtistInfo(fa.Artist, ref aArtistInfo))
            {
              aArtistInfo.Artist = fa.Artist;
            }
  
            scraper = new Scraper();
            FanartArtistInfo fai = scraper.GetArtistInfo(fa);
            scraper = null;
  
            if (fai.IsEmpty)
            {
              return false;
            }
  
            if (!string.IsNullOrEmpty(fai.GetBio()))
            {
              if (string.IsNullOrEmpty(aArtistInfo.AMGBio) || (!string.IsNullOrEmpty(aArtistInfo.AMGBio) && aArtistInfo.AMGBio != fai.GetBio()))
              {
                aArtistInfo.AMGBio = fai.GetBio();
              }
            }
  
            if (string.IsNullOrEmpty(aArtistInfo.Born) && !string.IsNullOrEmpty(fai.Born))
            {
              aArtistInfo.Born = fai.Born + (!string.IsNullOrEmpty(fai.Country) ? " in " + fai.Country : "");
            }
  
            if (string.IsNullOrEmpty(aArtistInfo.Genres) && !string.IsNullOrEmpty(fai.Genre))
            {
              aArtistInfo.Genres = fai.Genre;
            }
  
            if (string.IsNullOrEmpty(aArtistInfo.Styles) && !string.IsNullOrEmpty(fai.Style))
            {
              aArtistInfo.Styles = fai.Style;
            }
  
            if (string.IsNullOrEmpty(aArtistInfo.Image) && !string.IsNullOrEmpty(fai.Thumb))
            {
              aArtistInfo.Image = fai.Thumb;
            }
  
            // Update Artist Info
            lock (lockObject)
              m_db.AddArtistInfo(aArtistInfo);
            return true;
          }
          catch (Exception ex)
          {
            logger.Error("DoScrapeArtistInfo: " + ex);
            scraper = null;
          }
        }
        return false;
      }
  
      public bool DoScrapeAlbumInfo(FanartAlbum fa)
      {
        if (!Utils.GetAlbumInfo)
        {
          return false;
        }
  
        if (fa.IsEmpty)
        {
          return false;
        }
  
        if (!StopScraperInfo)
        {
          try
          {
            AlbumInfo aAlbumInfo = new AlbumInfo();
            if (!m_db.GetAlbumInfo(fa.Album, fa.Artist, ref aAlbumInfo))
            {
              aAlbumInfo.Artist = fa.Artist;
              aAlbumInfo.Album = fa.Album;
              int year = 0;
              if (Int32.TryParse(fa.Year, out year))
              {
                aAlbumInfo.Year = year;
              }
            }
  
            scraper = new Scraper();
            FanartAlbumInfo fai = scraper.GetAlbumInfo(fa);
            scraper = null;
  
            if (fai.IsEmpty)
            {
              return false;
            }
  
            if (!string.IsNullOrEmpty(fai.GetDescription()))
            {
              if (string.IsNullOrEmpty(aAlbumInfo.Review) || (!string.IsNullOrEmpty(aAlbumInfo.Review) && aAlbumInfo.Review != fai.GetDescription()))
              {
                aAlbumInfo.Review = fai.GetDescription();
              }
            }
  
            if (string.IsNullOrEmpty(aAlbumInfo.Genre) && !string.IsNullOrEmpty(fai.Genre))
            {
              aAlbumInfo.Genre = fai.Genre;
            }
  
            if (string.IsNullOrEmpty(aAlbumInfo.Styles) && !string.IsNullOrEmpty(fai.Style))
            {
              aAlbumInfo.Styles = fai.Style;
            }
  
            if (string.IsNullOrEmpty(aAlbumInfo.Image) && !string.IsNullOrEmpty(fai.Thumb))
            {
              aAlbumInfo.Image = fai.Thumb;
            }
  
            if (aAlbumInfo.Year == 0 && !string.IsNullOrEmpty(fai.Year))
            {
              int year = 0;
              if (Int32.TryParse(fai.Year, out year))
              {
                aAlbumInfo.Year = year;
              }
            }
  
            // Update Album Info
            lock (lockObject)
              m_db.AddAlbumInfo(aAlbumInfo);
            return true;
          }
          catch (Exception ex)
          {
            logger.Error("DoScrapeAlbumInfo: " + ex);
            scraper = null;
          }
        }
        return false;
      }
      #endregion
  
      #region Initial Scrape
      public void InitialScrape()
      {
        CurrArtistsBeingScraped = 0.0;
        TotArtistsBeingScraped = 0.0;
        CurrTextBeingScraped = string.Empty;
        
        if (!MediaPortal.Util.Win32API.IsConnectedToInternet())
        {
          logger.Debug("InitialScrape: No internet connection detected. Cancelling initial scrape.");
          return;
        }
  
        string strDateTwoWeeksAgo = DateTime.Today.AddDays(-14.0).ToString(dbDateFormat, CultureInfo.CurrentCulture);
        try
        {
          logger.Info("InitialScrape is starting...");
          var flag = true;
  
          Utils.SetProperty("scraper.task", Translation.ScrapeInitial + " - " + Translation.ScrapeInitializing);
          if (FanartHandlerSetup.Fh.MyScraperWorker != null && FanartHandlerSetup.Fh.MyScraperWorker.Work)
          {
            FanartHandlerSetup.Fh.MyScraperWorker.ReportProgress(0, Utils.Progress.Start);
          }
  
          #region Artists
          if (Utils.ScrapeFanart && !StopScraper && !Utils.GetIsStopping())
          {
            TotArtistsBeingScraped = 0.0;
            CurrArtistsBeingScraped = 0.0;
  
            Utils.SetProperty("scraper.task", Translation.ScrapeInitial + " - " + Translation.FHArtists);
            if (FanartHandlerSetup.Fh.MyScraperWorker != null && FanartHandlerSetup.Fh.MyScraperWorker.Work)
            {
              FanartHandlerSetup.Fh.MyScraperWorker.ReportProgress(0, Utils.Progress.Start);
            }
  
            ArrayList musicDatabaseArtists = new ArrayList();
            m_db.GetAllArtists(ref musicDatabaseArtists);
  
            #region AlbumArtist
            var musicAlbumArtists = Utils.GetMusicAlbumArtists(m_db.DatabaseName);
            if (musicAlbumArtists != null && musicAlbumArtists.Count > 0)
            {
              logger.Debug("InitialScrape add Album Artists [" + musicAlbumArtists.Count + "]...");
              musicDatabaseArtists.AddRange(musicAlbumArtists);
            }
            #endregion
  
            #region mvCentral
            var musicVideoArtists = Utils.GetMusicVideoArtists("mvCentral.db3");
            if (musicVideoArtists != null && musicVideoArtists.Count > 0)
            {
              logger.Debug("InitialScrape add Artists from mvCentral [" + musicVideoArtists.Count + "]...");
              musicDatabaseArtists.AddRange(musicVideoArtists);
            }
            #endregion
  
            if (musicDatabaseArtists != null && musicDatabaseArtists.Count > 0)
            {
              CurrArtistsBeingScraped = 0.0;
              TotArtistsBeingScraped = (float)checked(musicDatabaseArtists.Count);
              logger.Debug("InitialScrape initiating for Artists...");
              var htFanart = new Hashtable();
  
              var SQL = "SELECT DISTINCT Key1, sum(Count) as Count FROM (" +
                          "SELECT Key1, count(Key1) as Count " +
                            "FROM Image " +
                            "WHERE SubCategory IN (" + Utils.GetMusicFanartCategoriesInStatement(Utils.UseHighDefThumbnails) + ") AND " +
                                  "Time_Stamp >= '" + strDateTwoWeeksAgo + "' " +
                            "GROUP BY Key1 " +
                          "UNION ALL " +
                          "SELECT Key1, count(Key1) as Count " +
                            "FROM Image " +
                            "WHERE SubCategory IN (" + Utils.GetMusicFanartCategoriesInStatement(Utils.UseHighDefThumbnails) + ") AND " +
                                  "Enabled = 1 AND " +
                                  // 3.7 
                                  "((iWidth >= " + Utils.MinWResolution + " AND iHeight >= " + Utils.MinHResolution + (Utils.UseAspectRatio ? " AND Ratio >= 1.3 " : "") + ") OR (iWidth IS NULL AND iHeight IS NULL)) AND "+
                                  "DummyItem = 0 " +
                            "GROUP BY Key1 " +
                            "HAVING count(key1) >= " + Utils.ScraperMaxImages +
                        ") GROUP BY Key1;";
  
              SQLiteResultSet sqLiteResultSet;
              lock (lockObject)
                sqLiteResultSet = dbClient.Execute(SQL);
              var num = 0;
              while (num < sqLiteResultSet.Rows.Count)
              {
                var htArtist = Utils.UndoArtistPrefix(sqLiteResultSet.GetField(num, 0)).ToUpperInvariant();
                if (!htFanart.Contains(htArtist))
                  htFanart.Add(htArtist, sqLiteResultSet.GetField(num, 1));
                checked { ++num; }
              }
              logger.Debug("InitialScrape Artists: [" + htFanart.Count + "]/[" + musicDatabaseArtists.Count + "]");
  
              var index = 0;
              while (index < musicDatabaseArtists.Count)
              {
                var artist = musicDatabaseArtists[index].ToString();
                CurrTextBeingScraped = artist;
  
                if (!StopScraper && !Utils.GetIsStopping())
                {
                  var dbartist = Utils.GetArtist(artist);
                  var htArtist = Utils.UndoArtistPrefix(dbartist).ToUpperInvariant();
                  if (!htFanart.Contains(htArtist))
                  {
                    if (DoScrapeArtist(new FanartArtist(artist)) > 0)
                    {
                      htFanart.Add(htArtist, 1);
                      if (flag && FanartHandlerSetup.Fh.MyScraperNowWorker != null)
                      {
                        FanartHandlerSetup.Fh.MyScraperNowWorker.TriggerRefresh = true;
                        flag = false;
                      }
                    }
                  }
                  // Pipes Artists
                  string[] artists = Utils.HandleMultipleKeysToArray(artist);
                  if (artists != null)
                  {
                    foreach (string sartist in artists)
                    {
                      if (!sartist.Equals(artist, StringComparison.CurrentCulture))
                      {
                        dbartist = Utils.GetArtist(sartist);
                        htArtist = Utils.UndoArtistPrefix(dbartist).ToUpperInvariant();
                        if (!htFanart.Contains(htArtist))
                        {
                          if (DoScrapeArtist(new FanartArtist(sartist)) > 0)
                          {
                            htFanart.Add(htArtist, 1);
                            if (flag && FanartHandlerSetup.Fh.MyScraperNowWorker != null)
                            {
                              FanartHandlerSetup.Fh.MyScraperNowWorker.TriggerRefresh = true;
                              flag = false;
                            }
                          }
                        }
                      }
                    }
                  }
                  //
                  #region Report
                  ++CurrArtistsBeingScraped;
                  if (FanartHandlerSetup.Fh.MyScraperWorker != null && FanartHandlerSetup.Fh.MyScraperWorker.Work)
                  {
                    FanartHandlerSetup.Fh.MyScraperWorker.ReportProgress(Utils.Percent(CurrArtistsBeingScraped, TotArtistsBeingScraped), Utils.Progress.Progress);
                  }
                  #endregion
                  checked { ++index; }
                }
                else
                  break;
              }
              #region Report
              if (FanartHandlerSetup.Fh.MyScraperWorker != null && FanartHandlerSetup.Fh.MyScraperWorker.Work)
              {
                FanartHandlerSetup.Fh.MyScraperWorker.ReportProgress(100, Utils.Progress.Done);
              }
              #endregion
              logger.Debug("InitialScrape done for Artists.");
            }
            CurrTextBeingScraped = string.Empty;
            musicDatabaseArtists = null;
            RefreshAnyFanart(Utils.Category.MusicFanart, Utils.SubCategory.MusicFanartScraped, false);
            RefreshAnyLatestsFanart(Utils.Latests.Music, false);
          }
          #endregion
  
          #region Albums
          if (Utils.ScrapeThumbnailsAlbum && !StopScraper && !Utils.GetIsStopping())
          {
            CurrArtistsBeingScraped = 0.0;
            TotArtistsBeingScraped = 0.0;
  
            Utils.SetProperty("scraper.task", Translation.ScrapeInitial + " - " + Translation.FHAlbums);
            if (FanartHandlerSetup.Fh.MyScraperWorker != null && FanartHandlerSetup.Fh.MyScraperWorker.Work)
            {
              FanartHandlerSetup.Fh.MyScraperWorker.ReportProgress(0, Utils.Progress.Start);
            }
  
            List<AlbumInfo>  musicDatabaseAlbums = new List<AlbumInfo>();
            m_db.GetAllAlbums(ref musicDatabaseAlbums);
  
            #region mvCentral
            var musicVideoAlbums = Utils.GetMusicVideoAlbums("mvCentral.db3");
            if (musicVideoAlbums != null && musicVideoAlbums.Count > 0)
            {
              logger.Debug("InitialScrape add Artists - Albums from mvCentral [" + musicVideoAlbums.Count + "]...");
              musicDatabaseAlbums.AddRange(musicVideoAlbums);
            }
            #endregion
  
            if (musicDatabaseAlbums != null && musicDatabaseAlbums.Count > 0)
            {
              CurrArtistsBeingScraped = 0.0;
              TotArtistsBeingScraped = (float)checked(musicDatabaseAlbums.Count);
              logger.Debug("InitialScrape initiating for Artists - Albums...");
              var htAlbums = new Hashtable();
  
              var SQL = "SELECT DISTINCT Key1, Key2, sum(Count) as Count FROM (" +
                          "SELECT Key1, Key2, count(Key1) as Count " +
                            "FROM Image " +
                            "WHERE SubCategory IN ('" + Utils.SubCategory.MusicAlbumThumbScraped + "') AND " +
                                  "Time_Stamp >= '" + strDateTwoWeeksAgo + "' " +
                            "GROUP BY Key1, Key2 " +
                          "UNION ALL " +
                          "SELECT Key1, Key2, count(Key1) as Count " +
                            "FROM Image " +
                            "WHERE SubCategory IN ('" + Utils.SubCategory.MusicAlbumThumbScraped + "') AND " +
                                  "Enabled = 1 AND " +
                                  "DummyItem = 0 " +
                            "GROUP BY Key1, Key2 " +
                        ") GROUP BY Key1, Key2;";
              SQLiteResultSet sqLiteResultSet;
              lock (lockObject)
                sqLiteResultSet = dbClient.Execute(SQL);
  
              var i = 0;
              while (i < sqLiteResultSet.Rows.Count)
              {
                var htArtistAlbum = Utils.UndoArtistPrefix(sqLiteResultSet.GetField(i, 0)).ToUpperInvariant() + "-" + sqLiteResultSet.GetField(i, 1).ToUpperInvariant();
                if (!htAlbums.Contains(htArtistAlbum))
                  htAlbums.Add(htArtistAlbum, sqLiteResultSet.GetField(i, 2));
                checked { ++i; }
              }
  
              logger.Debug("InitialScrape Artists - Albums: [" + htAlbums.Count + "]/[" + musicDatabaseAlbums.Count + "]");
  
              var index = 0;
              while (index < musicDatabaseAlbums.Count)
              {
                // logger.Debug("*** "+musicDatabaseAlbums[index].Artist+"/"+musicDatabaseAlbums[index].AlbumArtist+" - "+musicDatabaseAlbums[index].Album);
                var album = Utils.RemoveMPArtistPipe(musicDatabaseAlbums[index].Album).Trim();
                if (!string.IsNullOrWhiteSpace(album))
                {
                  var artist = Utils.RemoveMPArtistPipe(musicDatabaseAlbums[index].Artist).Trim();
                  var albumartist = Utils.RemoveMPArtistPipe(musicDatabaseAlbums[index].AlbumArtist).Trim();
                  var dbartist = Utils.UndoArtistPrefix(Utils.GetArtist(artist));
                  var dbalbum = Utils.GetAlbum(album).ToLower();
                  var htArtistAlbum = dbartist.ToUpperInvariant() + "-" + dbalbum.ToUpperInvariant();
  
                  int idiscs = GetDiscTotalAlbumInMPMusicDatabase(artist, albumartist, album);
                  // logger.Debug("*** "+artist+" / "+albumartist+" - "+album);
  
                  // Artist - Album
                  if (!string.IsNullOrEmpty(artist))
                    if (!htAlbums.Contains(htArtistAlbum))
                    {
                      if (!StopScraper && !Utils.GetIsStopping())
                      {
                        CurrTextBeingScraped = string.Format(Utils.MusicMask, artist, album);
                        // logger.Debug("*** 1 "+CurrTextBeingScraped); 
                        if (DoScrapeAlbumThumbs(new FanartAlbum(artist, album, idiscs), true, false) > 0)
                        {
                          htAlbums.Add(htArtistAlbum, 1);
                        }
                      }
                      else
                        break;
                    }
  
                  // AlbumArtist - Album
                  if (!string.IsNullOrEmpty(albumartist))
                    if (!albumartist.Equals(artist, StringComparison.InvariantCultureIgnoreCase))
                    {
                      dbartist = Utils.UndoArtistPrefix(Utils.GetArtist(albumartist));
                      htArtistAlbum = dbartist.ToUpperInvariant() + "-" + dbalbum.ToUpperInvariant();
                      if (!htAlbums.Contains(htArtistAlbum))
                      {
                        if (!StopScraper && !Utils.GetIsStopping())
                        {
                          CurrTextBeingScraped = string.Format(Utils.MusicMask, albumartist, album);
                          // logger.Debug("*** 2"+CurrTextBeingScraped); 
                          if (DoScrapeAlbumThumbs(new FanartAlbum(albumartist, album, idiscs), true, false) > 0)
                          {
                            htAlbums.Add(htArtistAlbum, 1);
                          }
                        }
                        else
                          break;
                      }
                    }
  
                  // Piped Artists
                  var pipedartist = musicDatabaseAlbums[index].Artist.Trim() + " | " + musicDatabaseAlbums[index].AlbumArtist.Trim();
                  string[] artists = Utils.HandleMultipleKeysToArray(pipedartist);
                  if (artists != null)
                  {
                    foreach (string sartist in artists)
                    {
                      dbartist = Utils.UndoArtistPrefix(Utils.GetArtist(sartist));
                      htArtistAlbum = dbartist.ToUpperInvariant() + "-" + dbalbum.ToUpperInvariant();
                      if (!htAlbums.Contains(htArtistAlbum))
                      {
                        if (!StopScraper && !Utils.GetIsStopping())
                        {
                          CurrTextBeingScraped = string.Format(Utils.MusicMask, sartist, album);
                          // logger.Debug("*** 3 "+CurrTextBeingScraped); 
                          if (DoScrapeAlbumThumbs(new FanartAlbum(sartist, album, idiscs), true, false) > 0)
                          {
                            htAlbums.Add(htArtistAlbum, 1);
                          }
                        }
                        else
                          break;
                      }
                    }
                  }
                }
                #region Report
                ++CurrArtistsBeingScraped;
                if (FanartHandlerSetup.Fh.MyScraperWorker != null && FanartHandlerSetup.Fh.MyScraperWorker.Work)
                {
                  FanartHandlerSetup.Fh.MyScraperWorker.ReportProgress(Utils.Percent(CurrArtistsBeingScraped, TotArtistsBeingScraped), Utils.Progress.Progress);
                }
                #endregion
                checked { ++index; }
              }
              #region Report
              if (FanartHandlerSetup.Fh.MyScraperWorker != null && FanartHandlerSetup.Fh.MyScraperWorker.Work)
              {
                FanartHandlerSetup.Fh.MyScraperWorker.ReportProgress(100, Utils.Progress.Done);
              }
              #endregion
              logger.Debug("InitialScrape done for Artists - Albums.");
            }
            CurrTextBeingScraped = string.Empty;
            musicDatabaseAlbums = null;
          }
          #endregion
  
          #region Movies
          if (Utils.UseVideoFanart && !StopScraper && !Utils.GetIsStopping())
          {
            CurrArtistsBeingScraped = 0.0;
            TotArtistsBeingScraped = 0.0;
  
            Utils.SetProperty("scraper.task", Translation.ScrapeInitial + " - " + Translation.FHVideos);
            if (FanartHandlerSetup.Fh.MyScraperWorker != null && FanartHandlerSetup.Fh.MyScraperWorker.Work)
            {
              FanartHandlerSetup.Fh.MyScraperWorker.ReportProgress(0, Utils.Progress.Start);
            }
  
            ArrayList videoDatabaseMovies = new ArrayList();
            VideoDatabase.GetMovies(ref videoDatabaseMovies);
  
            if (videoDatabaseMovies != null && videoDatabaseMovies.Count > 0)
            {
              CurrArtistsBeingScraped = 0.0;
              TotArtistsBeingScraped = (float)checked(videoDatabaseMovies.Count);
              logger.Debug("InitialScrape initiating for Movies (MyVideo)...");
              var htMovies = new Hashtable();
  
              var SQL = "SELECT DISTINCT Key1, sum(Count) as Count FROM (" +
                          "SELECT Key1, count(Key1) as Count " +
                            "FROM Image " +
                            "WHERE SubCategory in ('" + Utils.SubCategory.MovieScraped + "') AND " +
                                  "Time_Stamp >= '" + strDateTwoWeeksAgo + "' " +
                            "GROUP BY Key1 " +
                          "UNION ALL " +
                          "SELECT Key1, count(Key1) as Count " +
                            "FROM Image " +
                            "WHERE SubCategory in ('" + Utils.SubCategory.MovieScraped + "') AND " +
                                  "Enabled = 1 AND " +
                                  "DummyItem = 0 " +
                            "GROUP BY Key1 " +
                            "HAVING count(key1) >= " + Utils.ScraperMaxImages +
                        ") GROUP BY Key1;";
              SQLiteResultSet sqLiteResultSet;
              lock (lockObject)
                sqLiteResultSet = dbClient.Execute(SQL);
  
              var i = 0;
              while (i < sqLiteResultSet.Rows.Count)
              {
                var htMovie = sqLiteResultSet.GetField(i, 0).ToUpperInvariant();
                if (!htMovies.Contains(htMovie))
                  htMovies.Add(htMovie, sqLiteResultSet.GetField(i, 1));
                checked { ++i; }
              }
  
              logger.Debug("InitialScrape Movies: [" + htMovies.Count + "]/[" + videoDatabaseMovies.Count + "]");
  
              var index = 0;
              while (index < videoDatabaseMovies.Count)
              {
                IMDBMovie details = new IMDBMovie();
                details = (IMDBMovie)videoDatabaseMovies[index];
                var movieID = details.ID.ToString().ToUpperInvariant();
                var movieIMDBID = details.IMDBNumber.Trim().ToLower().Replace("unknown", string.Empty);
                var movieTitle = details.Title.Trim();
                CurrTextBeingScraped = movieIMDBID + " - " + movieTitle;
  
                if (!string.IsNullOrEmpty(movieID) && !string.IsNullOrEmpty(movieIMDBID))
                {
                  if (!htMovies.Contains(movieID))
                  {
                    if (!StopScraper && !Utils.GetIsStopping())
                    {
                      if (DoScrapeMovies(new FanartMovie(movieID, movieTitle, movieIMDBID)) > 0)
                      {
                        htMovies.Add(movieID,1);
                      }
                    }
                    else
                      break;
                  }
                }
                #region Report
                ++CurrArtistsBeingScraped;
                if (FanartHandlerSetup.Fh.MyScraperWorker != null && FanartHandlerSetup.Fh.MyScraperWorker.Work)
                {
                  FanartHandlerSetup.Fh.MyScraperWorker.ReportProgress(Utils.Percent(CurrArtistsBeingScraped, TotArtistsBeingScraped), Utils.Progress.Progress);
                }
                #endregion
                checked { ++index; }
              }
              #region Report
              if (FanartHandlerSetup.Fh.MyScraperWorker != null && FanartHandlerSetup.Fh.MyScraperWorker.Work)
              {
                FanartHandlerSetup.Fh.MyScraperWorker.ReportProgress(100, Utils.Progress.Done);
              }
              #endregion
              logger.Debug("InitialScrape done for Movies.");
            }
            CurrTextBeingScraped = string.Empty;
            videoDatabaseMovies = null;
          }
          RefreshAnyFanart(Utils.Category.Movie, Utils.SubCategory.MovieScraped, false);
          RefreshAnyLatestsFanart(Utils.Latests.Movies, false);
          #endregion
  
          #region Movies Collections
          if ((Utils.FanartTVNeedDownloadMoviesCollection || Utils.TheMovieDBMoviesCollectionNeedDownload) && !StopScraper && !Utils.GetIsStopping())
          {
            CurrArtistsBeingScraped = 0.0;
            TotArtistsBeingScraped = 0.0;
  
            Utils.SetProperty("scraper.task", Translation.ScrapeInitial + " - " + Translation.FHCollections);
            if (FanartHandlerSetup.Fh.MyScraperWorker != null && FanartHandlerSetup.Fh.MyScraperWorker.Work)
            {
              FanartHandlerSetup.Fh.MyScraperWorker.ReportProgress(0, Utils.Progress.Start);
            }
  
            ArrayList videoDatabaseMovies = new ArrayList();
            VideoDatabase.GetCollections(videoDatabaseMovies);
  
            if (videoDatabaseMovies != null && videoDatabaseMovies.Count > 0)
            {
              CurrArtistsBeingScraped = 0.0;
              TotArtistsBeingScraped = (float)checked(videoDatabaseMovies.Count);
              logger.Debug("InitialScrape initiating for Movies (Collections)...");
              var htMovies = new Hashtable();
  
              var SQL = "SELECT DISTINCT Name FROM MovieCollections WHERE Time_Stamp >= '" + strDateTwoWeeksAgo + "';";
              SQLiteResultSet sqLiteResultSet;
              lock (lockObject)
                sqLiteResultSet = dbClient.Execute(SQL);
  
              var i = 0;
              while (i < sqLiteResultSet.Rows.Count)
              {
                var htMovie = sqLiteResultSet.GetField(i, 0).ToUpperInvariant();
                if (!htMovies.Contains(htMovie))
                  htMovies.Add(htMovie, 1);
                checked { ++i; }
              }
  
              logger.Debug("InitialScrape Movies (Collections): [" + htMovies.Count + "]/[" + videoDatabaseMovies.Count + "]");
  
              var index = 0;
              while (index < videoDatabaseMovies.Count)
              {
                string collectionTitle = (string)videoDatabaseMovies[index];
                CurrTextBeingScraped = collectionTitle;
  
                if (!string.IsNullOrEmpty(collectionTitle))
                {
                  string htCollection = collectionTitle.ToUpperInvariant();
                  if (!htMovies.Contains(htCollection))
                  {
                    if (!StopScraper && !Utils.GetIsStopping())
                    {
                      if (DoScrapeMovieCollections(new FanartMovieCollection(collectionTitle)) > 0)
                      {
                        htMovies.Add(htCollection, 1);
                      }
                    }
                    else
                      break;
                  }
                }
                #region Report
                ++CurrArtistsBeingScraped;
                if (FanartHandlerSetup.Fh.MyScraperWorker != null && FanartHandlerSetup.Fh.MyScraperWorker.Work)
                {
                  FanartHandlerSetup.Fh.MyScraperWorker.ReportProgress(Utils.Percent(CurrArtistsBeingScraped, TotArtistsBeingScraped), Utils.Progress.Progress);
                }
                #endregion
                checked { ++index; }
              }
              #region Report
              if (FanartHandlerSetup.Fh.MyScraperWorker != null && FanartHandlerSetup.Fh.MyScraperWorker.Work)
              {
                FanartHandlerSetup.Fh.MyScraperWorker.ReportProgress(100, Utils.Progress.Done);
              }
              #endregion
              logger.Debug("InitialScrape done for Movies (Collections).");
            }
            CurrTextBeingScraped = string.Empty;
            videoDatabaseMovies = null;
          }
          RefreshAnyFanart(Utils.Category.Movie, Utils.SubCategory.MovieScraped, false);
          RefreshAnyLatestsFanart(Utils.Latests.Movies, false);
          #endregion
  
          logger.Info("InitialScrape is done.");
        }
        catch (Exception ex)
        {
          logger.Error("InitialScrape: " + ex);
        }
      }
  
      public void InitialScrapeFanart()
      {
        InitialScrapeFanart(Utils.SubCategory.None); // Scrape All
      }
  
      public void InitialScrapeFanart(Utils.SubCategory param)
      {
        bool All = (param == Utils.SubCategory.None);
        string Text = (All ? "InitialScrapeFanart" : "ScrapeFanart [" + param + "]") + " ";
  
        CurrArtistsBeingScraped = 0.0;
        TotArtistsBeingScraped = 0.0;
        CurrTextBeingScraped = string.Empty;
  
        if (!MediaPortal.Util.Win32API.IsConnectedToInternet())
        {
          logger.Debug(Text + "No internet connection detected. Cancelling initial scrape.");
          return;
        }
  
        Utils.SetProperty("scraper.task", Translation.ScrapeInitialFanart + " - " + Translation.ScrapeInitializing);
        if (FanartHandlerSetup.Fh.MyScraperWorker != null && FanartHandlerSetup.Fh.MyScraperWorker.Work)
        {
          FanartHandlerSetup.Fh.MyScraperWorker.ReportProgress(0, Utils.Progress.Start);
        }
  
        string strDateTwoWeeksAgo = DateTime.Today.AddDays(-14.0).ToString(dbDateFormat, CultureInfo.CurrentCulture);
  
        logger.Info(Text + "is starting...");
        try
        {
          #region Artists
          if ((All || param == Utils.SubCategory.FanartTVArtist) && Utils.FanartTVNeedDownloadArtist && !StopScraper && !Utils.GetIsStopping())
          {
            CurrArtistsBeingScraped = 0.0;
            TotArtistsBeingScraped = 0.0;
  
            Utils.SetProperty("scraper.task", Translation.ScrapeInitialFanart + " - " + Translation.FHArtists);
            if (FanartHandlerSetup.Fh.MyScraperWorker != null && FanartHandlerSetup.Fh.MyScraperWorker.Work)
            {
              FanartHandlerSetup.Fh.MyScraperWorker.ReportProgress(0, Utils.Progress.Start);
            }
  
            ArrayList musicDatabaseArtists = new ArrayList();
            m_db.GetAllArtists(ref musicDatabaseArtists);
  
            #region AlbumArtist
            var musicAlbumArtists = Utils.GetMusicAlbumArtists(m_db.DatabaseName);
            if (musicAlbumArtists != null && musicAlbumArtists.Count > 0)
            {
              logger.Debug(Text + "add Album Artists [" + musicAlbumArtists.Count + "]...");
              musicDatabaseArtists.AddRange(musicAlbumArtists);
            }
            #endregion
  
            #region mvCentral
            var musicVideoArtists = Utils.GetMusicVideoArtists("mvCentral.db3");
            if (musicVideoArtists != null && musicVideoArtists.Count > 0)
            {
              logger.Debug(Text + "add Artists from mvCentral [" + musicVideoArtists.Count + "]...");
              musicDatabaseArtists.AddRange(musicVideoArtists);
            }
            #endregion
  
            if (musicDatabaseArtists != null && musicDatabaseArtists.Count > 0)
            {
              CurrArtistsBeingScraped = 0.0;
              TotArtistsBeingScraped = (float)checked(musicDatabaseArtists.Count);
  
              logger.Debug(Text + "initiating for Artists...");
              var htFanart = new Hashtable();
              var SQL = "SELECT DISTINCT Key1, count(Key1) as Count " +
                          "FROM Image " +
                          "WHERE SubCategory IN ('" + Utils.SubCategory.FanartTVArtist + "') AND " +
                                "Time_Stamp >= '" + strDateTwoWeeksAgo + "' " +
                          "GROUP BY Key1;";
              SQLiteResultSet sqLiteResultSet;
              lock (lockObject)
                sqLiteResultSet = dbClient.Execute(SQL);
              var i = 0;
              while (i < sqLiteResultSet.Rows.Count)
              {
                var htArtist = Utils.UndoArtistPrefix(sqLiteResultSet.GetField(i, 0)).ToUpperInvariant();
                if (!htFanart.Contains(htArtist))
                  htFanart.Add(htArtist, sqLiteResultSet.GetField(i, 1));
                checked { ++i; }
              }
              logger.Debug(Text + "Artists: [" + htFanart.Count + "]/[" + musicDatabaseArtists.Count + "]");
  
              var index = 0;
              while (index < musicDatabaseArtists.Count)
              {
                var artist = musicDatabaseArtists[index].ToString();
                if (!string.IsNullOrEmpty(artist))
                {
                  artist = artist.Trim();
                  CurrTextBeingScraped = artist;
  
                  if (!StopScraper && !Utils.GetIsStopping())
                  {
                    var htArtist = Utils.UndoArtistPrefix(Utils.GetArtist(artist)).ToUpperInvariant();
                    if (!htFanart.Contains(htArtist))
                    {
                      if (DoScrapeFanartTV(new FanartArtist(artist), Utils.FanartTV.MusicBanner, Utils.FanartTV.MusicClearArt) > 0)
                      {
                        htFanart.Add(htArtist,1);
                      }
                    }
                    else
                      break;
                  }
  
                  // Piped Artist
                  string[] artists = Utils.HandleMultipleKeysToArray(artist);
                  if (artists != null)
                  {
                    foreach (string sartist in artists)
                    {
                      if (!StopScraper && !Utils.GetIsStopping())
                      {
                        var htArtist = Utils.UndoArtistPrefix(Utils.GetArtist(sartist)).ToUpperInvariant();
                        if (!htFanart.Contains(htArtist))
                        {
                          if (DoScrapeFanartTV(new FanartArtist(sartist), Utils.FanartTV.MusicBanner, Utils.FanartTV.MusicClearArt) > 0)
                          {
                            htFanart.Add(htArtist, 1);
                          }
                        }
                      }
                      else
                        break;
                    }
                  }
                }
                #region Report
                ++CurrArtistsBeingScraped;
                if (FanartHandlerSetup.Fh.MyScraperWorker != null && FanartHandlerSetup.Fh.MyScraperWorker.Work)
                {
                  FanartHandlerSetup.Fh.MyScraperWorker.ReportProgress(Utils.Percent(CurrArtistsBeingScraped, TotArtistsBeingScraped), Utils.Progress.Progress);
                }
                #endregion
                // logger.Debug("*** {0}/{1}", CurrArtistsBeingScraped, TotArtistsBeingScraped);
                checked { ++index; }
              }
              #region Report
              if (FanartHandlerSetup.Fh.MyScraperWorker != null && FanartHandlerSetup.Fh.MyScraperWorker.Work)
              {
                FanartHandlerSetup.Fh.MyScraperWorker.ReportProgress(100, Utils.Progress.Done);
              }
              #endregion
              logger.Debug(Text + "done for Artists.");
            }
            CurrTextBeingScraped = string.Empty;
            musicDatabaseArtists = null;
          }
          #endregion
  
          #region Albums
          if ((All || param == Utils.SubCategory.FanartTVAlbum) && Utils.FanartTVNeedDownloadAlbum && !StopScraper && !Utils.GetIsStopping())
          {
            CurrArtistsBeingScraped = 0.0;
            TotArtistsBeingScraped = 0.0;
  
            Utils.SetProperty("scraper.task", Translation.ScrapeInitialFanart + " - " + Translation.FHAlbums);
            if (FanartHandlerSetup.Fh.MyScraperWorker != null && FanartHandlerSetup.Fh.MyScraperWorker.Work)
            {
              FanartHandlerSetup.Fh.MyScraperWorker.ReportProgress(0, Utils.Progress.Start);
            }
  
            List<AlbumInfo> musicDatabaseAlbums = new List<AlbumInfo>();
            m_db.GetAllAlbums(ref musicDatabaseAlbums);
  
            #region mvCentral
            var musicVideoAlbums = Utils.GetMusicVideoAlbums("mvCentral.db3");
            if (musicVideoAlbums != null && musicVideoAlbums.Count > 0)
            {
              logger.Debug(Text + "add Artists - Albums from mvCentral [" + musicVideoAlbums.Count + "]...");
              musicDatabaseAlbums.AddRange(musicVideoAlbums);
            }
            #endregion
  
            if (musicDatabaseAlbums != null && musicDatabaseAlbums.Count > 0)
            {
              CurrArtistsBeingScraped = 0.0;
              TotArtistsBeingScraped = (float)checked(musicDatabaseAlbums.Count);
  
              logger.Debug(Text + "initiating for Artists - Albums...");
              var htAlbums = new Hashtable();
              var SQL = "SELECT DISTINCT Key1, Key2, count(Key1) as Count " +
                          "FROM Image " +
                          "WHERE SubCategory IN ('" + Utils.SubCategory.FanartTVAlbum + "') AND " +
                                "Time_Stamp >= '" + strDateTwoWeeksAgo + "' " +
                          "GROUP BY Key1, Key2;";
              SQLiteResultSet sqLiteResultSet;
              lock (lockObject)
                sqLiteResultSet = dbClient.Execute(SQL);
              var i = 0;
              while (i < sqLiteResultSet.Rows.Count)
              {
                var htArtistAlbum = Utils.UndoArtistPrefix(sqLiteResultSet.GetField(i, 0)).ToUpperInvariant() + "-" + sqLiteResultSet.GetField(i, 1).ToUpperInvariant();
                if (!htAlbums.Contains(htArtistAlbum))
                  htAlbums.Add(htArtistAlbum, sqLiteResultSet.GetField(i, 2));
                checked { ++i; }
              }
              logger.Debug(Text + "Artists - Albums: [" + htAlbums.Count + "]/[" + musicDatabaseAlbums.Count + "]");
  
              var index = 0;
              while (index < musicDatabaseAlbums.Count)
              {
                var album = musicDatabaseAlbums[index].Album.Trim();
                if (!string.IsNullOrWhiteSpace(album))
                {
                  var artist = Utils.RemoveMPArtistPipe(musicDatabaseAlbums[index].Artist).Trim();
                  var albumartist = Utils.RemoveMPArtistPipe(musicDatabaseAlbums[index].AlbumArtist).Trim();
                  var dbalbum = Utils.GetAlbum(album).ToLower();
                  int idiscs = GetDiscTotalAlbumInMPMusicDatabase(artist, albumartist, album);
  
                  // Artist - Album
                  var htArtistAlbum = Utils.UndoArtistPrefix(Utils.GetArtist(artist)).ToUpperInvariant() + "-" + dbalbum.ToUpperInvariant();
  
                  if (!string.IsNullOrEmpty(artist))
                  {
                    if (!StopScraper && !Utils.GetIsStopping())
                    {
                      if (!htAlbums.Contains(htArtistAlbum))
                      {
                        CurrTextBeingScraped = string.Format(Utils.MusicMask, artist, album);
                        if (DoScrapeFanartTV(new FanartAlbum(artist, album, idiscs), Utils.FanartTV.MusicCDArt) > 0)
                        {
                          htAlbums.Add(htArtistAlbum, 1);
                        }
                      }
                    }
                    else
                      break;
                  }
  
                  // AlbumArtist - Album
                  if (!string.IsNullOrEmpty(albumartist))
                  {
                    if (!albumartist.Equals(artist, StringComparison.InvariantCultureIgnoreCase))
                    {
                      htArtistAlbum = Utils.UndoArtistPrefix(Utils.GetArtist(albumartist)).ToUpperInvariant() + "-" + dbalbum.ToUpperInvariant();
  
                      if (!StopScraper && !Utils.GetIsStopping())
                      {
                        if (!htAlbums.Contains(htArtistAlbum))
                        {
                          CurrTextBeingScraped = string.Format(Utils.MusicMask, albumartist, album);
                          if (DoScrapeFanartTV(new FanartAlbum(albumartist, album, idiscs), Utils.FanartTV.MusicCDArt) > 0)
                          {
                            htAlbums.Add(htArtistAlbum, 1);
                          }
                        }
                      }
                      else
                        break;
                    }
                  }
  
                  // Piped Artists
                  var pipedartist = musicDatabaseAlbums[index].Artist.Trim() + " | " + musicDatabaseAlbums[index].AlbumArtist.Trim();
                  string[] artists = Utils.HandleMultipleKeysToArray(pipedartist);
                  if (artists != null)
                  {
                    foreach (string sartist in artists)
                    {
                      string wartist = sartist.Trim();
                      htArtistAlbum = Utils.UndoArtistPrefix(Utils.GetArtist(wartist)).ToUpperInvariant() + "-" + dbalbum.ToUpperInvariant();
  
                      if (!StopScraper && !Utils.GetIsStopping())
                      {
                        if (!htAlbums.Contains(htArtistAlbum))
                        {
                          CurrTextBeingScraped = string.Format(Utils.MusicMask, wartist, album);
                          if (DoScrapeFanartTV(new FanartAlbum(wartist, album, idiscs), Utils.FanartTV.MusicCDArt) > 0)
                          {
                            htAlbums.Add(htArtistAlbum, 1);
                          }
                        }
                      }
                      else
                        break;
                    }
                  }
                }
                if (StopScraper || Utils.GetIsStopping())
                {
                  break;
                }
  
                #region Report
                ++CurrArtistsBeingScraped;
                if (FanartHandlerSetup.Fh.MyScraperWorker != null && FanartHandlerSetup.Fh.MyScraperWorker.Work)
                {
                  FanartHandlerSetup.Fh.MyScraperWorker.ReportProgress(Utils.Percent(CurrArtistsBeingScraped, TotArtistsBeingScraped), Utils.Progress.Progress);
                }
                #endregion
                checked { ++index; }
              }
              #region Report
              if (FanartHandlerSetup.Fh.MyScraperWorker != null && FanartHandlerSetup.Fh.MyScraperWorker.Work)
              {
                FanartHandlerSetup.Fh.MyScraperWorker.ReportProgress(100, Utils.Progress.Done);
              }
              #endregion
              logger.Debug(Text + "done for Artists - Albums.");
            }
            CurrTextBeingScraped = string.Empty;
            musicDatabaseAlbums = null;
          }
          #endregion
  
          #region Movies
          if ((All || param == Utils.SubCategory.FanartTVMovie) && Utils.FanartTVNeedDownloadMovies && !StopScraper && !Utils.GetIsStopping())
          {
            string MoviesText = string.Empty;
  
            CurrArtistsBeingScraped = 0.0;
            TotArtistsBeingScraped = 0.0;
  
            Utils.SetProperty("scraper.task", Translation.ScrapeInitialFanart + " - " + Translation.FHVideos);
            if (FanartHandlerSetup.Fh.MyScraperWorker != null && FanartHandlerSetup.Fh.MyScraperWorker.Work)
            {
              FanartHandlerSetup.Fh.MyScraperWorker.ReportProgress(0, Utils.Progress.Start);
            }
  
            ArrayList videoDatabaseMovies = new ArrayList();
            VideoDatabase.GetMovies(ref videoDatabaseMovies);
            MoviesText = "MyVideo";
  
            if (Utils.MovingPicturesEnabled)
            {
              logger.Debug(Text + "Add MovingPictures movies...");
              UtilsMovingPictures.GetMovingPicturesMoviesList(ref videoDatabaseMovies);
              MoviesText = MoviesText + (string.IsNullOrEmpty(MoviesText) ? string.Empty : "/") + "MovingPictures";
            }
            if (Utils.MyFilmsEnabled)
            {
              logger.Debug(Text + "Add MyFilms movies...");
              UtilsMyFilms.GetMyFilmsMoviesList(ref videoDatabaseMovies);
              MoviesText = MoviesText + (string.IsNullOrEmpty(MoviesText) ? string.Empty : "/") + "MyFilms";
            }
  
            if (videoDatabaseMovies != null && videoDatabaseMovies.Count > 0)
            {
              CurrArtistsBeingScraped = 0.0;
              TotArtistsBeingScraped = (float)checked(videoDatabaseMovies.Count);
  
              logger.Debug(Text + "initiating for Movies (" + MoviesText + ")...");
              var htMovies = new Hashtable();
              var SQL = "SELECT DISTINCT Key1, count(Key1) as Count " +
                          "FROM Image " +
                          "WHERE SubCategory IN ('" + Utils.SubCategory.FanartTVMovie + "') AND " +
                                "Time_Stamp >= '" + strDateTwoWeeksAgo + "' " +
                          "GROUP BY Key1;";
              SQLiteResultSet sqLiteResultSet;
              lock (lockObject)
                sqLiteResultSet = dbClient.Execute(SQL);
              var i = 0;
              while (i < sqLiteResultSet.Rows.Count)
              {
                var htMovie = sqLiteResultSet.GetField(i, 0).ToUpperInvariant();
                if (!htMovies.Contains(htMovie))
                  htMovies.Add(htMovie, sqLiteResultSet.GetField(i, 1));
                checked { ++i; }
              }
              logger.Debug(Text + "Movies: [" + htMovies.Count + "]/[" + videoDatabaseMovies.Count + "]");
  
              var index = 0;
              while (index < videoDatabaseMovies.Count)
              {
                IMDBMovie details = new IMDBMovie();
                details = (IMDBMovie)videoDatabaseMovies[index];
                var movieID = details.ID.ToString();
                var movieIMDBID = details.IMDBNumber.Trim().ToLower().Replace("unknown", string.Empty).ToUpperInvariant();
                var movieTitle = details.Title.Trim();
                CurrTextBeingScraped = movieIMDBID + " - " + movieTitle;
  
                if (!string.IsNullOrEmpty(movieID) && !string.IsNullOrEmpty(movieIMDBID))
                {
                  if (!StopScraper && !Utils.GetIsStopping())
                  {
                    if (!htMovies.Contains(movieIMDBID))
                    {
                      if (DoScrapeFanartTV(new FanartMovie(movieID, movieTitle, movieIMDBID), false, false, Utils.FanartTV.MoviesPoster, Utils.FanartTV.MoviesBanner, Utils.FanartTV.MoviesCDArt, Utils.FanartTV.MoviesClearArt, Utils.FanartTV.MoviesClearLogo) > 0)
                      {
                        htMovies.Add(movieIMDBID, 1);
                      }
                    }
                  }
                  else
                    break;
                }
                #region Report
                ++CurrArtistsBeingScraped;
                if (FanartHandlerSetup.Fh.MyScraperWorker != null && FanartHandlerSetup.Fh.MyScraperWorker.Work)
                {
                  FanartHandlerSetup.Fh.MyScraperWorker.ReportProgress(Utils.Percent(CurrArtistsBeingScraped, TotArtistsBeingScraped), Utils.Progress.Progress);
                }
                #endregion
                // logger.Debug("*** {0}/{1}", CurrArtistsBeingScraped, TotArtistsBeingScraped);
                checked { ++index; }
              }
              #region Report
              if (FanartHandlerSetup.Fh.MyScraperWorker != null && FanartHandlerSetup.Fh.MyScraperWorker.Work)
              {
                FanartHandlerSetup.Fh.MyScraperWorker.ReportProgress(100, Utils.Progress.Done);
              }
              #endregion
              logger.Debug(Text + "done for Movies.");
            }
            CurrTextBeingScraped = string.Empty;
            videoDatabaseMovies = null;
          }
          #endregion
  
          #region Movies Collections
          if ((All || param == Utils.SubCategory.FanartTVMovie || param == Utils.SubCategory.MovieCollection) && Utils.FanartTVNeedDownloadMoviesCollection && !StopScraper && !Utils.GetIsStopping())
          {
            CurrArtistsBeingScraped = 0.0;
            TotArtistsBeingScraped = 0.0;
  
            Utils.SetProperty("scraper.task", Translation.ScrapeInitialFanart + " - " + Translation.FHCollections);
            if (FanartHandlerSetup.Fh.MyScraperWorker != null && FanartHandlerSetup.Fh.MyScraperWorker.Work)
            {
              FanartHandlerSetup.Fh.MyScraperWorker.ReportProgress(0, Utils.Progress.Start);
            }
  
            ArrayList videoDatabaseMovies = new ArrayList();
            VideoDatabase.GetCollections(videoDatabaseMovies);
  
            if (videoDatabaseMovies != null && videoDatabaseMovies.Count > 0)
            {
              CurrArtistsBeingScraped = 0.0;
              TotArtistsBeingScraped = (float)checked(videoDatabaseMovies.Count);
              logger.Debug(Text + "initiating for Movies (Collections)...");
              var htMovies = new Hashtable();
  
              var SQL = "SELECT DISTINCT Name FROM MovieCollections WHERE Time_Stamp >= '" + strDateTwoWeeksAgo + "';";
              SQLiteResultSet sqLiteResultSet;
              lock (lockObject)
                sqLiteResultSet = dbClient.Execute(SQL);
  
              var i = 0;
              while (i < sqLiteResultSet.Rows.Count)
              {
                var htMovie = sqLiteResultSet.GetField(i, 0).ToUpperInvariant();
                if (!htMovies.Contains(htMovie))
                  htMovies.Add(htMovie, 1);
                checked { ++i; }
              }
  
              logger.Debug(Text + "Movies (Collections): [" + htMovies.Count + "]/[" + videoDatabaseMovies.Count + "]");
  
              var index = 0;
              while (index < videoDatabaseMovies.Count)
              {
                string collectionTitle = (string)videoDatabaseMovies[index];
                CurrTextBeingScraped = collectionTitle;
  
                if (!string.IsNullOrEmpty(collectionTitle))
                {
                  string htCollection = collectionTitle.ToUpperInvariant();
                  if (!htMovies.Contains(htCollection))
                  {
                    if (!StopScraper && !Utils.GetIsStopping())
                    {
                      if (DoScrapeFanartTV(new FanartMovieCollection(collectionTitle), false, false, Utils.FanartTV.MoviesCollectionBackground, Utils.FanartTV.MoviesCollectionPoster, Utils.FanartTV.MoviesCollectionBanner, Utils.FanartTV.MoviesCollectionCDArt, Utils.FanartTV.MoviesCollectionClearArt, Utils.FanartTV.MoviesCollectionClearLogo) > 0)
                      {
                        htMovies.Add(htCollection, 1);
                      }
                    }
                    else
                      break;
                  }
                }
                #region Report
                ++CurrArtistsBeingScraped;
                if (FanartHandlerSetup.Fh.MyScraperWorker != null && FanartHandlerSetup.Fh.MyScraperWorker.Work)
                {
                  FanartHandlerSetup.Fh.MyScraperWorker.ReportProgress(Utils.Percent(CurrArtistsBeingScraped, TotArtistsBeingScraped), Utils.Progress.Progress);
                }
                #endregion
                checked { ++index; }
              }
              #region Report
              if (FanartHandlerSetup.Fh.MyScraperWorker != null && FanartHandlerSetup.Fh.MyScraperWorker.Work)
              {
                FanartHandlerSetup.Fh.MyScraperWorker.ReportProgress(100, Utils.Progress.Done);
              }
              #endregion
              logger.Debug("InitialScrape done for Movies (Collections).");
            }
            CurrTextBeingScraped = string.Empty;
            videoDatabaseMovies = null;
          }
          RefreshAnyFanart(Utils.Category.Movie, Utils.SubCategory.MovieScraped, false);
          RefreshAnyLatestsFanart(Utils.Latests.Movies, false);
          #endregion
  
          #region Series
          if ((All || param == Utils.SubCategory.FanartTVSeries) && Utils.FanartTVNeedDownloadSeries && !StopScraper && !Utils.GetIsStopping())
          {
            CurrArtistsBeingScraped = 0.0;
            TotArtistsBeingScraped = 0.0;
  
            Utils.SetProperty("scraper.task", Translation.ScrapeInitialFanart + " - " + Translation.FHSeries);
            if (FanartHandlerSetup.Fh.MyScraperWorker != null && FanartHandlerSetup.Fh.MyScraperWorker.Work)
            {
              FanartHandlerSetup.Fh.MyScraperWorker.ReportProgress(0, Utils.Progress.Start);
            }
  
            var tvsDatabaseSeries = UtilsTVSeries.GetTVSeries(Utils.Category.TVSeries, Utils.SubCategory.TVSeriesScraped);
  
            if (tvsDatabaseSeries != null && tvsDatabaseSeries.Count > 0)
            {
              CurrArtistsBeingScraped = 0.0;
              TotArtistsBeingScraped = (float)checked(tvsDatabaseSeries.Count);
  
              logger.Debug(Text + "initiating for TV-Series...");
              var htSeries = new Hashtable();
              var SQL = "SELECT DISTINCT Key1, count(Key1) as Count " +
                          "FROM Image " +
                          "WHERE SubCategory IN ('" + Utils.SubCategory.FanartTVSeries + "') AND " +
                                "Time_Stamp >= '" + strDateTwoWeeksAgo + "' " +
                          "GROUP BY Key1;";
              SQLiteResultSet sqLiteResultSet;
              lock (lockObject)
                sqLiteResultSet = dbClient.Execute(SQL);
              var i = 0;
              while (i < sqLiteResultSet.Rows.Count)
              {
                var htSerie = sqLiteResultSet.GetField(i, 0).ToUpperInvariant();
                if (!htSeries.Contains(htSerie))
                  htSeries.Add(htSerie, sqLiteResultSet.GetField(i, 1));
                checked { ++i; }
              }
              logger.Debug(Text + "Series: [" + htSeries.Count + "]/[" + tvsDatabaseSeries.Count + "]");
  
              lock (tvsDatabaseSeries)
              {
                foreach (DictionaryEntry Serie in tvsDatabaseSeries)
                {
                  var serieID = Serie.Key.ToString().ToUpperInvariant();
                  var serieTitle = ((FanartTVSeries)Serie.Value).Name.Trim();
                  var serieSeasons = ((FanartTVSeries)Serie.Value).Seasons.Trim();
                  CurrTextBeingScraped = serieID + " - " + serieTitle + " " + serieSeasons;
  
                  if (!string.IsNullOrEmpty(serieID))
                  {
                    if (!StopScraper && !Utils.GetIsStopping())
                    {
                      if (!htSeries.Contains(serieID))
                      {
                        if (DoScrapeFanartTV((FanartTVSeries)Serie.Value, false, false, Utils.FanartTV.SeriesBanner, Utils.FanartTV.SeriesClearArt, Utils.FanartTV.SeriesClearLogo, Utils.FanartTV.SeriesCDArt, Utils.FanartTV.SeriesSeasonBanner, Utils.FanartTV.SeriesSeasonCDArt) > 0)
                        {
                          htSeries.Add(serieID, 1);
                        }
                      }
                    }
                    else
                      break;
                  }
                  #region Report
                  ++CurrArtistsBeingScraped;
                  if (FanartHandlerSetup.Fh.MyScraperWorker != null && FanartHandlerSetup.Fh.MyScraperWorker.Work)
                  {
                    FanartHandlerSetup.Fh.MyScraperWorker.ReportProgress(Utils.Percent(CurrArtistsBeingScraped, TotArtistsBeingScraped), Utils.Progress.Progress);
                  }
                  #endregion
                }
              }
              #region Report
              if (FanartHandlerSetup.Fh.MyScraperWorker != null && FanartHandlerSetup.Fh.MyScraperWorker.Work)
              {
                FanartHandlerSetup.Fh.MyScraperWorker.ReportProgress(100, Utils.Progress.Done);
              }
              #endregion
              logger.Debug(Text + "done for Series.");
            }
            CurrTextBeingScraped = string.Empty;
            tvsDatabaseSeries = null;
          }
          #endregion
  
          #region Music Labels
          if ((All || param == Utils.SubCategory.FanartTVRecordLabels) && Utils.FanartTVNeedDownloadLabels && !StopScraper && !Utils.GetIsStopping())
          {
            CurrArtistsBeingScraped = 0.0;
            TotArtistsBeingScraped = 0.0;
  
            Utils.SetProperty("scraper.task", Translation.ScrapeInitialFanart + " - " + Translation.FHLabels);
            if (FanartHandlerSetup.Fh.MyScraperWorker != null && FanartHandlerSetup.Fh.MyScraperWorker.Work)
            {
              FanartHandlerSetup.Fh.MyScraperWorker.ReportProgress(0, Utils.Progress.Start);
            }
  
            var SQL = "SELECT DISTINCT mbid, name " +
                        "FROM Labels " + 
                        "WHERE Time_Stamp < '" + strDateTwoWeeksAgo + "';";
            SQLiteResultSet sqLiteResultSet;
            lock (lockObject)
              sqLiteResultSet = dbClient.Execute(SQL);
  
            if (sqLiteResultSet.Rows.Count > 0)
            {
              CurrArtistsBeingScraped = 0.0;
              TotArtistsBeingScraped = (float)checked(sqLiteResultSet.Rows.Count);
  
              logger.Debug(Text + "initiating for Music Record Labels...");
  
              logger.Debug(Text + "Music Record Labels: [" + sqLiteResultSet.Rows.Count + "]");
  
              var htLabels = new Hashtable();
              var i = 0;
              while (i < sqLiteResultSet.Rows.Count)
              {
                var LabelId = sqLiteResultSet.GetField(i, 0);
                var LabelName = sqLiteResultSet.GetField(i, 1);
  
                CurrTextBeingScraped = LabelId + " - " + LabelName;
                if (!string.IsNullOrEmpty(LabelId))
                {
                  if (!StopScraper && !Utils.GetIsStopping())
                  {
                    if (!htLabels.Contains(LabelId))
                    {
                      FanartAlbum fa = new FanartAlbum();
                      fa.RecordLabel.Id = LabelId;
                      fa.RecordLabel.RecordLabel = LabelName;
                      if (DoScrapeMusicLabels(fa) > 0)
                      {
                        htLabels.Add(LabelId, 1);
                      }
                      UpdateMusicLabelTimeStamp(LabelId);
                    }
                  }
                  else
                    break;
                }
                #region Report
                ++CurrArtistsBeingScraped;
                if (FanartHandlerSetup.Fh.MyScraperWorker != null && FanartHandlerSetup.Fh.MyScraperWorker.Work)
                {
                  FanartHandlerSetup.Fh.MyScraperWorker.ReportProgress(Utils.Percent(CurrArtistsBeingScraped, TotArtistsBeingScraped), Utils.Progress.Progress);
                }
                #endregion
                checked { ++i; }
              }
              #region Report
              if (FanartHandlerSetup.Fh.MyScraperWorker != null && FanartHandlerSetup.Fh.MyScraperWorker.Work)
              {
                FanartHandlerSetup.Fh.MyScraperWorker.ReportProgress(100, Utils.Progress.Done);
              }
              #endregion
              logger.Debug(Text + "done for Music Record Labels.");
            }
            CurrTextBeingScraped = string.Empty;
          }
          #endregion
  
          logger.Info(Text + "is done.");
        }
        catch (Exception ex)
        {
          logger.Error(Text + ex);
        }
      }
  
      public void InitialScrapeAnimated()
      {
        InitialScrapeAnimated(Utils.SubCategory.None); // Scrape All
      }
  
      public void InitialScrapeAnimated(Utils.SubCategory param)
      {
        bool All = (param == Utils.SubCategory.None);
        string Text = (All ? "InitialScrapeAnimated" : "ScrapeAnimated [" + param + "]") + " ";
  
        CurrArtistsBeingScraped = 0.0;
        TotArtistsBeingScraped = 0.0;
        CurrTextBeingScraped = string.Empty;
  
        if (!MediaPortal.Util.Win32API.IsConnectedToInternet())
        {
          logger.Debug(Text + "No internet connection detected. Cancelling initial scrape.");
          return;
        }
  
        Utils.SetProperty("scraper.task", Translation.ScrapeInitialAnimated + " - " + Translation.ScrapeInitializing);
        if (FanartHandlerSetup.Fh.MyScraperWorker != null && FanartHandlerSetup.Fh.MyScraperWorker.Work)
        {
          FanartHandlerSetup.Fh.MyScraperWorker.ReportProgress(0, Utils.Progress.Start);
        }
  
        string strDateTwoWeeksAgo = DateTime.Today.AddDays(-14.0).ToString(dbDateFormat, CultureInfo.CurrentCulture);
  
        logger.Info(Text + "is starting...");
        try
        {
          #region Movies
          if ((All || param == Utils.SubCategory.AnimatedMovie) && Utils.AnimatedNeedDownloadMovies && !StopScraper && !Utils.GetIsStopping())
          {
            Utils.AnimatedLoad();
  
            string MoviesText = string.Empty;
  
            CurrArtistsBeingScraped = 0.0;
            TotArtistsBeingScraped = 0.0;
  
            Utils.SetProperty("scraper.task", Translation.ScrapeInitialAnimated + " - " + Translation.FHVideos);
            if (FanartHandlerSetup.Fh.MyScraperWorker != null && FanartHandlerSetup.Fh.MyScraperWorker.Work)
            {
              FanartHandlerSetup.Fh.MyScraperWorker.ReportProgress(0, Utils.Progress.Start);
            }
  
            ArrayList videoDatabaseMovies = new ArrayList();
            logger.Debug(Text + "Add MyVideo movies...");
            VideoDatabase.GetMovies(ref videoDatabaseMovies);
            MoviesText = "MyVideo";
  
            if (Utils.MovingPicturesEnabled)
            {
              logger.Debug(Text + "Add MovingPictures movies...");
              UtilsMovingPictures.GetMovingPicturesMoviesList(ref videoDatabaseMovies);
              MoviesText = MoviesText + (string.IsNullOrEmpty(MoviesText) ? string.Empty : "/") + "MovingPictures";
            }
            if (Utils.MyFilmsEnabled)
            {
              logger.Debug(Text + "Add MyFilms movies...");
              UtilsMyFilms.GetMyFilmsMoviesList(ref videoDatabaseMovies);
              MoviesText = MoviesText + (string.IsNullOrEmpty(MoviesText) ? string.Empty : "/") + "MyFilms";
            }
  
            if (videoDatabaseMovies != null && videoDatabaseMovies.Count > 0)
            {
              CurrArtistsBeingScraped = 0.0;
              TotArtistsBeingScraped = (float)checked(videoDatabaseMovies.Count);
  
              logger.Debug(Text + "initiating for Movies (" + MoviesText + ")...");
              var htMovies = new Hashtable();               
              var SQL = "SELECT DISTINCT Key1, count(Key1) as Count " +
                          "FROM Image " +
                          "WHERE SubCategory IN ('" + Utils.SubCategory.AnimatedMovie + "') AND " +
                                "Time_Stamp >= '" + strDateTwoWeeksAgo + "' " +
                          "GROUP BY Key1;";
              SQLiteResultSet sqLiteResultSet;
              lock (lockObject)
                sqLiteResultSet = dbClient.Execute(SQL);
              var i = 0;
              while (i < sqLiteResultSet.Rows.Count)
              {
                var htMovie = sqLiteResultSet.GetField(i, 0).ToUpperInvariant();
                if (!htMovies.Contains(htMovie))
                  htMovies.Add(htMovie, sqLiteResultSet.GetField(i, 1));
                checked { ++i; }
              }
              logger.Debug(Text + "Movies: [" + htMovies.Count + "]/[" + videoDatabaseMovies.Count + "]");
  
              var index = 0;
              while (index < videoDatabaseMovies.Count)
              {
                IMDBMovie details = new IMDBMovie();
                details = (IMDBMovie)videoDatabaseMovies[index];
                var movieID = details.ID.ToString();
                var movieIMDBID = details.IMDBNumber.Trim().ToLower().Replace("unknown", string.Empty).ToUpperInvariant();
                var movieTitle = details.Title.Trim();
                CurrTextBeingScraped = movieIMDBID + " - " + movieTitle;
  
                if (!string.IsNullOrEmpty(movieID) && !string.IsNullOrEmpty(movieIMDBID))
                {
                  if (!StopScraper && !Utils.GetIsStopping())
                  {
                    if (!htMovies.Contains(movieIMDBID))
                    {
                      if (DoScrapeAnimated(new FanartMovie(movieID, movieTitle, movieIMDBID), false, false, Utils.Animated.MoviesPoster, Utils.Animated.MoviesBackground) > 0)
                      {
                        htMovies.Add(movieIMDBID, 1);
                      }
                    }
                  }
                  else
                    break;
                }
                #region Report
                ++CurrArtistsBeingScraped;
                if (FanartHandlerSetup.Fh.MyScraperWorker != null && FanartHandlerSetup.Fh.MyScraperWorker.Work)
                {
                  FanartHandlerSetup.Fh.MyScraperWorker.ReportProgress(Utils.Percent(CurrArtistsBeingScraped, TotArtistsBeingScraped), Utils.Progress.Progress);
                }
                #endregion
                // logger.Debug("*** {0}/{1}", CurrArtistsBeingScraped, TotArtistsBeingScraped);
                checked { ++index; }
              }
              #region Report
              if (FanartHandlerSetup.Fh.MyScraperWorker != null && FanartHandlerSetup.Fh.MyScraperWorker.Work)
              {
                FanartHandlerSetup.Fh.MyScraperWorker.ReportProgress(100, Utils.Progress.Done);
              }
              #endregion
              logger.Debug(Text + "done for Movies.");
            }
            CurrTextBeingScraped = string.Empty;
            videoDatabaseMovies = null;
  
            Utils.AnimatedUnLoad();
          }
          #endregion
  
          logger.Info(Text + "is done.");
        }
        catch (Exception ex)
        {
          logger.Error(Text + ex);
        }
      }
  
      public void InitialThumbScrape(bool onlyMissing)
      {
        CurrArtistsBeingScraped = 0.0;
        TotArtistsBeingScraped = 0.0;
        CurrTextBeingScraped = string.Empty;
  
        if (!MediaPortal.Util.Win32API.IsConnectedToInternet())
        {
          logger.Debug("InitialThumbScrape: No internet connection detected. Cancelling thumb scrape.");
          return;
        }
  
        try
        {
          logger.Info("InitialThumbScrape is starting (Only missing = " + onlyMissing.ToString() + ")...");
          #region Artists
          if (Utils.ScrapeThumbnails)
          {
            ArrayList musicDatabaseArtists = new ArrayList();
            m_db.GetAllArtists(ref musicDatabaseArtists);
            
            #region AlbumArtist
            var musicAlbumArtists = Utils.GetMusicAlbumArtists(m_db.DatabaseName);
            if (musicAlbumArtists != null && musicAlbumArtists.Count > 0)
            {
              logger.Debug("InitialThumbScrape add Album Artists [" + musicAlbumArtists.Count + "]...");
              musicDatabaseArtists.AddRange(musicAlbumArtists);
            }
            #endregion
  
            #region mvCentral
            var musicVideoArtists = Utils.GetMusicVideoArtists("mvCentral.db3");
            if (musicVideoArtists != null && musicVideoArtists.Count > 0)
            {
              logger.Debug("InitialThumbScrape add Artists from mvCentral [" + musicVideoArtists.Count + "]...");
              musicDatabaseArtists.AddRange(musicVideoArtists);
            }
            #endregion
            
            if (musicDatabaseArtists != null && musicDatabaseArtists.Count > 0)
            {
              logger.Debug("InitialThumbScrape Artists: [" + musicDatabaseArtists.Count + "]");
              CurrArtistsBeingScraped = 0.0;
              TotArtistsBeingScraped = (float)checked(musicDatabaseArtists.Count);
              var index = 0;
              while (index < musicDatabaseArtists.Count)
              {
                var artist = musicDatabaseArtists[index].ToString();
                CurrTextBeingScraped = artist;
  
                if (!StopScraper && !Utils.GetIsStopping())
                  DoScrapeArtistThumbs(new FanartArtist(artist), onlyMissing);
                else
                  break;
                // Piped Artists
                string[] artists = Utils.HandleMultipleKeysToArray(artist);
                if (artists != null)
                {
                  foreach (string sartist in artists)
                  {
                    if (!StopScraper && !Utils.GetIsStopping())
                    {
                      DoScrapeArtistThumbs(new FanartArtist(sartist), onlyMissing);
                    }
                    else
                      break;
                  }
                }
                ++CurrArtistsBeingScraped;
                checked { ++index; }
              }
            }
            CurrTextBeingScraped = string.Empty;
            musicDatabaseArtists = null;
          }
          else
            logger.Debug("ThumbScrape for Artists disabled in config ...");
          #endregion
  
          #region Albums
          if ((Utils.ScrapeThumbnailsAlbum) && (!StopScraper && !Utils.GetIsStopping()))
          {
            List<AlbumInfo> musicDatabaseAlbums = new List<AlbumInfo>();
            m_db.GetAllAlbums(ref musicDatabaseAlbums);
            #region mvCentral
            var musicVideoAlbums = Utils.GetMusicVideoAlbums("mvCentral.db3");
            if (musicVideoAlbums != null && musicVideoAlbums.Count > 0)
            {
              logger.Debug("InitialThumbScrape add Artists - Albums from mvCentral [" + musicVideoAlbums.Count + "]...");
              musicDatabaseAlbums.AddRange(musicVideoAlbums);
            }
            #endregion
            if (musicDatabaseAlbums != null && musicDatabaseAlbums.Count > 0)
            {
              logger.Debug("InitialThumbScrape Artists - Albums: [" + musicDatabaseAlbums.Count + "]");
              TotArtistsBeingScraped = (float)checked(TotArtistsBeingScraped + musicDatabaseAlbums.Count);
              var index = 0;
              while (index < musicDatabaseAlbums.Count)
              {
                var album = Utils.RemoveMPArtistPipe(musicDatabaseAlbums[index].Album).Trim();
                if (!string.IsNullOrWhiteSpace(album))
                {
                  var artist = Utils.RemoveMPArtistPipe(musicDatabaseAlbums[index].Artist).Trim();
                  var albumartist = Utils.RemoveMPArtistPipe(musicDatabaseAlbums[index].AlbumArtist).Trim();
                  var dbartist = Utils.GetArtist(Utils.UndoArtistPrefix(artist));
                  var dbalbum = Utils.GetAlbum(album);
                  
                  int idiscs = GetDiscTotalAlbumInMPMusicDatabase(artist, albumartist, album);
  
                  // Artist - Album
                  CurrTextBeingScraped = string.Format(Utils.MusicMask, artist, album);
                  if (!string.IsNullOrEmpty(artist))
                    if (!HasAlbumThumb(dbartist, dbalbum) || !onlyMissing)
                    {
                      if (!StopScraper && !Utils.GetIsStopping())
                        DoScrapeAlbumThumbs(new FanartAlbum(artist, album, idiscs), false, false);
                      else
                        break;
                    }
  
                  // AlbumArtist - Album
                  CurrTextBeingScraped = string.Format(Utils.MusicMask, albumartist, album);
                  if (!string.IsNullOrEmpty(albumartist))
                    if (!albumartist.Equals(artist, StringComparison.InvariantCultureIgnoreCase))
                    {
                      dbartist = Utils.UndoArtistPrefix(Utils.GetArtist(albumartist));
                      if (!HasAlbumThumb(dbartist, dbalbum) || !onlyMissing)
                      {
                        if (!StopScraper && !Utils.GetIsStopping())
                          DoScrapeAlbumThumbs(new FanartAlbum(albumartist, album, idiscs), false, false);
                        else
                          break;
                      }
                    }
                  // Piped Artists
                  var pipedartist = musicDatabaseAlbums[index].Artist.Trim() + " | " + musicDatabaseAlbums[index].AlbumArtist.Trim();
                  string[] artists = Utils.HandleMultipleKeysToArray(pipedartist);
                  if (artists != null)
                  {
                    foreach (string sartist in artists)
                    {
                      CurrTextBeingScraped = string.Format(Utils.MusicMask, sartist, album);
                      dbartist = Utils.UndoArtistPrefix(Utils.GetArtist(sartist));
                      if (!HasAlbumThumb(dbartist, dbalbum) || !onlyMissing)
                      {
                        if (!StopScraper && !Utils.GetIsStopping())
                          DoScrapeAlbumThumbs(new FanartAlbum(sartist, album, idiscs), false, false);
                        else
                          break;
                      }
                    }
                  }
                }
                ++CurrArtistsBeingScraped;
                checked { ++index; }
              }
            }
            CurrTextBeingScraped = string.Empty;
            musicDatabaseAlbums = null;
          }
          else
            logger.Debug("ThumbScrape for Albums disabled in config ...");
          #endregion
          logger.Info("InitialThumbScrape is done.");
        }
        catch (Exception ex)
        {
          logger.Error("InitialThumbScrape: " + ex);
        }
      }
      #endregion
  
      #region Other Scrape
      public bool ArtistAlbumScrape(string artist, string album)
      {
        return ArtistAlbumScrape(artist, album, 0);
      }
  
      public bool ArtistAlbumScrape(string artist, string album, int discs)
      {
        if (!MediaPortal.Util.Win32API.IsConnectedToInternet())
        {
          logger.Debug("ArtistAlbumScrape: No internet connection detected. Cancelling new scrape.");
          return false;
        }
  
        try
        {
          logger.Info("ArtistAlbumScrape is starting for Artist: " + artist + ", Album: " + album + ".");
          if (DoScrapeArtistAlbum(artist, album, discs, true) > 0)
          {
            logger.Info("ArtistAlbumScrape is done. Found.");
            return true;
          }
          else
          {
            logger.Info("ArtistAlbumScrape is done. Not found.");
            return false;
          }
        }
        catch (Exception ex)
        {
          logger.Error("ArtistAlbumScrape: " + ex);
        }
        return false;
      }
  
      public bool NowPlayingScrape(FanartVideoTrack fmp)
      {
        if (!MediaPortal.Util.Win32API.IsConnectedToInternet())
        {
          logger.Debug("NowPlayingScrape: No internet connection detected. Cancelling new scrape.");
          return false;
        }
  
        string wartist = fmp.GetArtists;
        if (string.IsNullOrEmpty(wartist))
        {
          return false;
        }
  
        logger.Debug("--- Now Playing ---------------------");
        try
        {
          logger.Info("--- NowPlaying --- Scrape for Artist(s): " + wartist + (fmp.Album.IsEmpty ? string.Empty : " Album: " + fmp.TrackAlbum + " Year: " + fmp.Album.Year + " Genre: " + fmp.Genre));
          /*
          if (wartist.ToLower().Contains(" and "))
          {
            wartist = wartist + "|" + Regex.Replace(wartist, @"\sand\s", "|", RegexOptions.IgnoreCase);
            logger.Info("--- Updated: Scrape for Artist(s): " + wartist + (fmp.Album.IsEmpty ? string.Empty : " Album: " + fmp.TrackAlbum + " Year: " + fmp.Album.Year + " Genre: " + fmp.Genre));
          }
          */
          wartist = wartist + "|" + fmp.TrackArtist + "|" + fmp.TrackAlbumArtist + "|" + fmp.TrackVideoArtist;
          List<string> artists = Utils.HandleMultipleKeysToArray(wartist).ToList();
          /*
          List<string> artists = Utils.HandleMultipleKeysToArray(wartist).ToList();
          if (!fmp.Artist.IsEmpty && !fmp.TrackArtist.Contains("|") && !artists.Contains(fmp.TrackArtist))
          {
            artists.Add(fmp.TrackArtist);
          }
          if (!fmp.AlbumArtist.IsEmpty && !fmp.TrackAlbumArtist.Contains("|") && !artists.Contains(fmp.TrackAlbumArtist))
          {
            artists.Add(fmp.TrackAlbumArtist);
          }
          if (!fmp.VideoArtist.IsEmpty && !fmp.TrackVideoArtist.Contains("|") && !artists.Contains(fmp.TrackVideoArtist))
          {
            artists.Add(fmp.TrackVideoArtist);
          }
          */
          if (!fmp.Album.IsEmpty)
          {
            fmp.Album.CDs = GetDiscTotalAlbumInMPMusicDatabase(fmp.TrackArtist, fmp.TrackAlbumArtist, fmp.TrackAlbum);
          }
  
          double steps = 8.0; // Number of GetArtistFanart steps...
          CurrArtistsBeingScraped = 0.0;
          TotArtistsBeingScraped = (double)artists.Count * steps + 1.0;
  
          var flag = false;
          int i = 0;
          foreach (string sartist in artists)
          {
            CurrArtistsBeingScraped = (double)i * steps;
            logger.Debug("--- NowPlaying: Scrape starting for Artist: " + sartist + (fmp.Album.IsEmpty ? string.Empty : " Album: " + fmp.Album.Album));
            if (!StopScraper)
            {
              var result = (DoScrapeArtistAlbum(sartist, fmp.Album.Album, fmp.Album.CDs, false) > 0);
              flag = (flag || result);
            }
            else
              break;
           checked { ++i; }
          }
          logger.Info("--- NowPlaying --- Scrape is done. " + (flag ? "F" : "Not f") + "ound. Artist(s): " + wartist + (fmp.Album.IsEmpty ? string.Empty : " Album: " + fmp.TrackAlbum));
          CurrArtistsBeingScraped = TotArtistsBeingScraped;
          return flag;
        }
        catch (Exception ex)
        {
          logger.Error("NowPlayingScrape: " + ex);
          return false;
        }
      }
      #endregion
  
      #region Music Info
      public void GetMusicInfo()
      {
        if (!Utils.GetArtistInfo && !Utils.GetAlbumInfo)
        {
          return;
        }
  
        try
        {
          logger.Debug("Get Info starting...");
  
          #region Artists
          if (Utils.GetArtistInfo && NeedGetDummyInfo(Utils.Scrapper.ArtistInfo))
          {
            bool flag = (Utils.FullScanInfo && NeedGetDummyInfo(Utils.Scrapper.ArtistInfo, false));
            ArrayList musicDatabaseArtists = new ArrayList();
  
            if (flag)
            {
              m_db.GetAllArtists(ref musicDatabaseArtists);
  
              #region AlbumArtist
              var musicAlbumArtists = Utils.GetMusicAlbumArtists(m_db.DatabaseName);
              if (musicAlbumArtists != null && musicAlbumArtists.Count > 0)
              {
                musicDatabaseArtists.AddRange(musicAlbumArtists);
              }
              #endregion
            }
            else
            {
              musicDatabaseArtists = Utils.GetMusicInfoArtists(m_db.DatabaseName);
            }
  
            if (musicDatabaseArtists != null && musicDatabaseArtists.Count > 0)
            {
              logger.Debug("Get Info: Initiating for Artists [" + musicDatabaseArtists.Count + "] ...");
              var htInfo = new Hashtable();
  
              var index = 0;
              while (index < musicDatabaseArtists.Count)
              {
                FanartArtist fa = new FanartArtist(musicDatabaseArtists[index].ToString());
  
                DoScrapeArtistInfo(fa);
                if (StopScraperInfo)
                {
                  break;
                }
  
                checked { ++index; }
              }
              logger.Debug("Get Info: Done for Artists.");
            }
            musicDatabaseArtists = null;
            InsertDummyInfoItem(Utils.Scrapper.ArtistInfo, !flag);
            if (flag)
            {
              InsertDummyInfoItem(Utils.Scrapper.ArtistInfo);
            }
          }
          #endregion
  
          #region Albums
          if (Utils.GetAlbumInfo && NeedGetDummyInfo(Utils.Scrapper.AlbumInfo))
          {
            bool flag = (Utils.FullScanInfo && NeedGetDummyInfo(Utils.Scrapper.AlbumInfo, false));
            List<AlbumInfo> musicDatabaseAlbums = new List<AlbumInfo>();
  
            if (flag)
            {
              m_db.GetAllAlbums(ref musicDatabaseAlbums);
            }
            else
            {
              musicDatabaseAlbums = Utils.GetMusicInfoAlbums(m_db.DatabaseName);
            }
  
            if (musicDatabaseAlbums != null && musicDatabaseAlbums.Count > 0)
            {
              logger.Debug("Get Info: Initiating for Artists - Albums [" + musicDatabaseAlbums.Count + "]...");
              var htAlbums = new Hashtable();
  
              var index = 0;
              while (index < musicDatabaseAlbums.Count)
              {
                FanartAlbum fa = new FanartAlbum(musicDatabaseAlbums[index].Artist, musicDatabaseAlbums[index].Album);
  
                DoScrapeAlbumInfo(fa);
                if (StopScraperInfo)
                {
                  break;
                }
  
                checked { ++index; }
              }
  
              logger.Debug("Get Info: Done for Artists - Albums.");
            }
            musicDatabaseAlbums = null;
            InsertDummyInfoItem(Utils.Scrapper.AlbumInfo, !flag);
            if (flag)
            {
              InsertDummyInfoItem(Utils.Scrapper.AlbumInfo);
            }
          }
          #endregion
          StopScraperInfo = false;
          logger.Debug("Get Info is Done.");
        }
        catch (Exception ex)
        {
          logger.Error("Get Info: " + ex);
        }
      }
      #endregion
  
      #region Movie Info
  
      public void GetMoviesInfo()
      {
        if (!Utils.GetMoviesAwards)
        {
          return;
        }
  
        try
        {
          logger.Debug("Get Movies Info starting...");
  
          #region Movies
  
          if (Utils.GetMoviesAwards && NeedGetDummyInfo(Utils.Scrapper.MoviesAwards))
          {
            ArrayList moviesFormDatabase = new ArrayList();
            VideoDatabase.GetMovies(ref moviesFormDatabase);
  
            if (moviesFormDatabase != null && moviesFormDatabase.Count > 0)
            {
              logger.Debug("Get Awards: Initiating for Movies [" + moviesFormDatabase.Count + "] ...");
  
              var index = 0;
              while (index < moviesFormDatabase.Count)
              {
                IMDBMovie movie = moviesFormDatabase[index] as IMDBMovie;
                if (movie != null)
                {
                  if (string.IsNullOrEmpty(movie.MovieAwards) || movie.MovieAwards == "unknown" || movie.MovieAwards == "-")
                  {
                    string movieAwards = string.Empty;
                    try
                    {
                      movieAwards = Grabbers.Movies.AwardsGrabbers.AwardsGrabber.GetMovieAwards(movie.IMDBNumber, movie.TMDBNumber, movie.LocalDBNumber);
                      if (!string.IsNullOrEmpty(movieAwards))
                      {
                        movie.MovieAwards = movieAwards;
                        VideoDatabase.SetMovieInfoById(movie.ID, ref movie, false);
                      }
                    }
                    catch // (Exception ex)
                    {
                      // logger.Debug("*** GetMovieAwards: " + ex);
                    }
                    if (StopScraperMovieInfo)
                    {
                      break;
                    }
                  }
                }
                checked { ++index; }
              }
              logger.Debug("Get Awards: Done for Movies.");
              Grabbers.Movies.AwardsGrabbers.ResetGrabber();
            }
            moviesFormDatabase = null;
            InsertDummyInfoItem(Utils.Scrapper.MoviesAwards);
          }
  
          #endregion
  
          StopScraperMovieInfo = false;
  
gt:         logger.Debug("Get Movies Info is Done.");