NetNewsWire/Frameworks/Account/FeedProvider/Reddit/RedditFeedProvider.swift

452 lines
14 KiB
Swift
Raw Normal View History

//
// RedditFeedProvider.swift
// Account
//
// Created by Maurice Parker on 5/2/20.
// Copyright © 2020 Ranchero Software, LLC. All rights reserved.
//
import Foundation
import os.log
import OAuthSwift
import Secrets
import RSParser
import RSWeb
public enum RedditFeedProviderError: LocalizedError {
2020-05-07 08:58:43 +02:00
case rateLimitExceeded
case unknown
public var localizedDescription: String {
switch self {
2020-05-07 08:58:43 +02:00
case .rateLimitExceeded:
return NSLocalizedString("Reddit API rate limit has been exceeded. Please wait a short time and try again.", comment: "Rate Limit")
case .unknown:
return NSLocalizedString("A Reddit Feed Provider error has occurred.", comment: "Unknown error")
}
}
}
2020-05-10 18:44:30 +02:00
public enum RedditFeedType: Int {
case home = 0
case popular = 1
case all = 2
case subreddit = 3
}
public final class RedditFeedProvider: FeedProvider {
var log = OSLog(subsystem: Bundle.main.bundleIdentifier!, category: "RedditFeedProvider")
private static let homeURL = "https://www.reddit.com"
private static let server = "www.reddit.com"
private static let apiBase = "https://oauth.reddit.com"
private static let userAgentHeaders = UserAgent.headers() as! [String: String]
2020-05-09 17:23:37 +02:00
private static let pseudoSubreddits = [
"popular": NSLocalizedString("Popular", comment: "Popular"),
"all": NSLocalizedString("All", comment: "All")
]
2020-05-03 20:23:36 +02:00
public var username: String?
private var oauthToken: String
2020-05-03 20:23:36 +02:00
private var oauthRefreshToken: String
2020-05-03 20:23:36 +02:00
private var oauthSwift: OAuth2Swift?
private var client: OAuthSwiftClient? {
return oauthSwift?.client
}
2020-05-07 08:58:43 +02:00
private var rateLimitRemaining: Int?
private var rateLimitReset: Date?
public convenience init?(username: String) {
guard let tokenCredentials = try? CredentialsManager.retrieveCredentials(type: .oauthAccessToken, server: Self.server, username: username),
2020-05-03 20:23:36 +02:00
let refreshTokenCredentials = try? CredentialsManager.retrieveCredentials(type: .oauthRefreshToken, server: Self.server, username: username) else {
return nil
}
2020-05-03 20:23:36 +02:00
self.init(oauthToken: tokenCredentials.secret, oauthRefreshToken: refreshTokenCredentials.secret)
self.username = username
}
init(oauthToken: String, oauthRefreshToken: String) {
self.oauthToken = oauthToken
self.oauthRefreshToken = oauthRefreshToken
oauthSwift = Self.oauth2Swift
oauthSwift!.client.credential.oauthToken = oauthToken
oauthSwift!.client.credential.oauthRefreshToken = oauthRefreshToken
}
public func ability(_ urlComponents: URLComponents) -> FeedProviderAbility {
guard urlComponents.host?.hasSuffix("reddit.com") ?? false else {
return .none
}
if let username = urlComponents.user {
if username == username {
return .owner
} else {
return .none
}
}
return .available
}
public func iconURL(_ urlComponents: URLComponents, completion: @escaping (Result<String, Error>) -> Void) {
2020-05-06 00:14:23 +02:00
guard urlComponents.path.hasPrefix("/r/") else {
2020-05-05 01:13:15 +02:00
completion(.failure(RedditFeedProviderError.unknown))
return
}
subreddit(urlComponents) { result in
2020-05-05 01:13:15 +02:00
switch result {
case .success(let subreddit):
if let iconURL = subreddit.data?.iconURL, !iconURL.isEmpty {
completion(.success(iconURL))
} else {
completion(.failure(RedditFeedProviderError.unknown))
}
case .failure(let error):
completion(.failure(error))
}
}
}
public func metaData(_ urlComponents: URLComponents, completion: @escaping (Result<FeedProviderFeedMetaData, Error>) -> Void) {
let path = urlComponents.path
2020-05-09 17:23:37 +02:00
// Reddit Home
let splitPath = path.split(separator: "/")
2020-05-09 17:23:37 +02:00
if path == "" || path == "/" || (splitPath.count == 1 && RedditSort(rawValue: String(splitPath[0])) != nil) {
let name = NSLocalizedString("Reddit Home", comment: "Reddit Home")
let metaData = FeedProviderFeedMetaData(name: name, homePageURL: Self.homeURL)
completion(.success(metaData))
return
}
2020-05-09 17:23:37 +02:00
// Subreddits
2020-05-08 23:13:25 +02:00
guard splitPath.count > 1, splitPath.count < 4, splitPath[0] == "r" else {
completion(.failure(RedditFeedProviderError.unknown))
return
}
2020-05-08 23:13:25 +02:00
if splitPath.count == 3 && RedditSort(rawValue: String(splitPath[2])) == nil {
completion(.failure(RedditFeedProviderError.unknown))
return
}
let homePageURL = "https://www.reddit.com/\(splitPath[0])/\(splitPath[1])"
2020-05-09 17:23:37 +02:00
// Reddit Popular, Reddit All, etc...
if let subredditName = Self.pseudoSubreddits[String(splitPath[1])] {
let localized = NSLocalizedString("Reddit %@", comment: "Reddit")
let name = NSString.localizedStringWithFormat(localized as NSString, subredditName) as String
let metaData = FeedProviderFeedMetaData(name: name, homePageURL: homePageURL)
completion(.success(metaData))
return
}
subreddit(urlComponents) { result in
switch result {
case .success(let subreddit):
if let displayName = subreddit.data?.displayName {
completion(.success(FeedProviderFeedMetaData(name: displayName, homePageURL: homePageURL)))
} else {
completion(.failure(RedditFeedProviderError.unknown))
}
case .failure(let error):
completion(.failure(error))
}
}
}
public func refresh(_ webFeed: WebFeed, completion: @escaping (Result<Set<ParsedItem>, Error>) -> Void) {
guard let urlComponents = URLComponents(string: webFeed.url) else {
completion(.failure(TwitterFeedProviderError.unknown))
return
}
2020-05-05 01:13:15 +02:00
2020-05-10 18:44:30 +02:00
let api: String
if urlComponents.path.isEmpty {
api = "/.json"
} else {
api = "\(urlComponents.path).json"
}
2020-05-09 17:23:37 +02:00
let splitPath = urlComponents.path.split(separator: "/")
let identifySubreddit: Bool
if splitPath.count > 1 {
if Self.pseudoSubreddits.keys.contains(String(splitPath[1])) {
identifySubreddit = true
} else {
identifySubreddit = !urlComponents.path.hasPrefix("/r/")
}
} else {
identifySubreddit = true
}
2020-05-09 14:33:26 +02:00
fetch(api: api, parameters: [:], resultType: RedditLinkListing.self) { result in
2020-05-04 21:42:34 +02:00
switch result {
case .success(let linkListing):
2020-05-09 14:33:26 +02:00
let parsedItems = self.makeParsedItems(webFeed.url, identifySubreddit, linkListing)
2020-05-04 21:42:34 +02:00
completion(.success(parsedItems))
case .failure(let error):
completion(.failure(error))
}
}
}
2020-05-03 20:23:36 +02:00
public static func create(tokenSuccess: OAuthSwift.TokenSuccess, completion: @escaping (Result<RedditFeedProvider, Error>) -> Void) {
let oauthToken = tokenSuccess.credential.oauthToken
let oauthRefreshToken = tokenSuccess.credential.oauthRefreshToken
let redditFeedProvider = RedditFeedProvider(oauthToken: oauthToken, oauthRefreshToken: oauthRefreshToken)
2020-05-03 20:23:36 +02:00
2020-05-05 01:13:15 +02:00
redditFeedProvider.fetch(api: "/api/v1/me", resultType: RedditMe.self) { result in
2020-05-03 20:23:36 +02:00
switch result {
2020-05-04 23:08:55 +02:00
case .success(let user):
guard let username = user.name else {
completion(.failure(RedditFeedProviderError.unknown))
return
}
2020-05-03 20:23:36 +02:00
do {
redditFeedProvider.username = username
try storeCredentials(username: username, oauthToken: oauthToken, oauthRefreshToken: oauthRefreshToken)
2020-05-03 20:23:36 +02:00
completion(.success(redditFeedProvider))
} catch {
completion(.failure(error))
}
case .failure(let error):
completion(.failure(error))
}
}
}
public static func buildURL(_ type: RedditFeedType, username: String?, subreddit: String?, sort: RedditSort) -> URL? {
2020-05-10 18:44:30 +02:00
var components = URLComponents()
components.scheme = "https"
components.host = "www.reddit.com"
switch type {
case .home:
guard let username = username else {
return nil
}
components.user = username
components.path = "/\(sort.rawValue)"
2020-05-10 18:44:30 +02:00
case .popular:
components.path = "/r/popular/\(sort.rawValue)"
2020-05-10 18:44:30 +02:00
case .all:
components.path = "/r/all/\(sort.rawValue)"
2020-05-10 18:44:30 +02:00
case .subreddit:
guard let subreddit = subreddit else {
return nil
}
components.path = "/r/\(subreddit)/\(sort.rawValue)"
2020-05-10 18:44:30 +02:00
}
return components.url
}
}
// MARK: OAuth1SwiftProvider
extension RedditFeedProvider: OAuth2SwiftProvider {
public static var oauth2Swift: OAuth2Swift {
2020-05-03 20:23:36 +02:00
let oauth2 = OAuth2Swift(consumerKey: Secrets.redditConsumerKey,
consumerSecret: "",
authorizeUrl: "https://www.reddit.com/api/v1/authorize.compact?",
accessTokenUrl: "https://www.reddit.com/api/v1/access_token",
responseType: "token")
oauth2.accessTokenBasicAuthentification = true
return oauth2
}
public static var callbackURL: URL {
return URL(string: "netnewswire://success")!
}
public static var oauth2Vars: (state: String, scope: String, params: [String : String]) {
let state = generateState(withLength: 20)
let scope = "identity mysubreddits read"
let params = [
"client_id" : Secrets.redditConsumerKey,
"response_type" : "code",
"state" : state,
"redirect_uri" : "netnewswire://success",
"duration" : "permanent",
"scope" : scope
]
return (state: state, scope: scope, params: params)
}
}
2020-05-03 20:23:36 +02:00
private extension RedditFeedProvider {
func subreddit(_ urlComponents: URLComponents, completion: @escaping (Result<RedditSubreddit, Error>) -> Void) {
let splitPath = urlComponents.path.split(separator: "/")
guard splitPath.count > 1 else {
completion(.failure(RedditFeedProviderError.unknown))
return
}
let secondElement = String(splitPath[1])
let api = "/r/\(secondElement)/about.json"
fetch(api: api, parameters: [:], resultType: RedditSubreddit.self, completion: completion)
}
func fetch<R: Decodable>(api: String, parameters: [String: Any] = [:], resultType: R.Type, completion: @escaping (Result<R, Error>) -> Void) {
guard let client = client else {
completion(.failure(RedditFeedProviderError.unknown))
return
}
2020-05-03 20:23:36 +02:00
2020-05-07 08:58:43 +02:00
if let remaining = rateLimitRemaining, let reset = rateLimitReset, remaining < 1 && reset > Date() {
completion(.failure(RedditFeedProviderError.rateLimitExceeded))
return
}
let url = "\(Self.apiBase)\(api)"
var expandedParameters = parameters
expandedParameters["raw_json"] = "1"
client.get(url, parameters: expandedParameters, headers: Self.userAgentHeaders) { result in
switch result {
case .success(let response):
2020-05-08 09:37:27 +02:00
// let jsonString = String(data: response.data, encoding: .utf8)
// let url = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!.appendingPathComponent("reddit.json")
// print("******** writing to: \(url.path)")
// try? jsonString?.write(toFile: url.path, atomically: true, encoding: .utf8)
2020-05-07 08:58:43 +02:00
if let remaining = response.response.value(forHTTPHeaderField: "X-Ratelimit-Remaining") {
self.rateLimitRemaining = Int(remaining)
} else {
self.rateLimitRemaining = nil
2020-05-07 08:58:43 +02:00
}
2020-05-07 08:58:43 +02:00
if let reset = response.response.value(forHTTPHeaderField: "X-Ratelimit-Reset") {
self.rateLimitReset = Date(timeIntervalSinceNow: Double(reset) ?? 0)
} else {
self.rateLimitReset = nil
2020-05-07 08:58:43 +02:00
}
DispatchQueue.global(qos: .background).async {
let decoder = JSONDecoder()
do {
let result = try decoder.decode(resultType, from: response.data)
DispatchQueue.main.async {
completion(.success(result))
}
} catch {
DispatchQueue.main.async {
completion(.failure(error))
}
}
2020-05-04 21:42:34 +02:00
}
case .failure(let oathError):
self.handleFailure(error: oathError) { error in
if let error = error {
completion(.failure(error))
} else {
self.fetch(api: api, parameters: parameters, resultType: resultType, completion: completion)
}
}
}
}
}
2020-05-09 14:33:26 +02:00
func makeParsedItems(_ webFeedURL: String,_ identifySubreddit: Bool, _ linkListing: RedditLinkListing) -> Set<ParsedItem> {
2020-05-04 21:42:34 +02:00
var parsedItems = Set<ParsedItem>()
guard let linkDatas = linkListing.data?.children?.compactMap({ $0.data }), !linkDatas.isEmpty else {
return parsedItems
}
for linkData in linkDatas {
guard let permalink = linkData.permalink else { continue }
let parsedItem = ParsedItem(syncServiceID: nil,
uniqueID: permalink,
feedURL: webFeedURL,
url: "https://www.reddit.com\(permalink)",
externalURL: linkData.url,
title: linkData.title,
language: nil,
2020-05-09 14:33:26 +02:00
contentHTML: linkData.renderAsHTML(identifySubreddit: identifySubreddit),
2020-05-04 21:42:34 +02:00
contentText: linkData.selfText,
summary: nil,
imageURL: nil,
bannerImageURL: nil,
datePublished: linkData.createdDate,
dateModified: nil,
authors: makeParsedAuthors(linkData.author),
tags: nil,
attachments: nil)
parsedItems.insert(parsedItem)
}
return parsedItems
}
func makeParsedAuthors(_ username: String?) -> Set<ParsedAuthor>? {
guard let username = username else { return nil }
var urlComponents = URLComponents(string: "https://www.reddit.com")
urlComponents?.path = "/u/\(username)"
let userURL = urlComponents?.url?.absoluteString
2020-05-05 01:13:15 +02:00
return Set([ParsedAuthor(name: "u/\(username)", url: userURL, avatarURL: nil, emailAddress: nil)])
2020-05-04 21:42:34 +02:00
}
func handleFailure(error: OAuthSwiftError, completion: @escaping (Error?) -> Void) {
if case .tokenExpired = error {
os_log(.debug, log: self.log, "Access token expired, attempting to renew...")
2020-05-07 09:33:43 +02:00
oauthSwift?.renewAccessToken(withRefreshToken: oauthRefreshToken) { [weak self] result in
2020-05-07 09:33:43 +02:00
guard let strongSelf = self, let username = strongSelf.username else {
completion(nil)
return
}
switch result {
case .success(let tokenSuccess):
2020-05-07 09:33:43 +02:00
strongSelf.oauthToken = tokenSuccess.credential.oauthToken
strongSelf.oauthRefreshToken = tokenSuccess.credential.oauthRefreshToken
do {
2020-05-07 09:33:43 +02:00
try Self.storeCredentials(username: username, oauthToken: strongSelf.oauthToken, oauthRefreshToken: strongSelf.oauthRefreshToken)
os_log(.debug, log: strongSelf.log, "Access token renewed.")
} catch {
completion(error)
return
}
completion(nil)
case .failure(let oathError):
completion(oathError)
}
}
} else {
completion(error)
}
}
2020-05-06 00:14:23 +02:00
static func storeCredentials(username: String, oauthToken: String, oauthRefreshToken: String) throws {
let tokenCredentials = Credentials(type: .oauthAccessToken, username: username, secret: oauthToken)
try CredentialsManager.storeCredentials(tokenCredentials, server: Self.server)
let tokenSecretCredentials = Credentials(type: .oauthRefreshToken, username: username, secret: oauthRefreshToken)
try CredentialsManager.storeCredentials(tokenSecretCredentials, server: Self.server)
}
2020-05-05 01:13:15 +02:00
2020-05-03 20:23:36 +02:00
}