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
|
|
|
|
|
|
2017-12-01 22:19:30 +01:00
|
|
|
|
public func feedType(_ parserData: ParserData, isPartialData: Bool = false) -> FeedType {
|
2017-06-27 04:37:30 +02:00
|
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-01 22:19:30 +01:00
|
|
|
|
if isPartialData && nsdata.isProbablyJSON() {
|
|
|
|
|
// Might not be able to detect a JSON Feed without all data.
|
|
|
|
|
// Dr. Drang’s JSON Feed (see althis.json and allthis-partial.json in tests)
|
|
|
|
|
// has, at this writing, the JSON version element at the end of the feed,
|
|
|
|
|
// which is totally legal — but it means not being able to detect
|
|
|
|
|
// that it’s a JSON Feed without all the data.
|
|
|
|
|
// So this returns .unknown instead of .notAFeed.
|
|
|
|
|
return .unknown
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-27 04:37:30 +02:00
|
|
|
|
return .notAFeed
|
|
|
|
|
}
|