mirror of
https://github.com/yattee/yattee.git
synced 2025-08-09 20:24:06 +00:00
Popular videos, playing from mp4
This commit is contained in:
14
Model/DataProvider.swift
Normal file
14
Model/DataProvider.swift
Normal file
@@ -0,0 +1,14 @@
|
||||
import Alamofire
|
||||
import Foundation
|
||||
|
||||
class DataProvider: ObservableObject {
|
||||
static let instance = "https://invidious.home.arekf.net"
|
||||
|
||||
static func request(_ path: String) -> DataRequest {
|
||||
return AF.request(apiURLString(path))
|
||||
}
|
||||
|
||||
static func apiURLString(_ path: String) -> String {
|
||||
"\(instance)/api/v1/\(path)"
|
||||
}
|
||||
}
|
18
Model/PopularVideosProvider.swift
Normal file
18
Model/PopularVideosProvider.swift
Normal file
@@ -0,0 +1,18 @@
|
||||
import Alamofire
|
||||
import Foundation
|
||||
import SwiftyJSON
|
||||
|
||||
final class PopluarVideosProvider: DataProvider {
|
||||
@Published var videos = [Video]()
|
||||
|
||||
func load() {
|
||||
DataProvider.request("popular").responseJSON { response in
|
||||
switch response.result {
|
||||
case let .success(value):
|
||||
self.videos = JSON(value).arrayValue.map { Video($0) }
|
||||
case let .failure(error):
|
||||
print(error)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
39
Model/Video.swift
Normal file
39
Model/Video.swift
Normal file
@@ -0,0 +1,39 @@
|
||||
import Alamofire
|
||||
import Foundation
|
||||
import SwiftyJSON
|
||||
|
||||
class Video: Identifiable, ObservableObject {
|
||||
let id: String
|
||||
var title: String
|
||||
var thumbnailURL: URL
|
||||
var author: String
|
||||
|
||||
@Published var url: URL?
|
||||
@Published var error: Bool = false
|
||||
|
||||
init(id: String, title: String, thumbnailURL: URL, author: String) {
|
||||
self.id = id
|
||||
self.title = title
|
||||
self.thumbnailURL = thumbnailURL
|
||||
self.author = author
|
||||
}
|
||||
|
||||
init(_ json: JSON) {
|
||||
id = json["videoId"].stringValue
|
||||
title = json["title"].stringValue
|
||||
thumbnailURL = json["videoThumbnails"][0]["url"].url!
|
||||
author = json["author"].stringValue
|
||||
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
|
||||
}
|
||||
}
|
25
Model/VideoDetailsProvider.swift
Normal file
25
Model/VideoDetailsProvider.swift
Normal file
@@ -0,0 +1,25 @@
|
||||
import Alamofire
|
||||
import Foundation
|
||||
import SwiftyJSON
|
||||
|
||||
final class VideoDetailsProvider: DataProvider {
|
||||
@Published var video: Video?
|
||||
|
||||
var id: String
|
||||
|
||||
init(_ id: String) {
|
||||
self.id = id
|
||||
super.init()
|
||||
}
|
||||
|
||||
func load() {
|
||||
DataProvider.request("videos/\(id)").responseJSON { response in
|
||||
switch response.result {
|
||||
case let .success(value):
|
||||
self.video = Video(JSON(value))
|
||||
case let .failure(error):
|
||||
print(error)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user