XCDYouTubeKit icon indicating copy to clipboard operation
XCDYouTubeKit copied to clipboard

get video info via post request using innertube api key

Open armendh opened this issue 3 years ago • 5 comments

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)) }
        } ?? []
    }
}

armendh avatar Jul 24 '21 13:07 armendh

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"]; } } }

OlegBrailean avatar Jul 25 '21 10:07 OlegBrailean

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 :(

Dem909 avatar Jul 25 '21 19:07 Dem909

I pointed my Podfile to your repo pod 'XCDYouTubeKit', git: '[email protected]: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?

rbrand21 avatar Jul 29 '21 23:07 rbrand21

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

tommycarpi avatar Aug 02 '21 20:08 tommycarpi

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

qo4on avatar Mar 01 '23 19:03 qo4on