Reddit Latest Post & Comment Watcher

By Jethro on Jul 02, 2012

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.

Edit: I've updated the script to include the latest post as requested by maria previously.

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
    }
  }
}

Comments

Sign in to comment.
dma   -  Nov 27, 2015

the functions still work but i see no reddit urls

never mind i got.. but does still work in 2015?

 Respond  
Menteroth   -  Oct 31, 2013

get this sometimes too:

  • Line too long: $bvar (line 43, redditsub.ini)
 Respond  
Menteroth   -  Oct 24, 2013

http://www.reddit.com/r/leagueoflegends/comments/1p4ss1/mcdonalds_to_have_burger_named_after_nip_next/

this title turned into this in the channel from the bot: McDonald\u2019s
rest of the title was in channel too, just showing what was messed up.

Menteroth  -  Oct 26, 2013

here is another:

in channel: "The match you spectate shouldn't end with \ - http://redd.it/1p9aen by TalkManEUW"

http://www.reddit.com/r/leagueoflegends/comments/1p9aen/the_match_you_spectate_shouldnt_end_with_victory/

Menteroth  -  Nov 02, 2013

This one completely removed the title because it was all inside qoutationmarks
http://www.reddit.com/r/leagueoflegends/comments/1prpya/league_of_legends_will_never_be_a_real_sport/

(mentbot) \ - http://redd.it/1prpya by sophusserup

sorry for all the posts. just wanted to show all the different ones.

Menteroth  -  Nov 18, 2013

(mentbot) Faker vs Apdo, \uce68\ucc29\ud558\uac8c \ud0ac\ub530\ub0b4\ub294 \ud398\uc774\ucee4 \ub9ac\ube10 - http://redd.it/1qw62o by Vomygore

http://www.reddit.com/r/leagueoflegends/comments/1qw62o/faker_vs_apdo_korean_solo_queue_gods_clash/

Menteroth  -  Nov 18, 2013

the latest two seem to happen when the title is a direct link to a YouTube video.

'cause "reddit uses a slightly-customized version of Markdown (http://daringfireball.net/projects/markdown/syntax) for formatting"
which enables people to use "reddit!" to make a word a link.

I'm guessing that's the reason anyway. if you wanna look into it.

Sign in to comment

Menteroth   -  Oct 07, 2013

Will you possibly add a trigger command for this?
Would be nice to turn on and off remotely.

 Respond  
IndyAdvant   -  Mar 25, 2013

Thank you so much for this wonderful script. My only reservation is that the bot will only announce reddit to one out of the 3 channels it is working on. Is there a way I could change it to work for all 3 channels?

Hoping to hear from you.

Menteroth  -  Oct 07, 2013

This would be nice too and perhaps able to set different intervals for each channel.

Sign in to comment

Jethro   -  Jul 04, 2012

You're welcome and happy forth of July. :-) Please offer me a like for my work. =P

 Respond  
maria   -  Jul 04, 2012

too late. fixed everything. lol....

jethro, thank you for starting this project and creating something useful, without your help i wouldn't have been able to finish this off myself.

here's a Sub-Reddit Post .JSON Feed to Channel Script;

alias -l reddit {
  hadd -m reddit callback msg $1
  hadd reddit attr.count 0
  hdel reddit rcvd
  var %sockname = reddit2
  if ($sock(%sockname)) sockclose $v1
  sockopen %sockname www.reddit.com 80
}
alias -l trans {
  return $replace($1,&lt;,<,&gt;,>,&quot;,",&nbsp;,$chr(160),&amp;,&,&amp;#39;,')
}

on *:sockopen:reddit2:{
  if ($sockerr) { 
    echo 4 -ag $gettok($sock(reddit).mark,2,32) Error Connecting to $sock(reddit).addr
  }
  var %reddit2 = sockwrite -nt reddit2
  %reddit2 GET $+(/r/,%reddits,/new.json?sort=new&limit=1,$iif($hget(reddit,id),&after= $+ $v1)) HTTP/1.0-
  %reddit2 Cache-Control: max-age=0
  %reddit2 Connection: close
  %reddit2 User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0)
  %reddit2 Host: $+($sock($sockname).addr,$str($crlf,2))
}

on *:sockread:reddit2:{
  if ($sockerr) { 
    echo 4 $gettok($sock(reddit).mark,2,32) Error Reading $sock(reddit).addr
    return
  }
  sockread &reddit2
  noop $hget(reddit,rcvd,&rcvd)
  bcopy &rcvd $calc($bvar(&rcvd,0) + 1) &reddit2 1 -1
  hadd -b reddit rcvd &rcvd
}

on *:sockclose:reddit2:{
  reddit.finished
}

alias reddit.finished {
  noop $hget(reddit,rcvd,&rcvd)

  var %g2 = $bvar(&rcvd,1-).text
  if ($regex(%g2,/"title"\: "(.*)"/)) {
    hadd -m reddit title $gettok($trans($regml(1)),1,34)
    hinc reddit attr.count
  }
  if ($regex(%g2,/"id"\: "(.*)"/)) {
    hadd -m reddit status old
    if ($hget(reddit,id) != $gettok($trans($regml(1)),1,34)) {
      hadd -m reddit id $v2
      hadd -m reddit status new
      hinc reddit attr.count
    }
  }
  if ($regex(%g2,/"author"\: "(.*)"/)) { 
    hadd -m reddit author $gettok($regml(1),1,34)
    hinc reddit attr.count
  }

  if ($hget(reddit,status) == new) && ($hget(reddit,attr.count) == 3) {
    $hget(reddit,callback) $&
      $chr(29) $+ $hget(reddit,title) $+ $chr(29) - $+(http://redd.it/,$hget(reddit,id)) by 12 $+ $hget(reddit,author)
  }
}

on me:*:join:#reddit:{
  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
    }
  }
}

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!
  }
}
 Respond  
Jethro   -  Jul 03, 2012

I'll see what I can do for you later and show you the working script the way you want it.

 Respond  
maria   -  Jul 03, 2012

Jethro wrote:

Well, it's expected to get the result repeated if there's no new post made...
that creates a tiny little problem... if you have it checking the site every 30 seconds seconds for new posts it will just keep repeating the same already mentioned post if no new one is created. i tried creating a variable using

set %norepost $+($hget(data4,1).item)

and checking against it before messaging the channel. unfortunately i couldn't make this work. any ideas on how to check if you've already used a post before proceeding with the action?

this is what i have so far, but it stops displaying new posts to the channel :(

alias -l reddit {
  var %sockname = reddit2
  if ($sock(%sockname)) sockclose $v1
  sockopen %sockname www.reddit.com 80
  sockmark %sockname msg $1
  inc %i
}
alias -l trans {
  return $replace($1,&lt;,<,&gt;,>,&quot;,",&nbsp;,$chr(160),&amp;,&,&amp;#39;,')
}

on *:sockopen:reddit2:{
  if ($sockerr) { 
    echo 4 -ag $gettok($sock(reddit).mark,2,32) Error Connecting to $sock(reddit).addr
  }
  var %reddit2 = sockwrite -nt reddit2
  %reddit2 GET $+(/r/,%reddits,/new.json?sort=new,$iif($hget(reddit,id),&after= $+ $v1)) HTTP/1.0
  %reddit2 Connection: close
  %reddit2 Host: $+($sock($sockname).addr,$str($crlf,2))
}

on *:sockread:reddit2:{
  if ($sockerr) { 
    echo 4 $gettok($sock(reddit).mark,2,32) Error Reading $sock(reddit).addr
    return
  }
  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 reddit status old
    if ($hget(reddit,id) != $gettok($trans($regml(1)),1,34)) {
      hadd -m reddit id $v2
      hadd -m reddit status new
      hadd -m data4 $+($gettok($+(http://redd.it/,$&
        $trans($regml(1))),1,34))
    }
  }
  if ($regex(%g2,/"author"\: "(.*)"/)) { 
    hadd -m data5 $+($chr(3),12,$gettok($regml(1),1,34))
    reddit.finished
    sockclose $sockname
  }
}

on *:sockclose:reddit2:{
  reddit.finished
}

alias reddit.finished {
  if ($hget(reddit,status) == new) && ($hget(data3)) && ($hget(data4)) {
    $sock($sockname).mark $&
      $+($chr(29),$hget(data3,1).item,$chr(32),$hget(data3,1).data,$chr(29),$chr(32),$chr(2),-,$chr(2)) $&
      $hget(data4,1).item by $hget(data5,1).item 
    hfree -w data*
  }
}

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
    }
  }
}

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!
  }
}

seriously, been cracking at this all day and i can't figure out a way to stop messaging posts that it has already mentioned. it's driving me craaaaaaaaaaaazy

 Respond  
Jethro   -  Jul 03, 2012

maria wrote:> .json contain characters such as '<' it will reply with html code like '<', if you can convert the text in the title containing < to <I've fixed this matter. Apply to the latest update when you can.

maria wrote:> it keeps repeating the same last post/comment if no new commet/post is made.Well, it's expected to get the result repeated if there's no new post made...

 Respond  
RIcko   -  Jul 03, 2012

This works fine, @maria if you dont want it to repeat the same post then you shouldn't ask for the latest ones, make it random its better.

 Respond  
maria   -  Jul 03, 2012

amazing, but one small problem though. it keeps repeating the same last post/comment if no new commet/post is made.

also found another small bug... when titles from .json contain characters such as '<' it will reply with html code like '<', if you can convert the text in the title containing < to < i can learn from that and update the html code as they come by

 Respond  
Jethro   -  Jul 02, 2012

Ok, I've updated the script to include the latest post too. The output will come out like so once activated:> Latest Comment: Somewhere to learn archery? - www.reddit.com/tb/vv0si by Nihiliste

**Latest Post:** Playboy names The Draught House one of the country's best beer bars - http://redd.it/vyi5f by Dirtydog554
 Respond  
maria   -  Jul 02, 2012

is there a way to have grab the latest post instead of the latest comment reply?

 Respond  
Jethro   -  Jul 02, 2012

Yes, they do have API, but the main site's source gives me the upper hand to fetch the info I'm after.

 Respond  
Hawkee   -  Jul 02, 2012

Does reddit have an API? Seems like it'd be a more reliable way to gather this information.

 Respond  
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.