<commit_before>//
//  FeedListFeed.swift
//  Evergreen
//
//  Created by <NAME> on 11/4/17.
//  Copyright © 2017 Ranchero Software. All rights reserved.
//

import Foundation
import RSCore
import RSParser
import RSWeb

extension Notification.Name {

	public static let FeedListFeedDidBecomeAvailable = Notification.Name(rawValue: "FeedListFeedDidBecomeAvailable")
}

final class FeedListFeed: Hashable, DisplayNameProvider {

	let name: String
	let url: String
	let homePageURL: String
	let hashValue: Int
	var lastDownloadAttemptDate: Date? = nil

	var parsedFeed: ParsedFeed? = nil {
		didSet {
			postFeedListFeedDidBecomeAvailableNotification()
		}
	}

	var nameForDisplay: String { // DisplayNameProvider
		get {
			return name
		}
	}

	init(name: String, url: String, homePageURL: String) {

		self.name = name
		self.url = url
		self.homePageURL = homePageURL
		self.hashValue = url.hashValue
	}

	private struct Key {
		static let name = "name"
		static let editedName = "editedName" // Used in DefaultFeeds.plist
		static let url = "url"
		static let homePageURL = "homePageURL"
	}

	convenience init(dictionary: [String: String]) {

		let name = (dictionary[Key.name] ?? dictionary[Key.editedName])!
		let url = dictionary[Key.url]!
		let homePageURL = dictionary[Key.homePageURL]!

		self.init(name: name, url: url, homePageURL: homePageURL)
	}

	func downloadIfNeeded() {

		guard let lastDownloadAttemptDate = lastDownloadAttemptDate else {
			downloadFeed()
			return
		}

		let cutoffDate = Date().addingTimeInterval(-(30 * 60)) // 30 minutes in the past
		if lastDownloadAttemptDate < cutoffDate {
			downloadFeed()
		}
	}

	static func ==(lhs: FeedListFeed, rhs: FeedListFeed) -> Bool {

		return lhs.hashValue == rhs.hashValue && lhs.url == rhs.url && lhs.name == rhs.name && lhs.homePageURL == rhs.homePageURL
	}
}

private extension FeedListFeed {

	func postFeedListFeedDidBecomeAvailableNotification() {

		NotificationCenter.default.post(name: .FeedListFeedDidBecomeAvailable, object: self, userInfo: nil)
	}

	func downloadFeed() {

		lastDownloadAttemptDate = Date()
		guard let feedURL = URL(string: url) else {
			return
		}

		downloadUsingCache(feedURL) { (data, response, error) in

			guard let data = data, error == nil else {
				return
			}
			
			let parserData = ParserData(url: self.url, data: data)
			FeedParser.parse(parserData) { (parsedFeed, error) in

				if let parsedFeed = parsedFeed, parsedFeed.items.count > 0 {
					self.parsedFeed = parsedFeed
				}
			}
		}
	}<commit_msg>Skip downloading feeds for the Feed Directory, since feed previews have been punted to after 1.0.<commit_after>//
//  FeedListFeed.swift
//  Evergreen
//
//  Created by <NAME> on 11/4/17.
//  Copyright © 2017 Ranchero Software. All rights reserved.
//

import Foundation
import RSCore
import RSParser
import RSWeb

extension Notification.Name {

	public static let FeedListFeedDidBecomeAvailable = Notification.Name(rawValue: "FeedListFeedDidBecomeAvailable")
}

final class FeedListFeed: Hashable, DisplayNameProvider {

	let name: String
	let url: String
	let homePageURL: String
	let hashValue: Int
	var lastDownloadAttemptDate: Date? = nil

	var parsedFeed: ParsedFeed? = nil {
		didSet {
			postFeedListFeedDidBecomeAvailableNotification()
		}
	}

	var nameForDisplay: String { // DisplayNameProvider
		get {
			return name
		}
	}

	init(name: String, url: String, homePageURL: String) {

		self.name = name
		self.url = url
		self.homePageURL = homePageURL
		self.hashValue = url.hashValue
	}

	private struct Key {
		static let name = "name"
		static let editedName = "editedName" // Used in DefaultFeeds.plist
		static let url = "url"
		static let homePageURL = "homePageURL"
	}

	convenience init(dictionary: [String: String]) {

		let name = (dictionary[Key.name] ?? dictionary[Key.editedName])!
		let url = dictionary[Key.url]!
		let homePageURL = dictionary[Key.homePageURL]!

		self.init(name: name, url: url, homePageURL: homePageURL)
	}

	func downloadIfNeeded() {

		// Not doing feed previews until after 1.0.

//		guard let lastDownloadAttemptDate = lastDownloadAttemptDate else {
//			downloadFeed()
//			return
//		}
//
//		let cutoffDate = Date().addingTimeInterval(-(30 * 60)) // 30 minutes in the past
//		if lastDownloadAttemptDate < cutoffDate {
//			downloadFeed()
//		}
	}

	static func ==(lhs: FeedListFeed, rhs: FeedListFeed) -> Bool {

		return lhs.hashValue == rhs.hashValue && lhs.url == rhs.url && lhs.name == rhs.name && lhs.homePageURL == rhs.homePageURL
	}
}

private extension FeedListFeed {

	func postFeedListFeedDidBecomeAvailableNotification() {

//		NotificationCenter.default.post(name: .FeedListFeedDidBecomeAvailable, object: self, userInfo: nil)
	}

	func downloadFeed() {

//		lastDownloadAttemptDate = Date()
//		guard let feedURL = URL(string: url) else {
//			return
//		}
//
//		downloadUsingCache(feedURL) { (data, response, error) in
//
//			guard let data = data, error == nil else {
//				return
//			}
//
//			let parserData = ParserData(url: self.url, data: data)
//			FeedParser.parse(parserData) { (parsedFeed, error) in
//
//				if let parsedFeed = parsedFeed, parsedFeed.items.count > 0 {
//					self.parsedFeed = parsedFeed
//				}
//			}
//		}
	}
{'fraction_non_alphanumeric': 0.11219418960244648, 'fraction_numerical': 0.006880733944954129, 'mean_word_length': 3.6474245115452932, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 21, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1868522', 'n_tokens_mistral': 1915, 'n_tokens_neox': 1693, 'n_words': 448}