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,<,<,>,>,",", ,$chr(160),&,&,&#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
}
}
}
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.
(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/
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.
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,<,<,>,>,",", ,$chr(160),&,&,&#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!
}
}
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,<,<,>,>,",", ,$chr(160),&,&,&#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
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...
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
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
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/