<commit_before>//
//  LocalAccountRefresher.swift
//  LocalAccount
//
//  Created by <NAME> on 9/6/16.
//  Copyright © 2016 Ranchero Software, LLC. All rights reserved.
//

import Foundation
import RSCore
import RSXML
import RSWeb

final class LocalAccountRefresher: DownloadSessionDelegate {
	
	weak var account: LocalAccount?
	
	private lazy var downloadSession: DownloadSession = {
		return DownloadSession(delegate: self)
	}()

	var progress: DownloadProgress {
		get {
			return downloadSession.progress
		}
	}
	
	public func refreshFeeds(_ feeds: NSSet) {
		
		downloadSession.refreshObjects(feeds)
	}

	// MARK: DownloadSessionDelegate
	
	public func downloadSession(_ downloadSession: DownloadSession, requestForRepresentedObject representedObject: AnyObject) -> URLRequest? {
		
		guard let feed = representedObject as? LocalFeed else {
			return nil
		}
		
		guard let url = URL(string: feed.url) else {
			return nil
		}
		
		let request = NSMutableURLRequest(url: url)
		if let conditionalGetInfo = feed.conditionalGetInfo, !conditionalGetInfo.isEmpty {
			conditionalGetInfo.addRequestHeadersToURLRequest(request)
		}

		return request as URLRequest
	}
	
	public func downloadSession(_ downloadSession: DownloadSession, downloadDidCompleteForRepresentedObject representedObject: AnyObject, response: URLResponse?, data: Data, error: NSError?) {
		
		guard let feed = representedObject as? LocalFeed, !data.isEmpty else {
			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
		}
		
		let xmlData = RSXMLData(data: data, urlString: feed.url)
		RSParseFeed(xmlData) { (parsedFeed, error) in
			
			guard let account = self.account, let parsedFeed = parsedFeed, error == nil else {
				return
			}
			account.update(feed, parsedFeed: parsedFeed) {
				
				if let httpResponse = response as? HTTPURLResponse {
					let conditionalGetInfo = HTTPConditionalGetInfo(URLResponse: httpResponse)
					if !conditionalGetInfo.isEmpty || feed.conditionalGetInfo != nil {
						feed.conditionalGetInfo = conditionalGetInfo
					}
				}
				
				feed.contentHash = dataHash
			}
		}
	}
	
	public func downloadSession(_ downloadSession: DownloadSession, shouldContinueAfterReceivingData data: Data, representedObject: AnyObject) -> Bool {
		
		guard let feed = representedObject as? LocalFeed else {
			return false<commit_msg>Fix one of several build errors.<commit_after>//
//  LocalAccountRefresher.swift
//  LocalAccount
//
//  Created by <NAME> on 9/6/16.
//  Copyright © 2016 Ranchero Software, LLC. All rights reserved.
//

import Foundation
import RSCore
import RSXML
import RSWeb

final class LocalAccountRefresher: DownloadSessionDelegate {
	
	weak var account: LocalAccount?
	
	private lazy var downloadSession: DownloadSession = {
		return DownloadSession(delegate: self)
	}()

	var progress: DownloadProgress {
		get {
			return downloadSession.progress
		}
	}
	
	public func refreshFeeds(_ feeds: NSSet) {
		
		downloadSession.refreshObjects(feeds)
	}

	// MARK: DownloadSessionDelegate
	
	public func downloadSession(_ downloadSession: DownloadSession, requestForRepresentedObject representedObject: AnyObject) -> URLRequest? {
		
		guard let feed = representedObject as? LocalFeed else {
			return nil
		}
		
		guard let url = URL(string: feed.url) else {
			return nil
		}
		
		let request = NSMutableURLRequest(url: url)
		if let conditionalGetInfo = feed.conditionalGetInfo, !conditionalGetInfo.isEmpty {
			conditionalGetInfo.addRequestHeadersToURLRequest(request)
		}

		return request as URLRequest
	}
	
	public func downloadSession(_ downloadSession: DownloadSession, downloadDidCompleteForRepresentedObject representedObject: AnyObject, response: URLResponse?, data: Data, error: NSError?) {
		
		guard let feed = representedObject as? LocalFeed, !data.isEmpty else {
			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
		}
		
		let xmlData = RSXMLData(data: data, urlString: feed.url)
		RSParseFeed(xmlData) { (parsedFeed, error) in
			
			guard let account = self.account, let parsedFeed = parsedFeed, error == nil else {
				return
			}
			account.update(feed, parsedFeed: parsedFeed) {
				
				if let httpResponse = response as? HTTPURLResponse {
					let conditionalGetInfo = HTTPConditionalGetInfo(urlResponse: httpResponse)
					if !conditionalGetInfo.isEmpty || feed.conditionalGetInfo != nil {
						feed.conditionalGetInfo = conditionalGetInfo
					}
				}
				
				feed.contentHash = dataHash
			}
		}
	}
	
	public func downloadSession(_ downloadSession: DownloadSession, shouldContinueAfterReceivingData data: Data, representedObject: AnyObject) -> Bool {
		
		guard let feed = representedObject as? LocalFeed else {
			return false
{'fraction_non_alphanumeric': 0.08455954483029234, 'fraction_numerical': 0.003531489111241907, 'mean_word_length': 3.906641000962464, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 38, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5622916', 'n_tokens_mistral': 1716, 'n_tokens_neox': 1403, 'n_words': 414}