mirror of
https://github.com/Ranchero-Software/NetNewsWire
synced 2026-11-28 16:26:04 +00:00
Merge branch 'feature/rachel-by-the-bay-special-cases'
This commit is contained in:
commit
3ff09d71cc
12 files changed
+151
-71
No files matched your search
@@ -640,10 +640,17 @@ private extension CloudKitAccountDelegate {
|
||||
feed.editedName = editedName
|
||||
container.addWebFeed(feed)
|
||||
|
||||
InitialFeedDownloader.download(url) { parsedFeed in
|
||||
InitialFeedDownloader.download(url) { parsedFeed, _, response, _ in
|
||||
self.syncProgress.completeTask()
|
||||
feed.lastCheckDate = Date()
|
||||
|
||||
if let parsedFeed {
|
||||
// Save conditional GET info so that first refresh uses conditional GET.
|
||||
if let httpResponse = response as? HTTPURLResponse,
|
||||
let conditionalGetInfo = HTTPConditionalGetInfo(urlResponse: httpResponse) {
|
||||
feed.conditionalGetInfo = conditionalGetInfo
|
||||
}
|
||||
|
||||
if let parsedFeed = parsedFeed {
|
||||
account.update(feed, with: parsedFeed) { result in
|
||||
switch result {
|
||||
case .success:
|
||||
|
||||
@@ -7,14 +7,24 @@
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import os.log
|
||||
import RSParser
|
||||
import RSWeb
|
||||
import RSCore
|
||||
|
||||
final class FeedFinder {
|
||||
|
||||
|
||||
private static let logger = Logger(subsystem: Bundle.main.bundleIdentifier!, category: "FeedFinder")
|
||||
|
||||
static func find(url: URL, completion: @escaping (Result<Set<FeedSpecifier>, Error>) -> Void) {
|
||||
Task { @MainActor in
|
||||
// Check special cases first.
|
||||
if let feedSpecifier = FeedSpecifier.knownFeedSpecifier(url: url) {
|
||||
logger.info("FeedFinder: found special case feed URL: \(url.absoluteString) - \(feedSpecifier.urlString)")
|
||||
completion(.success(Set([feedSpecifier])))
|
||||
return
|
||||
}
|
||||
|
||||
Downloader.shared.download(url) { (data, response, error) in
|
||||
|
||||
if response?.forcedStatusCode == 404 {
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import RSWeb
|
||||
|
||||
struct FeedSpecifier: Hashable {
|
||||
|
||||
@@ -25,7 +26,17 @@ struct FeedSpecifier: Hashable {
|
||||
public var score: Int {
|
||||
return calculatedScore()
|
||||
}
|
||||
|
||||
|
||||
/// Some feed URLs are known in advance. Save time/bandwidth by special-casing those.
|
||||
static func knownFeedSpecifier(url: URL) -> FeedSpecifier? {
|
||||
if url.isRachelByTheBayURL {
|
||||
let feedURLString = "https://rachelbythebay.com/w/atom.xml"
|
||||
return FeedSpecifier(title: "writing - rachelbythebay", urlString: feedURLString, source: .UserEntered, orderFound: 0)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func feedSpecifierByMerging(_ feedSpecifier: FeedSpecifier) -> FeedSpecifier {
|
||||
// Take the best data (non-nil title, better source) to create a new feed specifier;
|
||||
|
||||
|
||||
@@ -12,17 +12,17 @@ import RSWeb
|
||||
|
||||
struct InitialFeedDownloader {
|
||||
|
||||
static func download(_ url: URL,_ completion: @escaping (_ parsedFeed: ParsedFeed?) -> Void) {
|
||||
static func download(_ url: URL,_ completion: @escaping (_ parsedFeed: ParsedFeed?, _ data: Data?, _ response: URLResponse?, _ error: Error?) -> Void) {
|
||||
Task { @MainActor in
|
||||
Downloader.shared.download(url) { (data, response, error) in
|
||||
guard let data = data else {
|
||||
completion(nil)
|
||||
completion(nil, nil, response, error)
|
||||
return
|
||||
}
|
||||
|
||||
let parserData = ParserData(url: url.absoluteString, data: data)
|
||||
FeedParser.parse(parserData) { (parsedFeed, error) in
|
||||
completion(parsedFeed)
|
||||
completion(parsedFeed, data, response, error)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -242,10 +242,18 @@ private extension LocalAccountDelegate {
|
||||
return
|
||||
}
|
||||
|
||||
InitialFeedDownloader.download(url) { parsedFeed in
|
||||
InitialFeedDownloader.download(url) { parsedFeed, _, response, _ in
|
||||
|
||||
if let parsedFeed = parsedFeed {
|
||||
if let parsedFeed {
|
||||
let feed = account.createWebFeed(with: nil, url: url.absoluteString, webFeedID: url.absoluteString, homePageURL: nil)
|
||||
feed.lastCheckDate = Date()
|
||||
|
||||
// Save conditional GET info so that first refresh uses conditional GET.
|
||||
if let httpResponse = response as? HTTPURLResponse,
|
||||
let conditionalGetInfo = HTTPConditionalGetInfo(urlResponse: httpResponse) {
|
||||
feed.conditionalGetInfo = conditionalGetInfo
|
||||
}
|
||||
|
||||
feed.editedName = editedName
|
||||
container.addWebFeed(feed)
|
||||
|
||||
|
||||
@@ -195,7 +195,17 @@ public final class WebFeed: Feed, Renamable, Hashable {
|
||||
metadata.folderRelationship = newValue
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// Last time an attempt was made to read the feed.
|
||||
/// (Not necessarily a successful attempt.)
|
||||
public var lastCheckDate: Date? {
|
||||
get {
|
||||
metadata.lastCheckDate
|
||||
}
|
||||
set {
|
||||
metadata.lastCheckDate = newValue
|
||||
}
|
||||
}
|
||||
// MARK: - DisplayNameProvider
|
||||
|
||||
public var nameForDisplay: String {
|
||||
|
||||
@@ -31,6 +31,7 @@ final class WebFeedMetadata: Codable {
|
||||
case cacheControlInfo
|
||||
case externalID = "subscriptionID"
|
||||
case folderRelationship
|
||||
case lastCheckDate
|
||||
}
|
||||
|
||||
var webFeedID: String {
|
||||
@@ -151,6 +152,16 @@ final class WebFeedMetadata: Codable {
|
||||
}
|
||||
}
|
||||
|
||||
/// Last time an attempt was made to read the feed.
|
||||
/// (Not necessarily a successful attempt.)
|
||||
var lastCheckDate: Date? {
|
||||
didSet {
|
||||
if lastCheckDate != oldValue {
|
||||
valueDidChange(.lastCheckDate)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
weak var delegate: WebFeedMetadataDelegate?
|
||||
|
||||
init(webFeedID: String) {
|
||||
|
||||
@@ -243,9 +243,7 @@ private extension DownloadSession {
|
||||
if let conditionalGetInfo = delegate.downloadSession(self, conditionalGetInfoFor: url) {
|
||||
conditionalGetInfo.addRequestHeadersToURLRequest(&request)
|
||||
}
|
||||
if url.isOpenRSSOrgURL {
|
||||
request.setValue(UserAgent.openRSSOrgUserAgent, forHTTPHeaderField: HTTPRequestHeader.userAgent)
|
||||
}
|
||||
request.addSpecialCaseUserAgentIfNeeded()
|
||||
return request
|
||||
}()
|
||||
|
||||
@@ -387,7 +385,7 @@ private extension DownloadSession {
|
||||
|
||||
func cancelAndRemoveTasksWithHost(_ host: String, in tasks: Set<URLSessionTask>) {
|
||||
|
||||
let lowercaseHost = host.lowercased()
|
||||
let lowercaseHost = host.lowercased(with: localeForLowercasing)
|
||||
|
||||
let tasksToRemove = tasks.filter { task in
|
||||
if let taskHost = task.lowercaseHost, taskHost.contains(lowercaseHost) {
|
||||
@@ -486,7 +484,7 @@ extension URLSessionTask {
|
||||
guard let request = currentRequest ?? originalRequest else {
|
||||
return nil
|
||||
}
|
||||
return request.url?.host()?.lowercased()
|
||||
return request.url?.host()?.lowercased(with: localeForLowercasing)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -17,38 +17,41 @@ public final class HTMLMetadataDownloader: Sendable {
|
||||
public static let shared = HTMLMetadataDownloader()
|
||||
|
||||
private static let logger = Logger(subsystem: Bundle.main.bundleIdentifier!, category: "HTMLMetadataDownloader")
|
||||
private static let debugLoggingEnabled = false
|
||||
|
||||
|
||||
private let cache = HTMLMetadataCache()
|
||||
private let attemptDatesLock = OSAllocatedUnfairLock(initialState: [String: Date]())
|
||||
private let urlsReturning4xxsLock = OSAllocatedUnfairLock(initialState: Set<String>())
|
||||
|
||||
public func cachedMetadata(for url: String) -> RSHTMLMetadata? {
|
||||
|
||||
if Self.debugLoggingEnabled {
|
||||
Self.logger.debug("HTMLMetadataDownloader requested cached metadata for \(url)")
|
||||
if Self.shouldSkipDownloadingMetadata(url) {
|
||||
Self.logger.debug("HTMLMetadataDownloader: Skipping requested cached metadata for \(url)")
|
||||
return nil
|
||||
}
|
||||
|
||||
Self.logger.debug("HTMLMetadataDownloader requested cached metadata for \(url)")
|
||||
|
||||
guard let htmlMetadata = cache[url] else {
|
||||
downloadMetadataIfNeeded(url)
|
||||
return nil
|
||||
}
|
||||
|
||||
if Self.debugLoggingEnabled {
|
||||
Self.logger.debug("HTMLMetadataDownloader returning cached metadata for \(url)")
|
||||
}
|
||||
Self.logger.debug("HTMLMetadataDownloader returning cached metadata for \(url)")
|
||||
return htmlMetadata
|
||||
}
|
||||
}
|
||||
|
||||
private extension HTMLMetadataDownloader {
|
||||
|
||||
private static let specialCasesToSkip = [SpecialCase.rachelByTheBayHostName, SpecialCase.openRSSOrgHostName]
|
||||
|
||||
static func shouldSkipDownloadingMetadata(_ urlString: String) -> Bool {
|
||||
SpecialCase.urlStringContainSpecialCase(urlString, specialCasesToSkip)
|
||||
}
|
||||
|
||||
func downloadMetadataIfNeeded(_ url: String) {
|
||||
|
||||
if urlShouldBeSkippedDueToPrevious4xxResponse(url) {
|
||||
if Self.debugLoggingEnabled {
|
||||
Self.logger.debug("HTMLMetadataDownloader skipping download for \(url) because an earlier request returned a 4xx response.")
|
||||
}
|
||||
Self.logger.debug("HTMLMetadataDownloader skipping download for \(url) because an earlier request returned a 4xx response.")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -59,9 +62,7 @@ private extension HTMLMetadataDownloader {
|
||||
|
||||
let hoursBetweenAttempts = 3 // arbitrary
|
||||
if let attemptDate = attemptDates[url], attemptDate > currentDate.bySubtracting(hours: hoursBetweenAttempts) {
|
||||
if Self.debugLoggingEnabled {
|
||||
Self.logger.debug("HTMLMetadataDownloader skipping download for \(url) because an attempt was made less than an hour ago.")
|
||||
}
|
||||
Self.logger.debug("HTMLMetadataDownloader skipping download for \(url) because an attempt was made less than an hour ago.")
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -77,15 +78,11 @@ private extension HTMLMetadataDownloader {
|
||||
func downloadMetadata(_ url: String) {
|
||||
|
||||
guard let actualURL = URL(string: url) else {
|
||||
if Self.debugLoggingEnabled {
|
||||
Self.logger.debug("HTMLMetadataDownloader skipping download for \(url) because it couldn’t construct a URL.")
|
||||
}
|
||||
Self.logger.debug("HTMLMetadataDownloader skipping download for \(url) because it couldn’t construct a URL.")
|
||||
return
|
||||
}
|
||||
|
||||
if Self.debugLoggingEnabled {
|
||||
Self.logger.debug("HTMLMetadataDownloader downloading for \(url)")
|
||||
}
|
||||
Self.logger.debug("HTMLMetadataDownloader downloading for \(url)")
|
||||
|
||||
Task { @MainActor in
|
||||
Downloader.shared.download(actualURL) { data, response, error in
|
||||
@@ -93,9 +90,7 @@ private extension HTMLMetadataDownloader {
|
||||
let urlToUse = response.url ?? actualURL
|
||||
let parserData = ParserData(url: urlToUse.absoluteString, data: data)
|
||||
let htmlMetadata = RSHTMLMetadataParser.htmlMetadata(with: parserData)
|
||||
if Self.debugLoggingEnabled {
|
||||
Self.logger.debug("HTMLMetadataDownloader caching parsed metadata for \(url)")
|
||||
}
|
||||
Self.logger.debug("HTMLMetadataDownloader caching parsed metadata for \(url)")
|
||||
self.cache[url] = htmlMetadata
|
||||
return
|
||||
}
|
||||
@@ -104,9 +99,7 @@ private extension HTMLMetadataDownloader {
|
||||
self.noteURLDidReturn4xx(url)
|
||||
}
|
||||
|
||||
if Self.debugLoggingEnabled {
|
||||
Self.logger.debug("HTMLMetadataDownloader failed download for \(url)")
|
||||
}
|
||||
Self.logger.debug("HTMLMetadataDownloader failed download for \(url)")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,45 +8,52 @@
|
||||
import Foundation
|
||||
import os
|
||||
|
||||
public let localeForLowercasing = Locale(identifier: "en_US")
|
||||
|
||||
public struct SpecialCase {
|
||||
public static let rachelByTheBayHostName = "rachelbythebay.com"
|
||||
public static let openRSSOrgHostName = "openrss.org"
|
||||
|
||||
public static func urlStringContainSpecialCase(_ urlString: String, _ specialCases: [String]) -> Bool {
|
||||
let lowerURLString = urlString.lowercased(with: localeForLowercasing)
|
||||
for specialCase in specialCases {
|
||||
if lowerURLString.contains(specialCase) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
extension URL {
|
||||
|
||||
private static let openRSSOrgURLCache = OSAllocatedUnfairLock(initialState: [URL: Bool]())
|
||||
|
||||
public var isOpenRSSOrgURL: Bool {
|
||||
|
||||
Self.openRSSOrgURLCache.withLock { cache in
|
||||
if let cachedResult = cache[self] {
|
||||
return cachedResult
|
||||
}
|
||||
|
||||
let result: Bool
|
||||
if let host = host(), host.contains("openrss.org") {
|
||||
result = true
|
||||
}
|
||||
else {
|
||||
result = false
|
||||
}
|
||||
|
||||
cache[self] = result
|
||||
return result
|
||||
guard let host = host() else {
|
||||
return false
|
||||
}
|
||||
return SpecialCase.urlStringContainSpecialCase(host, [SpecialCase.openRSSOrgHostName])
|
||||
}
|
||||
|
||||
public var isRachelByTheBayURL: Bool {
|
||||
guard let host = host() else {
|
||||
return false
|
||||
}
|
||||
return SpecialCase.urlStringContainSpecialCase(host, [SpecialCase.rachelByTheBayHostName])
|
||||
}
|
||||
}
|
||||
|
||||
extension Set where Element == URL {
|
||||
|
||||
func byRemovingOpenRSSOrgURLs() -> Set<URL> {
|
||||
|
||||
filter { !$0.isOpenRSSOrgURL }
|
||||
}
|
||||
|
||||
func openRSSOrgURLs() -> Set<URL> {
|
||||
|
||||
filter { $0.isOpenRSSOrgURL }
|
||||
}
|
||||
|
||||
func byRemovingAllButOneRandomOpenRSSOrgURL() -> Set<URL> {
|
||||
|
||||
if self.isEmpty || self.count == 1 {
|
||||
return self
|
||||
}
|
||||
@@ -69,17 +76,19 @@ extension Set where Element == URL {
|
||||
extension URLRequest {
|
||||
|
||||
mutating func addSpecialCaseUserAgentIfNeeded() {
|
||||
|
||||
if let url, url.isOpenRSSOrgURL {
|
||||
setValue(UserAgent.openRSSOrgUserAgent, forHTTPHeaderField: HTTPRequestHeader.userAgent)
|
||||
guard let url else {
|
||||
return
|
||||
}
|
||||
|
||||
if url.isOpenRSSOrgURL || url.isRachelByTheBayURL {
|
||||
setValue(UserAgent.extendedUserAgent, forHTTPHeaderField: HTTPRequestHeader.userAgent)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension UserAgent {
|
||||
|
||||
static let openRSSOrgUserAgent = {
|
||||
|
||||
static let extendedUserAgent = {
|
||||
#if os(iOS)
|
||||
let platform = "iOS"
|
||||
#else
|
||||
@@ -98,7 +107,6 @@ extension UserAgent {
|
||||
}()
|
||||
|
||||
private static func stringFromInfoPlist(_ key: String) -> String? {
|
||||
|
||||
guard let s = Bundle.main.object(forInfoDictionaryKey: key) as? String else {
|
||||
assertionFailure("Expected to get \(key) from infoDictionary.")
|
||||
return nil
|
||||
|
||||
@@ -75,9 +75,12 @@ final class FaviconDownloader {
|
||||
}
|
||||
|
||||
func favicon(for webFeed: WebFeed) -> IconImage? {
|
||||
|
||||
assert(Thread.isMainThread)
|
||||
|
||||
if shouldSkipDownloadingFavicon(feed: webFeed) {
|
||||
return nil
|
||||
}
|
||||
|
||||
var homePageURL = webFeed.homePageURL
|
||||
if let faviconURL = webFeed.faviconURL {
|
||||
return favicon(with: faviconURL, homePageURL: homePageURL)
|
||||
@@ -206,6 +209,12 @@ final class FaviconDownloader {
|
||||
|
||||
private extension FaviconDownloader {
|
||||
|
||||
static let specialCasesToSkip = [SpecialCase.rachelByTheBayHostName, SpecialCase.openRSSOrgHostName]
|
||||
|
||||
func shouldSkipDownloadingFavicon(feed: WebFeed) -> Bool {
|
||||
SpecialCase.urlStringContainSpecialCase(feed.url, Self.specialCasesToSkip)
|
||||
}
|
||||
|
||||
static let localeForLowercasing = Locale(identifier: "en_US")
|
||||
|
||||
func findFaviconURLs(with homePageURL: String) -> [String]? {
|
||||
|
||||
@@ -53,6 +53,9 @@ public final class FeedIconDownloader {
|
||||
if let homePageURLString = feed.homePageURL, let homePageURL = URL(string: homePageURLString), (homePageURL.host == "nnw.ranchero.com" || homePageURL.host == "netnewswire.blog") {
|
||||
return IconImage.nnwFeedIcon
|
||||
}
|
||||
if (Self.shouldSkipDownloadingFeedIcon(feed: feed)) {
|
||||
return nil
|
||||
}
|
||||
|
||||
func checkHomePageURL() {
|
||||
guard let homePageURL = feed.homePageURL else {
|
||||
@@ -113,11 +116,23 @@ public final class FeedIconDownloader {
|
||||
|
||||
private extension FeedIconDownloader {
|
||||
|
||||
static let homePagesWithUglyIcons: Set<String> = Set(["https://www.macsparky.com/", "https://xkcd.com/"])
|
||||
static let specialCasesToSkip = ["macsparky.com", "xkcd.com", SpecialCase.rachelByTheBayHostName, SpecialCase.openRSSOrgHostName]
|
||||
|
||||
static func shouldSkipDownloadingFeedIcon(feed: WebFeed) -> Bool {
|
||||
shouldSkipDownloadingFeedIcon(feed.url)
|
||||
}
|
||||
|
||||
static func shouldSkipDownloadingFeedIcon(_ urlString: String) -> Bool {
|
||||
SpecialCase.urlStringContainSpecialCase(urlString, specialCasesToSkip)
|
||||
}
|
||||
|
||||
func icon(forHomePageURL homePageURL: String, feed: WebFeed, _ resultBlock: @escaping (RSImage?, String?) -> Void) {
|
||||
if Self.shouldSkipDownloadingFeedIcon(homePageURL) {
|
||||
resultBlock(nil, nil)
|
||||
return
|
||||
}
|
||||
|
||||
if homePagesWithNoIconURL.contains(homePageURL) || Self.homePagesWithUglyIcons.contains(homePageURL) {
|
||||
if homePagesWithNoIconURL.contains(homePageURL) {
|
||||
resultBlock(nil, nil)
|
||||
return
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user