Instantly share code, notes, and snippets.
Last active
February 23, 2021 02:17
-
Star
(1)
1
You must be signed in to star a gist -
Fork
(0)
0
You must be signed in to fork a gist
-
Save Heilum/4b1cf4b9465ac6befb5e7173d073577a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// DouYinVideoParser.swift | |
// Transformer | |
// | |
// Created by Forever Positive on 2021/2/15. | |
// | |
import Foundation | |
class DouYinVideoDownloader:NSObject,URLSessionTaskDelegate { | |
private var callback:((URL?)->Void)! | |
private var queue:OperationQueue! | |
private static var detector:NSDataDetector = try! NSDataDetector(types: NSTextCheckingResult.CheckingType.link.rawValue) | |
/** | |
杨超越靠什么走上人生巅峰?她看似躺赢的人生背后到底是谁在暗中操控?今天的视频楠哥为你解谜:杨超越背后的资本迷局 https://v.douyin.com/JKEVKVQ/ 椱制此链接,打开Dou音搜索,値接观kan视频! | |
*/ | |
private static func parseURLInShareText(_ text:String) -> String?{ | |
let detector = Self.detector; | |
let matches = detector.matches(in: text, options: [], range: NSRange(location: 0, length: text.utf16.count)) | |
if let first = matches.first{ | |
let range = Range(first.range, in: text)! | |
let url = text[range]; | |
print("分享文字中的URL:\(url)") | |
return String(url); | |
} | |
return nil; | |
} | |
private func onFinish(localURL:URL?){ | |
let callback = self.callback; | |
DispatchQueue.main.async { | |
callback?(localURL) | |
} | |
} | |
public func beginDownloading(_ shareText:String,callback:@escaping (URL?) -> Void){ | |
self.callback = callback; | |
if let shareUrlStr = Self.parseURLInShareText(shareText),let url = URL(string: shareUrlStr){ | |
let queue = OperationQueue() | |
self.queue = queue; | |
self.queue.addOperation { | |
let config = URLSessionConfiguration.default | |
let session = URLSession(configuration: config, delegate: self, delegateQueue: queue); | |
let task = session.dataTask(with: url) {(data, response, error) in | |
} | |
task.resume() | |
} | |
} | |
} | |
// MARK: - URLSessionDelete | |
func urlSession(_ session: URLSession, task: URLSessionTask, willPerformHTTPRedirection response: HTTPURLResponse, newRequest request: URLRequest, completionHandler: @escaping (URLRequest?) -> Void) { | |
//redirect to: https://www.iesdouyin.com/share/video/6928193122853784845/?region=CN&mid=6928193379415182087&u_code=18mgdm019&titleType=title&did=1662096460754429&iid=2938317180253068×tamp=1613368782&app=aweme&utm_campaign=client_share&utm_medium=ios&tt_from=copy&utm_source=copy | |
//json url = https://www.iesdouyin.com/web/api/v2/aweme/iteminfo/?item_ids=6928193122853784845 | |
if let url = request.url{ | |
let videoId = url.lastPathComponent; | |
let dataURL = "https://www.iesdouyin.com/web/api/v2/aweme/iteminfo/?item_ids=\(videoId)" | |
let task = URLSession.shared.dataTask(with: URL(string: dataURL)!) {[weak self] (data, response, error) in | |
guard let `self` = self else{ | |
return | |
} | |
guard let data = data, let json = try? JSONSerialization.jsonObject(with: data, options: .allowFragments) as? [String:Any] else { | |
self.onFinish(localURL: nil); | |
return | |
} | |
print("视频JSON:\(json)") | |
if let item_list = json["item_list"] as? [[String:Any]],let content = item_list.first{ | |
guard let video = content["video"] as? [String:Any], | |
let play_addr = video["play_addr"] as? [String:Any], | |
let url_list = play_addr["url_list"] as? [String], | |
let wmVideoURLStr = url_list.first else{ | |
self.onFinish(localURL: nil); | |
return; | |
} | |
let videoURLString = wmVideoURLStr.replacingOccurrences(of: "playwm", with: "play") | |
let finalURL = URL(string: videoURLString)! | |
print("最终下载url:\(finalURL)") | |
let task = URLSession.shared.downloadTask(with: finalURL) { localURL, urlResponse, error in | |
if let localURL = localURL { | |
let localFileName = "\(videoId)-\(UInt64(Date.timeIntervalSinceReferenceDate)).mp4"; | |
let newLocalPath = (NSTemporaryDirectory() as NSString).appendingPathComponent(localFileName); | |
let newLocalURL = URL(fileURLWithPath: newLocalPath) | |
if let _ = try? FileManager.default.moveItem(at: localURL, to:newLocalURL){ | |
self.onFinish(localURL: newLocalURL) | |
}else{ | |
self.onFinish(localURL: nil) | |
} | |
} | |
} | |
task.resume() | |
}else{ | |
self.onFinish(localURL: nil); | |
} | |
} | |
task.resume() | |
}else{ | |
self.onFinish(localURL: nil); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment