public override void ProcessCommand() { logger.Info("Processing CommandRequest_DownloadImage: {0}", EntityID); string downloadURL = ""; try { ImageDownloadRequest req = null; switch (EntityTypeEnum) { case JMMImageType.AniDB_Cover: AniDB_AnimeRepository repAnime = new AniDB_AnimeRepository(); AniDB_Anime anime = repAnime.GetByID(EntityID); if (anime == null) { return; } req = new ImageDownloadRequest(EntityTypeEnum, anime, ForceDownload); break; case JMMImageType.TvDB_Episode: TvDB_EpisodeRepository repTvEp = new TvDB_EpisodeRepository(); TvDB_Episode ep = repTvEp.GetByID(EntityID); if (ep == null) { return; } if (string.IsNullOrEmpty(ep.Filename)) { return; } req = new ImageDownloadRequest(EntityTypeEnum, ep, ForceDownload); break; case JMMImageType.TvDB_FanArt: TvDB_ImageFanartRepository repFanart = new TvDB_ImageFanartRepository(); TvDB_ImageFanart fanart = repFanart.GetByID(EntityID); if (fanart == null) { return; } if (string.IsNullOrEmpty(fanart.BannerPath)) { return; } req = new ImageDownloadRequest(EntityTypeEnum, fanart, ForceDownload); break; case JMMImageType.TvDB_Cover: TvDB_ImagePosterRepository repPoster = new TvDB_ImagePosterRepository(); TvDB_ImagePoster poster = repPoster.GetByID(EntityID); if (poster == null) { return; } if (string.IsNullOrEmpty(poster.BannerPath)) { return; } req = new ImageDownloadRequest(EntityTypeEnum, poster, ForceDownload); break; case JMMImageType.TvDB_Banner: TvDB_ImageWideBannerRepository repBanners = new TvDB_ImageWideBannerRepository(); TvDB_ImageWideBanner wideBanner = repBanners.GetByID(EntityID); if (wideBanner == null) { return; } if (string.IsNullOrEmpty(wideBanner.BannerPath)) { return; } req = new ImageDownloadRequest(EntityTypeEnum, wideBanner, ForceDownload); break; case JMMImageType.MovieDB_Poster: MovieDB_PosterRepository repMoviePosters = new MovieDB_PosterRepository(); MovieDB_Poster moviePoster = repMoviePosters.GetByID(EntityID); if (moviePoster == null) { return; } if (string.IsNullOrEmpty(moviePoster.URL)) { return; } req = new ImageDownloadRequest(EntityTypeEnum, moviePoster, ForceDownload); break; case JMMImageType.MovieDB_FanArt: MovieDB_FanartRepository repMovieFanart = new MovieDB_FanartRepository(); MovieDB_Fanart movieFanart = repMovieFanart.GetByID(EntityID); if (movieFanart == null) { return; } if (string.IsNullOrEmpty(movieFanart.URL)) { return; } req = new ImageDownloadRequest(EntityTypeEnum, movieFanart, ForceDownload); break; case JMMImageType.Trakt_Poster: Trakt_ImagePosterRepository repTraktPosters = new Trakt_ImagePosterRepository(); Trakt_ImagePoster traktPoster = repTraktPosters.GetByID(EntityID); if (traktPoster == null) { return; } if (string.IsNullOrEmpty(traktPoster.ImageURL)) { return; } req = new ImageDownloadRequest(EntityTypeEnum, traktPoster, ForceDownload); break; case JMMImageType.Trakt_Fanart: Trakt_ImageFanartRepository repTraktFanarts = new Trakt_ImageFanartRepository(); Trakt_ImageFanart traktFanart = repTraktFanarts.GetByID(EntityID); if (traktFanart == null) { return; } if (string.IsNullOrEmpty(traktFanart.ImageURL)) { return; } req = new ImageDownloadRequest(EntityTypeEnum, traktFanart, ForceDownload); break; case JMMImageType.Trakt_Friend: Trakt_FriendRepository repFriends = new Trakt_FriendRepository(); Trakt_Friend friend = repFriends.GetByID(EntityID); if (friend == null) { return; } if (string.IsNullOrEmpty(friend.Avatar)) { return; } req = new ImageDownloadRequest(EntityTypeEnum, friend, ForceDownload); break; case JMMImageType.Trakt_Episode: Trakt_EpisodeRepository repTraktEpisodes = new Trakt_EpisodeRepository(); Trakt_Episode traktEp = repTraktEpisodes.GetByID(EntityID); if (traktEp == null) { return; } if (string.IsNullOrEmpty(traktEp.EpisodeImage)) { return; } req = new ImageDownloadRequest(EntityTypeEnum, traktEp, ForceDownload); break; case JMMImageType.AniDB_Character: AniDB_CharacterRepository repChars = new AniDB_CharacterRepository(); AniDB_Character chr = repChars.GetByID(EntityID); if (chr == null) { return; } req = new ImageDownloadRequest(EntityTypeEnum, chr, ForceDownload); break; case JMMImageType.AniDB_Creator: AniDB_SeiyuuRepository repCreator = new AniDB_SeiyuuRepository(); AniDB_Seiyuu creator = repCreator.GetByID(EntityID); if (creator == null) { return; } req = new ImageDownloadRequest(EntityTypeEnum, creator, ForceDownload); break; } if (req == null) { return; } List <string> fileNames = new List <string>(); List <string> downloadURLs = new List <string>(); string fileNameTemp = GetFileName(req, false); string downloadURLTemp = GetFileURL(req, false); fileNames.Add(fileNameTemp); downloadURLs.Add(downloadURLTemp); if (req.ImageType == JMMImageType.TvDB_FanArt) { fileNameTemp = GetFileName(req, true); downloadURLTemp = GetFileURL(req, true); fileNames.Add(fileNameTemp); downloadURLs.Add(downloadURLTemp); } for (int i = 0; i < fileNames.Count; i++) { string fileName = fileNames[i]; downloadURL = downloadURLs[i]; bool downloadImage = true; bool fileExists = File.Exists(fileName); if (fileExists) { if (!req.ForceDownload) { downloadImage = false; } else { downloadImage = true; } } else { downloadImage = true; } if (downloadImage) { string tempName = Path.Combine(ImageUtils.GetImagesTempFolder(), Path.GetFileName(fileName)); if (File.Exists(tempName)) { File.Delete(tempName); } try { if (fileExists) { File.Delete(fileName); } } catch (Exception ex) { Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(ServerSettings.Culture); string msg = string.Format(JMMServer.Properties.Resources.Command_DeleteError, fileName, ex.Message); logger.Warn(msg); return; } // download image using (WebClient client = new WebClient()) { client.Headers.Add("user-agent", "JMM"); //OnImageDownloadEvent(new ImageDownloadEventArgs("", req, ImageDownloadEventType.Started)); //BaseConfig.MyAnimeLog.Write("ProcessImages: Download: {0} *** to *** {1}", req.URL, fullName); if (downloadURL.Length > 0) { client.DownloadFile(downloadURL, tempName); string extension = ""; string contentType = client.ResponseHeaders["Content-type"].ToLower(); if (contentType.IndexOf("gif") >= 0) { extension = ".gif"; } if (contentType.IndexOf("jpg") >= 0) { extension = ".jpg"; } if (contentType.IndexOf("jpeg") >= 0) { extension = ".jpg"; } if (contentType.IndexOf("bmp") >= 0) { extension = ".bmp"; } if (contentType.IndexOf("png") >= 0) { extension = ".png"; } if (extension.Length > 0) { string newFile = Path.ChangeExtension(tempName, extension); if (!newFile.ToLower().Equals(tempName.ToLower())) { try { System.IO.File.Delete(newFile); } catch { //BaseConfig.MyAnimeLog.Write("DownloadedImage:Download() Delete failed:{0}", newFile); } System.IO.File.Move(tempName, newFile); tempName = newFile; } } } } // move the file to it's final location // check that the final folder exists string fullPath = Path.GetDirectoryName(fileName); if (!Directory.Exists(fullPath)) { Directory.CreateDirectory(fullPath); } System.IO.File.Move(tempName, fileName); logger.Info("Image downloaded: {0}", fileName); } } } catch (Exception ex) { logger.Warn("Error processing CommandRequest_DownloadImage: {0} ({1}) - {2}", downloadURL, EntityID, ex.Message); return; } }
private string GetFileName(ImageDownloadRequest req, bool thumbNailOnly) { switch (req.ImageType) { case JMMImageType.AniDB_Cover: AniDB_Anime anime = req.ImageData as AniDB_Anime; return(anime.PosterPath); case JMMImageType.TvDB_Episode: TvDB_Episode ep = req.ImageData as TvDB_Episode; return(ep.FullImagePath); case JMMImageType.TvDB_FanArt: TvDB_ImageFanart fanart = req.ImageData as TvDB_ImageFanart; if (thumbNailOnly) { return(fanart.FullThumbnailPath); } else { return(fanart.FullImagePath); } case JMMImageType.TvDB_Cover: TvDB_ImagePoster poster = req.ImageData as TvDB_ImagePoster; return(poster.FullImagePath); case JMMImageType.TvDB_Banner: TvDB_ImageWideBanner wideBanner = req.ImageData as TvDB_ImageWideBanner; return(wideBanner.FullImagePath); case JMMImageType.MovieDB_Poster: MovieDB_Poster moviePoster = req.ImageData as MovieDB_Poster; return(moviePoster.FullImagePath); case JMMImageType.MovieDB_FanArt: MovieDB_Fanart movieFanart = req.ImageData as MovieDB_Fanart; return(movieFanart.FullImagePath); case JMMImageType.Trakt_Poster: Trakt_ImagePoster traktPoster = req.ImageData as Trakt_ImagePoster; return(traktPoster.FullImagePath); case JMMImageType.Trakt_Fanart: Trakt_ImageFanart traktFanart = req.ImageData as Trakt_ImageFanart; return(traktFanart.FullImagePath); case JMMImageType.Trakt_Friend: Trakt_Friend traktFriend = req.ImageData as Trakt_Friend; return(traktFriend.FullImagePath); case JMMImageType.Trakt_Episode: Trakt_Episode traktEp = req.ImageData as Trakt_Episode; return(traktEp.FullImagePath); case JMMImageType.AniDB_Character: AniDB_Character chr = req.ImageData as AniDB_Character; return(chr.PosterPath); case JMMImageType.AniDB_Creator: AniDB_Seiyuu creator = req.ImageData as AniDB_Seiyuu; return(creator.PosterPath); default: return(""); } }
public override void ProcessCommand() { logger.Info("Processing CommandRequest_DownloadImage: {0}", EntityID); string downloadURL = string.Empty; try { ImageDownloadRequest req = null; switch (EntityTypeEnum) { case ImageEntityType.TvDB_Episode: TvDB_Episode ep = Repo.Instance.TvDB_Episode.GetByID(EntityID); if (string.IsNullOrEmpty(ep?.Filename)) { logger.Warn($"TvDB Episode image failed to download: Can't get episode with ID: {EntityID}"); return; } req = new ImageDownloadRequest(EntityTypeEnum, ep, ForceDownload); break; case ImageEntityType.TvDB_FanArt: TvDB_ImageFanart fanart = Repo.Instance.TvDB_ImageFanart.GetByID(EntityID); if (string.IsNullOrEmpty(fanart?.BannerPath)) { logger.Warn($"TvDB Fanart image failed to download: Can't find valid fanart with ID: {EntityID}"); RemoveImageRecord(); return; } req = new ImageDownloadRequest(EntityTypeEnum, fanart, ForceDownload); break; case ImageEntityType.TvDB_Cover: TvDB_ImagePoster poster = Repo.Instance.TvDB_ImagePoster.GetByID(EntityID); if (string.IsNullOrEmpty(poster?.BannerPath)) { logger.Warn($"TvDB Poster image failed to download: Can't find valid poster with ID: {EntityID}"); RemoveImageRecord(); return; } req = new ImageDownloadRequest(EntityTypeEnum, poster, ForceDownload); break; case ImageEntityType.TvDB_Banner: TvDB_ImageWideBanner wideBanner = Repo.Instance.TvDB_ImageWideBanner.GetByID(EntityID); if (string.IsNullOrEmpty(wideBanner?.BannerPath)) { logger.Warn($"TvDB Banner image failed to download: Can't find valid banner with ID: {EntityID}"); RemoveImageRecord(); return; } req = new ImageDownloadRequest(EntityTypeEnum, wideBanner, ForceDownload); break; case ImageEntityType.MovieDB_Poster: MovieDB_Poster moviePoster = Repo.Instance.MovieDB_Poster.GetByID(EntityID); if (string.IsNullOrEmpty(moviePoster?.URL)) { logger.Warn($"MovieDB Poster image failed to download: Can't find valid poster with ID: {EntityID}"); RemoveImageRecord(); return; } req = new ImageDownloadRequest(EntityTypeEnum, moviePoster, ForceDownload); break; case ImageEntityType.MovieDB_FanArt: MovieDB_Fanart movieFanart = Repo.Instance.MovieDB_Fanart.GetByID(EntityID); if (string.IsNullOrEmpty(movieFanart?.URL)) { logger.Warn($"MovieDB Fanart image failed to download: Can't find valid fanart with ID: {EntityID}"); return; } req = new ImageDownloadRequest(EntityTypeEnum, movieFanart, ForceDownload); break; case ImageEntityType.AniDB_Cover: SVR_AniDB_Anime anime = Repo.Instance.AniDB_Anime.GetByAnimeID(EntityID); if (anime == null) { logger.Warn($"AniDB poster image failed to download: Can't find AniDB_Anime with ID: {EntityID}"); return; } AniDbImageRateLimiter.Instance.EnsureRate(); req = new ImageDownloadRequest(EntityTypeEnum, anime, ForceDownload); break; case ImageEntityType.AniDB_Character: AniDB_Character chr = Repo.Instance.AniDB_Character.GetByCharID(EntityID); if (chr == null) { logger.Warn($"AniDB Character image failed to download: Can't find AniDB Character with ID: {EntityID}"); return; } AniDbImageRateLimiter.Instance.EnsureRate(); req = new ImageDownloadRequest(EntityTypeEnum, chr, ForceDownload); break; case ImageEntityType.AniDB_Creator: AniDB_Seiyuu creator = Repo.Instance.AniDB_Seiyuu.GetBySeiyuuID(EntityID); if (creator == null) { logger.Warn($"AniDB Seiyuu image failed to download: Can't find Seiyuu with ID: {EntityID}"); return; } AniDbImageRateLimiter.Instance.EnsureRate(); req = new ImageDownloadRequest(EntityTypeEnum, creator, ForceDownload); break; } if (req == null) { logger.Warn($"Image failed to download: No implementation found for {EntityTypeEnum}"); return; } List <string> fileNames = new List <string>(); List <string> downloadURLs = new List <string>(); string fileNameTemp = GetFileName(req, false); string downloadURLTemp = GetFileURL(req, false); fileNames.Add(fileNameTemp); downloadURLs.Add(downloadURLTemp); if (req.ImageType == ImageEntityType.TvDB_FanArt) { fileNameTemp = GetFileName(req, true); downloadURLTemp = GetFileURL(req, true); fileNames.Add(fileNameTemp); downloadURLs.Add(downloadURLTemp); } for (int i = 0; i < fileNames.Count; i++) { try { string fileName = fileNames[i]; downloadURL = downloadURLs[i]; bool downloadImage = true; bool fileExists = File.Exists(fileName); bool imageValid = fileExists && Misc.IsImageValid(fileName); if (imageValid && !req.ForceDownload) { downloadImage = false; } if (!downloadImage) { continue; } string tempName = Path.Combine(ImageUtils.GetImagesTempFolder(), Path.GetFileName(fileName)); try { if (fileExists) { File.Delete(fileName); } } catch (Exception ex) { Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(ServerSettings.Instance.Culture); logger.Warn(Resources.Command_DeleteError, fileName, ex.Message); return; } // If this has any issues, it will throw an exception, so the catch below will handle it RecursivelyRetryDownload(downloadURL, ref tempName, 0, 5); // move the file to it's final location // check that the final folder exists string fullPath = Path.GetDirectoryName(fileName); if (!Directory.Exists(fullPath)) { Directory.CreateDirectory(fullPath); } File.Move(tempName, fileName); logger.Info($"Image downloaded: {fileName} from {downloadURL}"); } catch (WebException e) { logger.Warn("Error processing CommandRequest_DownloadImage: {0} ({1}) - {2}", downloadURL, EntityID, e.Message); // Remove the record if the image doesn't exist or can't download RemoveImageRecord(); } } } catch (Exception ex) { logger.Warn("Error processing CommandRequest_DownloadImage: {0} ({1}) - {2}", downloadURL, EntityID, ex.Message); } }
public static string GetFileURL(ImageDownloadRequest req, bool thumbNailOnly) { switch (req.ImageType) { case JMMImageType.AniDB_Cover: AniDB_Anime anime = req.ImageData as AniDB_Anime; return(string.Format(Constants.URLS.AniDB_Images, anime.Picname)); case JMMImageType.TvDB_Episode: TvDB_Episode ep = req.ImageData as TvDB_Episode; return(string.Format(Constants.URLS.TvDB_Images, ep.Filename)); case JMMImageType.TvDB_FanArt: TvDB_ImageFanart fanart = req.ImageData as TvDB_ImageFanart; if (thumbNailOnly) { return(string.Format(Constants.URLS.TvDB_Images, fanart.ThumbnailPath)); } else { return(string.Format(Constants.URLS.TvDB_Images, fanart.BannerPath)); } case JMMImageType.TvDB_Cover: TvDB_ImagePoster poster = req.ImageData as TvDB_ImagePoster; return(string.Format(Constants.URLS.TvDB_Images, poster.BannerPath)); case JMMImageType.TvDB_Banner: TvDB_ImageWideBanner wideBanner = req.ImageData as TvDB_ImageWideBanner; return(string.Format(Constants.URLS.TvDB_Images, wideBanner.BannerPath)); case JMMImageType.MovieDB_Poster: MovieDB_Poster moviePoster = req.ImageData as MovieDB_Poster; return(string.Format(Constants.URLS.MovieDB_Images, moviePoster.URL)); case JMMImageType.MovieDB_FanArt: MovieDB_Fanart movieFanart = req.ImageData as MovieDB_Fanart; return(string.Format(Constants.URLS.MovieDB_Images, movieFanart.URL)); case JMMImageType.Trakt_Poster: Trakt_ImagePoster traktPoster = req.ImageData as Trakt_ImagePoster; return(traktPoster.ImageURL); case JMMImageType.Trakt_Fanart: Trakt_ImageFanart traktFanart = req.ImageData as Trakt_ImageFanart; return(traktFanart.ImageURL); case JMMImageType.Trakt_Friend: Trakt_Friend traktFriend = req.ImageData as Trakt_Friend; return(traktFriend.Avatar); case JMMImageType.Trakt_Episode: Trakt_Episode traktEp = req.ImageData as Trakt_Episode; return(traktEp.EpisodeImage); case JMMImageType.AniDB_Character: AniDB_Character chr = req.ImageData as AniDB_Character; return(string.Format(Constants.URLS.AniDB_Images, chr.PicName)); case JMMImageType.AniDB_Creator: AniDB_Seiyuu creator = req.ImageData as AniDB_Seiyuu; return(string.Format(Constants.URLS.AniDB_Images, creator.PicName)); default: return(""); } }
public void RefreshTraktFriends(bool traktScrobbles, bool traktShouts) { try { System.Windows.Application.Current.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, (Action) delegate() { TraktActivity.Clear(); }); JMMServerBinary.Contract_Trakt_Activity traktActivity = JMMServerVM.Instance.clientBinaryHTTP.GetTraktFriendInfo(AppSettings.Dash_TraktFriends_Items, AppSettings.Dash_TraktFriends_AnimeOnly, traktShouts, traktScrobbles); List <object> activity = new List <object>(); if (traktActivity.HasTraktAccount) { foreach (JMMServerBinary.Contract_Trakt_FriendFrequest contractFriend in traktActivity.TraktFriendRequests) { Trakt_FriendRequestVM req = new Trakt_FriendRequestVM(contractFriend); activity.Add(req); } foreach (JMMServerBinary.Contract_Trakt_FriendActivity contractAct in traktActivity.TraktFriendActivity) { if (contractAct.ActivityAction == (int)TraktActivityAction.Scrobble) { Trakt_ActivityScrobbleVM scrobble = new Trakt_ActivityScrobbleVM(contractAct); if (!string.IsNullOrEmpty(scrobble.UserFullImagePath) && !File.Exists(scrobble.UserFullImagePath)) { // re-download the friends avatar image try { System.Windows.Application.Current.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, (Action) delegate() { ImageDownloadRequest req = new ImageDownloadRequest(ImageEntityType.Trakt_ActivityScrobble, scrobble, true); MainWindow.imageHelper.DownloadImage(req); }); } catch (Exception ex) { logger.ErrorException(ex.ToString(), ex); } } activity.Add(scrobble); } else if (contractAct.ActivityAction == (int)TraktActivityAction.Shout) { if (contractAct.ActivityType == (int)TraktActivityType.Episode) { Trakt_ActivityShoutEpisodeVM shoutEp = new Trakt_ActivityShoutEpisodeVM(contractAct); activity.Add(shoutEp); } else { Trakt_ActivityShoutShowVM shoutShow = new Trakt_ActivityShoutShowVM(contractAct); activity.Add(shoutShow); } } } foreach (JMMServerBinary.Contract_Trakt_Friend contract in traktActivity.TraktFriends) { if (contract.WatchedEpisodes != null && contract.WatchedEpisodes.Count > 0) { Trakt_FriendVM friend = new Trakt_FriendVM(contract); activity.Add(friend); } } } else { Trakt_SignupVM signup = new Trakt_SignupVM(); activity.Add(signup); } System.Windows.Application.Current.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, (Action) delegate() { foreach (object act in activity) { TraktActivity.Add(act); } ViewTraktActivity.Refresh(); }); } catch (Exception ex) { logger.ErrorException(ex.ToString(), ex); } finally { } }