Jump to content
chain

Reddit Latest Post & Comment Watcher

Recommended Posts

  • Administrators

This is a forum request made by maria who wishes to have the latest comment checked via reddit.com. The script will then send the info to a channel with its title, short link and by whom. I thought I'd make this a snippet submission to benefit those who find it useful or usable.

Usage: right-click on your channel or nick list to toggle "Reddit On" and "Reddit Off" to get started.

 

alias -l reddit {
  var %i = 1, %socks = reddit reddit2
  while ($gettok(%socks,%i,32)) {
    var %v1 = $v1
    if ($sock(%v1)) sockclose $v1
    sockopen %v1 www.reddit.com 80
    sockmark %v1 msg $1
    inc %i
  }
}
alias -l trans {
  return $replace($1,&lt;,<,&gt;,>,&quot;,",&nbsp;,$chr(160),&amp;,&,&amp;#39;,')
}
on *:sockclose:reddit*:{
  if ($sock($sockname).name == reddit) {
    if ($hget(data2)) && ($hget(data3)) && ($hget(data4)) {
      $sock($sockname).mark $+($chr(2),Latest Comment:,$chr(2)) $&
        [ [ $remove($+($hget(data2,1).item,$chr(32),$hget(data2,1).data),r/) ] ]
      $sock($sockname).mark $+($chr(2),Latest Post:,$chr(2)) $&
        $+($hget(data3,1).item,$chr(32),$hget(data3,1).data) - $&
        $hget(data4,1).item by $hget(data5,1).item 
      hfree -w data*
    }
  }
  else { 
    if ($sock(reddit2)) sockclose $v1
    sockopen reddit2 www.reddit.com 80
    sockmark reddit2 msg $1
  }
}
menu channel,nicklist {
  Reddit
  .ON {
    if (!%reddits) {
      set -e %reddits $$?"Which Reddit to Watch?"
      set -e %reddittime $$?"How often do you want the latest post checked $&
        and sent to the channel in seconds?"
      $+(.timer,#,$network) 0 %reddittime reddit #
      echo 2 # * Reddit Set for: $+($chr(2),%reddits)
      echo 2 # * Time Interval in Seconds: $+($chr(2),%reddittime)
      if (%true) unset $v1
    }
    else echo 4 # * Reddit is Already Switched On!
  }
  .OFF { 
    if (%reddits) {
      $+(.timer,#,$network) off 
      echo 2 * Reddit Has Been Turned Off!
      unset %reddit* %true
    }
    else echo 4 # * Reddit is Already Switched Off!
  }
}
on *:sockopen:reddit*:{
  if ($sockerr) { 
    echo 4 $gettok($sock(reddit).mark,2,32) Error Connecting to $sock(reddit).addr
    sockclose reddit
  }
  if ($sock($sockname).name == reddit) {
    var %reddit = sockwrite -nt reddit
    %reddit GET $+(/r/,%reddits,/comments?limit=1) HTTP/1.0
    %reddit Connection: close
    %reddit Host: $+($sock(reddit).addr,$str($crlf,2))
  }
  else {
    var %reddit2 = sockwrite -nt reddit2
    %reddit2 GET $+(/r/,%reddits,/new.json?sort=new) HTTP/1.0
    %reddit2 Connection: close
    %reddit2 Host: $+($sock(reddit).addr,$str($crlf,2))
  }
}
on *:sockread:reddit*:{
  if ($sockerr) { 
    echo 4 $gettok($sock(reddit).mark,2,32) Error Reading $sock(reddit).addr
    sockclose reddit
  }
  if ($sock($sockname).name == reddit) {
    sockread &reddit
    var %g = $regsubex($bvar(&reddit,1-).text,/\/r\/|&#\d+;/g,)
    if ($regex(%g,/<\/div><\/form><ul class="flat-list buttons">(.*)/)) {
      hadd -m data $+($sock(reddit).addr,/tb/,$gettok($iif($regex($regml(1),$&
        /\/comments\/(\S+)\//),$trans($regml(1))),1,47))
    }
    if ($regex(%g,/class="title" rel="nofollow" >(.*)class="author/)) {
      hadd -m data2 $gettok($trans($regml(1)),1,60) - $&
        $!+($chr(3),12,$hget(data,1).item,$chr(3)) by $+($chr(3),05,$&
        $iif($regex($regml(1),/\/user\/(\S+)"/),$regml(1)))
    }
  }
  else {
    sockread &reddit2
    var %g2 = $bvar(&reddit2,1-).text
    if ($regex(%g2,/"title"\: "(.*)"/)) {
      hadd -m data3 $gettok($trans($regml(1)),1,34) 
    }
    if ($regex(%g2,/"id"\: "(.*)"/)) {
      hadd -m data4 $+($chr(3),12,$gettok($+(http://redd.it/,$&
        $trans($regml(1))),1,34),$chr(3))
      sockclose $sockname
    }
    if ($regex(%g2,/"author"\: "(.*)"/)) { 
      hadd -m data5 $+($chr(3),05,$gettok($regml(1),1,34))
    }
  }
}
on me:*:join:#:{
  if (%true) notice $me Reddit has been toggled off. Right-click on $&
    the nicklist or channel to toggle it on. | unset %true
}
on *:disconnect: if (%reddits) $+(.timer,#,$network) off | set -e %true 1
on me:*:part:#: if (%reddits) $+(.timer,#,$network) off | set -e %true 1
on *:kick:#:{
  if ($knick == $me) && (%reddits) $+(.timer,#,$network) off
  set -e %true 1
}
on *:quit:{
  if ($nick == $me) && (%reddits) {
    var %c = 1
    while ($comchan($me,%c)) {
      $+(.timer,$v1,$network) off
      set -e %true 1
      inc %c
    }
  }
}

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...


×
×
  • Create New...