Example #1
0
        public override bool getSeriesData(ref showClass show, ref Episode ep)
        {
            WebClient cli = new WebClient();
            XmlDocument xml = new XmlDocument();

            if (!File.Exists(episodeCache)) {
                if (show.ID != null && show.Year > 0) {
                    //I know which series - just download it
                    cli.DownloadFile(String.Format(detailUrl, show.ID, ep.season, show.Year, show.Lang),
                        episodeCache);
                    xml.Load(episodeCache);
                }
                else {
                    // Search for series
                    cli.DownloadFile(String.Format(queryUrl, ep.series, ep.season), searchCache);
                    xml.Load(searchCache);

                    List<showClass> shows = new List<showClass>();
                    XmlNodeList nodes;
                    // Shows found on episodeworld.com
                    if (xml.DocumentElement.Name == "search") {
                        nodes = xml.GetElementsByTagName("found");
                        foreach (XmlNode node in nodes) {
                            showClass sc = new showClass();
                            sc.ID = node.Attributes["id"].Value;
                            sc.Name = node.Attributes["name"].Value;
                            sc.Year = Int32.Parse(node.Attributes["year"].Value);
                            shows.Add(sc);
                        }

                        if (ep.series != ep.altSeries) {
                            // Check altSeries list as well
                            cli.DownloadFile(String.Format(queryUrl, ep.altSeries, ep.season), searchCache);
                            xml.Load(searchCache);

                            if ((xml.DocumentElement.ChildNodes.Count > 0) &&
                            (xml.DocumentElement.Name == "search")) {
                                nodes = xml.GetElementsByTagName("found");
                                foreach (XmlNode node in nodes) {
                                    showClass sc = new showClass();
                                    sc.ID = node.Attributes["id"].Value;
                                    sc.Name = node.Attributes["name"].Value;
                                    sc.Year = Int32.Parse(node.Attributes["year"].Value);
                                    shows.Add(sc);
                                }
                            }
                        }
                    }

                    if (xml.DocumentElement.Name == "series") {
                        // Found series directly.
                        FileInfo fi = new FileInfo(searchCache);
                        fi.MoveTo(episodeCache);
                    }
                    else {
                        show = chooseSeries(ep, shows);
                        if (show != null) {
                            cli.DownloadFile(
                                String.Format(detailUrl, show.ID, ep.season, show.Year, show.Lang), episodeCache
                                );
                        }
                        shows.Clear();
                    }
                }
            }

            // Check if cache is outdated
            if (File.Exists(episodeCache)) {
                DateTime dt = File.GetLastWriteTime(episodeCache);
                if (DateTime.Now.Subtract(dt).TotalDays > 3) {
                    // Log.Add( i18n.t( "oparse_older", ep.series) );
                    File.Delete(episodeCache);
                    cli.DownloadFile(String.Format(detailUrl, show.ID, ep.season, show.Year, show.Lang),
                        episodeCache);
                }
                if (File.Exists(episodeCache)) {
                    xml.Load(episodeCache);
                }

                if (xml.DocumentElement.Attributes.Count > 1) {
                    if (show.Lang == "") {
                        List<String> languages = new List<String>();
                        XmlNodeList nodes = xml.GetElementsByTagName("episode");
                        foreach (XmlNode node in nodes) {
                            String lang = node.Attributes["language"].Value;
                            if (!languages.Contains(lang)) {
                                languages.Add(lang);
                            }
                        }

                        if (languages.Count > 1) {
                            List<showClass> shows = new List<showClass>();
                            foreach (String lang in languages) {
                                showClass showTemp = new showClass();
                                showTemp.ID = xml.DocumentElement.Attributes["id"].Value;
                                showTemp.Name = xml.DocumentElement.Attributes["name"].Value;
                                showTemp.Year = Int32.Parse(xml.DocumentElement.Attributes["year"].Value);
                                showTemp.Lang = lang;
                                shows.Add(showTemp);
                            }
                            show = chooseSeries(ep, shows);
                        }
                        else {
                            show.ID = xml.DocumentElement.Attributes["id"].Value;
                            show.Name = xml.DocumentElement.Attributes["name"].Value;
                            show.Year = Int32.Parse(xml.DocumentElement.Attributes["year"].Value);
                            show.Lang = languages[0];
                        }
                    }
                    ep.language = show.Lang;
                    ep.series = show.Name;
                }
            }

            cli.Dispose();

            if (ep.language == null || ep.language == "")
                ep.language = "english";

            // Find title for episode
            XmlNodeList episodes = xml.GetElementsByTagName("episode");
            if (episodes.Count > 0) {
                bool foundEp = false;
                // Search episode
                foreach (XmlNode node in episodes) {
                    if ((node.Attributes["season"].Value == ep.season.ToString()) &&
                         (node.Attributes["episode"].Value == ep.episode.ToString()) &&
                         (node.Attributes["language"].Value == ep.language.ToString()) &&
                         (node.Attributes["special"].Value == "false")
                        ) {
                        foundEp = true;
                        ep.title = node.Attributes["title"].Value;
                        break;
                    }
                }

                if (!foundEp) {
                    // Not found - maybe special episode after all ?
                    foreach (XmlNode node in episodes) {
                        if ((node.Attributes["season"].Value == ep.season.ToString()) &&
                             (node.Attributes["episode"].Value == ep.episode.ToString()) &&
                             (node.Attributes["language"].Value == ep.language.ToString()) &&
                             (node.Attributes["special"].Value == "true")
                            ) {
                            foundEp = true;
                            ep.title = node.Attributes["title"].Value;
                            break;
                        }
                    }
                }

                if (foundEp) {
                    if (ep.episodes.Length > 1) {
                        if (ep.title.EndsWith(")")) {
                            ep.title = ep.title.Substring(0, ep.title.LastIndexOf("("));
                        }
                    }
                    ep.title = ep.title.Replace(".i.", "");
                    if (ep.title.IndexOf("aka") > 0) {
                        ep.title = Eregi.replace("\\(aka([^)]*)\\)", "", ep.title);
                    }
                    return true;
                }
            }

            return false;
            // Done
        }
        public override bool getSeriesData(ref showClass show, ref Episode ep)
        {
            XmlDocument xml = new XmlDocument();
            String url = "";

            episodeCache = String.Format(baseCache, seriesHash, "all");

            // Fix if older information is received
            if (show.Lang.Length > 2) {
                show.Lang = (String)languages[show.Lang.ToString()];
            }

            if (show.ID == String.Empty) {
                File.Delete(episodeCache);
            }

            if (!File.Exists(episodeCache)) {
                if (show.ID != String.Empty) {
                    //I know which series - just download it
                    url = String.Format(detailUrl, randomMirror(), show.ID, show.Lang);
                    downloadAndExtract(url, show.Lang);
                    xml.Load(episodeCache);
                }
                else {
                    // Search for series
                    xml.LoadXml(searchQuery(ep.series));

                    List<showClass> shows = new List<showClass>();
                    XmlNodeList nodes = xml.GetElementsByTagName("Series");
                    // Shows found on thetvdb.com
                    if (nodes.Count != 1) {
                        foreach (XmlNode node in nodes) {
                            showClass sc = new showClass();
                            sc.ID = node.SelectSingleNode("seriesid").InnerText;
                            sc.Name = node.SelectSingleNode("SeriesName").InnerText;
                            if (node.SelectSingleNode("FirstAired") == null)
                                continue;
                            sc.Year = Int32.Parse(node.SelectSingleNode("FirstAired").InnerText.Substring(0, 4));
                            sc.Lang = node.SelectSingleNode("language").InnerText;
                            shows.Add(sc);
                        }

                        // Check altSeries list as well
                        if (ep.series != ep.altSeries) {
                            xml.LoadXml(searchQuery(ep.altSeries));

                            nodes = xml.GetElementsByTagName("Series");

                            if (nodes.Count > 1) {
                                foreach (XmlNode node in nodes) {
                                    showClass sc = new showClass();
                                    sc.ID = node.SelectSingleNode("seriesid").InnerText;
                                    sc.Name = node.SelectSingleNode("SeriesName").InnerText;
                                    sc.Year = Int32.Parse(node.SelectSingleNode("FirstAired").InnerText.Substring(0, 4));
                                    sc.Lang = node.SelectSingleNode("language").InnerText;
                                    shows.Add(sc);
                                }
                            }
                        }
                    }

                    if (nodes.Count != 1 && shows.Count == 0) {
                        while (true) {
                            InputDialog input = new InputDialog("Couldn't find the matching series - please enter a valid series name:", Application.ProductName, ep.series);
                            if (input.ShowDialog() == DialogResult.OK) {
                                String seriesName = input.value;
                                xml.LoadXml(searchQuery(seriesName));
                                nodes = xml.GetElementsByTagName("Series");
                                if (nodes.Count > 0) {
                                    foreach (XmlNode node in nodes) {
                                        showClass sc = new showClass();
                                        sc.ID = node.SelectSingleNode("seriesid").InnerText;
                                        sc.Name = node.SelectSingleNode("SeriesName").InnerText;
                                        if (node.SelectSingleNode("FirstAired") == null)
                                            continue;
                                        sc.Year = Int32.Parse(node.SelectSingleNode("FirstAired").InnerText.Substring(0, 4));
                                        sc.Lang = node.SelectSingleNode("language").InnerText;
                                        shows.Add(sc);
                                    }
                                    break;
                                }
                            }
                            else {
                                break;
                            }
                        }
                    }

                    if (nodes.Count == 1) {
                        // Found series directly.
                        show.ID = xml.SelectSingleNode("//Data/Series/seriesid").InnerText;
                        show.Name = xml.SelectSingleNode("//Data/Series/SeriesName").InnerText;
                        XmlNode showNode = xml.SelectSingleNode("//Data/Series/FirstAired");
                        if (showNode != null) {
                            show.Year = Int32.Parse(showNode.InnerText.Substring(0, 4));
                        }
                        show.Lang = xml.SelectSingleNode("//Data/Series/language").InnerText;
                        shows.Add(show);
                    }

                    show = chooseSeries(ep, shows);
                    if (show != null) {
                        ep.language = show.Lang;
                        ep.series = show.Name;
                        url = String.Format(detailUrl, randomMirror(), show.ID, ep.language);
                        downloadAndExtract(url, show.Lang);
                    }
                    shows.Clear();
                }
            }

            // Check if cache is available
            if (File.Exists(episodeCache)) {
                DateTime dt = File.GetLastWriteTime(episodeCache);
                // Check if cache is outdated
                if (DateTime.Now.Subtract(dt).TotalDays > 5) {
                    try {
                        url = String.Format(detailUrl, randomMirror(), show.ID, show.Lang);
                        downloadAndExtract(url, show.Lang);
                    }
                    catch (Exception E) {
                        Thread.Sleep(1000);
                        #if DEBUG
                        Log.Error("Could not download updated episode file for " + ep.ToString(), E);
                        #endif
                    }
                }
                if (File.Exists(episodeCache)) {
                    xml.Load(episodeCache);
                }
            }

            if (ep.language == null || ep.language == "")
                ep.language = show.Lang;

            show.Name = xml.SelectSingleNode("//Data/Series/SeriesName").InnerText;
            show.ID = xml.SelectSingleNode("//Data/Series/id").InnerText;
            XmlNode yearNode = xml.SelectSingleNode("//Data/Series/FirstAired");
            if (yearNode != null && yearNode.InnerText != String.Empty && yearNode.InnerText.Length >= 4) {
                show.Year = Int32.Parse(yearNode.InnerText.Substring(0, 4));
            }

            // Find title for episode
            XmlNodeList episodes = xml.GetElementsByTagName("Episode");
            if (episodes.Count > 0) {
                // Search episode
                foreach (XmlNode episodeXml in episodes) {
                    Int32 season = Int32.Parse(episodeXml.SelectSingleNode("SeasonNumber").InnerText);
                    Int32 episode = Int32.Parse(episodeXml.SelectSingleNode("EpisodeNumber").InnerText);

                    if (season == ep.season && episode == ep.episode) {
                        ep.title = episodeXml.SelectSingleNode("EpisodeName").InnerText;

                        if (ep.episodes.Length > 1) {
                            if (ep.title.EndsWith(")")) {
                                ep.title = ep.title.Substring(0, ep.title.LastIndexOf("("));
                            }
                        }
                        ep.title = ep.title.Replace(".i.", "");
                        if (ep.title.IndexOf("aka") > 0) {
                            ep.title = Eregi.replace("\\(aka([^)]*)\\)", "", ep.title);
                        }

                        return true;
                    }
                }
            }

            return false;
            // Done
        }
        public void getEpisodeData(ref Episode ep)
        {
            try {
                showClass show = new showClass();
                show.Name = ep.series;
                String seriesOld = ep.series.ToLower();

                if (seriesList.ContainsKey(seriesOld)) {
                    show = (showClass)seriesList[seriesOld];
                    ep.series = show.Name;
                }
                else if (seriesList.ContainsKey(ep.altSeries.ToLower())) {
                    show = (showClass)seriesList[ep.altSeries.ToLower()];
                    ep.series = show.Name;
                }
                else {
                    // No data found yet.
                }
                seriesHash = MD5.createHash(show.Name);
                episodeCache = String.Format(baseCache, seriesHash, ep.season);

                if (ep.special) return;

                bool hr = getSeriesData(ref show, ref ep);

                if (show != null && show.ID != "") {
                    bool listChanged = false;
                    if (!seriesList.ContainsKey(ep.series.ToLower())) {
                        seriesList.Add(ep.series.ToLower(), show);
                        listChanged = true;
                    }
                    if (!seriesList.ContainsKey(seriesOld)) {
                        seriesList.Add(seriesOld, show);
                        listChanged = true;
                    }
                    if (!seriesList.ContainsKey(show.Name.ToLower())) {
                        seriesList.Add(show.Name.ToLower(), show);
                        listChanged = true;
                    }
                    if (listChanged)
                        Settings.SetValue(parserDataCache, seriesList);
                }

                /*if (!hr)
                {
                    this.renameGeneric(ref ep);
                }*/

                MatchCollection mcol = null;
                Match m = null;
                Regex uni = new Regex("&#([0-9]+);");
                mcol = uni.Matches(ep.title);
                if (mcol.Count > 0) {
                    Regex real = new Regex("\\(([-a-zA-Z0-9.,%:;!?'\"+() ]{5,})\\)");
                    m = real.Match(ep.title);
                    if (m.Success) {
                        ep.title = m.Groups[1].Captures[0].Value;
                    }
                }
            }
            catch (Exception E) {
                #if DEBUG
                Log.Error("Error fetching online data for:\n" +
                    ep.filename + "\n" +
                    ep.series + " " + ep.season + "x" + ep.episode + " " + ep.title,
                    E);
                #endif
            }
        }
 public abstract bool getSeriesData(ref showClass show, ref Episode ep);
Example #5
0
 private void showList_SelectedIndexChanged(object sender, System.EventArgs e)
 {
     if (showList.SelectedItem != null) {
         selectedShow = (showClass)showList.SelectedItem;
     }
 }
Example #6
0
 private void SelectShow_Load(object sender, System.EventArgs e)
 {
     if (showList.Items.Count > 0) {
         selectedShow = showList.Items[0] as showClass;
         showList.SelectedIndex = 0;
     }
 }