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
|
2017-11-26 23:03:08 +01:00
|
|
|
import RSParser
|
|
|
|
|
|
|
|
struct HTMLMetadataDownloader {
|
|
|
|
|
2017-12-18 19:20:28 +01:00
|
|
|
static let serialDispatchQueue = DispatchQueue(label: "HTMLMetadataDownloader")
|
|
|
|
|
2019-12-15 02:01:34 +01:00
|
|
|
static func downloadMetadata(for url: String, _ completion: @escaping (RSHTMLMetadata?) -> Void) {
|
2021-07-19 00:01:59 +02:00
|
|
|
guard let actualURL = URL(unicodeString: url) else {
|
2019-12-15 02:01:34 +01:00
|
|
|
completion(nil)
|
2017-11-26 23:03:08 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
downloadUsingCache(actualURL) { (data, response, error) in
|
|
|
|
if let data = data, !data.isEmpty, let response = response, response.statusIsOK, error == nil {
|
|
|
|
let urlToUse = response.url ?? actualURL
|
|
|
|
let parserData = ParserData(url: urlToUse.absoluteString, data: data)
|
2019-12-15 02:01:34 +01:00
|
|
|
parseMetadata(with: parserData, completion)
|
2017-11-26 23:03:08 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-12-15 02:01:34 +01:00
|
|
|
completion(nil)
|
2017-11-26 23:03:08 +01:00
|
|
|
}
|
|
|
|
}
|
2017-12-18 19:20:28 +01:00
|
|
|
|
2019-12-15 02:01:34 +01:00
|
|
|
private static func parseMetadata(with parserData: ParserData, _ completion: @escaping (RSHTMLMetadata?) -> Void) {
|
2017-12-18 19:20:28 +01:00
|
|
|
serialDispatchQueue.async {
|
|
|
|
let htmlMetadata = RSHTMLMetadataParser.htmlMetadata(with: parserData)
|
|
|
|
DispatchQueue.main.async {
|
2019-12-15 02:01:34 +01:00
|
|
|
completion(htmlMetadata)
|
2017-12-18 19:20:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-11-26 23:03:08 +01:00
|
|
|
}
|