Youtubeの動画情ほを取得してパースする
これでできたんだろうか。よくわからん。よくない。
実装
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let videoURL = "https://www.youtube.com/watch?v=KMU0tzLwhbE"
let url = NSURL(string: "https://www.youtubeinmp3.com/fetch/?format=JSON&video=\(videoURL)")
let sessionConfig = URLSessionConfiguration.default
let session = URLSession(configuration: sessionConfig, delegate: nil, delegateQueue: nil)
let request = NSMutableURLRequest(url: url! as URL)
request.httpMethod = "GET"
let task = session.dataTask(with: request as URLRequest, completionHandler: { (data: Data?, response: URLResponse?, error: Error?) -> Void in
if (error == nil) {
if let response = response as? HTTPURLResponse {
print("response=\(response)")
if response.statusCode == 200 {
if data != nil {
do {
let responseJSON = try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.allowFragments) as! NSDictionary;
let urlString = responseJSON["link"] as! String
let directDownloadURL = NSURL(string: urlString)
// Call your method loadFileAsync
// YourClass.loadFileAsync(directDownloadURL!, completion: { (path, error) -> Void in
// print(path)
// })
}
catch let JSONError as NSError {
print("\(JSONError)")
}
catch {
print("unknown error in JSON Parsing");
}
}
}
}
}
else {
print("Failure: \(error!.localizedDescription)");
}
})
task.resume()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
実行結果
response=<NSHTTPURLResponse: 0x600000034e60> { URL: https://www.youtubeinmp3.com/fetch/?format=JSON&video=https://www.youtube.com/watch?v=KMU0tzLwhbE } { status code: 200, headers {
"Access-Control-Allow-Origin" = "*";
"CF-RAY" = "38b8e073d7e40b44-NRT";
Connection = "keep-alive";
"Content-Encoding" = gzip;
"Content-Type" = "text/html";
Date = "Wed, 09 Aug 2017 07:07:09 GMT";
Server = "cloudflare-nginx";
"Set-Cookie" = "__cfduid=dc236608843f6bf4e2b20fbcee9729e881502262428; expires=Thu, 09-Aug-18 07:07:08 GMT; path=/; domain=.youtubeinmp3.com; HttpOnly";
"Transfer-Encoding" = Identity;
Vary = "Accept-Encoding";
"X-Powered-By" = "PHP/5.5.38";
} }