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
|
2024-04-02 04:31:57 +02:00
|
|
|
import Web
|
2024-04-03 06:43:06 +02:00
|
|
|
import Parser
|
2017-11-26 23:03:08 +01:00
|
|
|
|
2024-04-11 04:03:41 +02:00
|
|
|
extension RSHTMLMetadata: @unchecked Sendable {}
|
|
|
|
|
2017-11-26 23:03:08 +01:00
|
|
|
struct HTMLMetadataDownloader {
|
|
|
|
|
2024-04-11 04:03:41 +02:00
|
|
|
@MainActor static func downloadMetadata(for url: String) async -> RSHTMLMetadata? {
|
2017-12-18 19:20:28 +01:00
|
|
|
|
2021-07-19 00:01:59 +02:00
|
|
|
guard let actualURL = URL(unicodeString: url) else {
|
2024-04-11 04:03:41 +02:00
|
|
|
return nil
|
2017-11-26 23:03:08 +01:00
|
|
|
}
|
|
|
|
|
2024-04-11 04:03:41 +02:00
|
|
|
let downloadData = try? await downloadUsingCache(actualURL)
|
|
|
|
let data = downloadData?.data
|
|
|
|
let response = downloadData?.response
|
2017-11-26 23:03:08 +01:00
|
|
|
|
2024-04-11 04:03:41 +02: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
|
|
|
}
|
2024-04-11 04:03:41 +02:00
|
|
|
|
|
|
|
return nil
|
2017-11-26 23:03:08 +01:00
|
|
|
}
|
2017-12-18 19:20:28 +01:00
|
|
|
|
2024-04-11 04:03:41 +02:00
|
|
|
@MainActor private static func parseMetadata(with parserData: ParserData) async -> RSHTMLMetadata? {
|
|
|
|
|
|
|
|
let task = Task.detached { () -> RSHTMLMetadata? in
|
|
|
|
RSHTMLMetadataParser.htmlMetadata(with: parserData)
|
2017-12-18 19:20:28 +01:00
|
|
|
}
|
2024-04-11 04:03:41 +02:00
|
|
|
|
|
|
|
return await task.value
|
2017-12-18 19:20:28 +01:00
|
|
|
}
|
2017-11-26 23:03:08 +01:00
|
|
|
}
|