Browse Source

add support for jsonfeed 1.1

closes #105
pull/108/head
Aaron Parecki 2 years ago
parent
commit
123b08ccd2
5 changed files with 805 additions and 7 deletions
  1. +41
    -6
      lib/XRay/Formats/JSONFeed.php
  2. +1
    -1
      lib/XRay/Parser.php
  3. +51
    -0
      tests/FeedTest.php
  4. +686
    -0
      tests/data/feed.example.com/jsonfeed-1.1
  5. +26
    -0
      tests/data/feed.example.com/jsonfeed-top-level-author

+ 41
- 6
lib/XRay/Formats/JSONFeed.php View File

@ -51,13 +51,48 @@ class JSONFeed extends Format {
if(isset($feed['icon']))
$entry['author']['photo'] = $feed['icon'];
// If the feed has an author, use that instead
if(isset($feed['author'])) {
if(isset($feed['author']['url']))
$entry['author']['url'] = $feed['author']['url'];
if(isset($feed['author']['name']))
$entry['author']['name'] = $feed['author']['name'];
if(isset($feed['author']['avatar']))
$entry['author']['photo'] = $feed['author']['avatar'];
}
// Override the author if the item contains author info
if(isset($item['author']['name']))
$entry['author']['name'] = $item['author']['name'];
if(isset($item['author']['url']))
$entry['author']['url'] = $item['author']['url'];
if(isset($item['author']['avatar']))
$entry['author']['photo'] = $item['author']['avatar'];
if(isset($item['author'])) {
if(isset($item['author']['name']))
$entry['author']['name'] = $item['author']['name'];
if(isset($item['author']['url']))
$entry['author']['url'] = $item['author']['url'];
if(isset($item['author']['avatar']))
$entry['author']['photo'] = $item['author']['avatar'];
}
// JSONFeed 1.1 uses "authors" instead
if(isset($feed['authors'][0])) {
// We only support 1 author
$author = $feed['authors'][0];
if(isset($author['url']))
$entry['author']['url'] = $author['url'];
if(isset($author['name']))
$entry['author']['name'] = $author['name'];
if(isset($author['avatar']))
$entry['author']['photo'] = $author['avatar'];
}
if(isset($item['authors'][0])) {
$author = $item['authors'][0];
if(isset($author['url']))
$entry['author']['url'] = $author['url'];
if(isset($author['name']))
$entry['author']['name'] = $author['name'];
if(isset($author['avatar']))
$entry['author']['photo'] = $author['avatar'];
}
if(isset($item['url']))
$entry['url'] = $item['url'];

+ 1
- 1
lib/XRay/Parser.php View File

@ -140,7 +140,7 @@ class Parser {
if(is_string($body) && substr($body, 0, 1) == '{') {
$parsed = json_decode($body, true);
if($parsed && isset($parsed['version']) && $parsed['version'] == 'https://jsonfeed.org/version/1') {
if($parsed && isset($parsed['version']) && in_array($parsed['version'], ['https://jsonfeed.org/version/1','https://jsonfeed.org/version/1.1'])) {
$http_response['body'] = $parsed;
return Formats\JSONFeed::parse($http_response);
} elseif($parsed && isset($parsed['items'][0]['type']) && isset($parsed['items'][0]['properties'])) {

+ 51
- 0
tests/FeedTest.php View File

@ -299,6 +299,57 @@ class FeedTest extends PHPUnit\Framework\TestCase
}
}
public function testJSONFeed1Point1()
{
$url = 'http://feed.example.com/jsonfeed-1.1';
$response = $this->parse(['url' => $url, 'expect' => 'feed']);
$body = $response->getContent();
$this->assertEquals(200, $response->getStatusCode());
$result = json_decode($body);
$this->assertEquals('feed+json', $result->{'source-format'});
$data = $result->data;
$this->assertEquals(48, count($data->items));
for($i=0; $i<8; $i++) {
$this->assertEquals('entry', $data->items[$i]->type);
$this->assertEquals('John Gruber', $data->items[$i]->author->name);
$this->assertEquals('https://twitter.com/gruber', $data->items[$i]->author->url);
$this->assertNotEmpty($data->items[$i]->url);
$this->assertNotEmpty($data->items[$i]->uid);
$this->assertNotEmpty($data->items[$i]->published);
$this->assertNotEmpty($data->items[$i]->content->html);
$this->assertNotEmpty($data->items[$i]->content->text);
}
$this->assertEquals('article', $data->items[0]->{'post-type'});
$this->assertEquals('article', $data->items[4]->{'post-type'});
$this->assertEquals('The Talk Show: “Blofeld-69-420”', $data->items[7]->name);
$this->assertEquals('https://daringfireball.net/linked/2022/01/26/the-talk-show-335', $data->items[7]->url);
$this->assertEquals('https://daringfireball.net/linked/2022/01/26/the-talk-show-335', $data->items[7]->uid);
$this->assertEquals('2022-01-27T01:58:12Z', $data->items[7]->published);
$this->assertEquals('feed', $data->type);
}
public function testJSONFeedTopLevelAuthor()
{
$url = 'http://feed.example.com/jsonfeed-top-level-author';
$response = $this->parse(['url' => $url, 'expect' => 'feed']);
$body = $response->getContent();
$this->assertEquals(200, $response->getStatusCode());
$result = json_decode($body, true);
$this->assertEquals('feed+json', $result['source-format']);
$data = $result['data'];
$item = $data['items'][0];
$this->assertEquals('Author Name', $item['author']['name']);
$this->assertEquals('https://author.example.com', $item['author']['url']);
}
public function testJSONFeedRelativeImages()
{
$url = 'http://feed.example.com/jsonfeed';

+ 686
- 0
tests/data/feed.example.com/jsonfeed-1.1
File diff suppressed because it is too large
View File


+ 26
- 0
tests/data/feed.example.com/jsonfeed-top-level-author View File

@ -0,0 +1,26 @@
HTTP/1.1 200 OK
Date: Sat, 11 Nov 2017 15:35:23 GMT
Content-Type: application/json; charset=UTF-8
{
"version": "https://jsonfeed.org/version/1",
"user_comment": "This feed allows you to read the posts from this site in any feed reader that supports the JSON Feed format. To add this feed to your reader, copy the following URL -- http://www.manton.org/feed/json -- and add it your reader.",
"home_page_url": "http://www.manton.org",
"feed_url": "http://www.manton.org/feed/json",
"title": "Manton Reece",
"author": {
"name": "Author Name",
"url": "https://author.example.com"
},
"description": "",
"items": [
{
"id": "http://www.manton.org/2017/11/5993.html",
"url": "http://www.manton.org/2017/11/5993.html",
"title": "",
"content_html": "<p>I&#8217;ve updated Micro.blog&#8217;s Twitter cross-posting to support 280 characters. The apps still color the character counter blue until 140, and red after 280, just in case you want to stick to shorter posts.</p>\n",
"date_published": "2017-11-10T16:34:21+00:00",
"date_modified": "2017-11-10T16:34:21+00:00"
}
]
}

Loading…
Cancel
Save