2017-06-21 07:00:19 +02:00
|
|
|
|
//
|
|
|
|
|
// FeedType.swift
|
|
|
|
|
// RSParser
|
|
|
|
|
//
|
|
|
|
|
// Created by Brent Simmons on 6/20/17.
|
|
|
|
|
// Copyright © 2017 Ranchero Software, LLC. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
|
|
|
|
|
public enum FeedType {
|
|
|
|
|
case rss
|
|
|
|
|
case atom
|
|
|
|
|
case jsonFeed
|
|
|
|
|
case rssInJSON
|
|
|
|
|
case unknown
|
|
|
|
|
case notAFeed
|
|
|
|
|
}
|
2017-06-27 04:37:30 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private let minNumberOfBytesRequired = 128
|
|
|
|
|
|
|
|
|
|
public func feedType(_ parserData: ParserData) -> FeedType {
|
|
|
|
|
|
|
|
|
|
// Can call with partial data — while still downloading, for instance.
|
|
|
|
|
// If there’s not enough data, return .unknown. Ask again when there’s more data.
|
|
|
|
|
// If it’s definitely not a feed, return .notAFeed.
|
|
|
|
|
//
|
|
|
|
|
// This is fast enough to call on the main thread.
|
|
|
|
|
|
|
|
|
|
if parserData.data.count < minNumberOfBytesRequired {
|
|
|
|
|
return .unknown
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let nsdata = parserData.data as NSData
|
2017-11-11 23:27:56 +01:00
|
|
|
|
|
2017-06-27 04:37:30 +02:00
|
|
|
|
if nsdata.isProbablyJSONFeed() {
|
|
|
|
|
return .jsonFeed
|
|
|
|
|
}
|
|
|
|
|
if nsdata.isProbablyRSSInJSON() {
|
|
|
|
|
return .rssInJSON
|
|
|
|
|
}
|
|
|
|
|
if nsdata.isProbablyRSS() {
|
|
|
|
|
return .rss
|
|
|
|
|
}
|
|
|
|
|
if nsdata.isProbablyAtom() {
|
|
|
|
|
return .atom
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return .notAFeed
|
|
|
|
|
}
|