This repository was archived by the owner on Sep 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.swift
163 lines (138 loc) · 6.02 KB
/
main.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
import Foundation
import Darwin
import Rainbow
print("")
print(" dP dP M''''''''M M''MMMMM''M ")
print(" 88 88 Mmmm mmmM M MMMMM M ")
print(" 88d888b. .d8888b. .d8888b. 88d888b. MMMM MMMM M MMMMP M ")
print(" 88' `88 88' `88 Y8ooooo. 88' `88 MMMM MMMM M MMMM' .M ")
print(" 88. .88 88. .88 88 88 88 MMMM MMMM M MMP' .MM ")
print(" 88Y8888' `88888P8 `88888P' dP dP MMMM MMMM M .dMMM ")
print(" MMMMMMMMMM MMMMMMMMMMM ")
print("")
struct Broadcast {
let id: Int
let beginsAt: Int
let endsAt: Int
let duration: Int
let title: String
let channel: String
let videoURL: String?
}
// let channelNameSearch: String? = "ARD"
// let titleSearch: String? = "Fluch"
let nowInSeconds = Int(NSDate().timeIntervalSince1970)
let cli = CommandLine()
let channelNameSearch = StringOption(shortFlag: "c", longFlag: "channel", helpMessage: "Channel name match")
let titleSearch = StringOption(shortFlag: "b", longFlag: "broadcast", helpMessage: "Broadcast title")
let help = BoolOption(shortFlag: "h", longFlag: "help", helpMessage: "Prints a help message.")
let verbosity = CounterOption(shortFlag: "v", longFlag: "verbose", helpMessage: "Print verbose messages.")
let stream = IntOption(shortFlag: "s", longFlag: "stream", helpMessage: "Stream Show by ID.")
cli.addOptions(channelNameSearch, titleSearch, stream, help, verbosity)
do {
try cli.parse()
} catch {
cli.printUsage(error)
exit(EX_USAGE)
}
let rawBroadcasts = NSURLSession.requestSynchronousJSONWithURLString("https://raw.githubusercontent.com/Cosmo/bashtv-service/master/BashTv.CommandLine/bin/Debug/br.json")
var broadcasts = [Broadcast]()
if let rawBroadcasts = rawBroadcasts as? [[String: AnyObject]] {
for rawBroadcast in rawBroadcasts {
let rawBroadcast = rawBroadcast as [String: AnyObject]
if let
id = rawBroadcast["id"] as? Int,
beginsAt = rawBroadcast["start"] as? Int,
endsAt = rawBroadcast["end"] as? Int,
duration = rawBroadcast["duration"] as? Int,
title = rawBroadcast["title"] as? String,
channel = rawBroadcast["channel"] as? String
{
if
beginsAt <= nowInSeconds
&&
(channelNameSearch.value == nil || (channelNameSearch.value != nil && channel.containsString(channelNameSearch.value!)))
&&
(titleSearch.value == nil || (titleSearch.value != nil && title.containsString(titleSearch.value!)))
{
let broadcast = Broadcast(id: id, beginsAt: beginsAt, endsAt: endsAt, duration: duration, title: title, channel: channel, videoURL: rawBroadcast["videoUrl"] as? String)
broadcasts.append(broadcast)
}
}
}
}
if let streamId = stream.value {
for broadcast in broadcasts where broadcast.id == streamId {
if let url = broadcast.videoURL {
Streamer.startStreaming(url)
} else {
print("Stream not available")
}
}
exit(0)
}
if let
linesString = "tput li".call(),
columnsString = "tput co".call(),
lines = linesString.digitsOnly(),
columns = columnsString.digitsOnly()
{
let indexWidth = 8
let timeWidth = 12
let channelNameWidth = 24
let broadcastTitleWidth = columns - timeWidth - channelNameWidth - indexWidth
let top = [
"┌".stringByPaddingToLength(indexWidth, withString: "─", startingAtIndex: 0),
"┬".stringByPaddingToLength(timeWidth + 2, withString: "─", startingAtIndex: 0),
"┬".stringByPaddingToLength(broadcastTitleWidth - 4, withString: "─", startingAtIndex: 0),
"┬".stringByPaddingToLength(channelNameWidth + 1, withString: "─", startingAtIndex: 0),
"┐"
].joinWithSeparator("")
print(top)
let header = [
"│ ",
"ID".stringByPaddingToLength(indexWidth - 2, withString: " ", startingAtIndex: 0).bold,
"│ ",
"Time".stringByPaddingToLength(timeWidth , withString: " ", startingAtIndex: 0).bold,
"│ ",
"Name".stringByPaddingToLength(broadcastTitleWidth - 6, withString: " ", startingAtIndex: 0).bold,
"│ ",
"Channel".stringByPaddingToLength(channelNameWidth - 1, withString: " ", startingAtIndex: 0).bold,
"│"
].joinWithSeparator("")
print(header)
let separator = [
"├".stringByPaddingToLength(indexWidth, withString: "─", startingAtIndex: 0),
"┼".stringByPaddingToLength(timeWidth + 2, withString: "─", startingAtIndex: 0),
"┼".stringByPaddingToLength(broadcastTitleWidth - 4, withString: "─", startingAtIndex: 0),
"┼".stringByPaddingToLength(channelNameWidth + 1, withString: "─", startingAtIndex: 0),
"┤"
].joinWithSeparator("")
print(separator)
let dateForm = NSDateFormatter()
dateForm.dateFormat = "dd.MM HH:mm"
for lineData in broadcasts {
var date = NSDate(timeIntervalSince1970: Double(lineData.beginsAt))
let dateString = dateForm.stringFromDate(date)
let line = [
"│ ",
(lineData.videoURL != nil ? String( lineData.id) : "--").stringByPaddingToLength(indexWidth - 2, withString: " ", startingAtIndex: 0),
"│ ",
String(dateString).stringByPaddingToLength(timeWidth, withString: " ", startingAtIndex: 0),
"│ ",
lineData.title.stringByPaddingToLength(broadcastTitleWidth - 6, withString: " ", startingAtIndex: 0),
"│ ",
lineData.channel.stringByPaddingToLength(channelNameWidth - 1, withString: " ", startingAtIndex: 0),
"│"
].joinWithSeparator("")
print(line)
}
let bottom = [
"└".stringByPaddingToLength(indexWidth, withString: "─", startingAtIndex: 0),
"┴".stringByPaddingToLength(timeWidth + 2, withString: "─", startingAtIndex: 0),
"┴".stringByPaddingToLength(broadcastTitleWidth - 4, withString: "─", startingAtIndex: 0),
"┴".stringByPaddingToLength(channelNameWidth + 1, withString: "─", startingAtIndex: 0),
"┘"
].joinWithSeparator("")
print(bottom)
}