2017-06-25 20:48:44 +02:00
|
|
|
|
//
|
|
|
|
|
// JSONFeedParser.swift
|
|
|
|
|
// RSParser
|
|
|
|
|
//
|
|
|
|
|
// Created by Brent Simmons on 6/25/17.
|
|
|
|
|
// Copyright © 2017 Ranchero Software, LLC. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
|
|
|
|
|
// See https://jsonfeed.org/version/1
|
|
|
|
|
|
|
|
|
|
public struct JSONFeedParser {
|
|
|
|
|
|
2017-12-10 22:56:40 +01:00
|
|
|
|
struct Key {
|
|
|
|
|
static let version = "version"
|
|
|
|
|
static let items = "items"
|
|
|
|
|
static let title = "title"
|
|
|
|
|
static let homePageURL = "home_page_url"
|
|
|
|
|
static let feedURL = "feed_url"
|
|
|
|
|
static let feedDescription = "description"
|
|
|
|
|
static let nextURL = "next_url"
|
|
|
|
|
static let icon = "icon"
|
|
|
|
|
static let favicon = "favicon"
|
|
|
|
|
static let expired = "expired"
|
|
|
|
|
static let author = "author"
|
|
|
|
|
static let name = "name"
|
|
|
|
|
static let url = "url"
|
|
|
|
|
static let avatar = "avatar"
|
|
|
|
|
static let hubs = "hubs"
|
|
|
|
|
static let type = "type"
|
|
|
|
|
static let contentHTML = "content_html"
|
|
|
|
|
static let contentText = "content_text"
|
|
|
|
|
static let externalURL = "external_url"
|
|
|
|
|
static let summary = "summary"
|
|
|
|
|
static let image = "image"
|
|
|
|
|
static let bannerImage = "banner_image"
|
|
|
|
|
static let datePublished = "date_published"
|
|
|
|
|
static let dateModified = "date_modified"
|
|
|
|
|
static let tags = "tags"
|
|
|
|
|
static let uniqueID = "id"
|
|
|
|
|
static let attachments = "attachments"
|
|
|
|
|
static let mimeType = "mime_type"
|
|
|
|
|
static let sizeInBytes = "size_in_bytes"
|
|
|
|
|
static let durationInSeconds = "duration_in_seconds"
|
|
|
|
|
}
|
2017-06-25 20:48:44 +02:00
|
|
|
|
|
2017-12-10 22:56:40 +01:00
|
|
|
|
static let jsonFeedVersionPrefix = "https://jsonfeed.org/version/"
|
2017-06-25 20:48:44 +02:00
|
|
|
|
|
2017-12-10 22:56:40 +01:00
|
|
|
|
public static func parse(_ parserData: ParserData) throws -> ParsedFeed? {
|
2017-06-25 20:48:44 +02:00
|
|
|
|
|
2017-12-10 22:56:40 +01:00
|
|
|
|
guard let d = JSONUtilities.dictionary(with: parserData.data) else {
|
|
|
|
|
throw FeedParserError(.invalidJSON)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
guard let version = d[Key.version] as? String, version.hasPrefix(JSONFeedParser.jsonFeedVersionPrefix) else {
|
|
|
|
|
throw FeedParserError(.jsonFeedVersionNotFound)
|
|
|
|
|
}
|
|
|
|
|
guard let itemsArray = d[Key.items] as? JSONArray else {
|
|
|
|
|
throw FeedParserError(.jsonFeedItemsNotFound)
|
|
|
|
|
}
|
|
|
|
|
guard let title = d[Key.title] as? String else {
|
|
|
|
|
throw FeedParserError(.jsonFeedTitleNotFound)
|
|
|
|
|
}
|
2017-06-25 20:48:44 +02:00
|
|
|
|
|
2017-12-10 22:56:40 +01:00
|
|
|
|
let authors = parseAuthors(d)
|
|
|
|
|
let homePageURL = d[Key.homePageURL] as? String
|
|
|
|
|
let feedURL = d[Key.feedURL] as? String ?? parserData.url
|
|
|
|
|
let feedDescription = d[Key.feedDescription] as? String
|
|
|
|
|
let nextURL = d[Key.nextURL] as? String
|
|
|
|
|
let iconURL = d[Key.icon] as? String
|
|
|
|
|
let faviconURL = d[Key.favicon] as? String
|
|
|
|
|
let expired = d[Key.expired] as? Bool ?? false
|
|
|
|
|
let hubs = parseHubs(d)
|
2017-06-25 20:48:44 +02:00
|
|
|
|
|
2017-12-10 22:56:40 +01:00
|
|
|
|
let items = parseItems(itemsArray, parserData.url)
|
2017-06-25 20:48:44 +02:00
|
|
|
|
|
2017-12-10 22:56:40 +01:00
|
|
|
|
return ParsedFeed(type: .jsonFeed, title: title, homePageURL: homePageURL, feedURL: feedURL, feedDescription: feedDescription, nextURL: nextURL, iconURL: iconURL, faviconURL: faviconURL, authors: authors, expired: expired, hubs: hubs, items: items)
|
2017-06-25 20:48:44 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private extension JSONFeedParser {
|
|
|
|
|
|
2017-09-10 20:02:05 +02:00
|
|
|
|
static func parseAuthors(_ dictionary: JSONDictionary) -> Set<ParsedAuthor>? {
|
2017-06-25 20:48:44 +02:00
|
|
|
|
|
2017-12-10 22:56:40 +01:00
|
|
|
|
guard let authorDictionary = dictionary[Key.author] as? JSONDictionary else {
|
2017-06-25 20:48:44 +02:00
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-10 22:56:40 +01:00
|
|
|
|
let name = authorDictionary[Key.name] as? String
|
|
|
|
|
let url = authorDictionary[Key.url] as? String
|
|
|
|
|
let avatar = authorDictionary[Key.avatar] as? String
|
2017-06-25 20:48:44 +02:00
|
|
|
|
if name == nil && url == nil && avatar == nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
let parsedAuthor = ParsedAuthor(name: name, url: url, avatarURL: avatar, emailAddress: nil)
|
2017-09-10 20:02:05 +02:00
|
|
|
|
return Set([parsedAuthor])
|
2017-06-25 20:48:44 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-09-10 19:53:24 +02:00
|
|
|
|
static func parseHubs(_ dictionary: JSONDictionary) -> Set<ParsedHub>? {
|
2017-06-25 20:48:44 +02:00
|
|
|
|
|
2017-12-10 22:56:40 +01:00
|
|
|
|
guard let hubsArray = dictionary[Key.hubs] as? JSONArray else {
|
2017-06-25 20:48:44 +02:00
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-28 03:50:48 +01:00
|
|
|
|
let hubs = hubsArray.compactMap { (hubDictionary) -> ParsedHub? in
|
2017-12-10 22:56:40 +01:00
|
|
|
|
guard let hubURL = hubDictionary[Key.url] as? String, let hubType = hubDictionary[Key.type] as? String else {
|
2017-06-25 20:48:44 +02:00
|
|
|
|
return nil
|
|
|
|
|
}
|
2017-09-10 19:53:24 +02:00
|
|
|
|
return ParsedHub(type: hubType, url: hubURL)
|
2017-06-25 20:48:44 +02:00
|
|
|
|
}
|
2017-09-10 19:53:24 +02:00
|
|
|
|
return hubs.isEmpty ? nil : Set(hubs)
|
2017-06-25 20:48:44 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-09-10 03:46:58 +02:00
|
|
|
|
static func parseItems(_ itemsArray: JSONArray, _ feedURL: String) -> Set<ParsedItem> {
|
2017-06-25 20:48:44 +02:00
|
|
|
|
|
2018-01-28 03:50:48 +01:00
|
|
|
|
return Set(itemsArray.compactMap { (oneItemDictionary) -> ParsedItem? in
|
2017-07-02 02:22:19 +02:00
|
|
|
|
return parseItem(oneItemDictionary, feedURL)
|
2017-09-10 03:46:58 +02:00
|
|
|
|
})
|
2017-06-25 20:48:44 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-07-02 02:22:19 +02:00
|
|
|
|
static func parseItem(_ itemDictionary: JSONDictionary, _ feedURL: String) -> ParsedItem? {
|
2017-06-25 20:48:44 +02:00
|
|
|
|
|
|
|
|
|
guard let uniqueID = parseUniqueID(itemDictionary) else {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-10 22:56:40 +01:00
|
|
|
|
let contentHTML = itemDictionary[Key.contentHTML] as? String
|
|
|
|
|
let contentText = itemDictionary[Key.contentText] as? String
|
2017-06-25 20:48:44 +02:00
|
|
|
|
if contentHTML == nil && contentText == nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2017-11-18 21:41:15 +01:00
|
|
|
|
let decodedContentHTML = contentHTML?.rsparser_stringByDecodingHTMLEntities()
|
2017-06-25 20:48:44 +02:00
|
|
|
|
|
2017-12-10 22:56:40 +01:00
|
|
|
|
let url = itemDictionary[Key.url] as? String
|
|
|
|
|
let externalURL = itemDictionary[Key.externalURL] as? String
|
|
|
|
|
let title = itemDictionary[Key.title] as? String
|
|
|
|
|
let summary = itemDictionary[Key.summary] as? String
|
|
|
|
|
let imageURL = itemDictionary[Key.image] as? String
|
|
|
|
|
let bannerImageURL = itemDictionary[Key.bannerImage] as? String
|
2017-06-25 20:48:44 +02:00
|
|
|
|
|
2017-12-10 22:56:40 +01:00
|
|
|
|
let datePublished = parseDate(itemDictionary[Key.datePublished] as? String)
|
|
|
|
|
let dateModified = parseDate(itemDictionary[Key.dateModified] as? String)
|
2017-06-25 20:48:44 +02:00
|
|
|
|
|
|
|
|
|
let authors = parseAuthors(itemDictionary)
|
2017-09-10 20:18:15 +02:00
|
|
|
|
var tags: Set<String>? = nil
|
2017-12-10 22:56:40 +01:00
|
|
|
|
if let tagsArray = itemDictionary[Key.tags] as? [String] {
|
2017-09-10 20:18:15 +02:00
|
|
|
|
tags = Set(tagsArray)
|
|
|
|
|
}
|
2017-06-25 20:48:44 +02:00
|
|
|
|
let attachments = parseAttachments(itemDictionary)
|
|
|
|
|
|
2017-11-18 21:41:15 +01:00
|
|
|
|
return ParsedItem(syncServiceID: nil, uniqueID: uniqueID, feedURL: feedURL, url: url, externalURL: externalURL, title: title, contentHTML: decodedContentHTML, contentText: contentText, summary: summary, imageURL: imageURL, bannerImageURL: bannerImageURL, datePublished: datePublished, dateModified: dateModified, authors: authors, tags: tags, attachments: attachments)
|
2017-06-25 20:48:44 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-06-25 23:06:01 +02:00
|
|
|
|
static func parseUniqueID(_ itemDictionary: JSONDictionary) -> String? {
|
2017-06-25 20:48:44 +02:00
|
|
|
|
|
2017-12-10 22:56:40 +01:00
|
|
|
|
if let uniqueID = itemDictionary[Key.uniqueID] as? String {
|
2017-06-25 20:48:44 +02:00
|
|
|
|
return uniqueID // Spec says it must be a string
|
|
|
|
|
}
|
|
|
|
|
// Spec also says that if it’s a number, even though that’s incorrect, it should be coerced to a string.
|
2017-12-10 22:56:40 +01:00
|
|
|
|
if let uniqueID = itemDictionary[Key.uniqueID] as? Int {
|
2017-06-25 20:48:44 +02:00
|
|
|
|
return "\(uniqueID)"
|
|
|
|
|
}
|
2017-12-10 22:56:40 +01:00
|
|
|
|
if let uniqueID = itemDictionary[Key.uniqueID] as? Double {
|
2017-06-25 20:48:44 +02:00
|
|
|
|
return "\(uniqueID)"
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-25 23:06:01 +02:00
|
|
|
|
static func parseDate(_ dateString: String?) -> Date? {
|
2017-06-25 20:48:44 +02:00
|
|
|
|
|
|
|
|
|
guard let dateString = dateString, !dateString.isEmpty else {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
return RSDateWithString(dateString)
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-10 20:18:15 +02:00
|
|
|
|
static func parseAttachments(_ itemDictionary: JSONDictionary) -> Set<ParsedAttachment>? {
|
2017-06-25 20:48:44 +02:00
|
|
|
|
|
2017-12-10 22:56:40 +01:00
|
|
|
|
guard let attachmentsArray = itemDictionary[Key.attachments] as? JSONArray else {
|
2017-06-25 20:48:44 +02:00
|
|
|
|
return nil
|
|
|
|
|
}
|
2018-01-28 03:50:48 +01:00
|
|
|
|
return Set(attachmentsArray.compactMap { parseAttachment($0) })
|
2017-06-25 20:48:44 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-06-25 23:06:01 +02:00
|
|
|
|
static func parseAttachment(_ attachmentObject: JSONDictionary) -> ParsedAttachment? {
|
2017-06-25 20:48:44 +02:00
|
|
|
|
|
2017-12-10 22:56:40 +01:00
|
|
|
|
guard let url = attachmentObject[Key.url] as? String else {
|
2017-06-25 20:48:44 +02:00
|
|
|
|
return nil
|
|
|
|
|
}
|
2017-12-10 22:56:40 +01:00
|
|
|
|
guard let mimeType = attachmentObject[Key.mimeType] as? String else {
|
2017-06-25 20:48:44 +02:00
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-10 22:56:40 +01:00
|
|
|
|
let title = attachmentObject[Key.title] as? String
|
|
|
|
|
let sizeInBytes = attachmentObject[Key.sizeInBytes] as? Int
|
|
|
|
|
let durationInSeconds = attachmentObject[Key.durationInSeconds] as? Int
|
2017-06-25 23:06:01 +02:00
|
|
|
|
|
2017-06-25 20:48:44 +02:00
|
|
|
|
return ParsedAttachment(url: url, mimeType: mimeType, title: title, sizeInBytes: sizeInBytes, durationInSeconds: durationInSeconds)
|
|
|
|
|
}
|
|
|
|
|
}
|