Skip to content

Commit

Permalink
"Refactor RSSLoader to handle loading and parsing of RSS feeds, addin…
Browse files Browse the repository at this point in the history
…g support for Atom and JSON feeds as well."
  • Loading branch information
buhe committed Feb 12, 2024
1 parent ce5b869 commit 8a515de
Showing 1 changed file with 37 additions and 2 deletions.
39 changes: 37 additions & 2 deletions Sources/LangChain/document_loaders/RSSLoader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,43 @@
//

import Foundation
//import FeedKit
import FeedKit

public class RSSLoader {
public class RSSLoader: BaseLoader {
let url: String

public init(url: String, callbacks: [BaseCallbackHandler] = []) {
self.url = url
super.init(callbacks: callbacks)
}
public override func _load() async throws -> [Document] {
let feedURL = URL(string: url)!
let parser = FeedParser(URL: feedURL)
let result = parser.parse()
switch result {
case .success(let feed):

// Grab the parsed feed directly as an optional rss, atom or json feed object
switch feed {
case let .atom(feed):
print("🌈atom:\(feed)")
return []// Atom Syndication Format Feed Model
case let .rss(feed):
var content = [Document]()
for f in feed.items ?? [] {
content.append(Document(page_content: f.title ?? "", metadata: [:]))
}
return content// Really Simple Syndication Feed Model
case let .json(feed):
print("🌈json:\(feed)")
return []// JSON Feed Model
}


case .failure(let error):
print(error)
return []
}

}
}

0 comments on commit 8a515de

Please sign in to comment.