-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmTwitch.StateToTopic.mrc
176 lines (169 loc) · 5.7 KB
/
mTwitch.StateToTopic.mrc
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
on *:LOAD:{
if (!$mTwitch.has.Core) {
echo $color(info) -a [mTwitch->StateToTopic] mTwitch.Core.mrc is required
.unload -rs $qt($script)
}
elseif (1.?* !iswm $JSONVersion(short)) {
echo $color(info) -a [mTwitch->StateToTopic] JSONForMirc v1.x is required
.unload -rs $qt($script)
}
else {
mTwitch.StateToTopic
}
}
on *:START:{
if (!$mTwitch.has.Core) {
echo $color(info) -a [mTwitch->StateToTopic] mTwitch.Core.mrc is required
.unload -rs $qt($script)
}
elseif (1.?* !iswm $JSONVersion(short)) {
echo $color(info) -a [mTwitch->StateToTopic] JSONForMirc v1.x is required
.unload -rs $qt($script)
}
else {
mTwitch.StateToTopic
}
}
on *:UNLOAD: {
.timermTwitch.StateToTopic off
}
on me:*:JOIN:#:{
if ($mTwitch.isServer) {
if ($hget(mTwitch.StreamState, # $+ .ChanTopic)) {
parseline -itqpn :jtv!jtv@twitch.tv TOPIC # : $+ $v1
}
else {
.timermTwitch.StateToTopic.onJoin 1 5 mTwitch.StateToTopic
}
}
}
on *:SIGNAL:/^mTwitch\.(ChatState\.(Un)?(Slow|R9K|FollowersOnly|SubsOnly))|((Un)Host)$/i:{
mTwitch.StateToTopic.Set $1
}
alias mTwitch.has.StateToTopic {
return 0000.0000.0012
}
alias mTwitch.StateToTopic {
if ($isid || $0) {
return
}
.timermTwitch.StateToTopic -io 1 300 mTwitch.StateToTopic
hmake mTwitch.StateToTopic 1
var %Table = mTwitch.StateToTopic
var %ConnIndex = 0
var %ConnLength = $scon(0)
while (%ConnIndex < %ConnLength) {
inc %ConnIndex
scon %ConnIndex
if ($status == connected && $mTwitch.isServer && $chan(0)) {
var %ChanLength = $v1
var %ChanIndex = 0
var %Streams
while (%chanIndex < %ChanLength) {
inc %ChanIndex
var %Chan = $lower($chan(%ChanIndex))
if ($me ison %chan && !$hget(%Table, %Chan)) {
hadd %table %Chan -
%Streams = $addtok(%Streams, $mTwitch.UrlEncode($lower($iif(#* iswm %Chan, $mid(%Chan, 2-), %Chan))), 44)
if ($len(%Streams) >= 2500 || (%ConnIndex == %ConnLength && %ChanIndex == %ChanLength) || !$calc($numtok(%Streams, 44) % 100)) {
JSONOpen -uw mTwitch.StateToTopic https://api.twitch.tv/kraken/streams?limit=100&channel= $+ %Streams
JSONHttpHeader mTwitch.StateToTopic Client-ID e8e68mu4x2sxsewuw6w82wpfuyprrdx
JSONHttpFetch mTwitch.StateToTopic
%Streams = $null
if (!$JSONError && $JSON(mTwitch.StateToTopic).HttpStatus == 200) {
noop $JSONForEach($JSON(mTwitch.StateToTopic, streams), mTwitch.StateToTopic.UpdateStreamState)
}
JSONClose mTwitch.StateToTopic
}
}
}
}
}
noop $hfind(%Table, *, 0, w, mTwitch.StateToTopic.Set $1-)
hfree %Table
}
alias mTwitch.StateToTopic.UpdateStreamState {
var %chan = $chr(35) $+ $JSON($1, channel, name).value
var %online = $hget(mTwitch.StreamState, %chan $+ . $+ StreamOnline)
var %hadd = hadd -m mTwitch.StreamState %chan $+ .
%hadd $+ StreamOnline $true
%hadd $+ StreamStart $mTwitch.ConvertTime($JSON($1, created_at).value)
%hadd $+ StreamGame $JSON($1, game).value
%hadd $+ StreamTitle $JSON($1, channel, status).value
%hadd $+ StreamIsMature $JSON($1, channel, mature).value
if (!%Online) {
.signal mTwitch.StreamOnline $mid(%chan, 2-)
}
hadd -m mTwitch.StateToTopic %chan $true
}
alias -l mTwitch.StateToTopic.Set {
var %item = $lower(#$1), %Table = mTwitch.StreamState, %State, %Game, %Title, %Mature, %Start, %FolOnly, %SubOnly, %Slow, %R9K, %Topic
if (!$hfind(%Table, %item $+ .*, 1, w)) {
%Topic = 04Offline
}
else {
if ($hget(mTwitch.StateToTopic, %item) == -) {
if ($hget(%Table, %item $+ .StreamOnline)) {
.signal mTwitch.StreamOffline $mid(%item, 2-)
}
%State = Offline
hadd -m %Table %item $+ .StreamOnline $false
}
%State = $iif($hget(%table, %Item $+ .StreamOnline) , Online, Offline)
%Start = $hget(%Table, %Item $+ .StreamStart)
%Game = $hget(%Table, %Item $+ .StreamGame)
%Title = $hget(%Table, %Item $+ .StreamTitle)
%Mature = $hget(%Table, %Item $+ .StreamIsMature)
%FolOnly = $hget(%Table, %Item $+ .ChatFollowersOnly)
%SubOnly = $hget(%Table, %Item $+ .ChatSubOnly)
%Slow = $hget(%Table, %Item $+ .ChatSlow)
%R9k = $hget(%Table, %Item $+ .ChatR9K)
;; Online/Offline status
if (%State == Online) {
%Topic = $+($chr(3), 12Online since $chr(15), $asctime(%Start, ddd mm HH:nn))
}
else {
%Topic = $+($chr(3), 04Offline, $chr(15))
}
;; game title and mature
if (%Game) {
%Topic = %Topic $+($chr(3), 12Playing, $chr(15), :) %Game
}
if (%Title) {
%Topic = %Topic $+($chr(3), 12Title, $chr(15), :) %Title
}
if (%Mature) {
%Topic = %Topic [Mature]
}
;; Followers-only chat mode
if (%FolOnly !== $null) {
if (%FolOnly == 0) {
%Topic = %Topic [Followers Only]
}
elseif (%FolOnly !== -1) {
%Topic = %Topic [Followers Only: $+ %FolOnly $+ ]
}
}
;; Sub-only chat mode
if (%SubObly) {
%Topic = %Topic [Subs Only]
}
;; Slow chat mode
if (%Slow) {
%Topic = %Topic [Slow: $+ %Slow]
}
;; R9K mode
if (%R9k) {
%Topic = %Topic [R9K]
}
}
if (%Topic !== $hget(mTwitch.StreamState, %item $+ .ChanTopic)) {
hadd -m mTwitch.StreamState $+(%item, .ChanTopic) %Topic
scon -at1 mTwitch.StateToTopic.Update $1 $unsafe(%Topic)
}
}
alias -l mTwitch.StateToTopic.Update {
if ($mTwitch.isServer && $me ison $1 && $chan($1).topic !== $2-) {
.parseline -iqtn :jtv!jtv@twitch.tv TOPIC $1 : $+ $2-
}
}