yattee/Model/InvidiousAPI.swift

183 lines
5.5 KiB
Swift
Raw Normal View History

2021-07-07 22:39:18 +00:00
import Defaults
2021-06-28 10:43:07 +00:00
import Foundation
import Siesta
import SwiftyJSON
final class InvidiousAPI: Service {
static let shared = InvidiousAPI()
static let instance = "https://invidious.home.arekf.net"
static func proxyURLForAsset(_ url: String) -> URL? {
guard let instanceURLComponents = URLComponents(string: InvidiousAPI.instance),
var urlComponents = URLComponents(string: url) else { return nil }
urlComponents.scheme = instanceURLComponents.scheme
urlComponents.host = instanceURLComponents.host
return urlComponents.url
}
init() {
2021-07-07 22:39:18 +00:00
SiestaLog.Category.enabled = .common
let SwiftyJSONTransformer =
ResponseContentTransformer(transformErrors: true) { JSON($0.content as AnyObject) }
2021-06-28 10:43:07 +00:00
super.init(baseURL: "\(InvidiousAPI.instance)/api/v1")
configure {
$0.pipeline[.parsing].add(SwiftyJSONTransformer, contentTypes: ["*/json"])
}
configure("/auth/**") {
$0.headers["Cookie"] = self.authHeader
}
configure("**", requestMethods: [.post]) {
$0.pipeline[.parsing].removeTransformers()
}
configureTransformer("/popular", requestMethods: [.get]) { (content: Entity<JSON>) -> [Video] in
content.json.arrayValue.map(Video.init)
}
configureTransformer("/trending", requestMethods: [.get]) { (content: Entity<JSON>) -> [Video] in
content.json.arrayValue.map(Video.init)
}
configureTransformer("/search", requestMethods: [.get]) { (content: Entity<JSON>) -> [Video] in
content.json.arrayValue.map(Video.init)
}
2021-09-13 20:41:16 +00:00
configureTransformer("/search/suggestions", requestMethods: [.get]) { (content: Entity<JSON>) -> [String] in
if let suggestions = content.json.dictionaryValue["suggestions"] {
return suggestions.arrayValue.map(String.init)
}
return []
}
2021-06-28 10:43:07 +00:00
configureTransformer("/auth/playlists", requestMethods: [.get]) { (content: Entity<JSON>) -> [Playlist] in
content.json.arrayValue.map(Playlist.init)
}
2021-08-29 21:36:18 +00:00
configureTransformer("/auth/playlists/*", requestMethods: [.get]) { (content: Entity<JSON>) -> Playlist in
Playlist(content.json)
}
2021-07-08 17:18:36 +00:00
configureTransformer("/auth/playlists", requestMethods: [.post, .patch]) { (content: Entity<Data>) -> Playlist in
2021-07-08 15:14:54 +00:00
// hacky, to verify if possible to get it in easier way
Playlist(JSON(parseJSON: String(data: content.content, encoding: .utf8)!))
}
2021-06-28 10:43:07 +00:00
configureTransformer("/auth/feed", requestMethods: [.get]) { (content: Entity<JSON>) -> [Video] in
if let feedVideos = content.json.dictionaryValue["videos"] {
2021-08-25 22:12:59 +00:00
return feedVideos.arrayValue.map(Video.init)
2021-06-28 10:43:07 +00:00
}
return []
}
2021-08-25 22:12:59 +00:00
configureTransformer("/auth/subscriptions", requestMethods: [.get]) { (content: Entity<JSON>) -> [Channel] in
content.json.arrayValue.map(Channel.init)
}
configureTransformer("/channels/*", requestMethods: [.get]) { (content: Entity<JSON>) -> Channel in
Channel(json: content.json)
2021-06-28 10:43:07 +00:00
}
configureTransformer("/videos/*", requestMethods: [.get]) { (content: Entity<JSON>) -> Video in
Video(content.json)
}
}
var authHeader: String? = "SID=\(Profile().sid)"
var popular: Resource {
resource("/popular")
}
func trending(category: TrendingCategory, country: Country) -> Resource {
resource("/trending")
.withParam("type", category.name)
.withParam("region", country.rawValue)
}
2021-08-25 22:12:59 +00:00
var feed: Resource {
2021-06-28 10:43:07 +00:00
resource("/auth/feed")
}
2021-08-25 22:12:59 +00:00
var subscriptions: Resource {
resource("/auth/subscriptions")
}
func channelSubscription(_ id: String) -> Resource {
resource("/auth/subscriptions").child(id)
}
func channel(_ id: String) -> Resource {
2021-06-28 10:43:07 +00:00
resource("/channels/\(id)")
}
func video(_ id: String) -> Resource {
resource("/videos/\(id)")
}
var playlists: Resource {
resource("/auth/playlists")
}
2021-07-08 17:18:36 +00:00
func playlist(_ id: String) -> Resource {
resource("/auth/playlists/\(id)")
}
func playlistVideos(_ id: String) -> Resource {
playlist(id).child("videos")
}
func playlistVideo(_ playlistID: String, _ videoID: String) -> Resource {
playlist(playlistID).child("videos").child(videoID)
}
2021-07-07 22:39:18 +00:00
func search(_ query: SearchQuery) -> Resource {
var resource = resource("/search")
.withParam("q", searchQuery(query.query))
.withParam("sort_by", query.sortBy.parameter)
2021-07-08 15:14:54 +00:00
if let date = query.date?.rawValue {
resource = resource.withParam("date", date)
2021-07-07 22:39:18 +00:00
}
2021-07-08 15:14:54 +00:00
if let duration = query.duration?.rawValue {
resource = resource.withParam("duration", duration)
2021-07-07 22:39:18 +00:00
}
return resource
2021-06-28 10:43:07 +00:00
}
2021-09-13 20:41:16 +00:00
func searchSuggestions(query: String) -> Resource {
resource("/search/suggestions")
.withParam("q", query.lowercased())
}
2021-06-28 10:43:07 +00:00
private func searchQuery(_ query: String) -> String {
var searchQuery = query
let url = URLComponents(string: query)
if url != nil,
url!.host == "youtu.be"
{
searchQuery = url!.path.replacingOccurrences(of: "/", with: "")
}
let queryItem = url?.queryItems?.first { item in item.name == "v" }
if let id = queryItem?.value {
searchQuery = id
}
2021-07-08 15:14:54 +00:00
return searchQuery
2021-06-28 10:43:07 +00:00
}
}