mirror of
https://github.com/Ranchero-Software/NetNewsWire
synced 2025-08-12 06:26:36 +00:00
Remove spaces from folder names.
This commit is contained in:
111
Shared/ArticleExtractor/ArticleExtractor.swift
Normal file
111
Shared/ArticleExtractor/ArticleExtractor.swift
Normal file
@@ -0,0 +1,111 @@
|
||||
//
|
||||
// ArticleExtractor.swift
|
||||
// NetNewsWire
|
||||
//
|
||||
// Created by Maurice Parker on 9/18/19.
|
||||
// Copyright © 2019 Ranchero Software. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import Account
|
||||
import Secrets
|
||||
|
||||
public enum ArticleExtractorState {
|
||||
case ready
|
||||
case processing
|
||||
case failedToParse
|
||||
case complete
|
||||
case cancelled
|
||||
}
|
||||
|
||||
protocol ArticleExtractorDelegate {
|
||||
func articleExtractionDidFail(with: Error)
|
||||
func articleExtractionDidComplete(extractedArticle: ExtractedArticle)
|
||||
}
|
||||
|
||||
class ArticleExtractor {
|
||||
|
||||
private var dataTask: URLSessionDataTask? = nil
|
||||
|
||||
var state: ArticleExtractorState!
|
||||
var article: ExtractedArticle?
|
||||
var delegate: ArticleExtractorDelegate?
|
||||
var articleLink: String?
|
||||
|
||||
private var url: URL!
|
||||
|
||||
public init?(_ articleLink: String) {
|
||||
self.articleLink = articleLink
|
||||
|
||||
let clientURL = "https://extract.feedbin.com/parser"
|
||||
let username = SecretKey.mercuryClientID
|
||||
let signature = articleLink.hmacUsingSHA1(key: SecretKey.mercuryClientSecret)
|
||||
|
||||
if let base64URL = articleLink.data(using: .utf8)?.base64EncodedString() {
|
||||
let fullURL = "\(clientURL)/\(username)/\(signature)?base64_url=\(base64URL)"
|
||||
if let url = URL(string: fullURL) {
|
||||
self.url = url
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
public func process() {
|
||||
|
||||
state = .processing
|
||||
|
||||
dataTask = URLSession.shared.dataTask(with: url) { [weak self] data, response, error in
|
||||
|
||||
guard let self = self else { return }
|
||||
|
||||
if let error = error {
|
||||
self.state = .failedToParse
|
||||
DispatchQueue.main.async {
|
||||
self.delegate?.articleExtractionDidFail(with: error)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
guard let data = data else {
|
||||
self.state = .failedToParse
|
||||
DispatchQueue.main.async {
|
||||
self.delegate?.articleExtractionDidFail(with: URLError(.cannotDecodeContentData))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
do {
|
||||
let decoder = JSONDecoder()
|
||||
decoder.dateDecodingStrategy = .iso8601
|
||||
self.article = try decoder.decode(ExtractedArticle.self, from: data)
|
||||
|
||||
DispatchQueue.main.async {
|
||||
if self.article?.content == nil {
|
||||
self.state = .failedToParse
|
||||
self.delegate?.articleExtractionDidFail(with: URLError(.cannotDecodeContentData))
|
||||
} else {
|
||||
self.state = .complete
|
||||
self.delegate?.articleExtractionDidComplete(extractedArticle: self.article!)
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
self.state = .failedToParse
|
||||
DispatchQueue.main.async {
|
||||
self.delegate?.articleExtractionDidFail(with: error)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
dataTask!.resume()
|
||||
|
||||
}
|
||||
|
||||
public func cancel() {
|
||||
state = .cancelled
|
||||
dataTask?.cancel()
|
||||
}
|
||||
|
||||
}
|
||||
45
Shared/ArticleExtractor/ExtractedArticle.swift
Normal file
45
Shared/ArticleExtractor/ExtractedArticle.swift
Normal file
@@ -0,0 +1,45 @@
|
||||
//
|
||||
// ExtractedArticle.swift
|
||||
// NetNewsWire
|
||||
//
|
||||
// Created by Maurice Parker on 9/18/19.
|
||||
// Copyright © 2019 Ranchero Software. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
struct ExtractedArticle: Codable, Equatable {
|
||||
|
||||
let title: String?
|
||||
let author: String?
|
||||
let datePublished: String?
|
||||
let dek: String?
|
||||
let leadImageURL: String?
|
||||
let content: String?
|
||||
let nextPageURL: String?
|
||||
let url: String?
|
||||
let domain: String?
|
||||
let excerpt: String?
|
||||
let wordCount: Int?
|
||||
let direction: String?
|
||||
let totalPages: Int?
|
||||
let renderedPages: Int?
|
||||
|
||||
enum CodingKeys: String, CodingKey {
|
||||
case title = "title"
|
||||
case author = "author"
|
||||
case datePublished = "date_published"
|
||||
case dek = "dek"
|
||||
case leadImageURL = "lead_image_url"
|
||||
case content = "content"
|
||||
case nextPageURL = "next_page_url"
|
||||
case url = "url"
|
||||
case domain = "domain"
|
||||
case excerpt = "excerpt"
|
||||
case wordCount = "word_count"
|
||||
case direction = "direction"
|
||||
case totalPages = "total_pages"
|
||||
case renderedPages = "rendered_pages"
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user