mTwitch.StateToTopic.mrc

By SReject on Sep 01, 2015

version: 0000.0000.0008

Moved to GitHub

I've moved this project to github in hopes of alleviating some of the headaches caused by maintaining 5+ pages for what is essentially the same project. I also hope the move will motivate others to get involved and contribute to the codebase

About

This is an mTwitch module that uses the channel topic to detail if a stream is online or not, the game being played, and the "status" text of the stream. Once you join a stream's chat, an initial request for the information is made, then every 5 minutes after the information is updated if it has changed.

The topic is formatted as:
Offline
Online since [time] -- Playing: [game] -- Title: [title]

    [time]: The time at which the stream started
    [game]: The game being played
    [title]: The stream's title/status text.

Code

on *:LOAD:{
  if (!$mTwitch.has.Core) {
    echo $color(info) -a [mTwitch->GroupChat] mTwitch.Core.mrc is required
    .timer 1 0 .unload -rs $qt($script)
  }
  else {
    scid -a mTwitch.StateToTopic
  }
}
on *:START:{
  if (!$mTwitch.has.Core) {
    echo $color(info) -a [mTwitch->GroupChat] mTwitch.Core.mrc is required
    .timer 1 0 .unload -rs $qt($script)
  }
}
on *:UNLOAD: .timermTwitch.StateToTopic off
on me:*:JOIN:#:if ($mTwitch.isServer) { .timermTwitch.StateToTopic.onJoin 1 5 scid -a mTwitch.StateToTopic }
alias mTwitch.StateToTopic {
  if ($isid || $status !== connected || !$mTwitch.isServer || $0) {
    return
  }

  .timermTwitch.StateToTopic -io 1 300 scid -a mTwitch.StateToTopic

  var %x = 1, %streams, %len, %y, %stream, %game, %status, %since, %topic, %chan

  while (%x <= $chan(0)) {
    %streams = $addtok(%streams, $mid($chan(%x), 2-), 44)
    if (!$calc($numtok(%streams,44) % 10)) || (%streams && %x == $chan(0)) {
      JSONOpen -u mTwitch.StateToTopic https://api.twitch.tv/kraken/streams?channel= $+ %streams
      if (!$JSONError) {
        %len = $JSON(mTwitch.StateToTopic, streams, length)
        %y = 0
        while (%len > %y) {
          %stream = $JSON(mTwitch.StateToTopic, streams, %y, channel, name)
          %chan = $chr(35) $+ %stream
          %topic = Online since $ConvertTime($JSON(mTwitch.StateToTopic, streams, %y, created_at)) -- Playing: $JSON(mTwitch.StateToTopic, streams, %y, game) -- Title: $JSON(mTwitch.StateToTopic, streams, %y, channel, status)
          if ($chan(%chan).topic != %topic) {
            .parseline -iqptu0 :jtv!jtv@jtv.twitch.tv TOPIC %chan : $+ %topic
          }
          %streams = $remtok(%streams, %stream, 1, 44)
          inc %y
        }
        tokenize 44 %streams
        %y = 0
        while (%y < $0) {
          inc %y
          %stream = $chr(35) $+ $($ $+ %y, 2)
          if (!$regex($chan(%stream).topic, /^Offline(?:(?: since [^-]+)?(-- Playing: .*)?)?$/i)) {
            .parseline -iqptu0 :jtv!jtv@jtv.twitch.tv TOPIC %stream : $+ Offline $regml(1)
          }
        }
      }
      JSONClose mTwitch.StateToTopic
      %streams = $null
    }
    inc %x
  }

}
alias -l ConvertTime {
  if ($regex($1-, /^(\d{4})-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)Z$/)) {
    return $asctime($calc($ctime($+($gettok(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec, $regml(2), 32) $ord($base($regml(3), 10, 10)), $chr(44) $regml(1) $regml(4), :, $regml(5), :, $regml(6))) + ( $time(z) * 3600)), mmm dd @ HH:nn:ss)
  }
}
alias mTwitch.has.StateToTopic 0000.0000.0008

Updates

v0.0.0008: More reworking of /mTwitch.StateToTopic
v0.0.0007: Switched to $chr(35) to get # literal
v0.0.0006: More reworking of the /mTwitch.StateToTopic command
v0.0.0005: Reworked if statement fix from v0.0.0003
v0.0.0004: Removed -n from /parseline; should fix the topic being reset when it shouldn't
v0.0.0003: Fixed bad if statement

Comments

Sign in to comment.
Paper0rPlastic   -  Oct 26, 2015

Slight error on line 43 makes it keep updating topic of live streams even if it hasn't changed. "#" should be "$chr(35)"

SReject  -  Oct 26, 2015

Yea, there's a few other things that I have fixed on my local version; will upload shortly.

Sign in to comment

SReject   -  Oct 23, 2015

Updates

v0.0.0005: Reworked if statement fix from v0.0.0003
v0.0.0004: Removed -n from /parseline; should fix the topic being reset when it shouldn't
v0.0.0003: Fixed bad if statement

 Respond  
SReject   -  Sep 01, 2015

Removed coloring/formating - Users can do this with on topic if they choose

Wolfie713  -  Sep 19, 2015

Line #83 has a couple of problems with it.
First, missing a closing parenthesis (aka ")" symbol) at the end of the line. It throws an error without it.
Second, should perform "$len(%topic) &&" just before, otherwise a blank topic will throw another error.

Next, line 86 should have a conditional check to see if the topic needs to be changed, otherwise it changes the topic even if it's the same thing. That results in repeated lines of the topic being changed even though it hasn't changed.

Other than that, nice work. Don't suppose you know of a way of having user lists grabbed and making it properly +v subscribers...?

SReject  -  Oct 23, 2015

Sorry for late reply; your comment didn't pop up in notifications. I'll get these issues fixed and updated shortly. Stay tuned!

As far as subscribers is concerned, there's no web endpoint to access such information without having each streamer give the script access to such information via an OAUTH token. This would have to happen for each stream any user of the script would join. This is not viable/possible.

With that said, I am working on a script that will, when a subscriber speaks in a stream, give the user +h(half oped); followers get +v

Sign in to comment

Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.