Пример #1
0
        private bool ImportUrl(string htmlUrl, string rssUrl)
        {
            if (!htmlUrl.IsNullOrEmpty() && !htmlUrl.ToLower().StartsWith("http") && !htmlUrl.ToLower().StartsWith("https") && !htmlUrl.ToLower().StartsWith("www"))
            {
                htmlUrl = "http://" + htmlUrl;
            }

            try
            {
                var urls = new List <string>();
                if (rssUrl.IsNullOrEmpty() && htmlUrl.IsNullOrEmpty())
                {
                    return(false);
                }
                if (!htmlUrl.IsNullOrEmpty() && !htmlUrl.StartsWith("http://") && !htmlUrl.StartsWith("https"))
                {
                    htmlUrl = "http://" + rssUrl;
                }

                var exsFeed = FeedService.GetFeedBySiteUrl(!htmlUrl.IsNullOrEmpty() ? htmlUrl : rssUrl);
                if (exsFeed != null)
                {
                    var subscribe = true;// !Request["subscribe"].IsNullOrEmpty();
                    var userFeed  = CurrentUser.MyFeeds.Find(f => f.FeedId == exsFeed.Id);

                    if (userFeed == null)
                    {
                        userFeed = new UserFeed
                        {
                            FeedId     = exsFeed.Id,
                            UserId     = CurrentUser.Id,
                            Subscribed = subscribe,
                            Submited   = true
                        };
                        userFeed.Id = UserService.InsertUserFeed(userFeed);
                        CurrentUser.MyFeeds.Add(userFeed);
                        CurrentUser.Reputation += 3;
                        UserService.UpdateUser(CurrentUser);
                    }
                    return(true);
                }

                try
                {
                    var wc = new WebClient
                    {
                        Encoding = Encoding.UTF8
                    };
                    wc.Headers["User-Agent"] = "www.rssheap.com";
                    var str = wc.DownloadString(!rssUrl.IsNullOrEmpty() ? rssUrl : htmlUrl).Replace("media:thumbnail", "media");  //mashable fix

                    var reader = XmlReader.Create(new StringReader(str));

                    if (reader.IsXmlFeed())
                    {
                        urls.Add(!rssUrl.IsNullOrEmpty() ? rssUrl : htmlUrl);
                    }
                    else
                    {
                        //it's not an xml, try to find it from the tag
                        var request = FeedService.CreateRequest(!rssUrl.IsNullOrEmpty() ? rssUrl : htmlUrl);
                        request.Timeout = 5000;
                        var response = (HttpWebResponse)request.GetResponse();
                        try
                        {
                            var html = new HtmlDocument();
                            html.LoadHtml(new StreamReader(response.GetResponseStream(), Encoding.UTF8).ReadToEnd());
                            response.Close();

                            var links = html.DocumentNode
                                        .Descendants()
                                        .Where(l => l.Name == "link" &&
                                               l.Attributes["rel"] != null && !l.Attributes["rel"].Value.IsNullOrEmpty() && l.Attributes["rel"].Value.ToLower() == "alternate" &&
                                               l.Attributes["type"] != null && !l.Attributes["type"].Value.IsNullOrEmpty() && l.Attributes["type"].Value.Contains("xml") &&
                                               l.Attributes["href"] != null && !l.Attributes["href"].Value.IsNullOrEmpty() && !l.Attributes["href"].Value.ToLower().Contains("comment"))
                                        .ToList();

                            if (links.Count > 1)
                            {
                                //try to find atom first
                                var temp = links.Where(l => l.Attributes["type"].Value.ToLower() == "application/atom+xml");
                                if (temp.Count() > 0)
                                {
                                    links = temp.ToList();
                                }
                                else
                                {
                                    temp = links.Where(l => l.Attributes["type"].Value.ToLower() == "application/rss+xml");
                                    if (temp.Count() > 0)
                                    {
                                        links = temp.ToList();
                                    }
                                    else
                                    {
                                        links = new List <HtmlNode> {
                                            links.First()
                                        };
                                    }
                                }
                            }

                            urls = links.Select(l => l.Attributes["href"].Value)
                                   .ToList();

                            if (urls.Count > 1) //usualy you get main feed and post feed, ignore the post feed
                            {
                                urls = urls.Where(u => !u.Contains(rssUrl)).ToList();
                            }
                        }
                        catch { }
                    }
                    if (urls.Count == 0)
                    {
                        return(false);
                    }

                    foreach (var u in urls)
                    {
                        var url = u;
                        if (url.StartsWith("/"))
                        {
                            url = new Uri(!htmlUrl.IsNullOrEmpty() ? htmlUrl : rssUrl).GetLeftPart(UriPartial.Authority) + url;
                        }

                        var rFeed = FeedService.GetRemoteFeed(new Feed
                        {
                            Url     = url,
                            SiteUrl = htmlUrl
                        }, timeout: 3000);

                        if (rFeed != null)
                        {
                            if (!rFeed.Name.IsNullOrEmpty() && rFeed.Name.ToLower().Contains("comment"))
                            {
                                continue;
                            }

                            rFeed.Public = CurrentUser.IsAdmin;

                            var blogUrl = rFeed.SiteUrl;
                            if (blogUrl.IsNullOrEmpty())
                            {
                                blogUrl = rFeed.Url;
                            }

                            var lFeed  = FeedService.GetFeedBySiteUrl(blogUrl);
                            int feedId = lFeed == null?FeedService.InsertFeed(rFeed) : lFeed.Id;

                            var subscribe = !Request["subscribe"].IsNullOrEmpty();
                            var userFeed  = CurrentUser.MyFeeds.Find(f => f.FeedId == feedId);

                            if (userFeed == null)
                            {
                                userFeed = new UserFeed
                                {
                                    FeedId     = feedId,
                                    UserId     = CurrentUser.Id,
                                    Subscribed = subscribe,
                                    Submited   = true
                                };
                                userFeed.Id = UserService.InsertUserFeed(userFeed);
                                CurrentUser.MyFeeds.Add(userFeed);
                                CurrentUser.Reputation += 3;
                                UserService.UpdateUser(CurrentUser);
                            }
                            return(true);
                        }
                    }
                }
                catch
                {
                }
            }
            catch
            {
            }
            return(false);
        }