2020-03-31 22:21:53 +02:00
|
|
|
|
//
|
|
|
|
|
// OPMLNormalizer.swift
|
|
|
|
|
// Account
|
|
|
|
|
//
|
|
|
|
|
// Created by Maurice Parker on 3/31/20.
|
|
|
|
|
// Copyright © 2020 Ranchero Software, LLC. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
import RSParser
|
|
|
|
|
|
|
|
|
|
final class OPMLNormalizer {
|
|
|
|
|
|
|
|
|
|
var normalizedOPMLItems = [RSOPMLItem]()
|
|
|
|
|
|
|
|
|
|
static func normalize(_ items: [RSOPMLItem]) -> [RSOPMLItem] {
|
|
|
|
|
let opmlNormalizer = OPMLNormalizer()
|
2020-03-31 23:47:02 +02:00
|
|
|
|
opmlNormalizer.normalize(items)
|
2020-03-31 22:21:53 +02:00
|
|
|
|
return opmlNormalizer.normalizedOPMLItems
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-31 23:47:02 +02:00
|
|
|
|
private func normalize(_ items: [RSOPMLItem], parentFolder: RSOPMLItem? = nil) {
|
2020-03-31 22:21:53 +02:00
|
|
|
|
var feedsToAdd = [RSOPMLItem]()
|
|
|
|
|
|
|
|
|
|
items.forEach { (item) in
|
2020-04-13 14:11:03 +02:00
|
|
|
|
|
2020-03-31 22:21:53 +02:00
|
|
|
|
if let _ = item.feedSpecifier {
|
2020-04-13 01:29:34 +02:00
|
|
|
|
if !feedsToAdd.contains(where: { $0.feedSpecifier?.feedURL == item.feedSpecifier?.feedURL }) {
|
2020-03-31 22:21:53 +02:00
|
|
|
|
feedsToAdd.append(item)
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
guard let _ = item.titleFromAttributes else {
|
|
|
|
|
// Folder doesn’t have a name, so it won’t be created, and its items will go one level up.
|
|
|
|
|
if let itemChildren = item.children {
|
2020-03-31 23:47:02 +02:00
|
|
|
|
normalize(itemChildren, parentFolder: parentFolder)
|
2020-03-31 22:21:53 +02:00
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-13 01:29:34 +02:00
|
|
|
|
feedsToAdd.append(item)
|
2020-04-13 14:11:03 +02:00
|
|
|
|
if let itemChildren = item.children {
|
|
|
|
|
if let parentFolder = parentFolder {
|
|
|
|
|
normalize(itemChildren, parentFolder: parentFolder)
|
|
|
|
|
} else {
|
|
|
|
|
normalize(itemChildren, parentFolder: item)
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-03-31 22:21:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if let parentFolder = parentFolder {
|
|
|
|
|
for feed in feedsToAdd {
|
2020-04-13 01:29:34 +02:00
|
|
|
|
if !(parentFolder.children?.contains(where: { $0.feedSpecifier?.feedURL == feed.feedSpecifier?.feedURL}) ?? false) {
|
|
|
|
|
parentFolder.addChild(feed)
|
|
|
|
|
}
|
2020-03-31 22:21:53 +02:00
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
for feed in feedsToAdd {
|
|
|
|
|
normalizedOPMLItems.append(feed)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|