2017-11-26 22:48:40 +01:00
|
|
|
//
|
|
|
|
// FeedIconDownloader.swift
|
2018-08-29 07:18:24 +02:00
|
|
|
// NetNewsWire
|
2017-11-26 22:48:40 +01:00
|
|
|
//
|
|
|
|
// Created by Brent Simmons on 11/26/17.
|
|
|
|
// Copyright © 2017 Ranchero Software. All rights reserved.
|
|
|
|
//
|
|
|
|
|
2019-04-15 22:03:05 +02:00
|
|
|
import Foundation
|
2018-07-24 03:29:08 +02:00
|
|
|
import Articles
|
2018-07-28 21:16:14 +02:00
|
|
|
import Account
|
2019-04-12 00:53:03 +02:00
|
|
|
import RSCore
|
2017-11-26 22:48:40 +01:00
|
|
|
import RSWeb
|
|
|
|
import RSParser
|
|
|
|
|
2018-01-05 06:20:09 +01:00
|
|
|
extension Notification.Name {
|
|
|
|
|
2019-11-15 03:11:41 +01:00
|
|
|
static let WebFeedIconDidBecomeAvailable = Notification.Name("WebFeedIconDidBecomeAvailableNotification") // UserInfoKey.feed
|
2018-01-05 06:20:09 +01:00
|
|
|
}
|
|
|
|
|
2019-11-15 03:11:41 +01:00
|
|
|
public final class WebFeedIconDownloader {
|
2017-11-26 22:48:40 +01:00
|
|
|
|
2019-08-27 02:40:30 +02:00
|
|
|
private static let saveQueue = CoalescingQueue(name: "Cache Save Queue", interval: 1.0)
|
|
|
|
|
2017-11-26 22:48:40 +01:00
|
|
|
private let imageDownloader: ImageDownloader
|
2019-08-27 02:40:30 +02:00
|
|
|
|
2020-04-16 09:17:38 +02:00
|
|
|
private var feedURLToIconURLCache = [String: String]()
|
|
|
|
private var feedURLToIconURLCachePath: String
|
|
|
|
private var feedURLToIconURLCacheDirty = false {
|
|
|
|
didSet {
|
|
|
|
queueSaveFeedURLToIconURLCacheIfNeeded()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-26 22:48:40 +01:00
|
|
|
private var homePageToIconURLCache = [String: String]()
|
2019-08-27 02:40:30 +02:00
|
|
|
private var homePageToIconURLCachePath: String
|
|
|
|
private var homePageToIconURLCacheDirty = false {
|
|
|
|
didSet {
|
|
|
|
queueSaveHomePageToIconURLCacheIfNeeded()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-31 19:38:38 +01:00
|
|
|
private var homePagesWithNoIconURLCache = Set<String>()
|
|
|
|
private var homePagesWithNoIconURLCachePath: String
|
|
|
|
private var homePagesWithNoIconURLCacheDirty = false {
|
|
|
|
didSet {
|
|
|
|
queueHomePagesWithNoIconURLCacheIfNeeded()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-07 00:59:27 +01:00
|
|
|
private var homePagesWithUglyIcons: Set<String> = {
|
2020-12-15 04:36:47 +01:00
|
|
|
return Set(["https://www.macsparky.com/", "https://xkcd.com/"])
|
2019-11-07 00:59:27 +01:00
|
|
|
}()
|
|
|
|
|
2017-11-29 06:39:09 +01:00
|
|
|
private var urlsInProgress = Set<String>()
|
2019-11-15 03:11:41 +01:00
|
|
|
private var cache = [WebFeed: IconImage]()
|
|
|
|
private var waitingForFeedURLs = [String: WebFeed]()
|
2020-04-16 09:17:38 +02:00
|
|
|
|
2019-08-27 02:40:30 +02:00
|
|
|
init(imageDownloader: ImageDownloader, folder: String) {
|
2017-11-26 22:48:40 +01:00
|
|
|
self.imageDownloader = imageDownloader
|
2020-04-16 09:17:38 +02:00
|
|
|
self.feedURLToIconURLCachePath = (folder as NSString).appendingPathComponent("FeedURLToIconURLCache.plist")
|
2019-08-27 02:40:30 +02:00
|
|
|
self.homePageToIconURLCachePath = (folder as NSString).appendingPathComponent("HomePageToIconURLCache.plist")
|
2019-10-31 19:38:38 +01:00
|
|
|
self.homePagesWithNoIconURLCachePath = (folder as NSString).appendingPathComponent("HomePagesWithNoIconURLCache.plist")
|
2020-04-16 09:17:38 +02:00
|
|
|
loadFeedURLToIconURLCache()
|
2019-08-27 02:40:30 +02:00
|
|
|
loadHomePageToIconURLCache()
|
2019-10-31 19:38:38 +01:00
|
|
|
loadHomePagesWithNoIconURLCache()
|
2019-08-26 19:54:23 +02:00
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(imageDidBecomeAvailable(_:)), name: .ImageDidBecomeAvailable, object: imageDownloader)
|
2017-11-26 22:48:40 +01:00
|
|
|
}
|
|
|
|
|
2019-06-14 22:33:13 +02:00
|
|
|
func resetCache() {
|
2019-11-15 03:11:41 +01:00
|
|
|
cache = [WebFeed: IconImage]()
|
2019-06-14 22:33:13 +02:00
|
|
|
}
|
|
|
|
|
2019-11-15 03:11:41 +01:00
|
|
|
func icon(for feed: WebFeed) -> IconImage? {
|
2017-11-26 22:48:40 +01:00
|
|
|
|
2017-11-29 06:39:09 +01:00
|
|
|
if let cachedImage = cache[feed] {
|
|
|
|
return cachedImage
|
|
|
|
}
|
|
|
|
|
2020-03-30 17:56:52 +02:00
|
|
|
if let hpURLString = feed.homePageURL, let hpURL = URL(string: hpURLString), hpURL.host == "nnw.ranchero.com" {
|
|
|
|
return IconImage.appIcon
|
|
|
|
}
|
|
|
|
|
2019-05-18 08:02:18 +02:00
|
|
|
func checkHomePageURL() {
|
|
|
|
guard let homePageURL = feed.homePageURL else {
|
|
|
|
return
|
|
|
|
}
|
2019-08-26 19:54:23 +02:00
|
|
|
icon(forHomePageURL: homePageURL, feed: feed) { (image) in
|
2019-05-15 07:44:06 +02:00
|
|
|
if let image = image {
|
|
|
|
self.postFeedIconDidBecomeAvailableNotification(feed)
|
2019-11-06 01:05:57 +01:00
|
|
|
self.cache[feed] = IconImage(image)
|
2019-05-15 07:44:06 +02:00
|
|
|
}
|
2017-11-29 06:39:09 +01:00
|
|
|
}
|
2017-11-26 22:48:40 +01:00
|
|
|
}
|
2020-04-16 03:17:33 +02:00
|
|
|
|
|
|
|
func checkFeedIconURL() {
|
|
|
|
if let iconURL = feed.iconURL {
|
|
|
|
icon(forURL: iconURL, feed: feed) { (image) in
|
|
|
|
if let image = image {
|
|
|
|
self.postFeedIconDidBecomeAvailableNotification(feed)
|
|
|
|
self.cache[feed] = IconImage(image)
|
|
|
|
} else {
|
|
|
|
checkHomePageURL()
|
|
|
|
}
|
2019-05-18 08:02:18 +02:00
|
|
|
}
|
2020-04-16 03:17:33 +02:00
|
|
|
} else {
|
|
|
|
checkHomePageURL()
|
2017-11-29 06:39:09 +01:00
|
|
|
}
|
2017-11-26 22:48:40 +01:00
|
|
|
}
|
2020-04-16 09:17:38 +02:00
|
|
|
|
|
|
|
if let feedProviderURL = feedURLToIconURLCache[feed.url] {
|
|
|
|
self.icon(forURL: feedProviderURL, feed: feed) { (image) in
|
|
|
|
if let image = image {
|
|
|
|
self.postFeedIconDidBecomeAvailableNotification(feed)
|
|
|
|
self.cache[feed] = IconImage(image)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-24 20:33:43 +02:00
|
|
|
if let components = URLComponents(string: feed.url), let feedProvider = FeedProviderManager.shared.best(for: components) {
|
2020-05-05 00:48:53 +02:00
|
|
|
guard !urlsInProgress.contains(feed.url) else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
urlsInProgress.insert(feed.url)
|
|
|
|
|
2020-04-16 03:17:33 +02:00
|
|
|
feedProvider.iconURL(components) { result in
|
2020-05-05 00:48:53 +02:00
|
|
|
self.urlsInProgress.remove(feed.url)
|
2020-04-16 09:17:38 +02:00
|
|
|
switch result {
|
|
|
|
case .success(let feedProviderURL):
|
|
|
|
self.feedURLToIconURLCache[feed.url] = feedProviderURL
|
|
|
|
self.feedURLToIconURLCacheDirty = true
|
|
|
|
self.icon(forURL: feedProviderURL, feed: feed) { (image) in
|
2020-04-16 03:17:33 +02:00
|
|
|
if let image = image {
|
|
|
|
self.postFeedIconDidBecomeAvailableNotification(feed)
|
|
|
|
self.cache[feed] = IconImage(image)
|
|
|
|
}
|
|
|
|
}
|
2020-04-16 09:17:38 +02:00
|
|
|
case .failure:
|
|
|
|
checkFeedIconURL()
|
2020-04-16 03:17:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
checkFeedIconURL()
|
|
|
|
}
|
2017-11-26 22:48:40 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2019-08-26 19:54:23 +02:00
|
|
|
|
|
|
|
@objc func imageDidBecomeAvailable(_ note: Notification) {
|
|
|
|
guard let url = note.userInfo?[UserInfoKey.url] as? String, let feed = waitingForFeedURLs[url] else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
waitingForFeedURLs[url] = nil
|
|
|
|
_ = icon(for: feed)
|
|
|
|
}
|
|
|
|
|
2020-04-16 09:17:38 +02:00
|
|
|
@objc func saveFeedURLToIconURLCacheIfNeeded() {
|
|
|
|
if feedURLToIconURLCacheDirty {
|
|
|
|
saveFeedURLToIconURLCache()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-27 02:40:30 +02:00
|
|
|
@objc func saveHomePageToIconURLCacheIfNeeded() {
|
|
|
|
if homePageToIconURLCacheDirty {
|
|
|
|
saveHomePageToIconURLCache()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-31 19:38:38 +01:00
|
|
|
@objc func saveHomePagesWithNoIconURLCacheIfNeeded() {
|
|
|
|
if homePagesWithNoIconURLCacheDirty {
|
|
|
|
saveHomePagesWithNoIconURLCache()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-05 06:20:09 +01:00
|
|
|
}
|
|
|
|
|
2019-11-15 03:11:41 +01:00
|
|
|
private extension WebFeedIconDownloader {
|
2017-11-26 22:48:40 +01:00
|
|
|
|
2019-11-15 03:11:41 +01:00
|
|
|
func icon(forHomePageURL homePageURL: String, feed: WebFeed, _ imageResultBlock: @escaping (RSImage?) -> Void) {
|
2017-11-26 22:48:40 +01:00
|
|
|
|
2019-11-07 00:59:27 +01:00
|
|
|
if homePagesWithNoIconURLCache.contains(homePageURL) || homePagesWithUglyIcons.contains(homePageURL) {
|
2019-05-15 07:44:06 +02:00
|
|
|
imageResultBlock(nil)
|
|
|
|
return
|
2017-11-27 04:57:45 +01:00
|
|
|
}
|
|
|
|
|
2017-11-26 22:48:40 +01:00
|
|
|
if let iconURL = cachedIconURL(for: homePageURL) {
|
2019-08-26 19:54:23 +02:00
|
|
|
icon(forURL: iconURL, feed: feed, imageResultBlock)
|
2019-05-15 07:44:06 +02:00
|
|
|
return
|
2017-11-26 22:48:40 +01:00
|
|
|
}
|
2017-11-26 23:03:08 +01:00
|
|
|
|
2019-08-26 19:54:23 +02:00
|
|
|
findIconURLForHomePageURL(homePageURL, feed: feed)
|
2017-11-26 22:48:40 +01:00
|
|
|
}
|
|
|
|
|
2019-11-15 03:11:41 +01:00
|
|
|
func icon(forURL url: String, feed: WebFeed, _ imageResultBlock: @escaping (RSImage?) -> Void) {
|
2019-08-26 19:54:23 +02:00
|
|
|
waitingForFeedURLs[url] = feed
|
2019-05-15 07:44:06 +02:00
|
|
|
guard let imageData = imageDownloader.image(for: url) else {
|
|
|
|
imageResultBlock(nil)
|
|
|
|
return
|
2019-04-12 00:53:03 +02:00
|
|
|
}
|
2019-11-06 01:05:57 +01:00
|
|
|
RSImage.scaledForIcon(imageData, imageResultBlock: imageResultBlock)
|
2017-11-26 22:48:40 +01:00
|
|
|
}
|
|
|
|
|
2019-11-15 03:11:41 +01:00
|
|
|
func postFeedIconDidBecomeAvailableNotification(_ feed: WebFeed) {
|
2018-01-05 06:20:09 +01:00
|
|
|
|
|
|
|
DispatchQueue.main.async {
|
2019-11-15 03:11:41 +01:00
|
|
|
let userInfo: [AnyHashable: Any] = [UserInfoKey.webFeed: feed]
|
|
|
|
NotificationCenter.default.post(name: .WebFeedIconDidBecomeAvailable, object: self, userInfo: userInfo)
|
2018-01-05 06:20:09 +01:00
|
|
|
}
|
|
|
|
}
|
2017-11-26 22:48:40 +01:00
|
|
|
|
|
|
|
func cachedIconURL(for homePageURL: String) -> String? {
|
|
|
|
|
|
|
|
return homePageToIconURLCache[homePageURL]
|
|
|
|
}
|
|
|
|
|
|
|
|
func cacheIconURL(for homePageURL: String, _ iconURL: String) {
|
2019-10-31 19:38:38 +01:00
|
|
|
homePagesWithNoIconURLCache.remove(homePageURL)
|
|
|
|
homePagesWithNoIconURLCacheDirty = true
|
2017-11-26 22:48:40 +01:00
|
|
|
homePageToIconURLCache[homePageURL] = iconURL
|
2019-08-27 02:40:30 +02:00
|
|
|
homePageToIconURLCacheDirty = true
|
2017-11-26 22:48:40 +01:00
|
|
|
}
|
|
|
|
|
2019-11-15 03:11:41 +01:00
|
|
|
func findIconURLForHomePageURL(_ homePageURL: String, feed: WebFeed) {
|
2017-11-26 22:48:40 +01:00
|
|
|
|
2017-11-29 06:39:09 +01:00
|
|
|
guard !urlsInProgress.contains(homePageURL) else {
|
2017-11-27 04:57:45 +01:00
|
|
|
return
|
|
|
|
}
|
2017-11-29 06:39:09 +01:00
|
|
|
urlsInProgress.insert(homePageURL)
|
2017-11-27 04:57:45 +01:00
|
|
|
|
2017-11-26 23:03:08 +01:00
|
|
|
HTMLMetadataDownloader.downloadMetadata(for: homePageURL) { (metadata) in
|
2017-11-26 22:48:40 +01:00
|
|
|
|
2017-11-29 06:39:09 +01:00
|
|
|
self.urlsInProgress.remove(homePageURL)
|
2017-11-26 23:03:08 +01:00
|
|
|
guard let metadata = metadata else {
|
2017-11-26 22:48:40 +01:00
|
|
|
return
|
|
|
|
}
|
2019-08-26 19:54:23 +02:00
|
|
|
self.pullIconURL(from: metadata, homePageURL: homePageURL, feed: feed)
|
2017-11-26 22:48:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-15 03:11:41 +01:00
|
|
|
func pullIconURL(from metadata: RSHTMLMetadata, homePageURL: String, feed: WebFeed) {
|
2017-11-26 22:48:40 +01:00
|
|
|
|
2017-11-27 04:57:45 +01:00
|
|
|
if let url = metadata.bestWebsiteIconURL() {
|
|
|
|
cacheIconURL(for: homePageURL, url)
|
2019-08-26 19:54:23 +02:00
|
|
|
icon(forURL: url, feed: feed) { (image) in
|
2019-05-15 07:44:06 +02:00
|
|
|
}
|
2017-11-26 22:48:40 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-10-31 19:38:38 +01:00
|
|
|
homePagesWithNoIconURLCache.insert(homePageURL)
|
|
|
|
homePagesWithNoIconURLCacheDirty = true
|
2017-11-26 22:48:40 +01:00
|
|
|
}
|
2019-08-27 02:40:30 +02:00
|
|
|
|
2020-04-16 09:17:38 +02:00
|
|
|
func loadFeedURLToIconURLCache() {
|
|
|
|
let url = URL(fileURLWithPath: feedURLToIconURLCachePath)
|
|
|
|
guard let data = try? Data(contentsOf: url) else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
let decoder = PropertyListDecoder()
|
|
|
|
feedURLToIconURLCache = (try? decoder.decode([String: String].self, from: data)) ?? [String: String]()
|
|
|
|
}
|
|
|
|
|
2019-08-27 02:40:30 +02:00
|
|
|
func loadHomePageToIconURLCache() {
|
|
|
|
let url = URL(fileURLWithPath: homePageToIconURLCachePath)
|
|
|
|
guard let data = try? Data(contentsOf: url) else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
let decoder = PropertyListDecoder()
|
|
|
|
homePageToIconURLCache = (try? decoder.decode([String: String].self, from: data)) ?? [String: String]()
|
|
|
|
}
|
|
|
|
|
2019-10-31 19:38:38 +01:00
|
|
|
func loadHomePagesWithNoIconURLCache() {
|
|
|
|
let url = URL(fileURLWithPath: homePagesWithNoIconURLCachePath)
|
|
|
|
guard let data = try? Data(contentsOf: url) else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
let decoder = PropertyListDecoder()
|
|
|
|
let decoded = (try? decoder.decode([String].self, from: data)) ?? [String]()
|
|
|
|
homePagesWithNoIconURLCache = Set(decoded)
|
|
|
|
}
|
|
|
|
|
2020-04-16 09:17:38 +02:00
|
|
|
func queueSaveFeedURLToIconURLCacheIfNeeded() {
|
|
|
|
WebFeedIconDownloader.saveQueue.add(self, #selector(saveFeedURLToIconURLCacheIfNeeded))
|
|
|
|
}
|
|
|
|
|
2019-08-27 02:40:30 +02:00
|
|
|
func queueSaveHomePageToIconURLCacheIfNeeded() {
|
2019-11-15 03:11:41 +01:00
|
|
|
WebFeedIconDownloader.saveQueue.add(self, #selector(saveHomePageToIconURLCacheIfNeeded))
|
2019-08-27 02:40:30 +02:00
|
|
|
}
|
|
|
|
|
2019-10-31 19:38:38 +01:00
|
|
|
func queueHomePagesWithNoIconURLCacheIfNeeded() {
|
2019-11-15 03:11:41 +01:00
|
|
|
WebFeedIconDownloader.saveQueue.add(self, #selector(saveHomePagesWithNoIconURLCacheIfNeeded))
|
2019-10-31 19:38:38 +01:00
|
|
|
}
|
|
|
|
|
2020-04-16 09:17:38 +02:00
|
|
|
func saveFeedURLToIconURLCache() {
|
|
|
|
feedURLToIconURLCacheDirty = false
|
|
|
|
|
|
|
|
let encoder = PropertyListEncoder()
|
|
|
|
encoder.outputFormat = .binary
|
|
|
|
let url = URL(fileURLWithPath: feedURLToIconURLCachePath)
|
|
|
|
do {
|
|
|
|
let data = try encoder.encode(feedURLToIconURLCache)
|
|
|
|
try data.write(to: url)
|
|
|
|
} catch {
|
|
|
|
assertionFailure(error.localizedDescription)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-27 02:40:30 +02:00
|
|
|
func saveHomePageToIconURLCache() {
|
|
|
|
homePageToIconURLCacheDirty = false
|
|
|
|
|
|
|
|
let encoder = PropertyListEncoder()
|
|
|
|
encoder.outputFormat = .binary
|
|
|
|
let url = URL(fileURLWithPath: homePageToIconURLCachePath)
|
|
|
|
do {
|
|
|
|
let data = try encoder.encode(homePageToIconURLCache)
|
|
|
|
try data.write(to: url)
|
|
|
|
} catch {
|
|
|
|
assertionFailure(error.localizedDescription)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-31 19:38:38 +01:00
|
|
|
func saveHomePagesWithNoIconURLCache() {
|
|
|
|
homePagesWithNoIconURLCacheDirty = false
|
|
|
|
|
|
|
|
let encoder = PropertyListEncoder()
|
|
|
|
encoder.outputFormat = .binary
|
|
|
|
let url = URL(fileURLWithPath: homePagesWithNoIconURLCachePath)
|
|
|
|
do {
|
|
|
|
let data = try encoder.encode(Array(homePagesWithNoIconURLCache))
|
|
|
|
try data.write(to: url)
|
|
|
|
} catch {
|
|
|
|
assertionFailure(error.localizedDescription)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-26 22:48:40 +01:00
|
|
|
}
|