Add a special case to the JSON Feed parser for feeds that include HTML entities in their titles. At the moment this is used for kottke.org and pxlnv.com. More could be added later, and these feeds could be removed if fixed.

This commit is contained in:
Brent Simmons 2018-02-16 13:13:00 -08:00
parent d081f041f8
commit 891416e7b7
1 changed files with 29 additions and 1 deletions

View File

@ -134,7 +134,7 @@ private extension JSONFeedParser {
let url = itemDictionary[Key.url] as? String let url = itemDictionary[Key.url] as? String
let externalURL = itemDictionary[Key.externalURL] as? String let externalURL = itemDictionary[Key.externalURL] as? String
let title = itemDictionary[Key.title] as? String let title = parseTitle(itemDictionary, feedURL)
let summary = itemDictionary[Key.summary] as? String let summary = itemDictionary[Key.summary] as? String
let imageURL = itemDictionary[Key.image] as? String let imageURL = itemDictionary[Key.image] as? String
let bannerImageURL = itemDictionary[Key.bannerImage] as? String let bannerImageURL = itemDictionary[Key.bannerImage] as? String
@ -152,6 +152,34 @@ private extension JSONFeedParser {
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) 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)
} }
static func parseTitle(_ itemDictionary: JSONDictionary, _ feedURL: String) -> String? {
guard let title = itemDictionary[Key.title] as? String else {
return nil
}
if isSpecialCaseTitleWithEntitiesFeed(feedURL) {
return (title as NSString).rsparser_stringByDecodingHTMLEntities()
}
return title
}
static func isSpecialCaseTitleWithEntitiesFeed(_ feedURL: String) -> Bool {
// As of 16 Feb. 2018, Kottkes and Heers feeds includes HTML entities in the title elements.
// If we find more feeds like this, well add them here. If these feeds get fixed, well remove them.
let matchStrings = ["kottke.org", "pxlnv.com"]
for matchString in matchStrings {
if feedURL.contains(matchString) {
return true
}
}
return false
}
static func parseUniqueID(_ itemDictionary: JSONDictionary) -> String? { static func parseUniqueID(_ itemDictionary: JSONDictionary) -> String? {
if let uniqueID = itemDictionary[Key.uniqueID] as? String { if let uniqueID = itemDictionary[Key.uniqueID] as? String {