Class containing episode information to be passed to and from providers.
Exemplo n.º 1
0
        public DownloadHandler(int epid)
        {
            using (SQLiteCommand command = new SQLiteCommand("select pr.progid, pluginid, pr.image as progimg, ep.duration, ep.image as epimg, pr.extid as progextid, ep.extid as epextid from episodes as ep, programmes as pr where ep.epid=@epid and ep.progid=pr.progid", FetchDbConn()))
            {
                command.Parameters.Add(new SQLiteParameter("@epid", epid));

                using (SQLiteMonDataReader reader = new SQLiteMonDataReader(command.ExecuteReader()))
                {
                    if (!reader.Read())
                    {
                        throw new DataNotFoundException(epid, "Episode does not exist");
                    }

                    this.pluginId     = new Guid(reader.GetString(reader.GetOrdinal("pluginid")));
                    this.progExtId    = reader.GetString(reader.GetOrdinal("progextid"));
                    this.episodeExtId = reader.GetString(reader.GetOrdinal("epextid"));

                    this.progInfo    = new Model.Programme(reader.GetInt32(reader.GetOrdinal("progid")));
                    this.episodeInfo = new Model.Episode(epid);

                    this.providerProgInfo = new ProgrammeInfo(this.progInfo);

                    if (reader.IsDBNull(reader.GetOrdinal("progimg")))
                    {
                        this.providerProgInfo.Image = null;
                    }
                    else
                    {
                        this.providerProgInfo.Image = Database.RetrieveImage(reader.GetInt32(reader.GetOrdinal("progimg")));
                    }

                    this.providerEpisodeInfo = new EpisodeInfo(this.episodeInfo);

                    if (reader.IsDBNull(reader.GetOrdinal("duration")))
                    {
                        this.providerEpisodeInfo.Duration = null;
                    }
                    else
                    {
                        this.providerEpisodeInfo.Duration = reader.GetInt32(reader.GetOrdinal("duration"));
                    }

                    if (reader.IsDBNull(reader.GetOrdinal("epimg")))
                    {
                        this.providerEpisodeInfo.Image = null;
                    }
                    else
                    {
                        this.providerEpisodeInfo.Image = Database.RetrieveImage(reader.GetInt32(reader.GetOrdinal("epimg")));
                    }

                    using (SQLiteCommand extCommand = new SQLiteCommand("select name, value from episodeext where epid=@epid", FetchDbConn()))
                    {
                        extCommand.Parameters.Add(new SQLiteParameter("@epid", epid));

                        using (SQLiteMonDataReader extReader = new SQLiteMonDataReader(extCommand.ExecuteReader()))
                        {
                            while (extReader.Read())
                            {
                                this.providerEpisodeInfo.ExtInfo.Add(extReader.GetString(extReader.GetOrdinal("name")), extReader.GetString(extReader.GetOrdinal("value")));
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 2
0
        public string DownloadProgramme(string progExtId, string episodeExtId, ProgrammeInfo progInfo, EpisodeInfo epInfo, string finalName)
        {
            XmlDocument rss = this.LoadFeedXml(new Uri(progExtId));
            XmlNode itemNode = this.ItemNodeFromEpisodeID(rss, episodeExtId);
            XmlNode enclosureNode = itemNode.SelectSingleNode("./enclosure");
            XmlAttribute urlAttrib = enclosureNode.Attributes["url"];
            Uri downloadUrl = new Uri(urlAttrib.Value);

            int extensionPos = downloadUrl.AbsolutePath.LastIndexOf(".", StringComparison.Ordinal);
            string extension = "mp3";

            if (extensionPos > -1)
            {
                extension = downloadUrl.AbsolutePath.Substring(extensionPos + 1);
            }

            using (TempFile downloadFile = new TempFile(extension))
            {
                finalName += "." + extension;

                this.doDownload = new DownloadWrapper(downloadUrl, downloadFile.FilePath);
                this.doDownload.DownloadProgress += this.DoDownload_DownloadProgress;
                this.doDownload.Download();

                while ((!this.doDownload.Complete) && this.doDownload.Error == null)
                {
                    Thread.Sleep(500);

                    if (this.doDownload.Cancelled)
                    {
                        return null;
                    }
                }

                if (this.doDownload.Error != null)
                {
                    if (this.doDownload.Error is WebException)
                    {
                        WebException webExp = (WebException)this.doDownload.Error;

                        if (webExp.Status == WebExceptionStatus.NameResolutionFailure)
                        {
                            throw new DownloadException(ErrorType.NetworkProblem, "Unable to resolve " + downloadUrl.Host + " to download this episode from.  Check your internet connection or try again later.");
                        }
                        else if (webExp.Response is HttpWebResponse)
                        {
                            HttpWebResponse webErrorResponse = (HttpWebResponse)webExp.Response;

                            switch (webErrorResponse.StatusCode)
                            {
                                case HttpStatusCode.Forbidden:
                                    throw new DownloadException(ErrorType.RemoteProblem, downloadUrl.Host + " returned a status 403 (Forbidden) in response to the request for this episode.  You may need to contact the podcast publisher if this problem persists.");
                                case HttpStatusCode.NotFound:
                                    throw new DownloadException(ErrorType.NotAvailable, "This episode appears to be no longer available.  You can either try again later, or cancel the download to remove it from the list and clear the error.");
                            }
                        }
                    }

                    throw this.doDownload.Error;
                }

                if (this.Progress != null)
                {
                    this.Progress(100, ProgressType.Processing);
                }

                File.Move(downloadFile.FilePath, finalName);
            }

            return extension;
        }
Exemplo n.º 3
0
        public EpisodeInfo GetEpisodeInfo(string progExtId, ProgrammeInfo progInfo, string episodeExtId)
        {
            XmlDocument rss = this.LoadFeedXml(new Uri(progExtId));
            XmlNamespaceManager namespaceMgr = this.CreateNamespaceMgr(rss);
            XmlNode itemNode = this.ItemNodeFromEpisodeID(rss, episodeExtId);

            if (itemNode == null)
            {
                return null;
            }

            XmlNode titleNode = itemNode.SelectSingleNode("./title");
            XmlNode pubDateNode = itemNode.SelectSingleNode("./pubDate");
            XmlNode enclosureNode = itemNode.SelectSingleNode("./enclosure");

            if (enclosureNode == null)
            {
                return null;
            }

            XmlAttribute urlAttrib = enclosureNode.Attributes["url"];

            if (urlAttrib == null)
            {
                return null;
            }

            try
            {
                new Uri(urlAttrib.Value);
            }
            catch (UriFormatException)
            {
                // The enclosure url is empty or malformed
                return null;
            }

            EpisodeInfo episodeInfo = new EpisodeInfo();

            if (titleNode == null || string.IsNullOrEmpty(titleNode.InnerText))
            {
                return null;
            }

            episodeInfo.Name = titleNode.InnerText;

            XmlNode descriptionNode = null;

            // If the item has an itunes:summary tag use this for the description (as it shouldn't contain HTML)
            if (namespaceMgr.HasNamespace("itunes"))
            {
                descriptionNode = itemNode.SelectSingleNode("./itunes:summary", namespaceMgr);
            }

            if (descriptionNode != null && !string.IsNullOrEmpty(descriptionNode.InnerText))
            {
                episodeInfo.Description = this.TidyUpWhitespace(descriptionNode.InnerText);
            }
            else
            {
                // Fall back to the standard description tag, but strip the HTML
                descriptionNode = itemNode.SelectSingleNode("./description");

                if (descriptionNode != null && !string.IsNullOrEmpty(descriptionNode.InnerText))
                {
                    episodeInfo.Description = this.TidyUpWhitespace(HtmlToText.ConvertHtml(descriptionNode.InnerText));
                }
            }

            try
            {
                XmlNode durationNode = itemNode.SelectSingleNode("./itunes:duration", namespaceMgr);

                if (durationNode != null)
                {
                    string[] splitDuration = durationNode.InnerText.Split(new char[] { '.', ':' });

                    if (splitDuration.GetUpperBound(0) == 0)
                    {
                        episodeInfo.Duration = Convert.ToInt32(splitDuration[0], CultureInfo.InvariantCulture);
                    }
                    else if (splitDuration.GetUpperBound(0) == 1)
                    {
                        episodeInfo.Duration = (Convert.ToInt32(splitDuration[0], CultureInfo.InvariantCulture) * 60) + Convert.ToInt32(splitDuration[1], CultureInfo.InvariantCulture);
                    }
                    else
                    {
                        episodeInfo.Duration = (((Convert.ToInt32(splitDuration[0], CultureInfo.InvariantCulture) * 60) + Convert.ToInt32(splitDuration[1], CultureInfo.InvariantCulture)) * 60) + Convert.ToInt32(splitDuration[2], CultureInfo.InvariantCulture);
                    }
                }
                else
                {
                    episodeInfo.Duration = null;
                }
            }
            catch
            {
                episodeInfo.Duration = null;
            }

            if (pubDateNode != null)
            {
                string pubDate = pubDateNode.InnerText.Trim();
                int zonePos = pubDate.LastIndexOf(" ", StringComparison.Ordinal);
                TimeSpan offset = new TimeSpan(0);

                if (zonePos > 0)
                {
                    string zone = pubDate.Substring(zonePos + 1);
                    string zoneFree = pubDate.Substring(0, zonePos);

                    switch (zone)
                    {
                        case "GMT":
                            // No need to do anything
                            break;
                        case "UT":
                            offset = new TimeSpan(0);
                            pubDate = zoneFree;
                            break;
                        case "EDT":
                            offset = new TimeSpan(-4, 0, 0);
                            pubDate = zoneFree;
                            break;
                        case "EST":
                        case "CDT":
                            offset = new TimeSpan(-5, 0, 0);
                            pubDate = zoneFree;
                            break;
                        case "CST":
                        case "MDT":
                            offset = new TimeSpan(-6, 0, 0);
                            pubDate = zoneFree;
                            break;
                        case "MST":
                        case "PDT":
                            offset = new TimeSpan(-7, 0, 0);
                            pubDate = zoneFree;
                            break;
                        case "PST":
                            offset = new TimeSpan(-8, 0, 0);
                            pubDate = zoneFree;
                            break;
                        default:
                            if (zone.Length >= 4 && (Information.IsNumeric(zone) || Information.IsNumeric(zone.Substring(1))))
                            {
                                try
                                {
                                    int value = int.Parse(zone, NumberStyles.AllowLeadingSign, CultureInfo.InvariantCulture);
                                    offset = new TimeSpan(value / 100, value % 100, 0);
                                    pubDate = zoneFree;
                                }
                                catch (FormatException)
                                {
                                    // The last part of the date was not a time offset
                                }
                            }

                            break;
                    }
                }

                // Strip the day of the week from the beginning of the date string if it is there,
                // as it can contradict the date itself.
                string[] days =
                        {
                            "mon,",
                            "tue,",
                            "wed,",
                            "thu,",
                            "fri,",
                            "sat,",
                            "sun,"
                        };

                foreach (string day in days)
                {
                    if (pubDate.StartsWith(day, StringComparison.OrdinalIgnoreCase))
                    {
                        pubDate = pubDate.Substring(day.Length).Trim();
                        break;
                    }
                }

                try
                {
                    episodeInfo.Date = DateTime.Parse(pubDate, null, DateTimeStyles.AssumeUniversal);
                    episodeInfo.Date = episodeInfo.Date.Value.Subtract(offset);
                }
                catch (FormatException)
                {
                    episodeInfo.Date = null;
                }
            }
            else
            {
                episodeInfo.Date = null;
            }

            episodeInfo.Image = this.RSSNodeImage(itemNode, namespaceMgr);

            return episodeInfo;
        }
Exemplo n.º 4
0
        public DownloadHandler(int epid)
        {
            using (SQLiteCommand command = new SQLiteCommand("select pr.progid, pluginid, pr.image as progimg, ep.duration, ep.image as epimg, pr.extid as progextid, ep.extid as epextid from episodes as ep, programmes as pr where ep.epid=@epid and ep.progid=pr.progid", FetchDbConn()))
            {
                command.Parameters.Add(new SQLiteParameter("@epid", epid));

                using (SQLiteMonDataReader reader = new SQLiteMonDataReader(command.ExecuteReader()))
                {
                    if (!reader.Read())
                    {
                        throw new DataNotFoundException(epid, "Episode does not exist");
                    }

                    this.pluginId = new Guid(reader.GetString(reader.GetOrdinal("pluginid")));
                    this.progExtId = reader.GetString(reader.GetOrdinal("progextid"));
                    this.episodeExtId = reader.GetString(reader.GetOrdinal("epextid"));

                    this.progInfo = new Model.Programme(reader.GetInt32(reader.GetOrdinal("progid")));
                    this.episodeInfo = new Model.Episode(epid);

                    this.providerProgInfo = new ProgrammeInfo(this.progInfo);

                    if (reader.IsDBNull(reader.GetOrdinal("progimg")))
                    {
                        this.providerProgInfo.Image = null;
                    }
                    else
                    {
                        this.providerProgInfo.Image = Database.RetrieveImage(reader.GetInt32(reader.GetOrdinal("progimg")));
                    }

                    this.providerEpisodeInfo = new EpisodeInfo(this.episodeInfo);

                    if (reader.IsDBNull(reader.GetOrdinal("duration")))
                    {
                        this.providerEpisodeInfo.Duration = null;
                    }
                    else
                    {
                        this.providerEpisodeInfo.Duration = reader.GetInt32(reader.GetOrdinal("duration"));
                    }

                    if (reader.IsDBNull(reader.GetOrdinal("epimg")))
                    {
                        this.providerEpisodeInfo.Image = null;
                    }
                    else
                    {
                        this.providerEpisodeInfo.Image = Database.RetrieveImage(reader.GetInt32(reader.GetOrdinal("epimg")));
                    }

                    using (SQLiteCommand extCommand = new SQLiteCommand("select name, value from episodeext where epid=@epid", FetchDbConn()))
                    {
                        extCommand.Parameters.Add(new SQLiteParameter("@epid", epid));

                        using (SQLiteMonDataReader extReader = new SQLiteMonDataReader(extCommand.ExecuteReader()))
                        {
                            while (extReader.Read())
                            {
                                this.providerEpisodeInfo.ExtInfo.Add(extReader.GetString(extReader.GetOrdinal("name")), extReader.GetString(extReader.GetOrdinal("value")));
                            }
                        }
                    }
                }
            }
        }