Skip to content

Commit

Permalink
Revert back to .web client type to get audio-ful streams
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeehut committed Sep 26, 2023
1 parent f565cac commit e08fe8f
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 9 deletions.
8 changes: 3 additions & 5 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
// swift-tools-version:5.5
// The swift-tools-version declares the minimum version of Swift required to build this package.

// swift-tools-version:5.9
import PackageDescription

let package = Package(
name: "YouTubeKit",
/*platforms: [
platforms: [
.macOS(.v12), .iOS(.v15), .watchOS(.v8), .tvOS(.v15)
],*/
],
products: [
// Products define the executables and libraries a package produces, and make them visible to other packages.
.library(
Expand Down
11 changes: 10 additions & 1 deletion Sources/YouTubeKit/Extraction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,21 @@ class Extraction {
class func parseForObjectFromStartpoint<T: Decodable>(_ type: T.Type, html: String, startPoint: String.Index) throws -> T {
let fullObject = try findObjectFromStartpoint(html: html, startPoint: startPoint)
let objectData = fullObject.data(using: .utf8) ?? Data()


if type is PlayerConfig.Type {
Logger().info("Full object is:\n\(fullObject)")
}

do {
return try JSONDecoder().decode(type, from: objectData)
} catch let error {
// TODO: try different evaluation (like in Python: ast.literal_eval)
os_log("Failed to decode object from given start point: %{public}@", log: log, type: .error, error.localizedDescription)

if type is PlayerConfig.Type {
return PlayerConfig(assets: nil) as! T
}

throw YouTubeKitError.htmlParseError
}
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/YouTubeKit/InnerTube.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class InnerTube {

private let baseURL = "https://www.youtube.com/youtubei/v1"

init(client: ClientType = .ios, useOAuth: Bool = false, allowCache: Bool = true) {
init(client: ClientType = .web, useOAuth: Bool = false, allowCache: Bool = true) {
self.context = defaultClients[client]!.context
self.apiKey = defaultClients[client]!.apiKey
self.headers = defaultClients[client]!.headers
Expand Down
3 changes: 3 additions & 0 deletions Sources/YouTubeKit/YouTube.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//

import Foundation
import OSLog

@available(iOS 13.0, watchOS 6.0, tvOS 13.0, macOS 10.15, *)
public class YouTube {
Expand Down Expand Up @@ -68,6 +69,8 @@ public class YouTube {
var request = URLRequest(url: watchURL)
request.setValue("Mozilla/5.0", forHTTPHeaderField: "User-Agent")
request.setValue("en-US,en", forHTTPHeaderField: "accept-language")

Logger().info("Sending request to \(request) with headers \(request.allHTTPHeaderFields!)")
let (data, _) = try await URLSession.shared.data(for: request)
_watchHTML = String(data: data, encoding: .utf8) ?? ""
return _watchHTML!
Expand Down
15 changes: 13 additions & 2 deletions Tests/YouTubeKitTests/YouTubeKitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,20 @@ final class YouTubeKitTests: XCTestCase {
XCTFail("did throw error: \(error)")
}
}


func testReproduceBug() async throws {
let youTubeVideo = YouTube(videoID: "lD8XURzIibk")
let compatibleStreams = try await youTubeVideo.streams.filter { $0.subtype.lowercased() == "mp4" && $0.includesAudioTrack && $0.includesVideoTrack }

let maxResolution = 1080 // this avoid long loading times, 4K isn't needed for a trailer – TODO: make adjustable by user in settings
let streamURL = compatibleStreams.filter { $0.itag.videoResolution != nil }.filter { $0.itag.videoResolution! <= maxResolution }.highestResolutionStream()?.url

XCTAssert(compatibleStreams.count > 0)
XCTAssertNotNil(streamURL)
}

func testSampleVideo1() async {
let youtube = YouTube(videoID: "9bZkp7q19f0")
let youtube = YouTube(videoID: "lD8XURzIibk")
do {
let streams = try await youtube.streams
XCTAssert(streams.count > 0)
Expand Down

0 comments on commit e08fe8f

Please sign in to comment.