Create ReaderAPI module.

This commit is contained in:
Brent Simmons
2024-04-06 13:06:24 -07:00
parent 552753abd2
commit 5555ae5adc
15 changed files with 152 additions and 72 deletions

8
ReaderAPI/.gitignore vendored Normal file
View File

@@ -0,0 +1,8 @@
.DS_Store
/.build
/Packages
xcuserdata/
DerivedData/
.swiftpm/configuration/registries.json
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
.netrc

28
ReaderAPI/Package.swift Normal file
View File

@@ -0,0 +1,28 @@
// swift-tools-version: 5.10
import PackageDescription
let package = Package(
name: "ReaderAPI",
platforms: [.macOS(.v14), .iOS(.v17)],
products: [
.library(
name: "ReaderAPI",
targets: ["ReaderAPI"]),
],
dependencies: [
.package(path: "../FoundationExtras")
],
targets: [
.target(
name: "ReaderAPI",
dependencies: ["FoundationExtras"],
swiftSettings: [
.enableExperimentalFeature("StrictConcurrency")
]
),
.testTarget(
name: "ReaderAPITests",
dependencies: ["ReaderAPI"]),
]
)

View File

@@ -0,0 +1,133 @@
//
// ReaderAPIArticle.swift
// Account
//
// Created by Jeremy Beker on 5/28/19.
// Copyright © 2017 Ranchero Software, LLC. All rights reserved.
//
import Foundation
public struct ReaderAPIEntryWrapper: Codable, Sendable {
public let id: String
public let updated: Int
public let entries: [ReaderAPIEntry]
enum CodingKeys: String, CodingKey {
case id = "id"
case updated = "updated"
case entries = "items"
}
}
/* {
"id": "tag:google.com,2005:reader/item/00058a3b5197197b",
"crawlTimeMsec": "1559362260113",
"timestampUsec": "1559362260113787",
"published": 1554845280,
"title": "",
"summary": {
"content": "\n<p>Found an old screenshot of NetNewsWire 1.0 for iPhone!</p>\n\n<p><img src=\"https://nnw.ranchero.com/uploads/2019/c07c0574b1.jpg\" alt=\"Netnewswire 1.0 for iPhone screenshot showing the list of feeds.\" title=\"NewsGator got renamed to Sitrion, years later, and then renamed again as Limeade.\" border=\"0\" width=\"260\" height=\"320\"></p>\n"
},
"alternate": [
{
"href": "https://nnw.ranchero.com/2019/04/09/found-an-old.html"
}
],
"categories": [
"user/-/state/com.google/reading-list",
"user/-/label/Uncategorized"
],
"origin": {
"streamId": "feed/130",
"title": "NetNewsWire"
}
}
*/
public struct ReaderAPIEntry: Codable, Sendable {
public let articleID: String
public let title: String?
public let author: String?
public let publishedTimestamp: Double?
public let crawledTimestamp: String?
public let timestampUsec: String?
public let summary: ReaderAPIArticleSummary
public let alternates: [ReaderAPIAlternateLocation]?
public let categories: [String]
public let origin: ReaderAPIEntryOrigin
enum CodingKeys: String, CodingKey {
case articleID = "id"
case title = "title"
case author = "author"
case summary = "summary"
case alternates = "alternate"
case categories = "categories"
case publishedTimestamp = "published"
case crawledTimestamp = "crawlTimeMsec"
case origin = "origin"
case timestampUsec = "timestampUsec"
}
public func parseDatePublished() -> Date? {
guard let unixTime = publishedTimestamp else {
return nil
}
return Date(timeIntervalSince1970: unixTime)
}
public func uniqueID(variant: ReaderAPIVariant) -> String {
// Should look something like "tag:google.com,2005:reader/item/00058b10ce338909"
// REGEX feels heavy, I should be able to just split on / and take the last element
guard let idPart = articleID.components(separatedBy: "/").last else {
return articleID
}
guard variant != .theOldReader else {
return idPart
}
// Convert hex representation back to integer and then a string representation
guard let idNumber = Int(idPart, radix: 16) else {
return articleID
}
return String(idNumber, radix: 10, uppercase: false)
}
}
public struct ReaderAPIArticleSummary: Codable, Sendable {
public let content: String?
enum CodingKeys: String, CodingKey {
case content = "content"
}
}
public struct ReaderAPIAlternateLocation: Codable, Sendable {
public let url: String?
enum CodingKeys: String, CodingKey {
case url = "href"
}
}
public struct ReaderAPIEntryOrigin: Codable, Sendable {
public let streamId: String?
public let title: String?
enum CodingKeys: String, CodingKey {
case streamId = "streamId"
case title = "title"
}
}

View File

@@ -0,0 +1,114 @@
//
// ReaderAPISubscription.swift
// Account
//
// Created by Jeremy Beker on 5/28/19.
// Copyright © 2017 Ranchero Software, LLC. All rights reserved.
//
import Foundation
import FoundationExtras
/*
{
"numResults":0,
"error": "Already subscribed! https://inessential.com/xml/rss.xml
}
*/
public struct ReaderAPIQuickAddResult: Codable {
public let numResults: Int
public let error: String?
public let streamId: String?
enum CodingKeys: String, CodingKey {
case numResults = "numResults"
case error = "error"
case streamId = "streamId"
}
}
public struct ReaderAPISubscriptionContainer: Codable, Sendable {
public let subscriptions: [ReaderAPISubscription]
enum CodingKeys: String, CodingKey {
case subscriptions = "subscriptions"
}
}
/*
{
"id": "feed/1",
"title": "Questionable Content",
"categories": [
{
"id": "user/-/label/Comics",
"label": "Comics"
}
],
"url": "http://www.questionablecontent.net/QCRSS.xml",
"htmlUrl": "http://www.questionablecontent.net",
"iconUrl": "https://rss.confusticate.com/f.php?24decabc"
}
*/
public struct ReaderAPISubscription: Codable, Sendable {
public let feedID: String
public let name: String?
public let categories: [ReaderAPICategory]
public let feedURL: String?
public let homePageURL: String?
public let iconURL: String?
enum CodingKeys: String, CodingKey {
case feedID = "id"
case name = "title"
case categories = "categories"
case feedURL = "url"
case homePageURL = "htmlUrl"
case iconURL = "iconUrl"
}
public var url: String {
if let feedURL = feedURL {
return feedURL
} else {
return feedID.stripping(prefix: "feed/")
}
}
}
public struct ReaderAPICategory: Codable, Sendable {
public let categoryId: String
public let categoryLabel: String
enum CodingKeys: String, CodingKey {
case categoryId = "id"
case categoryLabel = "label"
}
}
struct ReaderAPICreateSubscription: Codable {
let feedURL: String
enum CodingKeys: String, CodingKey {
case feedURL = "feed_url"
}
}
struct ReaderAPISubscriptionChoice: Codable {
let name: String?
let url: String
enum CodingKeys: String, CodingKey {
case name = "title"
case url = "feed_url"
}
}

View File

@@ -0,0 +1,37 @@
//
// ReaderAPICompatibleTag.swift
// Account
//
// Created by Jeremy Beker on 5/28/19.
// Copyright © 2019 Ranchero Software, LLC. All rights reserved.
//
import Foundation
public struct ReaderAPITagContainer: Codable, Sendable {
public let tags: [ReaderAPITag]
enum CodingKeys: String, CodingKey {
case tags = "tags"
}
}
public struct ReaderAPITag: Codable, Sendable {
public let tagID: String
public let type: String?
enum CodingKeys: String, CodingKey {
case tagID = "id"
case type = "type"
}
public var folderName: String? {
guard let range = tagID.range(of: "/label/") else {
return nil
}
return String(tagID.suffix(from: range.upperBound))
}
}

View File

@@ -0,0 +1,35 @@
//
// ReaderAPICompatibleTagging.swift
// Account
//
// Created by Jeremy Beker on 5/28/19.
// Copyright © 2018 Ranchero Software, LLC. All rights reserved.
//
import Foundation
struct ReaderAPITagging: Codable {
let taggingID: Int
let feedID: Int
let name: String
enum CodingKeys: String, CodingKey {
case taggingID = "id"
case feedID = "feed_id"
case name = "name"
}
}
struct ReaderAPICreateTagging: Codable {
let feedID: Int
let name: String
enum CodingKeys: String, CodingKey {
case feedID = "feed_id"
case name = "name"
}
}

View File

@@ -0,0 +1,29 @@
//
// ReaderAPIUnreadEntry.swift
// Account
//
// Created by Jeremy Beker on 5/28/19.
// Copyright © 2019 Ranchero Software, LLC. All rights reserved.
//
import Foundation
public struct ReaderAPIReferenceWrapper: Codable, Sendable {
public let itemRefs: [ReaderAPIReference]?
public let continuation: String?
enum CodingKeys: String, CodingKey {
case itemRefs = "itemRefs"
case continuation = "continuation"
}
}
public struct ReaderAPIReference: Codable, Sendable {
public let itemId: String?
enum CodingKeys: String, CodingKey {
case itemId = "id"
}
}

View File

@@ -0,0 +1,30 @@
//
// ReaderAPIVariant.swift
//
//
// Created by Maurice Parker on 10/23/20.
//
import Foundation
public enum ReaderAPIVariant {
case generic
case freshRSS
case inoreader
case bazQux
case theOldReader
public var host: String {
switch self {
case .inoreader:
return "https://www.inoreader.com"
case .bazQux:
return "https://bazqux.com"
case .theOldReader:
return "https://theoldreader.com"
default:
return ""
}
}
}

View File

@@ -0,0 +1,12 @@
import XCTest
@testable import ReaderAPI
final class ReaderAPITests: XCTestCase {
func testExample() throws {
// XCTest Documentation
// https://developer.apple.com/documentation/xctest
// Defining Test Cases and Test Methods
// https://developer.apple.com/documentation/xctest/defining_test_cases_and_test_methods
}
}