0xced / XCDYouTubeKit

YouTube video player for iOS, tvOS and macOS
MIT License
2.92k stars 626 forks source link

get video info via post request using innertube api key #545

Open armendh opened 3 years ago

armendh commented 3 years ago

the innertube api key is hardcoded and we don't know how long it will last so it is recommended to scrap the api key from youtube and set it via XCDYouTubeClient.setInnertubeApiKey

func scrapInnertubeApiKey(){
    let link = "https://www.youtube.com"
    Alamofire.request(link).responseString { (response) in  // network lib https://github.com/Alamofire/Alamofire
        if let html = response.value {
            if let doc = try? HTML(html: html, encoding: .utf8) {   // HTML parser https://github.com/tid-kijyun/Kanna
                if let text = doc.xpath("//script[contains(., 'INNERTUBE_API_KEY')]/text()").first?.text {
                    if let results = text.match("ytcfg.set\\((\\{.*?\\})\\)").last?.last {
                        if let data = results.data(using: .utf8), let model = try? JSONDecoder().decode(InnertubeScrap.self, from: data) {
                            let key = model.INNERTUBE_API_KEY
                            XCDYouTubeClient.setInnertubeApiKey(key)
                        }
                    }
                }
            }
        }
    }
}

struct InnertubeScrap : Codable {
    let INNERTUBE_API_KEY : String
}

extension String {
    func match(_ regex: String) -> [[String]] {
        let nsString = self as NSString
        return (try? NSRegularExpression(pattern: regex, options: []))?.matches(in: self, options: [], range: NSMakeRange(0, nsString.length)).map { match in
            (0..<match.numberOfRanges).map { match.range(at: $0).location == NSNotFound ? "" : nsString.substring(with: match.range(at: $0)) }
        } ?? []
    }
}
OlegBrailean commented 3 years ago

Thank you! Worked for me! Additionally fix extract images from the new response: In file: XCDYouTubeVideo.m After line: NSArray<NSDictionary *> *thumbnails = XCDThumnailArrayWithString(playerResponse); Add this code: if((thumbnails==nil || thumbnails.count<1) && info[@"videoDetails"] ) { NSDictionary *videoDetails = info[@"videoDetails"]; if(videoDetails && videoDetails[@"thumbnail"]) { NSDictionary *thumbnail = videoDetails[@"thumbnail"]; if(thumbnail && thumbnail[@"thumbnails"]) { thumbnails = thumbnail[@"thumbnails"]; } } }

Dem909 commented 3 years ago

Thank you! Worked for me! Additionally fix extract images from the new response: In file: XCDYouTubeVideo.m After line: NSArray<NSDictionary *> *thumbnails = XCDThumnailArrayWithString(playerResponse); Add this code: if((thumbnails==nil || thumbnails.count<1) && info[@"videoDetails"] ) { NSDictionary *videoDetails = info[@"videoDetails"]; if(videoDetails && videoDetails[@"thumbnail"]) { NSDictionary *thumbnail = videoDetails[@"thumbnail"]; if(thumbnail && thumbnail[@"thumbnails"]) { thumbnails = thumbnail[@"thumbnails"]; } } }

Can you give an example of how you work with videos? It's just that I still get an error and I can't do anything about it :(

rbrand21 commented 3 years ago

I pointed my Podfile to your repo pod 'XCDYouTubeKit', git: 'git@github.com:armendh/XCDYouTubeKit.git', branch: 'master' and videos work 🙇‍♂️ However, my logs are spammed with the warning on XCDYouTubeVideo.m L:96. Do you know what this is from or how I fix it?

tommycarpi commented 3 years ago

It's not working on my side, anyone found a working solution?

qo4on commented 1 year ago

Has anyone been able to do this? It returns "status": "PERMISSION_DENIED".