2017-05-23 22:24:42 +02:00
|
|
|
//
|
|
|
|
// LocalAccountRefresher.swift
|
|
|
|
// LocalAccount
|
|
|
|
//
|
|
|
|
// 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
|
2017-09-17 21:08:50 +02:00
|
|
|
import Data
|
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
|
|
|
|
|
|
|
private lazy var downloadSession: DownloadSession = {
|
|
|
|
return DownloadSession(delegate: self)
|
|
|
|
}()
|
|
|
|
|
|
|
|
var progress: DownloadProgress {
|
|
|
|
get {
|
|
|
|
return downloadSession.progress
|
|
|
|
}
|
|
|
|
}
|
2017-10-07 23:40:14 +02:00
|
|
|
|
2017-10-07 21:40:14 +02:00
|
|
|
public func refreshFeeds(_ feeds: Set<Feed>) {
|
|
|
|
|
|
|
|
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-05-23 22:24:42 +02:00
|
|
|
|
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-05-23 22:24:42 +02:00
|
|
|
|
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 {
|
|
|
|
// print("Hashed content of \(feed.url) has not changed.")
|
|
|
|
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-05-23 22:24:42 +02:00
|
|
|
|
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-08 05:25:17 +02:00
|
|
|
feed.account?.dirty = true
|
2017-05-23 22:24:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-07 21:40:14 +02:00
|
|
|
func downloadSession(_ downloadSession: DownloadSession, shouldContinueAfterReceivingData data: Data, representedObject: AnyObject) -> Bool {
|
2017-05-23 22:24:42 +02:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func downloadSession(_ downloadSession: DownloadSession, didReceiveUnexpectedResponse response: URLResponse, representedObject: AnyObject) {
|
|
|
|
|
2018-01-25 06:48:20 +01:00
|
|
|
// guard let feed = representedObject as? Feed else {
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// print("Unexpected response \(response) for \(feed.url).")
|
2017-05-23 22:24:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func downloadSession(_ downloadSession: DownloadSession, didReceiveNotModifiedResponse: URLResponse, representedObject: AnyObject) {
|
|
|
|
|
2017-10-08 10:56:18 +02:00
|
|
|
// guard let feed = representedObject as? Feed else {
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// print("Not modified response for \(feed.url).")
|
2017-05-23 22:24:42 +02:00
|
|
|
}
|
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
|
|
|
}
|