This repository was archived by the owner on Sep 15, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathStatsTopVideosTimeIntervalData.swift
More file actions
79 lines (69 loc) · 2.36 KB
/
Copy pathStatsTopVideosTimeIntervalData.swift
File metadata and controls
79 lines (69 loc) · 2.36 KB
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
public struct StatsTopVideosTimeIntervalData {
public let period: StatsPeriodUnit
public let periodEndDate: Date
public let totalPlaysCount: Int
public let otherPlayCount: Int
public let videos: [StatsVideo]
public init(period: StatsPeriodUnit,
periodEndDate: Date,
videos: [StatsVideo],
totalPlaysCount: Int,
otherPlayCount: Int) {
self.period = period
self.periodEndDate = periodEndDate
self.videos = videos
self.totalPlaysCount = totalPlaysCount
self.otherPlayCount = otherPlayCount
}
}
public struct StatsVideo {
public let postID: Int
public let title: String
public let playsCount: Int
public let videoURL: URL?
public init(postID: Int,
title: String,
playsCount: Int,
videoURL: URL?) {
self.postID = postID
self.title = title
self.playsCount = playsCount
self.videoURL = videoURL
}
}
extension StatsTopVideosTimeIntervalData: StatsTimeIntervalData {
public static var pathComponent: String {
return "stats/video-plays"
}
public init?(date: Date, period: StatsPeriodUnit, jsonDictionary: [String: AnyObject]) {
guard
let unwrappedDays = type(of: self).unwrapDaysDictionary(jsonDictionary: jsonDictionary),
let totalPlayCount = unwrappedDays["total_plays"] as? Int,
let otherPlays = unwrappedDays["other_plays"] as? Int,
let videos = unwrappedDays["plays"] as? [[String: AnyObject]]
else {
return nil
}
self.period = period
self.periodEndDate = date
self.totalPlaysCount = totalPlayCount
self.otherPlayCount = otherPlays
self.videos = videos.compactMap { StatsVideo(jsonDictionary: $0) }
}
}
extension StatsVideo {
init?(jsonDictionary: [String: AnyObject]) {
guard
let postID = jsonDictionary["post_id"] as? Int,
let title = jsonDictionary["title"] as? String,
let playsCount = jsonDictionary["plays"] as? Int,
let url = jsonDictionary["url"] as? String
else {
return nil
}
self.postID = postID
self.title = title
self.playsCount = playsCount
self.videoURL = URL(string: url)
}
}