-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathTVShow.swift
207 lines (149 loc) · 6.64 KB
/
TVShow.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
//
// Show.swift
// Fetch
//
// Created by Stephen Radford on 10/10/2015.
// Copyright © 2015 Cocoon Development Ltd. All rights reserved.
//
import Downpour
import Alamofire
public class TVShow {
/// The TV Show ID
public var id: Int?
/// Delegate for the TVShow
public var delegate: TVShowDelegate?
/// URL to the backdrop on the API
public var backdropURL: String?
/// The backdrop of the image
public var backdrop: UIImage?
/// URL to the poster on the API
public var posterURL: String?
/// The poster image
public var poster: UIImage?
/// The name of the TV Show
public var title: String?
/// Title to sort alphabetically witout "The"
public var sortableTitle: String? {
get {
if let range = title?.rangeOfString("The ") {
if range.startIndex == title?.startIndex {
return title?.stringByReplacingCharactersInRange(range, withString: "")
}
}
return title
}
}
/// Description of the TV Show
public var overview: String?
/// voting average of the tv show
public var voteAverage: Float64?
/// TV show genre
public var genres: [Genre]?
/// Putio Files
public var files: [File] = []
public var seasons: [String:[TVEpisode]] = [:]
var requests = 0
private var completed = 0
public var completedPercent: Float {
get {
return (Float(self.completed) / Float(self.files.count))
}
}
/**
Convert the files to TV Episodes
*/
public func convertFilesToEpisodes() {
guard seasons.count == 0 else {
self.delegate?.tvEpisodesLoaded()
return
}
TMDB.sharedInstance.requests = 0
for file in files {
requests += 1
let d = Downpour(string: file.name)
if d.season != nil && d.episode != nil {
TMDB.fetchEpisodeForSeason(d.season!, episode: d.episode!, showId: id!) { episode in
self.requests -= 1
self.completed += 1
self.delegate?.percentUpdated()
if let ep = episode {
if ep.seasonNo != nil {
ep.file = file
if self.seasons["Season \(ep.seasonNo!)"] != nil {
self.seasons["Season \(ep.seasonNo!)"]!.append(ep)
} else {
self.seasons["Season \(ep.seasonNo!)"] = [ep]
}
}
}
if self.requests == 0 {
TMDB.sharedInstance.requests = 0
self.delegate?.tvEpisodesLoaded()
}
}
}
}
}
/// Load the movie poster
public func loadPoster(callback: (UIImage) -> Void) {
let documents = NSURL(string: NSSearchPathForDirectoriesInDomains(.CachesDirectory, .UserDomainMask, true)[0])
let lcrPath = documents!.URLByAppendingPathComponent("\(id!).lcr")
let pngPath = documents!.URLByAppendingPathComponent("\(id!).png")
let fm = NSFileManager.defaultManager()
var isDir: ObjCBool = false
if fm.fileExistsAtPath(lcrPath!.absoluteString!, isDirectory: &isDir) {
poster = UIImage(contentsOfFile: lcrPath!.absoluteString!)
if self.poster != nil {
callback(self.poster!)
}
} else if fm.fileExistsAtPath(pngPath!.absoluteString!, isDirectory: &isDir) {
poster = UIImage(contentsOfFile: pngPath!.absoluteString!)
if self.poster != nil {
callback(self.poster!)
}
} else {
let params = ["title": title!]
Alamofire.request(.GET, "http://lsrdb.com/search", parameters: params)
.responseData { response in
if response.result.isSuccess && response.response!.statusCode == 200 {
if let image = response.result.value {
image.writeToFile(lcrPath!.absoluteString!, atomically: true)
self.poster = UIImage(contentsOfFile: lcrPath!.absoluteString!)
if self.poster != nil {
callback(self.poster!)
}
}
} else if let url = self.posterURL {
Alamofire.request(.GET, "https://image.tmdb.org/t/p/w500\(url)")
.responseImage { response in
if let image = response.result.value {
UIImagePNGRepresentation(image)?.writeToFile(pngPath!.absoluteString!, atomically: true)
self.poster = image
if self.poster != nil {
callback(self.poster!)
}
}
}
}
}
}
}
public class func fromCache(cache: [String:AnyObject]) -> TVShow {
let files: [File] = (cache["files"] as! [[String:AnyObject]]).map { file in
let id = Int32(file["id"] as! Int)
let name = file["name"] as! String
let screenshot = file["screenshot"] as! String
let start_from = file["start_from"] as! Double
let accessed = file["accessed"] as! Bool
let f = File(id: id, name: name, size: 0, icon: "", content_type: "video/mp4", has_mp4: true, parent_id: 0, subtitles: "", accessed: accessed, screenshot: screenshot, is_shared: false, start_from: start_from)
return f
}
let show = TVShow()
show.id = Int(cache["id"] as! String)
show.title = cache["title"] as? String
show.posterURL = cache["posterURL"] as? String
show.overview = cache["overview"] as? String
show.files = files
return show
}
}