Add Atom feed detection to FeedType.

This commit is contained in:
Brent Simmons
2024-09-11 21:56:37 -07:00
parent 860ecfd58c
commit 1159d45e5f
2 changed files with 30 additions and 22 deletions

View File

@@ -10,7 +10,7 @@ import Foundation
import SAX
public enum FeedType: Sendable {
case rss
case atom
case jsonFeed
@@ -42,6 +42,9 @@ public enum FeedType: Sendable {
if isProbablyRSS(cCharPointer, count) {
return .rss
}
if isProbablyAtom(cCharPointer, count) {
return .atom
}
return .unknown
}
@@ -82,6 +85,11 @@ private extension FeedType {
return didFindString("<channel>", bytes, count) && didFindString("<pubDate>", bytes, count)
}
static func isProbablyAtom(_ bytes: UnsafePointer<CChar>, _ count: Int) -> Bool {
didFindString("<feed", bytes, count)
}
static func didFindString(_ string: UnsafePointer<CChar>, _ bytes: UnsafePointer<CChar>, _ numberOfBytes: Int) -> Bool {
let foundString = strnstr(bytes, string, numberOfBytes)

View File

@@ -114,27 +114,27 @@ class FeedParserTypeTests: XCTestCase {
// MARK: Atom
// func testDaringFireballAtomType() {
//
// // File extension is .rss, but its really an Atom feed.
// let d = parserData("DaringFireball", "rss", "http://daringfireball.net/")
// let type = feedType(d)
// XCTAssertTrue(type == .atom)
// }
//
// func testOneFootTsunamiAtomType() {
//
// let d = parserData("OneFootTsunami", "atom", "http://onefoottsunami.com/")
// let type = feedType(d)
// XCTAssertTrue(type == .atom)
// }
//
// func testRussCoxAtomType() {
// let d = parserData("russcox", "atom", "https://research.swtch.com/")
// let type = feedType(d)
// XCTAssertTrue(type == .atom)
// }
//
func testDaringFireballAtomType() {
// File extension is .rss, but its really an Atom feed.
let d = parserData("DaringFireball", "rss", "http://daringfireball.net/")
let type = FeedType.feedType(d.data)
XCTAssertTrue(type == .atom)
}
func testOneFootTsunamiAtomType() {
let d = parserData("OneFootTsunami", "atom", "http://onefoottsunami.com/")
let type = FeedType.feedType(d.data)
XCTAssertTrue(type == .atom)
}
func testRussCoxAtomType() {
let d = parserData("russcox", "atom", "https://research.swtch.com/")
let type = FeedType.feedType(d.data)
XCTAssertTrue(type == .atom)
}
// // MARK: RSS-in-JSON
//
// func testScriptingNewsJSONType() {