Beispiel #1
0
        private void ParseRss(FeedItem feedItem, string strDownloadlocation)
        {
            log4net.NDC.Push("ParseRss");
            try
            {
                //RssReader rssReader = new RssReader(feedItem,false,null);

                //rssReader.RdfMode = false;

                if (SetFeedStatus != null)
                {
                    SetFeedStatus(lvi, 1, feedItem.Pubdate, FormStrings.RetrievingRSS);
                }
                //RssFeed feed = rssReader.Retrieve();
                try
                {
                    Rss.RssFeed feed = Utils.GetFeed(feedItem);

                    if (feed != null)
                    {
                        // first clean up the history
                        CleanHistory(feedItem, feed);

                        // set the description of the feed


                        //if ( feed.ErrorMessage == null || feed.ErrorMessage == "" )
                        //{
                        if (feed.Exceptions.Count == 0)
                        {
                            //RssItems items = feed.Items;
                            //int intItems = items.Count;

                            int intItemCount = 0;
                            //for(int q=0;q<intItems;q++)
                            //{
                            feed.Channels[0].Items.Sort();
                            foreach (Rss.RssItem rssItem in feed.Channels[0].Items)
                            {
                                if (SetFeedStatus != null)
                                {
                                    SetFeedStatus(lvi, 1, feedItem.Pubdate, "Parsing items");
                                }
                                //hist.ReRead();
                                //Application.DoEvents();
                                //RssItem rssItem = items[q];


                                #region // Default : Enclosures
                                //if(rssItem.Enclosures != null)
                                //{
                                if (rssItem.Enclosure != null)
                                {
                                    if (intItemCount < feedItem.RetrieveNumberOfFiles || feedItem.RetrieveNumberOfFiles == 0)
                                    {
                                        intItemCount++;

                                        //    EnclosureItem enc = rssItem.Enclosures[y];
                                        bool       validUri = false;
                                        System.Uri uri      = null;
                                        try
                                        {
                                            uri      = new Uri(rssItem.Enclosure.Url);
                                            validUri = true;
                                        }
                                        catch (UriFormatException ex)
                                        {
                                            boolError = true;
                                            if (Settings.Default.LogLevel > 0)
                                            {
                                                log.Error(String.Format("Invalid enclosure url for {0} ({1})", feedItem.Title, rssItem.Enclosure.Url), ex);
                                            }
                                        }
                                        if (validUri)
                                        {
                                            // check if the enclosure is already in the history
                                            if (Settings.Default.History[rssItem.GetHashCode().ToString()] == null)
                                            {
                                                string strPath     = GetValidFilename(uri.LocalPath.Substring(uri.LocalPath.LastIndexOf("/") + 1));
                                                string strFilename = Path.Combine(strDownloadlocation, strPath);

                                                if (!Directory.Exists(strDownloadlocation))
                                                {
                                                    try
                                                    {
                                                        Directory.CreateDirectory(strDownloadlocation);
                                                    }
                                                    catch (DirectoryNotFoundException ex)
                                                    {
                                                        if (Settings.Default.LogLevel > 1)
                                                        {
                                                            log.Error("Error creating directory", ex);
                                                        }
                                                        boolError = true;
                                                        return;
                                                    }
                                                }

                                                DownloadPackage downloadPackage  = GetDownloadPackage(rssItem, feedItem, strDownloadlocation);
                                                long            longDownloadsize = downloadPackage.DownloadSize;

                                                if (downloadPackage.strFilename != null)
                                                {
                                                    strFilename = downloadPackage.strFilename;
                                                }

                                                if (longDownloadsize > 0)
                                                {
                                                    if (feedItem.Authenticate == true)
                                                    {
                                                        string strPassword = EncDec.Decrypt(feedItem.Password, feedItem.Username);
                                                        FileFound(feedItem, rssItem.Enclosure.Url, longDownloadsize, downloadPackage.strFilename, rssItem, downloadPackage.ByteRanging);
                                                    }
                                                    else
                                                    {
                                                        FileFound(feedItem, rssItem.Enclosure.Url, longDownloadsize, downloadPackage.strFilename, rssItem, downloadPackage.ByteRanging);
                                                    }
                                                }

                                                // update the FeedItem with an last update date and the description
                                                feedItem.Pubdate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                                                if (feed.Channels[0].Description != null)
                                                {
                                                    feedItem.Description = feed.Channels[0].Description;
                                                }
                                                Settings.Default.Feeds[feedItem.GUID] = feedItem;
                                            }
                                        }
                                    }
                                }

                                #endregion
                            }
                        }
                    }
                }
                catch (WebException wex)
                {
                    if (Settings.Default.LogLevel > 0)
                    {
                        log.Error("Retriever", wex);
                    }
                }
            }
            catch (ThreadAbortException)
            {
                //MessageBox.Show("TA");
            }
            log4net.NDC.Pop();
        }
Beispiel #2
0
        private DownloadPackage GetDownloadPackage(Rss.RssItem rssItem, FeedItem feedItem, string strDownloadlocation)
        {
            bool          boolAcceptableFile    = false;
            bool          byteRanging           = true;
            string        strFilenameWithoutExt = null;
            string        strFilenameExt        = null;
            string        strPath = null;
            DirectoryInfo dirInfo = null;

            FileInfo[] fileInfo   = null;
            long       longReturn = 0;
            //bool boolGuid = false;

            string strFilename = null;

            // new
            WebResponse response = null;

            // first try a HEAD call (less intrusive)
            if (feedItem.Authenticate == true)
            {
                string strPassword = EncDec.Decrypt(feedItem.Password, feedItem.Username);
                response = GetWebResponseObjectHEAD(rssItem.Enclosure.Url, feedItem.Username, strPassword);
            }
            else
            {
                response = GetWebResponseObjectHEAD(rssItem.Enclosure.Url, "", "");
            }


            // the HEAD call didn't seem to work. We'll use a GET now
            if (response == null)
            {
                if (feedItem.Authenticate == true)
                {
                    string strPassword = EncDec.Decrypt(feedItem.Password, feedItem.Username);
                    response = GetWebResponseObjectGET(rssItem.Enclosure.Url, feedItem.Username, strPassword);
                }
                else
                {
                    response = GetWebResponseObjectGET(rssItem.Enclosure.Url, "", "");
                }
            }

            if (response != null)
            {
                long longDownloadsize = response.ContentLength;
                if (longDownloadsize == -1)
                {
                    longDownloadsize = 1;
                }


                WebHeaderCollection responseHeaders = response.Headers;
                string[]            strAcceptRange  = responseHeaders.GetValues("Accept-Ranges");
                if (strAcceptRange != null && strAcceptRange[0] == "none")
                {
                    // no byte ranging
                    byteRanging = false;
                }

                // check if there is content-disposition header
                string[] strContDisp   = responseHeaders.GetValues("content-disposition");
                string   strAttachment = "";
                string   strHeader     = "";
                if (strContDisp != null && strContDisp.Length > 0)
                {
                    for (int l = 0; l < strContDisp.Length; l++)
                    {
                        strHeader = strContDisp[l];
                        if (strHeader.ToLower().StartsWith("attachment"))
                        {
                            // attachment header

                            strAttachment = strHeader.Substring(strHeader.ToLower().IndexOf("filename=") + 9);
                        }
                    }
                }
                if (strAttachment != "")
                {
                    if (feedItem.UseTitleForFiles)
                    {
                        FileInfo f = new FileInfo(strAttachment);
                        strPath = Utils.GetValidFileName(rssItem.Title + f.Extension);
                    }
                    else
                    {
                        strPath = strAttachment;
                        strPath = Utils.GetValidFileName(strPath);
                    }
                    strFilename = strDownloadlocation + "\\" + strPath;
                    if (strFilename.IndexOf(".") > 0)
                    {
                        FileInfo f = new FileInfo(strFilename);
                        strFilenameWithoutExt = f.FullName.Substring(0, f.FullName.Length - f.Extension.Length);
                        //strFilenameWithoutExt = strFilename.Substring(0, strFilename.LastIndexOf("."));
                        strFilenameExt = f.Extension.Substring(1);
                        //strFilenameExt = strFilename.Substring(strFilename.LastIndexOf(".") + 1);
                    }
                    else
                    {
                        strFilenameWithoutExt = strFilename;
                        strFilenameExt        = "";
                    }
                }
                else
                {
                    try
                    {
                        Uri    uriEnclosure    = new Uri(rssItem.Enclosure.Url);
                        string strEnclosureUrl = uriEnclosure.GetLeftPart(UriPartial.Path);

                        if (feedItem.UseTitleForFiles)
                        {
                            strPath = strEnclosureUrl.Substring(strEnclosureUrl.LastIndexOf("/") + 1);
                            FileInfo f = new FileInfo(strPath);
                            strPath = Utils.GetValidFileName(rssItem.Title + f.Extension);
                        }
                        else
                        {
                            strPath = strEnclosureUrl.Substring(strEnclosureUrl.LastIndexOf("/") + 1);
                        }
                        if (strPath == "")
                        {
                            // check the query
                            strEnclosureUrl = uriEnclosure.GetLeftPart(UriPartial.Query);
                            if (feedItem.UseTitleForFiles)
                            {
                                strPath = strEnclosureUrl.Substring(strEnclosureUrl.LastIndexOf("/") + 1);
                                FileInfo f = new FileInfo(strPath);
                                strPath = Utils.GetValidFileName(rssItem.Title + f.Extension);
                            }
                            else
                            {
                                strPath = strEnclosureUrl.Substring(strEnclosureUrl.LastIndexOf("/") + 1);
                            }
                        }
                        // empty path? not good. Generate one

                        //strPath = System.Web.HttpUtility.UrlDecode(strPath);
                        strFilename = strDownloadlocation + "\\" + strPath;
                        FileInfo fileInfo1 = new FileInfo(strFilename);
                        strFilenameWithoutExt = fileInfo1.FullName.Substring(0, fileInfo1.FullName.Length - fileInfo1.Extension.Length);
                        //strFilenameWithoutExt = strFilename.Substring(0, strFilename.LastIndexOf("."));
                        strFilenameExt = fileInfo1.Extension.Substring(1);
                        //strFilenameExt = strFilename.Substring(strFilename.LastIndexOf(".") + 1);
                        //strFilenameWithoutExt = strFilename.Substring(0, strFilename.LastIndexOf("."));
                        //strFilenameExt = strFilename.Substring(strFilename.LastIndexOf(".") + 1);
                    } catch
                    {
                        strFilename           = "";
                        strFilenameExt        = "";
                        strFilenameWithoutExt = "";
                        boolAcceptableFile    = false;
                    }
                }

                if (Settings.Default.DefaultMediaAction == 0)
                {
                    boolAcceptableFile = true;
                }
                else
                {
                    foreach (string extension in Utils.AudioExtensions)
                    {
                        if (extension.ToUpper() == strFilenameExt.ToUpper())
                        {
                            boolAcceptableFile = true;
                            break;
                        }
                    }
                    if (!boolAcceptableFile)
                    {
                        // check if it is a video file
                        foreach (string extension in Utils.VideoExtensions)
                        {
                            if (extension.ToUpper() == strFilenameExt.ToUpper())
                            {
                                boolAcceptableFile = true;
                                break;
                            }
                        }
                    }
                }

                if (boolAcceptableFile)
                {
                    try
                    {
                        dirInfo = new System.IO.DirectoryInfo(strDownloadlocation);

                        fileInfo = dirInfo.GetFiles(strPath);
                    }
                    catch (Exception fex)
                    {
                        if (Settings.Default.LogLevel > 0)
                        {
                            log.Error("Retriever", fex);
                        }
                        strFilenameExt = "";
                    }


                    // check if the file is available on the filesystem
                    bool boolContinue = true;
                    if (fileInfo.Length > 0)
                    {
                        if ((fileInfo.Length > 0 && fileInfo[0].Length != longDownloadsize))
                        {
                            // there is a file with the same name
                            switch (Settings.Default.DuplicateAction)
                            {
                            case 1:
                                strFilename  = strFilenameWithoutExt + DateTime.Now.ToString("yyyyMMddTHHMMss") + "." + strFilenameExt;
                                boolContinue = true;
                                break;

                            default:
                                boolContinue = false;
                                break;
                            }
                        }
                        else if (fileInfo.Length > 0)
                        {
                            // add the item to the history
                            HistoryItem historyItem = new HistoryItem();
                            historyItem.FeedGUID = feedItem.GUID;
                            historyItem.FeedUrl  = feedItem.Url;
                            historyItem.Title    = rssItem.Title;
                            historyItem.Hashcode = rssItem.GetHashCode();
                            //historyItem.LocalFilename = fileInfo[0].FullName;
                            historyItem.ItemDate = fileInfo[0].CreationTime.ToString("yyyy-MM-dd HH:MM:ss");
                            historyItem.FileName = fileInfo[0].Name;
                            //historyItem.URL = enc.EnclosureURL;
                            Settings.Default.History.Add(historyItem);
                            boolContinue = false;
                        }
                        else
                        {
                            boolContinue = false;
                        }
                    }
                    if (boolContinue)
                    {
                        // did the user specify a text filter on the item?
                        if (feedItem.Textfilter != null && feedItem.Textfilter != "")
                        {
                            System.Text.RegularExpressions.Regex regEx = new System.Text.RegularExpressions.Regex(feedItem.Textfilter);
                            if (regEx.Match(rssItem.Description).Success || regEx.Match(rssItem.Title).Success)
                            {
                                //	if(rssItem.Description.ToUpper().IndexOf(feedItem.textFilter.ToUpper()) >= 0 || rssItem.Title.ToUpper().IndexOf(feedItem.textFilter.ToUpper()) >= 0)
                                //	{
                                longReturn = longDownloadsize;
                            }
                            else if (rssItem.Description.ToUpper().IndexOf(feedItem.Textfilter.ToUpper()) >= 0 || rssItem.Title.ToUpper().IndexOf(feedItem.Textfilter.ToUpper()) >= 0)
                            {
                                longReturn = longDownloadsize;
                            }
                        }
                        else
                        {
                            longReturn = longDownloadsize;
                        }
                    }
                }
            }
            DownloadPackage downloadPackage = new DownloadPackage();

            downloadPackage.ByteRanging  = byteRanging;
            downloadPackage.DownloadSize = longReturn;
            downloadPackage.strFilename  = strFilename;
            return(downloadPackage);
        }
Beispiel #3
0
        public void ParseTemporaryRss(Object stateInfo)
        {
            DownloadPackage downloadPackage = new DownloadPackage();

            Rss.RssChannel rssChannel = (Rss.RssChannel)stateInfo;
            string         strDir     = Utils.GetValidFolderPath(feedItem.Title);

            string strDownloadlocation;

            if (feedItem.OverrideDownloadsFolder)
            {
                strDownloadlocation = feedItem.DownloadsFolder;
            }
            else
            {
                strDownloadlocation = Settings.Default.DownloadLocation + "\\" + strDir;
            }
            if (!Directory.Exists(strDownloadlocation))
            {
                Directory.CreateDirectory(strDownloadlocation);
            }
            try
            {
                try
                {
                    foreach (Rss.RssItem rssItem in rssChannel.Items)
                    {
                        if (rssItem.Enclosure != null)
                        {
                            bool validUri = false;
                            Uri  uri      = null;
                            try
                            {
                                uri      = new Uri(rssItem.Enclosure.Url);
                                validUri = true;
                            }
                            catch (UriFormatException ex)
                            {
                                boolError = true;
                                if (Settings.Default.LogLevel > 0)
                                {
                                    log.Error(String.Format("Invalid enclosure url for {0} ({1})", feedItem.Title, rssItem.Enclosure.Url), ex);
                                }
                            }
                            if (validUri)
                            {
                                string strPath     = GetValidFilename(uri.LocalPath.Substring(uri.LocalPath.LastIndexOf("/") + 1));
                                string strFilename = Path.Combine(strDownloadlocation, strPath);
                                if (!Directory.Exists(strDownloadlocation))
                                {
                                    Directory.CreateDirectory(strDownloadlocation);
                                }

                                downloadPackage = GetDownloadPackage(rssItem, feedItem, strDownloadlocation);
                                long longDownloadsize = downloadPackage.DownloadSize;

                                if (downloadPackage.strFilename != null)
                                {
                                    strFilename = downloadPackage.strFilename;
                                }

                                if (longDownloadsize > 0)
                                {
                                    if (feedItem.Authenticate == true)
                                    {
                                        string strPassword = EncDec.Decrypt(feedItem.Password, feedItem.Username);
                                        FileFound(feedItem, rssItem.Enclosure.Url, longDownloadsize, downloadPackage.strFilename, rssItem, downloadPackage.ByteRanging);
                                    }
                                    else
                                    {
                                        FileFound(feedItem, rssItem.Enclosure.Url, longDownloadsize, downloadPackage.strFilename, rssItem, downloadPackage.ByteRanging);
                                    }
                                }
                            }
                        }
                        //RetrieverSingleFileComplete(feedItem, downloadPackage);
                        RetrieveCompleteCallback(feedItem, true);
                    }
                }
                catch (WebException wex)
                {
                    if (Settings.Default.LogLevel > 0)
                    {
                        log.Error("Retriever", wex);
                    }
                }
            }
            catch (ThreadAbortException)
            {
                //MessageBox.Show("TA");
            }
        }