Exemplo n.º 1
0
        public static async Task <ISyndicationFeedReader> GetSyndicationFeedReader(IUriEntity uriEntity)
        {
            if (ReferenceEquals(uriEntity, null) || string.IsNullOrWhiteSpace(uriEntity.Uri))
            {
                throw new ArgumentException(nameof(uriEntity));
            }

            var syndicationFeedType = await GetSyndicationFeedType(uriEntity);

            switch (syndicationFeedType)
            {
            case SyndicationFeedType.Rss:
                return(new RssFeedReader(XmlReader.Create(uriEntity.Uri, new XmlReaderSettings {
                    Async = true
                })));

            case SyndicationFeedType.Atom:
                return(new AtomFeedReader(XmlReader.Create(uriEntity.Uri, new XmlReaderSettings {
                    Async = true
                })));

            default:
                throw new UriFormatException(nameof(uriEntity));
            }
        }
Exemplo n.º 2
0
        public async Task <List <News> > GetNews(IUriEntity uriEntity)
        {
            if (string.IsNullOrWhiteSpace(uriEntity.Uri))
            {
                return(null);
            }

            var items = new HashSet <ISyndicationItem>();

            try
            {
                var feedReader = await SyndicationFactory.GetSyndicationFeedReader(uriEntity);

                while (await feedReader.Read())
                {
                    switch (feedReader.ElementType)
                    {
                    case SyndicationElementType.Item:
                        var item = await feedReader.ReadItem();

                        items.Add(item);
                        break;
                    }
                }
            }
            catch (Exception exception)
            {
                _logger.LogError($"Something went wrong: {exception.InnerException?.Message ?? exception.Message}");
            }

            return(items.Select(i => i.GetNews()).ToList());
        }
Exemplo n.º 3
0
        public static bool UriEquals(this IUriEntity entity, object obj)
        {
            if (!(obj is IUriEntity uriEntity))
            {
                return(false);
            }

            if (ReferenceEquals(entity, uriEntity))
            {
                return(true);
            }

            if (entity.Uri.Length != uriEntity.Uri.Length)
            {
                return(false);
            }

            return(entity.Uri == uriEntity.Uri);
        }
Exemplo n.º 4
0
        public static int GetUriHashCode(this IUriEntity entity)
        {
            if (string.IsNullOrWhiteSpace(entity.Uri))
            {
                return(0);
            }

            var bytes = Encoding.Unicode.GetBytes(entity.Uri);
            var sum   = 0;

            foreach (var @byte in bytes)
            {
                for (var i = 0; i < 8; i++)
                {
                    sum += (@byte >> i) & 1;
                }
            }

            return(sum);
        }
Exemplo n.º 5
0
        private static async Task <SyndicationFeedType> GetSyndicationFeedType(IUriEntity uriEntity)
        {
            if (ReferenceEquals(uriEntity, null) || string.IsNullOrWhiteSpace(uriEntity.Uri))
            {
                throw new ArgumentException(nameof(uriEntity));
            }

            using (var xmlReader = XmlReader.Create(uriEntity.Uri, new XmlReaderSettings {
                Async = true
            }))
            {
                for (var i = 0; i < 2; i++)
                {
                    if (await xmlReader.ReadAsync() && xmlReader.IsStartElement() && !xmlReader.IsEmptyElement)
                    {
                        if (xmlReader.HasAttributes)
                        {
                            for (var j = 0; j < xmlReader.AttributeCount; j++)
                            {
                                var result = GetSyndicationFeedTypeFromAttribute(xmlReader.GetAttribute(j));
                                if (result != SyndicationFeedType.None)
                                {
                                    return(result);
                                }
                            }
                        }
                        else
                        {
                            var result = GetSyndicationFeedTypeFromElement(xmlReader.Name);
                            if (result != SyndicationFeedType.None)
                            {
                                return(result);
                            }
                        }
                    }
                }
            }

            throw new ArgumentException(nameof(uriEntity));
        }