@billy2020

Как получить более 50 видео при помощи youtube api v3 используя язык swift?

всем Привет подскажите пожалуйста как я могу получить более 50 видео с плейлиста youtube

вот мой код

import UIKit
import Alamofire

protocol videoModelDelegate{
func videoAreReady()
}

class VideoModel: NSObject {

private var API_KEY = "----"
private var urlString = "https://www.googleapis.com/youtube/v3/playlistItems"
private var playlistID = "PLUumXRq81CH-wZh0vgWDP-Eoi3mzB8nUa"
var delegate: videoModelDelegate!

var video = [Video]()

func getVideo(){

Alamofire.request(urlString, method: .get, parameters: ["part":"snippet", "key":API_KEY, "playlistId":playlistID, "maxResults":"50",], encoding: URLEncoding.default, headers: nil).responseJSON { (response) in

if let JSON = response.value as? [String:Any]{
var arrayOfTheVideo = [Video]()

for video in JSON["items"] as! NSArray {
let videoObject = Video()


videoObject.videoId = (video as! NSDictionary).value(forKeyPath: "snippet.resourceId.videoId") as! String
videoObject.videoTitle = (video as! NSDictionary).value(forKeyPath: "snippet.title") as! String
videoObject.videoDescription = (video as! NSDictionary).value(forKeyPath: "snippet.description") as! String


if (video as! NSDictionary).value(forKeyPath: "snippet.thumbnails.maxres.url") != nil{
videoObject.videoThumbnailUrl = (video as! NSDictionary).value(forKeyPath: "snippet.thumbnails.maxres.url") as! String
}else if (video as! NSDictionary).value(forKeyPath: "snippet.thumbnails.hqdefault.url") != nil{
videoObject.videoThumbnailUrl = (video as! NSDictionary).value(forKeyPath: "snippet.thumbnails.hqdefault.url") as! String
}else if (video as! NSDictionary).value(forKeyPath: "snippet.thumbnails.sddefault.url") != nil{
videoObject.videoThumbnailUrl = (video as! NSDictionary).value(forKeyPath: "snippet.thumbnails.sddefault.url") as! String
}else if (video as! NSDictionary).value(forKeyPath: "snippet.thumbnails.mqdefault.url") != nil{
videoObject.videoThumbnailUrl = (video as! NSDictionary).value(forKeyPath: "snippet.thumbnails.mqdefault.url") as! String
}else if (video as! NSDictionary).value(forKeyPath: "snippet.thumbnails.default.url") != nil{
videoObject.videoThumbnailUrl = (video as! NSDictionary).value(forKeyPath: "snippet.thumbnails.default.url") as! String
}else{

}

arrayOfTheVideo.append(videoObject)

}

self.video = arrayOfTheVideo

if self.delegate != nil{
self.delegate!.videoAreReady()
}

}

}

}

}
  • Вопрос задан
  • 219 просмотров
Пригласить эксперта
Ответы на вопрос 1
briahas
@briahas
ObjC, Swift, Python
Я делал аплоад видео через гугл фреймворки
pod 'GoogleAPIClientForREST'
    pod 'GoogleAPIClientForREST/YouTube'
    pod 'Google/SignIn'


Сделай по аналогии только получение видео.
в Бриджинг файле ОБЯЗАТЕЛЬНО прописать
#import <Google/SignIn.h>
#import <GTMSessionFetcher/GTMSessionFetcher.h>
#import <GTMSessionFetcher/GTMSessionFetcherService.h>


Разбивай и помещай код там где надо тебе
let service =  GTLRYouTubeService()
.....
        var configureError: NSError?
        GGLContext.sharedInstance().configureWithError(&configureError)
        assert(configureError == nil, "Error configuring Google services: \(configureError)")

        GIDSignIn.sharedInstance().delegate = self
        GIDSignIn.sharedInstance().uiDelegate = self
        GIDSignIn.sharedInstance().scopes = [kGTLRAuthScopeYouTube, kGTLRAuthScopeYouTubeUpload]
        GIDSignIn.sharedInstance().shouldFetchBasicProfile = true
        GIDSignIn.sharedInstance().signIn()

        service.authorizer = GIDSignIn.sharedInstance().currentUser.authentication.fetcherAuthorizer()
let status = GTLRYouTube_VideoStatus()
        status.privacyStatus = kGTLRYouTube_VideoStatus_PrivacyStatus_Public
        
        let snippet = GTLRYouTube_VideoSnippet()
        snippet.title = "Lalala"
        snippet.descriptionProperty = "TestUpload"
        snippet.tags = "test,video,upload".components(separatedBy: ",")
        
        let youtubeVideo = GTLRYouTube_Video()
        youtubeVideo.snippet = snippet
        youtubeVideo.status = status
        
        let uploadParams = GTLRUploadParameters(fileURL: url, mimeType: "video/mp4")
        
        let uploadQuery = GTLRYouTubeQuery_VideosInsert.query(withObject: youtubeVideo, part: "snippet,status", uploadParameters: uploadParams)
        
        uploadQuery.executionParameters.uploadProgressBlock = {(progressTicket, totalBytesUploaded, totalBytesExpectedToUpload) in
            print("Uploaded", totalBytesUploaded)
        }

        service.executeQuery(uploadQuery) { (ticket, obj, error) in
            print(ticket)
            print(obj)
            print(error)
        }
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы