-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEndocrator.swift
executable file
·115 lines (95 loc) · 3.02 KB
/
Endocrator.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
//
// m3u8Handler.swift
// DownLoadFile
//
// Created by yang on 2017/10/5.
// Copyright © 2017年 yang. All rights reserved.
//
import Foundation
public protocol EndocratorDelegate: class {
func videoDownloadSucceeded()
func videoDownloadFailed()
func update(_ endocrator: Endocrator ,progress: Float, with directoryName: String)
func updateSize(_ endocrator: Endocrator ,_ progress: Double, size: Int, currentSize: Int)
}
open class Endocrator {
var filesize: Int = 0
var array = [[String: Int]]()
var sizeDic: [String: Int] = [:]
public let downloader = VideoDownloader()
public var progress: Float = 0.0
public var id: String!
public var lectureFileName: String = "" {
didSet {
m3u8Parser.identifier = lectureFileName
}
}
public var courseFileName: String = "" {
didSet {
m3u8Parser.videoIndet = courseFileName
}
}
public var m3u8URL = ""
private let m3u8Parser = M3u8Parser()
public weak var delegate: EndocratorDelegate?
public init() {
}
open func parse() {
downloader.delegate = self
m3u8Parser.delegate = self
m3u8Parser.parse(with: m3u8URL)
}
}
extension Endocrator: M3u8ParserDelegate {
func parseM3u8Succeeded(by parser: M3u8Parser) {
downloader.tsPlaylist = parser.tsPlaylist
//print(parser.tsPlaylist.tsSegmentArray)
downloader.m3u8Data = parser.m3u8Data
downloader.startDownload()
}
func parseM3u8Failed(by parser: M3u8Parser) {
//print("Parse m3u8 file failed.")
}
}
extension Endocrator: VideoDownloaderDelegate {
func videoDownloadSucceeded(by downloader: VideoDownloader) {
delegate?.videoDownloadSucceeded()
}
func videoDownloadFailed(by downloader: VideoDownloader) {
delegate?.videoDownloadFailed()
}
func updateFileNumber(_ progress: Float) {
self.progress = progress
delegate?.update(self, progress: progress, with: lectureFileName)
// guard let size = sizeDic[id] else {
// return
// }
// print(filesize,"++++++++++++++++++++",self.id)
realmDownload = (self,Double(self.progress),filesize / 1024 / 1024)
}
func updateSizeProgress(_ progress: Double, size: Int, currentSize: Int) {
//print(size,"________-----------__________------")
var dicKeys: Set<String> = []
if progress == 1 {
array.append([self.id : size])
for dic in array {
dicKeys.insert(dic.keys.first!)
}
}
for key in dicKeys {
let dicArry = array.filter { (dic) -> Bool in
return dic.keys.first == key
}
var intArr: [Int] = []
for aaa in dicArry {
intArr.append(aaa.values.first!)
filesize += aaa.values.first!
}
// sizeDic.updateValue(filesize, forKey: self.id)
}
// if !sizeDic.isEmpty {
// //print(sizeDic,"333333333333333333")
// }
delegate?.updateSize(self, Double(self.progress), size: filesize/1024/1024, currentSize: currentSize)
}
}