2017-05-23 22:24:42 +02:00
|
|
|
//
|
|
|
|
// LocalAccountRefresher.swift
|
2019-07-09 07:58:19 +02:00
|
|
|
// NetNewsWire
|
2017-05-23 22:24:42 +02:00
|
|
|
//
|
|
|
|
// Created by Brent Simmons on 9/6/16.
|
|
|
|
// Copyright © 2016 Ranchero Software, LLC. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import RSCore
|
2017-07-02 02:22:19 +02:00
|
|
|
import RSParser
|
2017-05-23 22:24:42 +02:00
|
|
|
import RSWeb
|
2018-07-24 03:29:08 +02:00
|
|
|
import Articles
|
2017-05-23 22:24:42 +02:00
|
|
|
|
2017-10-07 21:40:14 +02:00
|
|
|
final class LocalAccountRefresher {
|
2017-05-23 22:24:42 +02:00
|
|
|
|
2019-10-02 23:41:32 +02:00
|
|
|
private var completion: (() -> Void)?
|
|
|
|
|
2019-04-26 21:21:17 +02:00
|
|
|
private lazy var downloadSession: DownloadSession = {
|
|
|
|
return DownloadSession(delegate: self)
|
2017-05-23 22:24:42 +02:00
|
|
|
}()
|
|
|
|
|
2019-04-26 21:21:17 +02:00
|
|
|
var progress: DownloadProgress {
|
|
|
|
return downloadSession.progress
|
2017-05-23 22:24:42 +02:00
|
|
|
}
|
2019-04-26 21:21:17 +02:00
|
|
|
|
2019-10-02 23:41:32 +02:00
|
|
|
public func refreshFeeds(_ feeds: Set<Feed>, completion: @escaping () -> Void) {
|
|
|
|
self.completion = completion
|
2019-04-26 21:21:17 +02:00
|
|
|
downloadSession.downloadObjects(feeds as NSSet)
|
2017-05-23 22:24:42 +02:00
|
|
|
}
|
2017-10-07 21:40:14 +02:00
|
|
|
}
|
2017-05-23 22:24:42 +02:00
|
|
|
|
2017-10-07 21:40:14 +02:00
|
|
|
// MARK: - DownloadSessionDelegate
|
|
|
|
|
|
|
|
extension LocalAccountRefresher: DownloadSessionDelegate {
|
|
|
|
|
|
|
|
func downloadSession(_ downloadSession: DownloadSession, requestForRepresentedObject representedObject: AnyObject) -> URLRequest? {
|
2017-09-17 21:08:50 +02:00
|
|
|
guard let feed = representedObject as? Feed else {
|
2017-05-23 22:24:42 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
guard let url = URL(string: feed.url) else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
let request = NSMutableURLRequest(url: url)
|
2017-09-27 06:43:40 +02:00
|
|
|
if let conditionalGetInfo = feed.conditionalGetInfo {
|
2017-05-23 22:24:42 +02:00
|
|
|
conditionalGetInfo.addRequestHeadersToURLRequest(request)
|
|
|
|
}
|
|
|
|
|
|
|
|
return request as URLRequest
|
|
|
|
}
|
|
|
|
|
2017-10-07 21:40:14 +02:00
|
|
|
func downloadSession(_ downloadSession: DownloadSession, downloadDidCompleteForRepresentedObject representedObject: AnyObject, response: URLResponse?, data: Data, error: NSError?) {
|
2017-09-17 21:08:50 +02:00
|
|
|
guard let feed = representedObject as? Feed, !data.isEmpty else {
|
2017-05-23 22:24:42 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if let error = error {
|
|
|
|
print("Error downloading \(feed.url) - \(error)")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let dataHash = (data as NSData).rs_md5HashString()
|
|
|
|
if dataHash == feed.contentHash {
|
|
|
|
return
|
|
|
|
}
|
2017-10-04 22:28:48 +02:00
|
|
|
|
2017-07-02 02:22:19 +02:00
|
|
|
let parserData = ParserData(url: feed.url, data: data)
|
|
|
|
FeedParser.parse(parserData) { (parsedFeed, error) in
|
2017-09-17 21:08:50 +02:00
|
|
|
guard let account = feed.account, let parsedFeed = parsedFeed, error == nil else {
|
2017-05-23 22:24:42 +02:00
|
|
|
return
|
|
|
|
}
|
2017-09-17 21:08:50 +02:00
|
|
|
account.update(feed, with: parsedFeed) {
|
2017-05-23 22:24:42 +02:00
|
|
|
if let httpResponse = response as? HTTPURLResponse {
|
2017-09-27 06:43:40 +02:00
|
|
|
feed.conditionalGetInfo = HTTPConditionalGetInfo(urlResponse: httpResponse)
|
2017-05-23 22:24:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
feed.contentHash = dataHash
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-07 21:40:14 +02:00
|
|
|
func downloadSession(_ downloadSession: DownloadSession, shouldContinueAfterReceivingData data: Data, representedObject: AnyObject) -> Bool {
|
2017-09-17 21:08:50 +02:00
|
|
|
guard let feed = representedObject as? Feed else {
|
2017-05-23 22:24:42 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if data.isEmpty {
|
|
|
|
return true
|
|
|
|
}
|
2017-05-26 22:07:55 +02:00
|
|
|
if data.isDefinitelyNotFeed() {
|
2017-05-23 22:24:42 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if data.count > 4096 {
|
2017-07-02 02:22:19 +02:00
|
|
|
let parserData = ParserData(url: feed.url, data: data)
|
2017-12-01 22:19:30 +01:00
|
|
|
return FeedParser.mightBeAbleToParseBasedOnPartialData(parserData)
|
2017-05-23 22:24:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-04-26 21:21:17 +02:00
|
|
|
func downloadSession(_ downloadSession: DownloadSession, didReceiveUnexpectedResponse response: URLResponse, representedObject: AnyObject) {
|
2019-04-25 14:13:14 +02:00
|
|
|
}
|
2019-04-26 21:21:17 +02:00
|
|
|
|
2019-04-25 14:13:14 +02:00
|
|
|
func downloadSession(_ downloadSession: DownloadSession, didReceiveNotModifiedResponse: URLResponse, representedObject: AnyObject) {
|
|
|
|
}
|
2019-10-02 23:41:32 +02:00
|
|
|
|
|
|
|
func downloadSessionDidCompleteDownloadObjects(_ downloadSession: DownloadSession) {
|
|
|
|
completion?()
|
|
|
|
completion = nil
|
|
|
|
}
|
|
|
|
|
2017-05-26 22:07:55 +02:00
|
|
|
}
|
2017-05-23 22:24:42 +02:00
|
|
|
|
2017-10-07 21:40:14 +02:00
|
|
|
// MARK: - Utility
|
|
|
|
|
2017-05-26 22:07:55 +02:00
|
|
|
private extension Data {
|
|
|
|
|
|
|
|
func isDefinitelyNotFeed() -> Bool {
|
|
|
|
// We only detect a few image types for now. This should get fleshed-out at some later date.
|
|
|
|
return (self as NSData).rs_dataIsImage()
|
|
|
|
}
|
2017-05-23 22:24:42 +02:00
|
|
|
}
|