givesmehope socket

By jsg55 on Feb 03, 2010

my first atempt at a socket, grabs a random quote from givesmehope.com, hope i didnt mess too much stuff up. also i used the nohtml alias from a script i saw knoeki post on this site once. which would be found here http://www.hawkee.com/snippet/5880/ thanks knoeki, there was some gmh snippets posted in that link but i couldnt get any of them to work, and thanks for any constructive criticism from anyone since sockets are still a bit difficult at this point lol
UPDATE: got it working a lot faster then it was, so far its returning what it is supposed to and not just a blank line. working now to use the api random function. thanks for the help fordlawnmower & napa :)

on $*:TEXT:/^[!@.]gmh/Si:#:{
sockopen gmh givesmehope.com 80
if (@ isin $1) sockmark gmh msg #
elseif (! isin $1) || (. isin $1) sockmark gmh notice $nick
}
on *:sockopen:gmh: {
var %x $r(1,154)
sockwrite -nt $sockname GET /?page= $+ %x HTTP/1.1
sockwrite -n $sockname Host: $+(www.givesmehope.com,$str($crlf,2))
}
on *:sockread:gmh: {
var %temptext
sockread %temptext
if (summary: isin %temptext) { var %gmh $nohtml(%temptext) | $sock(gmh).mark *** [12GMH] $right($left($remove($remove($remove(%gmh, ),summary:),\), -2), -1) | sockclose gmh }
}
alias -l nohtml {
  var %x, %i = $regsub($1-,/(^[^<]*>|<[^>]*>|<[^>]*$)/g,$null,%x), %x = $remove(%x,&nbsp;)
  return %x
}

Comments

Sign in to comment.
napa182   -  Feb 05, 2010

FordLawnmower Said:

still think it makes no sense at all to generate a random number to get the GMH message from the top of the page when you can get a truly random GMH message from here -->> http://api.givesmehope.com/view/random

 on $*:text:/^([!@])gmh$/iS:#: { 
  if ($sock(gmh)) sockclose gmh 
  sockopen gmh api.givesmehope.com 80 
  sockmark gmh $iif($regml(1) == @,msg #,.notice $nick) 
}
on *:sockopen:gmh: { 
  sockwrite -nt $sockname GET /view/random HTTP/1.1 
  sockwrite -nt $sockname Host: $+(api.givesmehope.com,$str($crlf,2)) 
}
on *:sockread:gmh: {
  var %gmh | sockread %gmh
  if ($regex(%gmh,/<author>(.+)<\/author>/)) { sockmark gmh $addtok($sock(gmh).mark,$regml(1),32) }
  if ($regex(%gmh,/<date>(.+)<\/date>/)) { sockmark gmh $addtok($sock(gmh).mark,$regml(1),32) }
  if ($regex(%gmh,/<text><\S+(.+)\]\]><\/text>/)) { $sock(gmh).mark $replace($remove($regml(1),<b>,</b>,<BR>),&quot;,") | sockclose gmh }
}
@gmh <&Sick0> gabysm11 2009-09-02 I was 10 I went to a presentation in other country with the dance group from school. When we got back, my mom was supposed to pick me up but something came up and I had to wait hours. My dance teacher took me to her house and even made me dinner while my mom got her things sorted and could get there. She GMH.
!gmh -Sick0- Anon123 2009-06-29 family and I disowned my sister for her years of her drugs and alcohol abuse. I found out she was in a homeless shelter so I picked her up and took her in. She has been sober for a year, and helps my brother every day with his baby because his wife is terminally ill with cancer. Now, she is the one holding the family together. GMH
 Respond  
FordLawnmower   -  Feb 04, 2010

I still think it makes no sense at all to generate a random number to get the GMH message from the top of the page when you can get a truly random GMH message from here -->> http://api.givesmehope.com/view/random
There are 7 other messages on each page that this script can't possibly return. Making the script like this has really just reduced the whole site to 154 possible messages.
You might as well just use a text file ;/

 Respond  
napa182   -  Feb 04, 2010

jsg55 instead of using this

 on $*:TEXT:/^[!@.]gmh/Si:#:{
sockopen gmh givesmehope.com 80
if (@ isin $1) sockmark gmh msg #
elseif (! isin $1) || (. isin $1) sockmark gmh notice $nick
}
on *:sockopen:gmh: {
var %x $r(1,154)
sockwrite -nt $sockname GET /?page= $+ %x HTTP/1.1
sockwrite -n $sockname Host: $+(www.givesmehope.com,$str($crlf,2))
}

i would use this instead

 on $*:TEXT:/^([!.@])gmh$/iS:#:{
  if ($sock(gmh)) sockclose gmh
  sockopen gmh givesmehope.com 80
  sockmark gmh $r(1,154) $iif($istok(!|.,$regml(1),124),.notice $nick,msg #)
}
on *:sockopen:gmh: {
  sockwrite -nt $sockname GET $+(/?page=,$gettok($sock(gmh).mark,1,32)) HTTP/1.1
  sockwrite -n $sockname Host: $+(www.givesmehope.com,$str($crlf,2))
}

then on output to the chan or notice use

$gettok($sock(gmh).mark,2-,32)
 Respond  
FordLawnmower   -  Feb 03, 2010

@jsg55 If you got it to work way better, you should edit your post to reflect the changes.

 Respond  
jsg55   -  Feb 03, 2010

actually, thanks even more after trying out your suggestions lol got this working way better you guys are awesome

 Respond  
jsg55   -  Feb 03, 2010

ha yeah i guess it was a pretty lame first try but thanks a lot for the input both of you :)

 Respond  
FordLawnmower   -  Feb 03, 2010

You should really use the API with the random option for this -->> http://api.givesmehope.com/view/random <<-- Click this to see how simple.

HTTP/2.0?? What the $#@!? -- I always use 1.0 and if I have a problem I will change it to 1.1

Also using on sockclose to trigger the output to the channel/notice is a waste of time.

When you find the data you want in on sockread, just send it to channel/notice, close the socket, clear any variables and you are done.

On sockclose is triggered when the server/site you are connecting to realizes that you are done sending/receiving data and decides to disconnect. In some cases this can be more than a minute. I have no idea who started using on sockclose in scripts like this, but it is absolutely the wrong way to script this.

Good job for first try though jsg55 :) Probably looks better than my first one did.

Laughs @ Hope

 Respond  
napa182   -  Feb 03, 2010

ur socket don't work so well
it lags a lot and sometimes it will say something other times it will only say *** [GMH]

no need to use so many vars be them local or global.

try using sockmark more

i remember posting a gives me hope socket as a comment on this snippet
http://www.hawkee.com/snippet/5880/ i tested it and it still works

 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.