2017-12-03 02:47:08 +01:00
|
|
|
//
|
|
|
|
// InitialFeedDownloader.swift
|
2018-08-29 07:18:24 +02:00
|
|
|
// NetNewsWire
|
2017-12-03 02:47:08 +01:00
|
|
|
//
|
|
|
|
// Created by Brent Simmons on 9/3/16.
|
|
|
|
// Copyright © 2016 Ranchero Software, LLC. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
2024-04-03 06:43:06 +02:00
|
|
|
import Parser
|
2024-04-02 04:31:57 +02:00
|
|
|
import Web
|
2017-12-03 02:47:08 +01:00
|
|
|
|
2024-04-08 00:20:59 +02:00
|
|
|
public struct InitialFeedDownloader {
|
2017-12-03 02:47:08 +01:00
|
|
|
|
2024-04-08 00:20:59 +02:00
|
|
|
@MainActor public static func download(_ url: URL) async -> ParsedFeed? {
|
2024-04-03 06:25:05 +02:00
|
|
|
|
2024-04-03 07:07:19 +02:00
|
|
|
await withCheckedContinuation { @MainActor continuation in
|
2024-04-03 06:25:05 +02:00
|
|
|
self.download(url) { parsedFeed in
|
|
|
|
continuation.resume(returning: parsedFeed)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-08 00:25:12 +02:00
|
|
|
@MainActor public static func download(_ url: URL,_ completion: @escaping @Sendable (_ parsedFeed: ParsedFeed?) -> Void) {
|
2017-12-03 02:47:08 +01:00
|
|
|
|
2024-04-17 07:03:20 +02:00
|
|
|
Task {
|
|
|
|
|
|
|
|
guard let downloadData = try? await downloadUsingCache(url) else {
|
|
|
|
completion(nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
guard let data = downloadData.data else {
|
2019-12-15 01:14:55 +01:00
|
|
|
completion(nil)
|
2017-12-03 02:47:08 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let parserData = ParserData(url: url.absoluteString, data: data)
|
2024-04-17 07:03:20 +02:00
|
|
|
|
|
|
|
Task.detached {
|
|
|
|
guard let parsedFeed = try? await FeedParser.parse(parserData) else {
|
|
|
|
completion(nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-12-15 01:14:55 +01:00
|
|
|
completion(parsedFeed)
|
2017-12-03 02:47:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|