2017-06-26 01:32:07 +02:00
|
|
|
//
|
|
|
|
// RSParsedFeedTransformer.swift
|
|
|
|
// RSParser
|
|
|
|
//
|
|
|
|
// Created by Brent Simmons on 6/25/17.
|
|
|
|
// Copyright © 2017 Ranchero Software, LLC. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
|
|
|
// RSRSSParser and RSAtomParser were written in Objective-C quite a while ago.
|
|
|
|
// They create an RSParsedFeed object and related Objective-C objects.
|
|
|
|
// These functions take an RSParsedFeed and return a Swift-y ParsedFeed,
|
|
|
|
// which is part of providing a single API for feed parsing.
|
|
|
|
|
|
|
|
struct RSParsedFeedTransformer {
|
|
|
|
|
|
|
|
static func parsedFeed(_ rsParsedFeed: RSParsedFeed) -> ParsedFeed {
|
|
|
|
|
|
|
|
let items = parsedItems(rsParsedFeed.articles)
|
|
|
|
return ParsedFeed(type: .rss, title: rsParsedFeed.title, homePageURL: rsParsedFeed.link, feedURL: rsParsedFeed.urlString, feedDescription: nil, nextURL: nil, iconURL: nil, faviconURL: nil, authors: nil, expired: false, hubs: nil, items: items)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private extension RSParsedFeedTransformer {
|
|
|
|
|
2017-09-10 03:46:58 +02:00
|
|
|
static func parsedItems(_ parsedArticles: Set<RSParsedArticle>) -> Set<ParsedItem> {
|
2017-06-26 01:32:07 +02:00
|
|
|
|
2017-09-10 03:46:58 +02:00
|
|
|
// Create Set<ParsedItem> from Set<RSParsedArticle>
|
2017-06-26 01:32:07 +02:00
|
|
|
|
2017-09-10 03:46:58 +02:00
|
|
|
return Set(parsedArticles.map(parsedItem))
|
2017-06-26 01:32:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static func parsedItem(_ parsedArticle: RSParsedArticle) -> ParsedItem {
|
|
|
|
|
|
|
|
let uniqueID = parsedArticle.articleID
|
|
|
|
let url = parsedArticle.permalink
|
|
|
|
let externalURL = parsedArticle.link
|
|
|
|
let title = parsedArticle.title
|
|
|
|
let contentHTML = parsedArticle.body
|
|
|
|
let datePublished = parsedArticle.datePublished
|
|
|
|
let dateModified = parsedArticle.dateModified
|
|
|
|
let authors = parsedAuthors(parsedArticle.author)
|
|
|
|
|
2017-09-03 01:08:02 +02:00
|
|
|
return ParsedItem(syncServiceID: nil, uniqueID: uniqueID, feedURL: parsedArticle.feedURL, url: url, externalURL: externalURL, title: title, contentHTML: contentHTML, contentText: nil, summary: nil, imageURL: nil, bannerImageURL: nil, datePublished: datePublished, dateModified: dateModified, authors: authors, tags: nil, attachments: nil)
|
2017-06-26 01:32:07 +02:00
|
|
|
}
|
|
|
|
|
2017-09-10 20:02:05 +02:00
|
|
|
static func parsedAuthors(_ authorEmailAddress: String?) -> Set<ParsedAuthor>? {
|
2017-06-26 01:32:07 +02:00
|
|
|
|
|
|
|
guard let authorEmailAddress = authorEmailAddress else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
let author = ParsedAuthor(name: nil, url: nil, avatarURL: nil, emailAddress: authorEmailAddress)
|
2017-09-10 20:02:05 +02:00
|
|
|
return Set([author])
|
2017-06-26 01:32:07 +02:00
|
|
|
}
|
|
|
|
}
|