diff --git a/RSWeb/Sources/RSWeb/SpecialCases.swift b/RSWeb/Sources/RSWeb/SpecialCases.swift new file mode 100644 index 000000000..49c311f8a --- /dev/null +++ b/RSWeb/Sources/RSWeb/SpecialCases.swift @@ -0,0 +1,98 @@ +// +// SpecialCases.swift +// RSWeb +// +// Created by Brent Simmons on 12/12/24. +// + +import Foundation +import os + +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 + } + } +} + +extension Set where Element == URL { + + func byRemovingOpenRSSOrgURLs() -> Set { + + filter { !$0.isOpenRSSOrgURL } + } + + func openRSSOrgURLs() -> Set { + + filter { $0.isOpenRSSOrgURL } + } + + func byRemovingAllButOneRandomOpenRSSOrgURL() -> Set { + + if self.isEmpty || self.count == 1 { + return self + } + + let openRSSOrgURLs = openRSSOrgURLs() + if openRSSOrgURLs.isEmpty || openRSSOrgURLs.count == 1 { + return self + } + + let randomIndex = Int.random(in: 0.. String? { + + guard let s = Bundle.main.object(forInfoDictionaryKey: key) as? String else { + assertionFailure("Expected to get \(key) from infoDictionary.") + return nil + } + return s + } +}