Add search

This commit is contained in:
Arkadiusz Fal
2021-06-11 14:36:26 +02:00
parent 5efb3a798f
commit 4bc70f351d
10 changed files with 162 additions and 46 deletions

View File

@@ -0,0 +1,19 @@
import Foundation
import SwiftyJSON
class SearchedVideosProvider: DataProvider {
@Published var videos = [Video]()
var query: String = ""
func load() {
let searchPath = "search?q=\(query.addingPercentEncoding(withAllowedCharacters: .alphanumerics)!)"
DataProvider.request(searchPath).responseJSON { response in
switch response.result {
case let .success(value):
self.videos = JSON(value).arrayValue.map { Video($0) }
case let .failure(error):
print(error)
}
}
}
}

View File

@@ -25,26 +25,34 @@ final class Video: Identifiable, ObservableObject {
}
init(_ json: JSON) {
func extractThumbnailURL(from details: JSON) -> URL {
if details["videoThumbnails"].arrayValue.isEmpty {
return URL(string: "https://invidious.home.arekf.net/vi/LuKwJyBNBsE/maxres.jpg")!
}
return details["videoThumbnails"][0]["url"].url!
}
func extractFormatStreamURL(from streams: [JSON]) -> URL? {
if streams.isEmpty {
error = true
return nil
}
let stream = streams.last!
return stream["url"].url
}
id = json["videoId"].stringValue
title = json["title"].stringValue
thumbnailURL = json["videoThumbnails"][0]["url"].url!
thumbnailURL = extractThumbnailURL(from: json)
author = json["author"].stringValue
length = json["lengthSeconds"].doubleValue
published = json["publishedText"].stringValue
views = json["viewCount"].intValue
url = formatStreamURL(from: json["formatStreams"].arrayValue)
}
func formatStreamURL(from streams: [JSON]) -> URL? {
if streams.isEmpty {
error = true
return nil
}
let stream = streams.last!
return stream["url"].url
url = extractFormatStreamURL(from: json["formatStreams"].arrayValue)
}
var playTime: String? {