NetNewsWire/Shared/HTMLMetadata/HTMLMetadataDownloader.swift

45 lines
1.1 KiB
Swift
Raw Normal View History

2017-11-26 23:03:08 +01:00
//
// HTMLMetadataDownloader.swift
2018-08-29 07:18:24 +02:00
// NetNewsWire
2017-11-26 23:03:08 +01:00
//
// Created by Brent Simmons on 11/26/17.
// Copyright © 2017 Ranchero Software. All rights reserved.
//
import Foundation
import Web
import Parser
2017-11-26 23:03:08 +01:00
extension RSHTMLMetadata: @unchecked Sendable {}
2017-11-26 23:03:08 +01:00
struct HTMLMetadataDownloader {
@MainActor static func downloadMetadata(for url: String) async -> RSHTMLMetadata? {
guard let actualURL = URL(unicodeString: url) else {
return nil
2017-11-26 23:03:08 +01:00
}
let downloadData = try? await downloadUsingCache(actualURL)
let data = downloadData?.data
let response = downloadData?.response
2017-11-26 23:03:08 +01:00
if let data, !data.isEmpty, let response, response.statusIsOK {
let urlToUse = response.url ?? actualURL
let parserData = ParserData(url: urlToUse.absoluteString, data: data)
return await parseMetadata(with: parserData)
2017-11-26 23:03:08 +01:00
}
return nil
2017-11-26 23:03:08 +01:00
}
@MainActor private static func parseMetadata(with parserData: ParserData) async -> RSHTMLMetadata? {
let task = Task.detached { () -> RSHTMLMetadata? in
RSHTMLMetadataParser.htmlMetadata(with: parserData)
}
return await task.value
}
2017-11-26 23:03:08 +01:00
}