Exemplo n.º 1
0
        private static void setPoster(string the_directory = "", mediaTypes type = mediaTypes.movie, bool container = false)
        {
            try
            {
                //mediaTypes type;
                List <DirectoryInfo> directories = new List <DirectoryInfo>();
                if (the_directory != "")
                {
                    if (container)
                    {
                        directories = new DirectoryInfo(the_directory).GetDirectories().ToList();
                    }
                    else
                    {
                        directories.Add(new DirectoryInfo(the_directory));
                    }

                    main.log(@"Getting poster for """ + the_directory + @""" " + type, msgType.success);
                    goto dataProvided;
                }

                using (FolderBrowserDialog Folder_Browser_Dialog = main.get_folderbrowserdialog())
                {
                    if (Folder_Browser_Dialog == null)
                    {
                        return;
                    }
                    if (new choice_box().ShowDialog() == DialogResult.OK)
                    {
                        if (choice.series)
                        {
                            type = mediaTypes.tv;
                        }
                        else
                        {
                            type = mediaTypes.movie;
                        }
                        if (choice.container)
                        {
                            directories = new DirectoryInfo(Folder_Browser_Dialog.SelectedPath).GetDirectories().ToList();
                        }
                        else
                        {
                            directories.Add(new DirectoryInfo(Folder_Browser_Dialog.SelectedPath));
                        }
                    }
                    else
                    {
                        return;
                    }
                }

dataProvided:
                main.max_progressbar(directories.Count, main.ProgressBar);
                main.update_progressbar(0, main.ProgressBar);


                foreach (DirectoryInfo directory in directories)
                {
                    if (Path.GetFileNameWithoutExtension(directory.Name).ToLower() != "tv")
                    {
                        FileInfo[] imageInfo = directory.GetFiles("*.*", SearchOption.TopDirectoryOnly).Where(f => extension.imageEXT.Contains(f.Extension.ToLower())).ToArray();
                        string     iniPath   = directory.FullName + @"\desktop.ini";
                        string     icoPath   = directory.FullName + @"\folder.ico";
                        if (File.Exists(icoPath))
                        {
                            File.Delete(icoPath);
                        }
                        if (File.Exists(iniPath))
                        {
                            File.Delete(iniPath);
                        }

                        Bitmap mainBitmap = null;

                        if (imageInfo.Length > 0 && imageInfo.Where(f => f.Name.Contains("YTS")).Count() <= 0)
                        {
                            mainBitmap = new Bitmap(imageInfo[0].FullName);
                        }
                        else
                        {
                            string     name  = Path.GetFileNameWithoutExtension(directory.Name);
                            List <int> years = new List <int>();

                            removeFromName <quality>(ref name);

                            foreach (Match match in new Regex(@"\d{4}").Matches(name))
                            {
                                years.Add(Convert.ToInt32(match.Value));
                            }

                            years.Sort((a, b) => b.CompareTo(a));

                            try { name = Regex.Replace(name, $@"\b{years[0]}\b", string.Empty); } catch (Exception) { }

                            if (years.Count() == 0)
                            {
                                years.Add(0);
                            }

                            string folderName = name;
                            int    s_number   = 0;

                            name = name.Replace(".", " ");

                            if (type == mediaTypes.tv)
                            {
                                try
                                {
                                    if (name.Contains("第") && name.Contains("期"))
                                    {
                                        s_number = Convert.ToInt32(Regex.Match(Regex.Match(name, @"(?:\s+第\s+\d+\s+期|\s+第\s+\d{2}\s+期)|\s+\d+|\s+\d{2}").ToString(), @"\d{2}|\d+").Groups[0].Value);
                                    }
                                    else
                                    {
                                        s_number = Convert.ToInt32(Regex.Match(Regex.Match(name, @"(?:\s+Season\s+\d+|\s+Season\s+\d{2})|(\s+S\d{2}E\d{2})|(\s+S\d{2})|(\s+S\d+)|(\s+S\d+E\d+)|(\d+)|(\d{2})").ToString(), @"\d{2}|\d+").Groups[0].Value);
                                    }
                                }
                                catch (Exception ex) { main.log(ex.ToString(), msgType.error); }
                            }

                            removeFromName <audio_codec>(ref name);
                            removeFromName <encoder>(ref name);
                            removeFromName <source>(ref name);
                            removeFromName <video_codec>(ref name);
                            //removeFromName<languageDB>(ref name);

                            name = Regex.Replace(name, @"[^0-9a-zA-Z\s&一-龯ぁ-んァ-ン\w!:/・]", string.Empty);
                            name = new Regex("[ ]{2,}", RegexOptions.None).Replace(name, " ");
                            name = name.ToLower();

                            if (name.Contains("collection"))
                            {
                                type = mediaTypes.collection;
                                name = name.Replace("collection", string.Empty);
                                name = new Regex("[ ]{2,}", RegexOptions.None).Replace(name, " ");
                            }

                            removeFromName <audio_channel>(ref name);

                            main.log("Simplified name:= " + name, msgType.message);

                            main.max_progressbar(name.Count(f => (f == ' ')), main.Mini_ProgressBar);
                            main.update_progressbar(0, main.Mini_ProgressBar);

                            while (name != string.Empty)
                            {
                                foreach (int year in years)
                                {
                                    RestResponse response = null;
                                    response = new RestClient("https://api.themoviedb.org/3/search/" + type.ToString() + "?api_key=9a49cbab6d640fd9483fbdd2abe22b94&query=" + System.Web.HttpUtility.UrlEncode(name) + "&page=1&include_adult=true&" + mediaType.getDateQuery((int)type) + "=" + year.ToString()).ExecuteAsync(new RestRequest("", Method.Get)).Result;

                                    dynamic responseContent = mediaType.getResponce((int)type, response.Content);

                                    if (responseContent.TotalResults > 0)
                                    {
                                        string closest_index = string.Empty;

                                        if (responseContent.TotalResults == 1)
                                        {
                                            closest_index = "0";
                                        }
                                        else
                                        {
                                            double?most_match = 0.0;
                                            foreach (dynamic result in responseContent.Results)
                                            {
                                                var title = string.Empty;
                                                var year2 = string.Empty;
                                                if (type == mediaTypes.collection)
                                                {
                                                    title = result.Name;
                                                }
                                                else if (type == mediaTypes.movie)
                                                {
                                                    title = result.Title;
                                                    try { year2 = result.ReleaseDate.Year.ToString(); } catch (Exception) { }
                                                }
                                                else if (type == mediaTypes.tv)
                                                {
                                                    title = result.OriginalName;
                                                    try { year2 = result.FirstAirDate.Year.ToString(); } catch (Exception) { }
                                                }
                                                if (year == 0 || year.ToString() == year2)
                                                {
                                                    title = title.Replace(".", " ");
                                                    title = Regex.Replace(title, @"[^0-9a-zA-Z\s&一-龯ぁ-んァ-ン\w!:/・]", string.Empty);
                                                    title = new Regex("[ ]{2,}", RegexOptions.None).Replace(title, " ");

                                                    double match_case = CalculateSimilarity(title.ToLower(), name.ToLower());
                                                    //Log(title.ToLower() + " := " + name.ToLower() + " with " + match_case.ToString(), "Msg");
                                                    if (match_case > most_match)
                                                    {
                                                        closest_index = responseContent.Results.IndexOf(result).ToString();
                                                        most_match    = match_case;
                                                    }
                                                }
                                            }
                                        }
                                        try
                                        {
                                            if (closest_index != string.Empty)
                                            {
                                                if (type == mediaTypes.tv)
                                                {
                                                    // Set your Apikey
                                                    if (s_number > 0)
                                                    {
                                                        //main.Log("https://api.themoviedb.org/3/tv/" + responseContent.Results[Convert.ToInt32(closest_index)].Id + "/season/" + s_number + "?api_key=9a49cbab6d640fd9483fbdd2abe22b94", msgType.warning);
                                                        RestResponse seasonResponse = new RestClient("https://api.themoviedb.org/3/tv/" + responseContent.Results[Convert.ToInt32(closest_index)].Id + "/season/" + s_number + "?api_key=9a49cbab6d640fd9483fbdd2abe22b94").ExecuteAsync(new RestRequest("", Method.Get)).Result;
                                                        try
                                                        {
                                                            dynamic seasonResponseContent = Newtonsoft.Json.Linq.JObject.Parse(seasonResponse.Content);
                                                            mainBitmap = new Bitmap(WebRequest.Create("https://image.tmdb.org/t/p/w500" + seasonResponseContent.poster_path).GetResponse().GetResponseStream());
                                                        }
                                                        catch (Exception ex)
                                                        {
                                                            main.log(ex.ToString(), msgType.error);
                                                            mainBitmap = new Bitmap(WebRequest.Create("https://image.tmdb.org/t/p/w500" + responseContent.Results[Convert.ToInt32(closest_index)].PosterPath).GetResponse().GetResponseStream());
                                                        }
                                                    }
                                                    else
                                                    {
                                                        mainBitmap = new Bitmap(WebRequest.Create("https://image.tmdb.org/t/p/w500" + responseContent.Results[Convert.ToInt32(closest_index)].PosterPath).GetResponse().GetResponseStream());
                                                    }

                                                    main.log("Poster Found.", msgType.success);
                                                    goto posterFound;
                                                }
                                                else
                                                {
                                                    mainBitmap = new Bitmap(WebRequest.Create("https://image.tmdb.org/t/p/w500" + responseContent.Results[Convert.ToInt32(closest_index)].PosterPath).GetResponse().GetResponseStream());
                                                    main.log("Poster Found.", msgType.success);
                                                    goto posterFound;
                                                }
                                            }
                                        }
                                        catch (Exception) { }
                                    }

                                    if (name.Contains(" "))
                                    {
                                        name = name.Substring(0, name.LastIndexOf(" "));
                                    }
                                    else
                                    {
                                        name = string.Empty;
                                    }

                                    main.update_progressbar(1, main.Mini_ProgressBar);
                                }
                            }
                        }
posterFound:
                        if (mainBitmap != null)
                        {
                            Bitmap convertedBitmap = new Bitmap(256, 256);

                            double scale = mainBitmap.Height / 256.0;

                            var width  = (int)(mainBitmap.Width / scale);
                            var height = (int)(mainBitmap.Height / scale);

                            var destRect  = new Rectangle(0, 0, width, height);
                            var destImage = new Bitmap(width, height);

                            using (var graphics = Graphics.FromImage(destImage))
                            {
                                graphics.CompositingMode    = System.Drawing.Drawing2D.CompositingMode.SourceCopy;
                                graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                                graphics.InterpolationMode  = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                                graphics.SmoothingMode      = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                                graphics.PixelOffsetMode    = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;

                                using (var wrapMode = new ImageAttributes())
                                {
                                    wrapMode.SetWrapMode(System.Drawing.Drawing2D.WrapMode.TileFlipXY);
                                    graphics.DrawImage(mainBitmap, destRect, 0, 0, mainBitmap.Width, mainBitmap.Height, GraphicsUnit.Pixel, wrapMode);
                                }
                            }

                            convertedBitmap.MakeTransparent(System.Drawing.Color.Transparent);
                            Graphics g = Graphics.FromImage(convertedBitmap);
                            g.DrawImage(destImage, new Point((256 - destImage.Width) / 2, 0));
                            SaveAsIcon(convertedBitmap, directory.FullName + @"\folder.ico");

                            File.WriteAllLines(iniPath, new String[] { "[.ShellClassInfo]", "IconFile=" + directory.FullName + @"\folder.ico,0", "[ViewState]", "Mode=", "Vid=", "FolderType=Videos" });

                            hideFile(iniPath);
                            hideFile(icoPath);

                            SHFOLDERCUSTOMSETTINGS folderSettings = new SHFOLDERCUSTOMSETTINGS
                            {
                                dwMask      = FolderCustomSettingsMask.IconFile,
                                pszIconFile = "folder.ico",
                                iIconIndex  = 0
                            };

                            SHGetSetFolderCustomSettings(ref folderSettings, directory.FullName, FolderCustomSettingsRW.ForceWrite);

                            main.log("Done.", msgType.success);
                        }
                        else
                        {
                            main.log("No Posters Found.", msgType.error);
                        }

                        main.update_progressbar(main.Mini_ProgressBar.Maximum, main.Mini_ProgressBar);
                    }

                    main.increment_progressbar(1, main.ProgressBar);
                }
                main.change_status("Done", msgType.success);
                //}
            }
            catch (Exception) { }//MessageBox.Show(e.ToString());
            finally { }
        }
Exemplo n.º 2
0
        public static void SetPoster(string the_directory = "", mediaTypes type = mediaTypes.movie, bool container = false)
        {
            Task set_poster_task = Task.Run(() => setPoster(the_directory, type, container));

            set_poster_task.Wait();
        }