/// <summary>
        /// The function receives a subtitles page id number, a list of user selected
        /// languages and the current subtitles list and adds all found subtitles matching
        /// the language selection to the subtitles list.
        /// </summary>
        /// <param name="subtitlePageId">subtitle page id</param>
        /// <param name="languageList">list of languages</param>
        /// <returns></returns>
        private static IEnumerable <SubtitleData> GetAllSubtitles(string subtitlePageId, ICollection <string> languageList)
        {
            // return object
            var subtitlesList = new List <SubtitleData>();

            // Retrieve the subtitles page (html)
            var subtitlePage = GetUrl(BaseUrl + subtitlePageId + "?m=subtitles#!subtitles");

            if (subtitlePage == null)
            {
                return(subtitlesList);
            }

            // Compile the RegEx string
            var subtitleListRegex = new Regex(SubtitleListPattern);

            // Create a list of all subtitles found on page
            var foundSubtitles = subtitleListRegex.Matches(subtitlePage);

            // Subtitles
            foreach (var foundSubtitle in foundSubtitles)
            {
                var groupCollection = subtitleListRegex.Match(foundSubtitle.ToString()).Groups;
                var fid             = groupCollection["fid"].Value;
                var language        = groupCollection["language"].Value;
                var title           = groupCollection["title"].Value;

                // skip if title is unknown
                if (title == null || title.Length == 0)
                {
                    continue;
                }

                // Check if the subtitles found match one of our languages was selected by the user
                if (language != null)
                {
                    string languageTwoLetter;
                    ToOpenSubtitlesTwoLetters.TryGetValue(language, out languageTwoLetter);
                    if (languageList.Contains(languageTwoLetter))
                    {
                        subtitlesList.Add(new SubtitleData(title, fid, language));
                    }
                }
            }

            return(subtitlesList);
        }
        public List <FileInfo> SaveSubtitle(Subtitle subtitle)
        {
            var    fileName     = subtitle.FileName;
            var    subtitleId   = subtitle.Id;
            var    languageName = subtitle.LanguageCode;
            string languageCode;

            ToOpenSubtitlesTwoLetters.TryGetValue(languageName, out languageCode);

            var subtitleData = new SubtitleData(fileName, subtitleId, languageName);
            var subtitles    = new List <SubtitleData>(1)
            {
                subtitleData
            };

            var savedSubtitle = DownloadSubtitles(subtitles, 0);

            return(savedSubtitle);
        }
        /// <summary>
        /// Same as getAllSubtitles() but receives season and episode numbers and find them.
        /// </summary>
        /// <param name="subtitlePageId">subtitle page id</param>
        /// <param name="languageList">list of languages</param>
        /// <param name="season">season number</param>
        /// <param name="episode">episode number</param>
        /// <returns></returns>
        private static IEnumerable <SubtitleData> GetAllTvSubtitles(string subtitlePageId, ICollection <string> languageList, string season, string episode)
        {
            // return object
            var subtitlesList = new List <SubtitleData>();

            // Retrieve the subtitles page (html)
            var subtitlePage = GetUrl(BaseUrl + "viewseries.php?id=" + subtitlePageId + "&m=subtitles#");

            if (subtitlePage == null)
            {
                return(subtitlesList);
            }

            // Compile the RegEx string
            var tvSeasonRegex     = new Regex(TvSeasonPattern);
            var tvEpisodeRegex    = new Regex(TvEpisodePattern);
            var subtitleListRegex = new Regex(SubtitleListPattern);

            // Retrieve the requested season
            var foundSeasons = tvSeasonRegex.Matches(subtitlePage);

            // Seaosons
            foreach (var foundSeason in foundSeasons)
            {
                var tvSeasonGroups = tvSeasonRegex.Match(foundSeason.ToString()).Groups;
                var seasonLink     = tvSeasonGroups["season_link"].Value;
                var seasonNum      = tvSeasonGroups["season_num"].Value;

                if (seasonNum.Equals(season))
                {
                    // Retrieve the requested episode
                    subtitlePage = GetUrl(BaseUrl + "getajax.php?seasonid=" + seasonLink);
                    if (subtitlePage == null)
                    {
                        break;
                    }

                    var foundEpisodes = tvEpisodeRegex.Matches(subtitlePage);

                    // Episodes
                    foreach (var foundEpisode in foundEpisodes)
                    {
                        var episodeGroups = tvEpisodeRegex.Match(foundEpisode.ToString()).Groups;
                        var episodeLink   = episodeGroups["episode_link"].Value;
                        var episodeNum    = episodeGroups["episode_num"].Value;

                        if (episodeNum.Equals(episode))
                        {
                            subtitlePage =
                                GetUrl(BaseUrl + "getajax.php?episodedetails=" + episodeLink);
                            if (subtitlePage == null)
                            {
                                break;
                            }

                            // Create a list of all subtitles found on page
                            var foundSubtitles = subtitleListRegex.Matches(subtitlePage);

                            // Subtitles
                            foreach (var foundSubtitle in foundSubtitles)
                            {
                                var subtitleGroups = subtitleListRegex.Match(foundSubtitle.ToString()).Groups;
                                var fid            = subtitleGroups["fid"].Value;
                                var language       = subtitleGroups["language"].Value;
                                var title          = subtitleGroups["title"].Value;

                                // skip if title is unknown
                                if (title == null || title.Length == 0)
                                {
                                    continue;
                                }

                                // Check if the subtitles found match one of our languages was selected by the user
                                if (language != null)
                                {
                                    string languageTwoLetter;
                                    ToOpenSubtitlesTwoLetters.TryGetValue(language, out languageTwoLetter);
                                    if (languageList.Contains(languageTwoLetter))
                                    {
                                        subtitlesList.Add(new SubtitleData(title, fid, language));
                                    }
                                }
                            }
                        }
                    }
                }
            }

            return(subtitlesList);
        }