/// <summary>
 /// Import an RSS feed into the JSON feed
 /// </summary>
 /// <param name="importReader">RssFeed Object</param>
 public void ImportRssFeed(RssReader importReader)
 {
     // Initialise the JSON feed
     outputFeed = new JsonFeed
     {
         items       = new List <JsonFeedItem>(),
         title       = importReader.Feed.Title,
         description = importReader.Feed.Description,
         link        = importReader.Feed.Link
     };
     // Add each item to the feed.
     foreach (RssReader.RssItem currentItem in importReader.Feed.Items.Values)
     {
         if (!alreadyLoaded(currentItem) && isToday(currentItem))
         {
             outputFeed.items.Add(new JsonFeedItem
             {
                 title       = currentItem.Title,
                 description = currentItem.Description,
                 link        = currentItem.Link,
                 pubDate     = currentItem.PubDate
             });
         }
     }
 }
Пример #2
0
        public void WriteFeedToString()
        {
            var jsonFeed = new JsonFeed
            {
                Title       = "Dan Rigby",
                Description = "Mobile App Development & More.",
                HomePageUrl = @"http://danrigby.com",
                FeedUrl     = @"http://danrigby.com/feed.json",
                Author      = new JsonFeedAuthor
                {
                    Name = "Dan Rigby",
                    Url  = @"https://twitter.com/DanRigby",
                },
                Items = new List <JsonFeedItem>
                {
                    new JsonFeedItem
                    {
                        Id            = @"http://danrigby.com/2015/09/12/inotifypropertychanged-the-net-4-6-way/",
                        Url           = @"http://danrigby.com/2015/09/12/inotifypropertychanged-the-net-4-6-way/",
                        Title         = "INotifyPropertyChanged, The .NET 4.6 Way",
                        ContentText   = @"Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
                        DatePublished = new DateTime(2015, 09, 12)
                    }
                }
            };

            string jsonFeedString = jsonFeed.Write();

            Assert.IsNotEmpty(jsonFeedString);
        }
        public void FormatSampleFeedEmpty()
        {
            var feed = new JsonFeed
            {
                Items = new List <JsonFeedItem>
                {
                    new JsonFeedItem(),
                },
            };

            var tryFormatResult = JsonFeedFormatter.TryFormatJsonFeed(feed, out var document);

            Assert.True(tryFormatResult);

            var targetEncoding = Encoding.UTF8;

            using (var memoryStream = new MemoryStream())
                using (var streamWriter = new StreamWriter(memoryStream, targetEncoding))
                    using (var jsonWriter = new JsonTextWriter(streamWriter)
                    {
                        Formatting = Formatting.Indented,
                        StringEscapeHandling = StringEscapeHandling.EscapeHtml,
                        Indentation = 4,
                    })
                    {
                        document.WriteTo(jsonWriter);
                        jsonWriter.Flush();

                        var jsonString = targetEncoding.GetString(memoryStream.ToArray());
                        Assert.NotEmpty(jsonString);
                    }
        }
 public void Dispose()
 {
     inputFeed.items.Clear();
     inputFeed = null;
     outputFeed.items.Clear();
     outputFeed = null;
 }
Пример #5
0
        public void GetCategoriesTestHappyPath()
        {
            var jsonFeed = new JsonFeed(nameGeneratorURL, 0);

            string[] categories = JsonFeed.GetCategories();

            Assert.IsTrue(categories.Length > 0);
        }
Пример #6
0
        public async Task ParseFromUriWithCustomHttpMessageHandler()
        {
            JsonFeed jsonFeed = await JsonFeed.ParseFromUriAsync(new Uri("https://jsonfeed.org/feed.json"), new HttpClientHandler());

            string outputJsonFeed = jsonFeed.Write();

            Assert.IsNotEmpty(outputJsonFeed);
        }
Пример #7
0
        public void RoundtripMaybePizzaBlog()
        {
            string   inputJsonFeed  = GetResourceAsString("MaybePizzaBlog.json");
            JsonFeed jsonFeed       = JsonFeed.Parse(inputJsonFeed);
            string   outputJsonFeed = jsonFeed.Write();

            Assert.AreEqual(inputJsonFeed.Length, outputJsonFeed.Length);
        }
Пример #8
0
        public void RoundtripTimeTablePodcast()
        {
            string   inputJsonFeed  = GetResourceAsString("TimeTablePodcast.json");
            JsonFeed jsonFeed       = JsonFeed.Parse(inputJsonFeed);
            string   outputJsonFeed = jsonFeed.Write();

            Assert.AreEqual(inputJsonFeed.Length, outputJsonFeed.Length);
        }
Пример #9
0
        public void RoundtripSimple()
        {
            string   inputJsonFeed  = GetResourceAsString("Simple.json");
            JsonFeed jsonFeed       = JsonFeed.Parse(inputJsonFeed);
            string   outputJsonFeed = jsonFeed.Write();

            Assert.AreEqual(inputJsonFeed, outputJsonFeed);
        }
Пример #10
0
        public async Task ParseFromUri()
        {
            JsonFeed jsonFeed = await JsonFeed.ParseFromUriAsync(new Uri("https://jsonfeed.org/feed.json"));

            string outputJsonFeed = jsonFeed.Write();

            Assert.IsNotEmpty(outputJsonFeed);
        }
Пример #11
0
        public async Task <Tuple <string, string> > GetNames()
        {
            var     feed   = new JsonFeed("https://www.names.privserv.com/api/");
            dynamic result = await feed.Getnames();

            var names = Tuple.Create(result.name.ToString(), result.surname.ToString());

            return(names);
        }
Пример #12
0
        public void GetRandomJokesTestHappyPath()
        {
            var jsonFeed = new JsonFeed(nameGeneratorURL, 0);

            string[] jokes = JsonFeed.GetRandomJokes(firstName, lastName, null);

            Assert.IsTrue(jokes.Length > 0);
            Assert.IsTrue(jokes[0].Contains(firstName) && jokes[0].Contains(lastName));
        }
Пример #13
0
        public void GetNames()
        {
            var request = new NameRequest();
            var feed    = new JsonFeed();//("https://api.chucknorris.io");//, 1);

            request.Uri = "https://www.names.privserv.com/api/";
            var res = feed.GetNames(request);

            Assert.IsNotNull(res);
        }
Пример #14
0
        public void GetCategories()
        {
            var request = new CategoryRequest();
            var feed    = new JsonFeed();// ("https://api.chucknorris.io");//, 1);

            request.Uri = "https://api.chucknorris.io";
            var res = feed.GetCategories(request);

            Assert.IsNotNull(res);
        }
Пример #15
0
        public async Task ExampleUsage1()
        {
            var feed = await JsonFeed.Get(new Uri("https://hnrss.org/frontpage.jsonfeed"));

            Assert.NotEmpty(feed.items);
            foreach (var item in feed.items)
            {
                Log.d(item.title + " - " + item.url);
            }
        }
Пример #16
0
        public async Task <List <string> > GetRandomJokes(string category, int number, Tuple <string, string> names)
        {
            var feed  = new JsonFeed("https://api.chucknorris.io");
            var jokes = new List <String>();

            for (var i = 0; i < number; i++)
            {
                var result = await feed.GetRandomJokes(names?.Item1, names?.Item2, category);

                jokes.Add(result);
            }
            return(jokes);
        }
        public void FormatSampleFeedWithBlueShedSampleExtension()
        {
            var feed = new JsonFeed
            {
                Items = new List <JsonFeedItem>
                {
                    new JsonFeedItem {
                        Id = "abc"
                    },
                },
                Extensions =
                {
                    new BlueShedSampleExtension
                    {
                        About     = "https://blueshed-podcasts.com/json-feed-extension-docs",
                        Explicit  = false,
                        Copyright = "1948 by George Orwell",
                        Owner     = "Big Brother and the Holding Company",
                        Subtitle  = "All shouting, all the time. Double. Plus. Good.",
                    }
                }
            };

            var tryFormatResult = JsonFeedFormatter.TryFormatJsonFeed(feed, out var document, new ExtensionManifestDirectory {
                new BlueShedSampleExtensionManifest()
            });

            Assert.True(tryFormatResult);

            var targetEncoding = Encoding.UTF8;

            using (var memoryStream = new MemoryStream())
                using (var streamWriter = new StreamWriter(memoryStream, targetEncoding))
                    using (var jsonWriter = new JsonTextWriter(streamWriter)
                    {
                        Formatting = Formatting.Indented,
                        StringEscapeHandling = StringEscapeHandling.EscapeHtml,
                        Indentation = 4,
                    })
                    {
                        document.WriteTo(jsonWriter);
                        jsonWriter.Flush();

                        var jsonString = targetEncoding.GetString(memoryStream.ToArray());
                        Assert.Contains("_blue_shed", jsonString);
                        Assert.NotEmpty(jsonString);
                    }
        }
Пример #18
0
        public void Version_1_1()
        {
            string   inputJsonFeed  = GetResourceAsString("json_v1.1.json");
            JsonFeed jsonFeed       = JsonFeed.Parse(inputJsonFeed);
            string   outputJsonFeed = jsonFeed.Write().Replace("\r\n", "\n");


            Assert.AreEqual(1, jsonFeed.Authors.Length);
            Assert.AreEqual("John Gruber", jsonFeed.Authors[0].Name);
            Assert.AreEqual("https://twitter.com/gruber", jsonFeed.Authors[0].Url);
            Assert.AreEqual(48, jsonFeed.Items.Count);
            Assert.AreEqual(1, jsonFeed.Items[0].Authors.Length);
            Assert.AreEqual("John Gruber", jsonFeed.Items[0].Authors[0].Name);

            Assert.AreEqual(inputJsonFeed, outputJsonFeed);
            Assert.AreEqual(inputJsonFeed.Length, outputJsonFeed.Length);
        }
Пример #19
0
        public void WriteFeedToStream()
        {
            string   inputJsonFeed = GetResourceAsString("Simple.json");
            JsonFeed jsonFeed      = JsonFeed.Parse(inputJsonFeed);

            using (var memoryStream = new MemoryStream())
            {
                jsonFeed.Write(memoryStream);
                memoryStream.Position = 0;

                using (var reader = new StreamReader(memoryStream))
                {
                    string outputJsonFeed = reader.ReadToEnd();
                    Assert.AreEqual(inputJsonFeed, outputJsonFeed);
                }
            }
        }
Пример #20
0
        public void GetRandomJokes()// (string[] expectedResult)
        {
            var request = new JokeRequest();

            request.FirstName = "Maggie";
            request.LastName  = "Chen";
            request.Category  = "animal"; // "aminal";
            request.Uri       = "https://api.chucknorris.io";
            var feed = new JsonFeed();    // ("https://api.chucknorris.io");//, 1);
            var res  = feed.GetRandomJokes(request);

            Assert.IsNotNull(res);

            //mockFeed = new Mock<IJsonFeed>(MockBehavior.Strict);
            //mockFeed.Setup(p => p.GetRandomJokes(request)).Returns(expectedResult);

            //systemUnderTest = new JsonFeed();
            //var result = systemUnderTest.GetRandomJokes(request);
            //Assert.IsNotNull(result);
        }
Пример #21
0
    public async Task <List <SourceData> > GetAsync(DateTime sinceWhen)
    {
        var currentTime = DateTime.UtcNow;
        var sourceItems = new List <SourceData>();

        _logger.LogDebug("Checking the Json feed '{_jsonFeedReaderSettings.FeedUrl}' for new posts since '{sinceWhen:u}'",
                         _jsonFeedReaderSettings.FeedUrl, sinceWhen);

        List <JsonFeedItem> items = null;

        try
        {
            var jsonFeed = await JsonFeed.ParseFromUriAsync(new Uri(_jsonFeedReaderSettings.FeedUrl));

            items = jsonFeed.Items.Where(i => i.DateModified > sinceWhen || i.DatePublished > sinceWhen).ToList();
        }
        catch (Exception e)
        {
            _logger.LogError(e, "Error parsing the Json feed for: {_jsonFeedReaderSettings.FeedUrl}.",
                             _jsonFeedReaderSettings.FeedUrl);
            throw;
        }
        _logger.LogDebug($"Found {items.Count} posts.");

        foreach (var jsonFeedItem in items)
        {
            sourceItems.Add(new SourceData(SourceSystems.SyndicationFeed)
            {
                Author          = jsonFeedItem.Author?.Name,
                PublicationDate = jsonFeedItem.DatePublished?.UtcDateTime ?? currentTime,
                UpdatedOnDate   = jsonFeedItem.DateModified?.UtcDateTime ?? currentTime,
                //Text = jsonFeedItem.ContentHtml,
                Title    = jsonFeedItem.Title,
                Url      = jsonFeedItem.Id,
                EndAfter = null,
                AddedOn  = currentTime,
                Tags     = jsonFeedItem.Tags is null ? null : string.Join(",", jsonFeedItem.Tags)
            });
Пример #22
0
        public void TestGenerateUrl()
        {
            string actual = JsonFeed.GenerateUrl("host.com", "endpoint/api", null);

            Assert.AreEqual <string>("host.com/endpoint/api", actual);

            actual = JsonFeed.GenerateUrl("host.com", "/endpoint/api", null);
            Assert.AreEqual <string>("host.com/endpoint/api", actual);

            actual = JsonFeed.GenerateUrl("host.com/", "endpoint/api", null);
            Assert.AreEqual <string>("host.com/endpoint/api", actual);

            actual = JsonFeed.GenerateUrl("host.com/", "/endpoint/api", null);
            Assert.AreEqual <string>("host.com/endpoint/api", actual);

            Dictionary <string, string> parameters = new Dictionary <string, string>
            {
                { "key1", "value1" }
            };

            actual = JsonFeed.GenerateUrl("host.com", "endpoint/api", parameters);
            Assert.AreEqual <string>("host.com/endpoint/api?key1=value1", actual);

            parameters = new Dictionary <string, string>
            {
                { "key1", "value1" },
                { "key2", "value2" }
            };
            actual = JsonFeed.GenerateUrl("host.com", "endpoint/api", parameters);
            Assert.AreEqual <string>("host.com/endpoint/api?key1=value1&key2=value2", actual);

            parameters = new Dictionary <string, string>
            {
                { "key 1", "value 1!" }
            };
            actual = JsonFeed.GenerateUrl("host.com", "endpoint/api", parameters);
            Assert.AreEqual <string>("host.com/endpoint/api?key%201=value%201%21", actual);
        }
 /// <summary>
 /// Load all JSON files for today into today's feed
 /// </summary>
 public void Load()
 {
     inputFeed = new JsonFeed {
         items = new List <JsonFeedItem>()
     };
     if (Directory.Exists(folderPath))
     {
         // Get all of today's JSON files
         string   fileMatch   = DateTime.Now.Date.ToString("yyyy-MM-dd") + "*.json";
         string[] todaysFiles = Directory.GetFiles(folderPath, fileMatch);
         if (todaysFiles.Length > 0)
         {
             foreach (string filename in todaysFiles)
             {
                 // For each JSON file found...
                 using (StreamReader reader = new StreamReader(filename))
                 {
                     string   json    = reader.ReadToEnd();
                     JsonFeed newFeed = JsonConvert.DeserializeObject <JsonFeed>(json);   // Load the feeds
                     if (string.IsNullOrEmpty(inputFeed.title))
                     {
                         inputFeed.title = newFeed.title;
                     }
                     if (string.IsNullOrEmpty(inputFeed.description))
                     {
                         inputFeed.title = newFeed.description;
                     }
                     if (string.IsNullOrEmpty(inputFeed.link))
                     {
                         inputFeed.title = newFeed.link;
                     }
                     inputFeed.items.AddRange(newFeed.items);
                 }
             }
         }
     }
 }
Пример #24
0
        public async Task <List <String> > GetCategories()
        {
            var feed = new JsonFeed("https://api.chucknorris.io/jokes/categories");

            return(await feed.GetCategories());
        }
Пример #25
0
        public void GetResponse_TestReturnType()
        {
            JsonFeed jsonFeed = new JsonFeed("http://uinames.com/api/");

            Assert.IsInstanceOfType(jsonFeed.GetResponse(""), (new JObject()).GetType());
        }
Пример #26
0
        public static bool TryFormatJsonFeed(JsonFeed feedToFormat, out JObject feedObject, ExtensionManifestDirectory extensionManifestDirectory = null)
        {
            feedObject = default;

            if (feedToFormat == null)
            {
                return(false);
            }

            feedObject = new JObject
            {
                new JProperty("version", new JValue(JsonFeedConstants.Version)),
            };

            if (extensionManifestDirectory == null)
            {
                extensionManifestDirectory = ExtensionManifestDirectory.DefaultForJsonFeed;
            }

            if (TryFormatJsonFeedRequiredStringProperty("title", feedToFormat.Title, out var titleProperty))
            {
                feedObject.Add(titleProperty);
            }

            if (TryFormatJsonFeedOptionalStringProperty("home_page_url", feedToFormat.HomePageUrl, out var homePageUrlProperty))
            {
                feedObject.Add(homePageUrlProperty);
            }

            if (TryFormatJsonFeedOptionalStringProperty("feed_url", feedToFormat.FeedUrl, out var feedUrlProperty))
            {
                feedObject.Add(feedUrlProperty);
            }

            if (TryFormatJsonFeedOptionalStringProperty("description", feedToFormat.Description, out var descriptionProperty))
            {
                feedObject.Add(descriptionProperty);
            }

            if (TryFormatJsonFeedOptionalStringProperty("user_comment", feedToFormat.UserComment, out var userCommentProperty))
            {
                feedObject.Add(userCommentProperty);
            }

            if (TryFormatJsonFeedOptionalStringProperty("next_url", feedToFormat.NextUrl, out var nextUrlProperty))
            {
                feedObject.Add(nextUrlProperty);
            }

            if (TryFormatJsonFeedOptionalStringProperty("icon", feedToFormat.Icon, out var iconProperty))
            {
                feedObject.Add(iconProperty);
            }

            if (TryFormatJsonFeedOptionalStringProperty("favicon", feedToFormat.Favicon, out var faviconProperty))
            {
                feedObject.Add(faviconProperty);
            }

            if (TryFormatJsonFeedAuthorProperty("author", feedToFormat.Author, out var authorProperty))
            {
                feedObject.Add(authorProperty);
            }

            if (TryFormatJsonFeedOptionalBoolProperty("expired", feedToFormat.Expired, out var expiredProperty))
            {
                feedObject.Add(expiredProperty);
            }

            if (TryFormatJsonFeedHubsProperty("hubs", feedToFormat.Hubs, out var hubsProperty))
            {
                feedObject.Add(hubsProperty);
            }

            if (TryFormatJsonFeedItemsProperty("items", feedToFormat.Items, extensionManifestDirectory, out var itemsProperty))
            {
                feedObject.Add(itemsProperty);
            }

            // extensions
            if (ExtensibleEntityFormatter.TryFormatJObjectExtensions(feedToFormat, extensionManifestDirectory, out var extensionTokens))
            {
                feedObject.AddRange(extensionTokens);
            }

            return(true);
        }
        public void FormatSampleFeed()
        {
            var feed = new JsonFeed
            {
                FeedUrl     = "https://example.org/feed.json",
                Title       = "This is my feed title",
                HomePageUrl = "https://example.org/homepage",
                Author      = new JsonFeedAuthor
                {
                    Name   = "John Doe",
                    Avatar = "https://example.org/john-doe/avatar.png",
                    Url    = "mailto:[email protected]",
                },
                Description = "This is my feed description",
                Expired     = false,
                Favicon     = "https://example.org/favicon.ico",
                Icon        = "https://example.org/icon.png",
                NextUrl     = "https://example.org/feed.json?offset=1",
                UserComment = "This is a user comment",
                Hubs        = new List <JsonFeedHub>
                {
                    new JsonFeedHub
                    {
                        Type = "rssCloud",
                        Url  = "https://example.org/rss-cloud-hub",
                    },
                    new JsonFeedHub
                    {
                        Type = "WebSub",
                        Url  = "https://example.org/web-sub-hub",
                    },
                },
                Items = new List <JsonFeedItem>
                {
                    new JsonFeedItem
                    {
                        Id            = "1",
                        Title         = "My awesome article",
                        DatePublished = new DateTimeOffset(2019, 01, 01, 5, 30, 00, TimeSpan.FromHours(0)),
                        Url           = "https://example.org/article",
                        ExternalUrl   = "https://example.org/article-external",
                        Image         = "https://example.org/article-image.png",
                        DateModified  = new DateTimeOffset(2019, 01, 02, 5, 30, 00, TimeSpan.FromHours(0)),
                        ContentText   = "This is a text content",
                        ContentHtml   = "This is a <strong>HTML</strong> content",
                        BannerImage   = "https://example.org/article-banner-image.png",
                        Summary       = "This is a summary of the article",
                        Author        = new JsonFeedAuthor
                        {
                            Name   = "Jane Doe",
                            Avatar = "https://example.org/jane-doe/avatar.png",
                            Url    = "mailto:[email protected]",
                        },
                        Tags = new List <string> {
                            "alpha", "beta", "gama"
                        },
                        Attachments = new List <JsonFeedAttachment>
                        {
                            new JsonFeedAttachment
                            {
                                MimeType          = "video/mp4",
                                Title             = "Video attachment",
                                Url               = "https://example.org/video.mp4",
                                SizeInBytes       = 3000,
                                DurationInSeconds = 16,
                            },
                        },
                    },
                },
            };

            var tryFormatResult = JsonFeedFormatter.TryFormatJsonFeed(feed, out var document);

            Assert.True(tryFormatResult);

            var targetEncoding = Encoding.UTF8;

            using (var memoryStream = new MemoryStream())
                using (var streamWriter = new StreamWriter(memoryStream, targetEncoding))
                    using (var jsonWriter = new JsonTextWriter(streamWriter)
                    {
                        Formatting = Formatting.Indented,
                        StringEscapeHandling = StringEscapeHandling.EscapeHtml,
                        Indentation = 4,
                    })
                    {
                        document.WriteTo(jsonWriter);
                        jsonWriter.Flush();

                        var jsonString = targetEncoding.GetString(memoryStream.ToArray());
                        Assert.NotEmpty(jsonString);
                    }
        }