Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

armendh
Copy link

@armendh armendh commented Jul 24, 2021

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
Copy link

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
Copy link

Dem909 commented Jul 25, 2021

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

sixty8 added a commit to sixty8/XCDYouTubeKit that referenced this pull request Jul 27, 2021
get video info via post request using innertube api key 0xced#545
sixty8 added a commit to sixty8/XCDYouTubeKit that referenced this pull request Jul 27, 2021
get video info via post request using innertube api key 0xced#545
sixty8 added a commit to sixty8/XCDYouTubeKit that referenced this pull request Jul 27, 2021
get video info via post request using innertube api key 0xced#545
@rbrand21
Copy link

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
Copy link

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

@@ -14,6 +14,8 @@ @implementation XCDYouTubeClient

@synthesize languageIdentifier = _languageIdentifier;

static NSString * _innertubeApiKey = @"AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8";

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't its private someone's key? I think you shouldn't commit it.

@qo4on
Copy link

qo4on commented Mar 1, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants