From edba636121cd44d6e708adad7f35b929f9a34ac2 Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Sun, 26 Nov 2017 14:03:08 -0800 Subject: [PATCH] Add HTMLMetadataDownloader. --- Evergreen.xcodeproj/project.pbxproj | 13 ++++++ .../HTMLMetadata/HTMLMetadataDownloader.swift | 40 +++++++++++++++++++ Evergreen/Images/FeedIconDownloader.swift | 20 +++------- 3 files changed, 58 insertions(+), 15 deletions(-) create mode 100644 Evergreen/HTMLMetadata/HTMLMetadataDownloader.swift diff --git a/Evergreen.xcodeproj/project.pbxproj b/Evergreen.xcodeproj/project.pbxproj index 5f4db5b85..941c60af1 100644 --- a/Evergreen.xcodeproj/project.pbxproj +++ b/Evergreen.xcodeproj/project.pbxproj @@ -8,6 +8,7 @@ /* Begin PBXBuildFile section */ 8426118A1FCB67AA0086A189 /* FeedIconDownloader.swift in Sources */ = {isa = PBXBuildFile; fileRef = 842611891FCB67AA0086A189 /* FeedIconDownloader.swift */; }; + 8426119E1FCB6ED40086A189 /* HTMLMetadataDownloader.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8426119D1FCB6ED40086A189 /* HTMLMetadataDownloader.swift */; }; 842E45CE1ED8C308000A8B52 /* AppNotifications.swift in Sources */ = {isa = PBXBuildFile; fileRef = 842E45CD1ED8C308000A8B52 /* AppNotifications.swift */; }; 842E45DD1ED8C54B000A8B52 /* Browser.swift in Sources */ = {isa = PBXBuildFile; fileRef = 842E45DC1ED8C54B000A8B52 /* Browser.swift */; }; 842E45E31ED8C681000A8B52 /* KeyboardDelegateProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = 842E45E21ED8C681000A8B52 /* KeyboardDelegateProtocol.swift */; }; @@ -405,6 +406,7 @@ /* Begin PBXFileReference section */ 842611891FCB67AA0086A189 /* FeedIconDownloader.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FeedIconDownloader.swift; sourceTree = ""; }; + 8426119D1FCB6ED40086A189 /* HTMLMetadataDownloader.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HTMLMetadataDownloader.swift; sourceTree = ""; }; 842E45CD1ED8C308000A8B52 /* AppNotifications.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = AppNotifications.swift; path = Evergreen/AppNotifications.swift; sourceTree = ""; }; 842E45DC1ED8C54B000A8B52 /* Browser.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Browser.swift; path = Evergreen/Browser.swift; sourceTree = ""; }; 842E45E21ED8C681000A8B52 /* KeyboardDelegateProtocol.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = KeyboardDelegateProtocol.swift; sourceTree = ""; }; @@ -536,6 +538,15 @@ /* End PBXFrameworksBuildPhase section */ /* Begin PBXGroup section */ + 8426119C1FCB6ED40086A189 /* HTMLMetadata */ = { + isa = PBXGroup; + children = ( + 8426119D1FCB6ED40086A189 /* HTMLMetadataDownloader.swift */, + ); + name = HTMLMetadata; + path = Evergreen/HTMLMetadata; + sourceTree = ""; + }; 842E45E01ED8C587000A8B52 /* Preferences */ = { isa = PBXGroup; children = ( @@ -801,6 +812,7 @@ 84DAEE201F86CAE00058304B /* Importers */, 84F2D5341FC22FCB00998D64 /* PseudoFeeds */, 849A97561ED9EB0D007D329B /* Data */, + 8426119C1FCB6ED40086A189 /* HTMLMetadata */, 848F6AE31FC29CFA002D422E /* Favicons */, 845213211FCA5B10003B6E93 /* Images */, 849A97961ED9EFAA007D329B /* Extensions */, @@ -1374,6 +1386,7 @@ 849A97431ED9EAA9007D329B /* AddFolderWindowController.swift in Sources */, 849A97921ED9EF65007D329B /* IndeterminateProgressWindowController.swift in Sources */, 849A97801ED9EC42007D329B /* DetailViewController.swift in Sources */, + 8426119E1FCB6ED40086A189 /* HTMLMetadataDownloader.swift in Sources */, 849A976E1ED9EBC8007D329B /* TimelineViewController.swift in Sources */, 849A978D1ED9EE4D007D329B /* FeedListWindowController.swift in Sources */, 849A97771ED9EC04007D329B /* TimelineCellData.swift in Sources */, diff --git a/Evergreen/HTMLMetadata/HTMLMetadataDownloader.swift b/Evergreen/HTMLMetadata/HTMLMetadataDownloader.swift new file mode 100644 index 000000000..24206fff7 --- /dev/null +++ b/Evergreen/HTMLMetadata/HTMLMetadataDownloader.swift @@ -0,0 +1,40 @@ +// +// HTMLMetadataDownloader.swift +// Evergreen +// +// Created by Brent Simmons on 11/26/17. +// Copyright © 2017 Ranchero Software. All rights reserved. +// + +import Foundation +import RSWeb +import RSParser + +struct HTMLMetadataDownloader { + + static func downloadMetadata(for url: String, _ callback: @escaping (RSHTMLMetadata?) -> Void) { + + guard let actualURL = URL(string: url) else { + callback(nil) + return + } + + downloadUsingCache(actualURL) { (data, response, error) in + + if let data = data, !data.isEmpty, let response = response, response.statusIsOK, error == nil { + + let urlToUse = response.url ?? actualURL + let parserData = ParserData(url: urlToUse.absoluteString, data: data) + let metadata = RSHTMLMetadataParser.htmlMetadata(with: parserData) + callback(metadata) + return + } + + if let error = error { + appDelegate.logMessage("Error downloading metadata at \(url): \(error)", type: .warning) + } + + callback(nil) + } + } +} diff --git a/Evergreen/Images/FeedIconDownloader.swift b/Evergreen/Images/FeedIconDownloader.swift index cd5d0c307..16a67b812 100644 --- a/Evergreen/Images/FeedIconDownloader.swift +++ b/Evergreen/Images/FeedIconDownloader.swift @@ -39,7 +39,7 @@ public final class FeedIconDownloader { if let iconURL = cachedIconURL(for: homePageURL) { return icon(forURL: iconURL) } - + findIconURLForHomePageURL(homePageURL) return nil } @@ -60,27 +60,17 @@ private extension FeedIconDownloader { func cacheIconURL(for homePageURL: String, _ iconURL: String) { homePageToIconURLCache[homePageURL] = iconURL + let _ = icon(forURL: iconURL) } func findIconURLForHomePageURL(_ homePageURL: String) { - guard let url = URL(string: homePageURL) else { - return - } + HTMLMetadataDownloader.downloadMetadata(for: homePageURL) { (metadata) in - 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) + guard let metadata = metadata else { return } - - if let error = error { - appDelegate.logMessage("Error finding icon url at \(homePageURL): \(error)", type: .warning) - } + self.pullIconURL(from: metadata, homePageURL: homePageURL) } }