Exemple #1
0
        public bool ProcessTVDBResponse(Stream str, int?codeHint)
        {
            // Will have one or more series, and episodes
            // all wrapped in <Data> </Data>

            // e.g.:
            //<Data>
            // <Series>
            //  <id>...</id>
            //  etc.
            // </Series>
            // <Episode>
            //  <id>...</id>
            //  blah blah
            // </Episode>
            // <Episode>
            //  <id>...</id>
            //  blah blah
            // </Episode>
            // ...
            //</Data>

            if (!this.GetLock("ProcessTVDBResponse"))
            {
                return(false);
            }

            try
            {
                XmlReaderSettings settings = new XmlReaderSettings
                {
                    IgnoreComments   = true,
                    IgnoreWhitespace = true
                };
                XmlReader r = XmlReader.Create(str, settings);

                r.Read();

                while (!r.EOF)
                {
                    if ((r.Name == "Data") && !r.IsStartElement())
                    {
                        break; // that's it.
                    }
                    if (r.Name == "Series")
                    {
                        // The <series> returned by GetSeries have
                        // less info than other results from
                        // thetvdb.com, so we need to smartly merge
                        // in a <Series> if we already have some/all
                        // info on it (depending on which one came
                        // first).

                        SeriesInfo si = new SeriesInfo(r.ReadSubtree());
                        if (this.Series.ContainsKey(si.TVDBCode))
                        {
                            this.Series[si.TVDBCode].Merge(si, this.RequestLanguage);
                        }
                        else
                        {
                            this.Series[si.TVDBCode] = si;
                        }
                        r.Read();
                    }
                    else if (r.Name == "Episode")
                    {
                        Episode e = new Episode(null, null, r.ReadSubtree(), Args);
                        if (e.OK())
                        {
                            if (!this.Series.ContainsKey(e.SeriesID))
                            {
                                throw new TVDBException("Can't find the series to add the episode to (TheTVDB).");
                            }
                            SeriesInfo ser  = this.Series[e.SeriesID];
                            Season     seas = ser.GetOrAddSeason(e.ReadSeasonNum, e.SeasonID);

                            bool added = false;
                            for (int i = 0; i < seas.Episodes.Count; i++)
                            {
                                Episode ep = seas.Episodes[i];
                                if (ep.EpisodeID == e.EpisodeID)
                                {
                                    seas.Episodes[i] = e;
                                    added            = true;
                                    break;
                                }
                            }
                            if (!added)
                            {
                                seas.Episodes.Add(e);
                            }
                            e.SetSeriesSeason(ser, seas);
                        }
                        r.Read();
                    }
                    else if (r.Name == "xml")
                    {
                        r.Read();
                    }
                    else if (r.Name == "Data")
                    {
                        string time = r.GetAttribute("time");
                        if (time != null)
                        {
                            this.New_Srv_Time = int.Parse(time);
                        }
                        r.Read();
                    }
                    else
                    {
                        r.ReadOuterXml();
                    }
                }
            }
            catch (XmlException e)
            {
                if (!this.Args.Unattended)
                {
                    string message = "Error processing data from TheTVDB (top level).";
                    message += "\r\n" + e.Message;
                    String name = "";
                    if (codeHint.HasValue && Series.ContainsKey(codeHint.Value))
                    {
                        name += "Show \"" + Series[codeHint.Value].Name + "\" ";
                    }
                    if (codeHint.HasValue)
                    {
                        name += "ID #" + codeHint.Value + " ";
                    }
                    MessageBox.Show(name + message, "TVRename", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    // throw new TVDBException(e.Message);
                }
                return(false);
            }
            finally
            {
                this.Unlock("ProcessTVDBResponse");
            }
            return(true);
        }