Add HTMLMetadataDownloader.

This commit is contained in:
Brent Simmons 2017-11-26 14:03:08 -08:00
parent 82dace2acc
commit edba636121
3 changed files with 58 additions and 15 deletions

View File

@ -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 = "<group>"; };
8426119D1FCB6ED40086A189 /* HTMLMetadataDownloader.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HTMLMetadataDownloader.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>"; };
842E45E21ED8C681000A8B52 /* KeyboardDelegateProtocol.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = KeyboardDelegateProtocol.swift; sourceTree = "<group>"; };
@ -536,6 +538,15 @@
/* End PBXFrameworksBuildPhase section */
/* Begin PBXGroup section */
8426119C1FCB6ED40086A189 /* HTMLMetadata */ = {
isa = PBXGroup;
children = (
8426119D1FCB6ED40086A189 /* HTMLMetadataDownloader.swift */,
);
name = HTMLMetadata;
path = Evergreen/HTMLMetadata;
sourceTree = "<group>";
};
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 */,

View File

@ -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)
}
}
}

View File

@ -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)
}
}