Start FeedIconDownloader.
This commit is contained in:
parent
441a8f49b3
commit
82dace2acc
@ -7,6 +7,7 @@
|
|||||||
objects = {
|
objects = {
|
||||||
|
|
||||||
/* Begin PBXBuildFile section */
|
/* Begin PBXBuildFile section */
|
||||||
|
8426118A1FCB67AA0086A189 /* FeedIconDownloader.swift in Sources */ = {isa = PBXBuildFile; fileRef = 842611891FCB67AA0086A189 /* FeedIconDownloader.swift */; };
|
||||||
842E45CE1ED8C308000A8B52 /* AppNotifications.swift in Sources */ = {isa = PBXBuildFile; fileRef = 842E45CD1ED8C308000A8B52 /* AppNotifications.swift */; };
|
842E45CE1ED8C308000A8B52 /* AppNotifications.swift in Sources */ = {isa = PBXBuildFile; fileRef = 842E45CD1ED8C308000A8B52 /* AppNotifications.swift */; };
|
||||||
842E45DD1ED8C54B000A8B52 /* Browser.swift in Sources */ = {isa = PBXBuildFile; fileRef = 842E45DC1ED8C54B000A8B52 /* Browser.swift */; };
|
842E45DD1ED8C54B000A8B52 /* Browser.swift in Sources */ = {isa = PBXBuildFile; fileRef = 842E45DC1ED8C54B000A8B52 /* Browser.swift */; };
|
||||||
842E45E31ED8C681000A8B52 /* KeyboardDelegateProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = 842E45E21ED8C681000A8B52 /* KeyboardDelegateProtocol.swift */; };
|
842E45E31ED8C681000A8B52 /* KeyboardDelegateProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = 842E45E21ED8C681000A8B52 /* KeyboardDelegateProtocol.swift */; };
|
||||||
@ -403,6 +404,7 @@
|
|||||||
/* End PBXCopyFilesBuildPhase section */
|
/* End PBXCopyFilesBuildPhase section */
|
||||||
|
|
||||||
/* Begin PBXFileReference section */
|
/* Begin PBXFileReference section */
|
||||||
|
842611891FCB67AA0086A189 /* FeedIconDownloader.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FeedIconDownloader.swift; sourceTree = "<group>"; };
|
||||||
842E45CD1ED8C308000A8B52 /* AppNotifications.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = AppNotifications.swift; path = Evergreen/AppNotifications.swift; sourceTree = "<group>"; };
|
842E45CD1ED8C308000A8B52 /* AppNotifications.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = AppNotifications.swift; path = Evergreen/AppNotifications.swift; sourceTree = "<group>"; };
|
||||||
842E45DC1ED8C54B000A8B52 /* Browser.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Browser.swift; path = Evergreen/Browser.swift; sourceTree = "<group>"; };
|
842E45DC1ED8C54B000A8B52 /* Browser.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Browser.swift; path = Evergreen/Browser.swift; sourceTree = "<group>"; };
|
||||||
842E45E21ED8C681000A8B52 /* KeyboardDelegateProtocol.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = KeyboardDelegateProtocol.swift; sourceTree = "<group>"; };
|
842E45E21ED8C681000A8B52 /* KeyboardDelegateProtocol.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = KeyboardDelegateProtocol.swift; sourceTree = "<group>"; };
|
||||||
@ -567,6 +569,7 @@
|
|||||||
children = (
|
children = (
|
||||||
845213221FCA5B10003B6E93 /* ImageDownloader.swift */,
|
845213221FCA5B10003B6E93 /* ImageDownloader.swift */,
|
||||||
84E850851FCB60CE0072EA88 /* AuthorAvatarDownloader.swift */,
|
84E850851FCB60CE0072EA88 /* AuthorAvatarDownloader.swift */,
|
||||||
|
842611891FCB67AA0086A189 /* FeedIconDownloader.swift */,
|
||||||
);
|
);
|
||||||
name = Images;
|
name = Images;
|
||||||
path = Evergreen/Images;
|
path = Evergreen/Images;
|
||||||
@ -1349,6 +1352,7 @@
|
|||||||
849A97651ED9EB96007D329B /* SidebarTreeControllerDelegate.swift in Sources */,
|
849A97651ED9EB96007D329B /* SidebarTreeControllerDelegate.swift in Sources */,
|
||||||
849A97671ED9EB96007D329B /* UnreadCountView.swift in Sources */,
|
849A97671ED9EB96007D329B /* UnreadCountView.swift in Sources */,
|
||||||
84AFBB3C1FBE76D800BA41CF /* PanicButtonWindowController.swift in Sources */,
|
84AFBB3C1FBE76D800BA41CF /* PanicButtonWindowController.swift in Sources */,
|
||||||
|
8426118A1FCB67AA0086A189 /* FeedIconDownloader.swift in Sources */,
|
||||||
84E95D241FB1087500552D99 /* ArticlePasteboardWriter.swift in Sources */,
|
84E95D241FB1087500552D99 /* ArticlePasteboardWriter.swift in Sources */,
|
||||||
84A6B6961FB8DBD2006754AC /* DinosaursWindowController.swift in Sources */,
|
84A6B6961FB8DBD2006754AC /* DinosaursWindowController.swift in Sources */,
|
||||||
849A975B1ED9EB0D007D329B /* ArticleUtilities.swift in Sources */,
|
849A975B1ED9EB0D007D329B /* ArticleUtilities.swift in Sources */,
|
||||||
|
119
Evergreen/Images/FeedIconDownloader.swift
Normal file
119
Evergreen/Images/FeedIconDownloader.swift
Normal file
@ -0,0 +1,119 @@
|
|||||||
|
//
|
||||||
|
// FeedIconDownloader.swift
|
||||||
|
// Evergreen
|
||||||
|
//
|
||||||
|
// Created by Brent Simmons on 11/26/17.
|
||||||
|
// Copyright © 2017 Ranchero Software. All rights reserved.
|
||||||
|
//
|
||||||
|
|
||||||
|
import Cocoa
|
||||||
|
import Data
|
||||||
|
import RSWeb
|
||||||
|
import RSParser
|
||||||
|
|
||||||
|
public final class FeedIconDownloader {
|
||||||
|
|
||||||
|
private let imageDownloader: ImageDownloader
|
||||||
|
private var homePageToIconURLCache = [String: String]()
|
||||||
|
|
||||||
|
init(imageDownloader: ImageDownloader) {
|
||||||
|
|
||||||
|
self.imageDownloader = imageDownloader
|
||||||
|
}
|
||||||
|
|
||||||
|
func icon(for feed: Feed) -> NSImage? {
|
||||||
|
|
||||||
|
if let iconURL = feed.iconURL {
|
||||||
|
return icon(forURL: iconURL)
|
||||||
|
}
|
||||||
|
|
||||||
|
if let homePageURL = feed.homePageURL {
|
||||||
|
return icon(forHomePageURL: homePageURL)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func icon(forHomePageURL homePageURL: String) -> NSImage? {
|
||||||
|
|
||||||
|
if let iconURL = cachedIconURL(for: homePageURL) {
|
||||||
|
return icon(forURL: iconURL)
|
||||||
|
}
|
||||||
|
|
||||||
|
findIconURLForHomePageURL(homePageURL)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func icon(forURL url: String) -> NSImage? {
|
||||||
|
|
||||||
|
return imageDownloader.image(for: url)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private extension FeedIconDownloader {
|
||||||
|
|
||||||
|
func cachedIconURL(for homePageURL: String) -> String? {
|
||||||
|
|
||||||
|
return homePageToIconURLCache[homePageURL]
|
||||||
|
}
|
||||||
|
|
||||||
|
func cacheIconURL(for homePageURL: String, _ iconURL: String) {
|
||||||
|
|
||||||
|
homePageToIconURLCache[homePageURL] = iconURL
|
||||||
|
}
|
||||||
|
|
||||||
|
func findIconURLForHomePageURL(_ homePageURL: String) {
|
||||||
|
|
||||||
|
guard let url = URL(string: homePageURL) else {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
downloadUsingCache(url) { (data, response, error) in
|
||||||
|
|
||||||
|
if let data = data, !data.isEmpty, let response = response, response.statusIsOK, error == nil {
|
||||||
|
|
||||||
|
let parserData = ParserData(url: homePageURL, data: data)
|
||||||
|
let metadata = RSHTMLMetadataParser.htmlMetadata(with: parserData)
|
||||||
|
self.pullIconURL(from: metadata, homePageURL: homePageURL)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if let error = error {
|
||||||
|
appDelegate.logMessage("Error finding icon url at \(homePageURL): \(error)", type: .warning)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func pullIconURL(from metadata: RSHTMLMetadata, homePageURL: String) {
|
||||||
|
|
||||||
|
if let openGraphImageURL = largestOpenGraphImageURL(from: metadata) {
|
||||||
|
cacheIconURL(for: homePageURL, openGraphImageURL)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if let twitterImageURL = metadata.twitterProperties.imageURL {
|
||||||
|
cacheIconURL(for: homePageURL, twitterImageURL)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func largestOpenGraphImageURL(from metadata: RSHTMLMetadata) -> String? {
|
||||||
|
|
||||||
|
guard let openGraphImages = metadata.openGraphProperties?.images else {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var bestImage: RSHTMLOpenGraphImage? = nil
|
||||||
|
|
||||||
|
for image in openGraphImages {
|
||||||
|
if bestImage == nil {
|
||||||
|
bestImage = image
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if image.height > bestImage!.height && image.width > bestImage!.width {
|
||||||
|
bestImage = image
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return bestImage?.secureURL ?? bestImage?.url
|
||||||
|
}
|
||||||
|
}
|
@ -114,8 +114,8 @@ private final class DownloadWithCacheManager {
|
|||||||
|
|
||||||
static let shared = DownloadWithCacheManager()
|
static let shared = DownloadWithCacheManager()
|
||||||
private var cache = WebCache()
|
private var cache = WebCache()
|
||||||
private static let timeToLive: TimeInterval = 5 * 60 // five minutes
|
private static let timeToLive: TimeInterval = 10 * 60 // 10 minutes
|
||||||
private static let cleanupInterval: TimeInterval = 20 * 60 // 20 minutes
|
private static let cleanupInterval: TimeInterval = 30 * 60 // 30 minutes
|
||||||
|
|
||||||
func download(_ url: URL, _ callback: @escaping OneShotDownloadCallback) {
|
func download(_ url: URL, _ callback: @escaping OneShotDownloadCallback) {
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user