Reddit Latest Post & Comment Watcher

Platform:  mIRC
Published  Jul 02, 2012
Updated  Jul 05, 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.
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.
 Respond  
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;

Code

 
 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:

Quote

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

Code

 
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 :(

Code

 


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:

Quote

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

maria wrote:

Quote

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 '&lt;', if you can convert the text in the title containing &lt; 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:

Quote

<Jethro> Latest Comment: Somewhere to learn archery? - www.reddit.com/tb/vv0si by Nihiliste
<Jethro> 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.