From 91895d4066bf1627015aaba2b47bc74379f8552c Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Mon, 20 Nov 2017 13:16:06 -0800 Subject: [PATCH] Create FaviconURLFinder, which pulls the favicon URL from the metadata of a web page. --- Evergreen.xcodeproj/project.pbxproj | 4 +++ Evergreen/Favicons/FaviconURLFinder.swift | 43 +++++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 Evergreen/Favicons/FaviconURLFinder.swift diff --git a/Evergreen.xcodeproj/project.pbxproj b/Evergreen.xcodeproj/project.pbxproj index 5d3c845ed..827e6f99d 100644 --- a/Evergreen.xcodeproj/project.pbxproj +++ b/Evergreen.xcodeproj/project.pbxproj @@ -106,6 +106,7 @@ 84F2D53A1FC2308B00998D64 /* UnreadFeed.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84F2D5391FC2308B00998D64 /* UnreadFeed.swift */; }; 84FB9A2F1EDCD6C4003D53B9 /* Sparkle.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 84FB9A2D1EDCD6B8003D53B9 /* Sparkle.framework */; }; 84FB9A301EDCD6C4003D53B9 /* Sparkle.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 84FB9A2D1EDCD6B8003D53B9 /* Sparkle.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; }; + 84FF69B11FC3793300DC198E /* FaviconURLFinder.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84FF69B01FC3793300DC198E /* FaviconURLFinder.swift */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -488,6 +489,7 @@ 84F2D5361FC22FCB00998D64 /* TodayFeedDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TodayFeedDelegate.swift; sourceTree = ""; }; 84F2D5391FC2308B00998D64 /* UnreadFeed.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UnreadFeed.swift; sourceTree = ""; }; 84FB9A2D1EDCD6B8003D53B9 /* Sparkle.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Sparkle.framework; path = Frameworks/Vendor/Sparkle.framework; sourceTree = SOURCE_ROOT; }; + 84FF69B01FC3793300DC198E /* FaviconURLFinder.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FaviconURLFinder.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -579,6 +581,7 @@ isa = PBXGroup; children = ( 848F6AE41FC29CFA002D422E /* FaviconDownloader.swift */, + 84FF69B01FC3793300DC198E /* FaviconURLFinder.swift */, ); name = Favicons; path = Evergreen/Favicons; @@ -1315,6 +1318,7 @@ 84DAEE301F86CAFE0058304B /* OPMLImporter.swift in Sources */, 849A975C1ED9EB0D007D329B /* DefaultFeedsImporter.swift in Sources */, 849A97891ED9ECEF007D329B /* ArticleStyle.swift in Sources */, + 84FF69B11FC3793300DC198E /* FaviconURLFinder.swift in Sources */, 849A978A1ED9ECEF007D329B /* ArticleStylesManager.swift in Sources */, 849A97791ED9EC04007D329B /* TimelineStringUtilities.swift in Sources */, 84F204CE1FAACB660076E152 /* FeedListViewController.swift in Sources */, diff --git a/Evergreen/Favicons/FaviconURLFinder.swift b/Evergreen/Favicons/FaviconURLFinder.swift new file mode 100644 index 000000000..36511637e --- /dev/null +++ b/Evergreen/Favicons/FaviconURLFinder.swift @@ -0,0 +1,43 @@ +// +// FaviconURLFinder.swift +// Evergreen +// +// Created by Brent Simmons on 11/20/17. +// Copyright © 2017 Ranchero Software. All rights reserved. +// + +import Foundation +import RSParser +import RSWeb + +// The favicon URL may be specified in the head section of the home page. + +struct FaviconURLFinder { + + static func findFaviconURL(_ homePageURL: String, _ callback: @escaping (String?) -> Void) { + + guard let url = URL(string: homePageURL) else { + callback(nil) + return + } + + download(url) { (data, response, error) in + + guard let data = data, let response = response, response.statusIsOK else { + callback(nil) + return + } + + let link = faviconURL(homePageURL, data) + callback(link) + } + } + + static private func faviconURL(_ url: String, _ webPageData: Data) -> String? { + + let parserData = ParserData(url: url, data: webPageData) + let htmlMetadata = RSHTMLMetadataParser.htmlMetadata(with: parserData) + return htmlMetadata.faviconLink + } +} +