mIRC Home    About    Download    Register    News    Help

Topic Options
#234415 - 24/10/11 04:15 AM Assistance with socket communication, please?
ThatGuy Offline
Ameglian cow

Registered: 25/08/11
Posts: 21
Loc: Waterloo, Ontario, Canada
Greetings, brothers and sisters.

I have been trying for a couple days to get an old script working. It's a Google translate script. I haven't used it for ages, and obviously Google has changed a little since then.

Code:
alias _trans {
  set %gt.lang $gettok($readini(languages\settings.ini,selected, [ $+ [ $ini(languages\settings.ini,selected,1) ] ] ),1,44)
  set %gt.phrase $1-
  if ($sock(trans)) {
    sockclose trans
  }
  sockopen trans translate.google.ca 80 
}


on *:sockopen:trans: {
  if ($sockerr) {
    echo -s 4Sockopen Error
    echo -s $chr(1) $+ 7Socket Name: $sockname
    echo -a $chr(1) $+ 7Error Code: $sockerr
    halt
  }
  else {
    echo -s 11on Sockopen
    sockwrite -n $sockname GET /?hl=en&tab=wT#en| [ $+ [ %gt.lang ] $+ ] | [ $+ [ %gt.phrase ] ]
    ; HTTP/1.1 <-I don't know if I need this on the end of the above line for it to work or not.  With it on, it says " [color:#CC0000] /HTTP/1.1: Not connected to server [/color] "
    sockwrite -n $sockname Host: translate.google.ca
    sockwrite -n $sockname User-Agent: Opera 9.6
  }
}


I'm using Firefox with the Firebug plugin to find the exact location of the translated text:
Code:
<span class="hps">(translated text appears here)</span>


I have looked at examples galore, and still can not figure out how to make the socket point at the correct piece of code.

Can someone please nudge me in the right direction...

Top
#234421 - 24/10/11 01:59 PM Re: Assistance with socket communication, please? [Re: ThatGuy]
ThatGuy Offline
Ameglian cow

Registered: 25/08/11
Posts: 21
Loc: Waterloo, Ontario, Canada
Hmm. That was slightly annoying. I refresh the page to see if there were any replies, and my post is disappeared. Good thing I know how to use the "My Stuff" menu to find my posts.

It would have been nice for the moderator to leave a little "moved to:" message as a reply, or even left a ghost post in the old forum for a few hours.

Top
#234422 - 24/10/11 02:12 PM Re: Assistance with socket communication, please? [Re: ThatGuy]
Riamus2 Offline
Planetary brain

Registered: 13/10/04
Posts: 8327
Loc: MA, USA
The forum software doesn't support ghost topics afaik. And leaving posts that just say "moved to" just fill up the forum with unnecessary topics. It's true that it can confuse some people when their post suddenly "disappears" on them, but the forum doesn't get enough new topics that it is all that difficult to find the moved topic. Also, moved topics are marked as unread even if you're read it in the previous forum, so it is even easier to find.

That said, maybe moderators can consider leaving a PM to the poster when moving a message to the appropriate forum? Just a thought. wink


As far as your question... you'll need a sockread event and just check if the line matches what you're looking for. As long as there is only one line that starts with <span class="hps">, you can check if the text read in using /sockread <span class="hps">* iswm %var and then do whatever you want with it. Any socket script can be used for examples of how to use sockread and check if the text matches something you're looking for.

Regarding HTTP/1.1, you need to include HTTP/1.0 or HTTP 1.1 in your script. Often, 1.0 works better than 1.1. However, your error about not being connected suggests that there is another problem. I can't test anything at work, but maybe someone else can take a look for you.
_________________________
Invision Support
#Invision on irc.irchighway.net

Top
#234423 - 24/10/11 03:08 PM Re: Assistance with socket communication, please? [Re: Riamus2]
ThatGuy Offline
Ameglian cow

Registered: 25/08/11
Posts: 21
Loc: Waterloo, Ontario, Canada
Greetings Riamus.

The original used
Code:
var %r
sockread %r
$replace($regsubex(%r,/[^*]*<div id=result_box dir="ltr">|</div>[^*]*/g,$null),&#39;,') 


Even though the line of code on the google site just above the line I am targeting is pretty much like the original, it can be narrowed down to just the resulting translation.

I tried to change it to
Code:
$regsubex(%r,/[^*]*<span class="hps">|</div>[^*]*/g,$null)

and various other forms with no luck. I haven't used "regex" or any of its siblings for any length of time, and I am unfamiliar with all the "/\/$/%\*" stuff I see in many regex codes.

When I get home I will try the "sockread iswm" thinger you suggested below.

Top
#234424 - 24/10/11 04:32 PM Re: Assistance with socket communication, please? [Re: ThatGuy]
Wims Offline
Planetary brain

Registered: 31/07/06
Posts: 3494
Loc: France
Here is something clearer that I made based on your code:
Code:
;encode the parameter passed to /_trans as required by http
alias -l urlencode return $replace($regsubex($1,/(\W)/g,% $+ $base($asc(\1),10,16,2)),$chr(32),+)
alias _trans {
  ;close the socket in case it's in use, open it, and mark it with the encoded version of the parameters
  sockclose trans
  sockopen trans translate.google.com 80
  sockmark trans $urlencode($$1-)
}
on *:sockopen:trans:{
  if ($sockerr) echo 4 -s * /_trans: sockopen error: $v1
  else {
    ;the GET request, that use what we originally stored in the mark
    sockwrite -n trans GET $+(/translate_a/t?client=t&text=,$sock(trans).mark,&sl=en&tl=fr) HTTP/1.0
    sockwrite trans Host: translate.google.com $str($crlf,2)
    ;delete the mark (used later for another purpose)
    sockmark trans
  }
}

on *:sockread:trans:{
  if ($sockerr) echo 4 -s * /_trans: sockread error: $v1
  ;read a line or what is inside the buffer
  var %a
  sockread -f %a
  ;we use the mark as a boolen value ($null/1) to know if we received the headers 
  if ($sock(trans).mark) {
    ;seperate the line according to comma
    tokenize 44 $remove(%a,],[,")
    echo -a Translation of $qt($2) is $qt($1)
  }
  ;if the mark is empty and if %a is $null, we received the headers
  elseif (!%a) sockmark trans 1
}
Languages are harcoded in the GET request, "&sl=en&tl=fr" means translate from english to french. Type /_trans <word or sentence here>, hope this helps.

Edit: I added some comments


Edited by Wims (24/10/11 04:46 PM)
_________________________
Looking for a good help channel about mIRC? Check #mircscripting @ irc.swiftirc.net

Top
#234425 - 24/10/11 04:40 PM Re: Assistance with socket communication, please? [Re: ThatGuy]
Riamus2 Offline
Planetary brain

Registered: 13/10/04
Posts: 8327
Loc: MA, USA
Regex can be useful when the information you're trying to obtain isn't necessarily straightforward. But if the only line in the source data that includes that text is the one you want, you can make it much easier for you to understand by just using an IF statement with iswm. Later on, you can always work on learning regex if you're interested.

Code:
if (<span class="hps">* iswm %r) { do something }


Note that you can also see about using echo to display your data to see what is going wrong...

Code:
echo -a Regex: %r -- $regsubex(%r,/[^*]*<span class="hps">|</div>[^*]*/g,$null)


Just be aware that this will display an echo for every line in the source data. You can use IF statements to limit how much is echoed if you want.

As far as this code goes, though... it appears to just remove the HTML around the specific line (but not any other lines). You could always consider using a standard $htmlfree() identifier to remove all HTML code instead of using specific regex for each line you want to access.

I think you must have something else in the sockread event that you displayed. It just looks like something is missing. As it is now, with those 3 lines, you're not doing anything to verify that you're using the right line.
_________________________
Invision Support
#Invision on irc.irchighway.net

Top
#234426 - 24/10/11 06:57 PM Re: Assistance with socket communication, please? [Re: ThatGuy]
Tomao Offline
Hoopy frood

Registered: 07/07/07
Posts: 1129
Loc: United States
Google used to have API for its translator, but they modified the terms of their service by saying, "These changes are being made due to the substantial economic burden caused by extensive abuse."

They have since made the API a paid service and restrict the allowance limit in translation one can make per day.

It would've been a lot easier to use API back then when it was free.

Top
#234432 - 25/10/11 02:39 AM Re: Assistance with socket communication, please? [Re: Riamus2]
ThatGuy Offline
Ameglian cow

Registered: 25/08/11
Posts: 21
Loc: Waterloo, Ontario, Canada
Originally Posted By: Riamus2
.....
I think you must have something else in the sockread event that you displayed. It just looks like something is missing. As it is now, with those 3 lines, you're not doing anything to verify that you're using the right line.
.....

Yes. I did not post all of the code I have. There were many lines that were ;commented out because I was trying various versions of the original code. I felt it would be counter productive to show the world my mistakes.

I will copy the examples posted by Wims, Tomao(Tomao didn't actualy give an example, just pointed out that the original was in deed created back when the API was free) and yourself. Maybe I can pick at the code-strings and unravel the knot in my brain. I will post my success (or failure, whatever the case turns out to be...).

THank you very much for your help!


Edited by ThatGuy (25/10/11 02:40 AM)

Top
#234436 - 25/10/11 11:11 AM Re: Assistance with socket communication, please? [Re: ThatGuy]
Riamus2 Offline
Planetary brain

Registered: 13/10/04
Posts: 8327
Loc: MA, USA
Wims' code does work if you want to just use that.
_________________________
Invision Support
#Invision on irc.irchighway.net

Top
#234454 - 25/10/11 09:56 PM Re: Assistance with socket communication, please? [Re: Wims]
ThatGuy Offline
Ameglian cow

Registered: 25/08/11
Posts: 21
Loc: Waterloo, Ontario, Canada
How would I change this to display other languages properly (utf-8?)? I am attempting to translate to and from Arabic, and other languages. It appears garbled in my status window.


I tried to post what it looks like on my end, but it appeared worse than what's in my status window x_x

Top
#234455 - 25/10/11 10:15 PM Re: Assistance with socket communication, please? [Re: ThatGuy]
Wims Offline
Planetary brain

Registered: 31/07/06
Posts: 3494
Loc: France
Change the /echo line to:
Code:
echo -a Translation of $qt($2) is $qt($regsubex($1,/\\u([^\\]+)/g,$chr($base(\1,16,10))))

_________________________
Looking for a good help channel about mIRC? Check #mircscripting @ irc.swiftirc.net

Top
#234458 - 26/10/11 04:56 AM Re: Assistance with socket communication, please? [Re: Wims]
ThatGuy Offline
Ameglian cow

Registered: 25/08/11
Posts: 21
Loc: Waterloo, Ontario, Canada
Changing that line didn't do anything.

This is what I have;
Code:
/*
I did have the language list here, but this forum doesn't like the alt+255 I used to keep the spacing.
*/
;~~~~~~~~~~~~~~~
alias -l urlencode {
  return $replace($regsubex($1,/(\W)/g,% $+ $base($asc(\1),10,16,2)),$chr(32),+)
}
alias _trans {
  if ($sock(trans)) {
    .sockclose trans
  }
  sockopen trans translate.google.ca 80
  sockmark trans $urlencode($$1-)
}
on *:sockopen:trans: {
  if ($sockerr) {
    echo 4 -s * /_trans: sockopen error: $v1
  }
  else {
    set %trans.lang $gettok($readini(languages\settings.ini,selected, [ $+ [ $ini(languages\settings.ini,selected,1) ] ] ),1,44)
    sockwrite -n trans GET $+(/translate_a/t?client=t&text=,$sock(trans).mark,&sl=en&tl= [ $+ [ %trans.lang ] ] ) HTTP/1.0
    sockwrite trans Host: translate.google.ca $str($crlf,2)
    sockmark trans
  }
}
on *:sockread:trans: {
  if ($sockerr) {
    echo 4 -s * /_trans: sockread error: $v1
  }
  var %a
  sockread -f %a
  if ($sock(trans).mark) {
    tokenize 44 $remove(%a,],[,")
    echo -a Translation of $qt($2) from English to $ini(languages\settings.ini,selected,1) is $qt($regsubex($1,/\\u([^\\]+)/g,$chr($base(\1,16,10))))
  }
  elseif (!%a) {
    sockmark trans 1
  }
}
;~~~~~~~~~~~~~~~
alias langfull {
  if ($1 == Auto) { return Auto }
  elseif ($1 == af) { return Afrikaans }
  elseif ($1 == sq) { return Albanian }
  elseif ($1 == ar) { return Arabic }
  elseif ($1 == hy) { return Armenian }
  elseif ($1 == az) { return Azerbaijani }
  elseif ($1 == eu) { return Basque }
  elseif ($1 == be) { return Belarusian }
  elseif ($1 == bn) { return Bengali }
  elseif ($1 == bg) { return Bulgarian }
  elseif ($1 == ca) { return Catalan }
  elseif ($1 == zh-TW) { return Chinese (Tradiditonal) }
  elseif ($1 == zh-CN) { return Chinese (Simplified) }
  elseif ($1 == hr) { return Croatian }
  elseif ($1 == cs) { return Czech }
  elseif ($1 == da) { return Danish }
  elseif ($1 == nl) { return Dutch }
  elseif ($1 == en) { return English }
  elseif ($1 == ct) { return Estonian }
  elseif ($1 == tl) { return Filipino }
  elseif ($1 == fi) { return Finnish }
  elseif ($1 == fr) { return French }
  elseif ($1 == gl) { return Galician }
  elseif ($1 == ka) { return Georgian }
  elseif ($1 == de) { return German }
  elseif ($1 == el) { return Greek }
  elseif ($1 == gu) { return Gujarati }
  elseif ($1 == ht) { return Hatian Creole }
  elseif ($1 == iw) { return Hebrew }
  elseif ($1 == hi) { return Hindi }
  elseif ($1 == hu) { return Hungarian }
  elseif ($1 == is) { return Icelandic }
  elseif ($1 == id) { return Indonesian }
  elseif ($1 == ga) { return Irish }
  elseif ($1 == it) { return Italian }
  elseif ($1 == ja) { return Japanese }
  elseif ($1 == kn) { return Kannada }
  elseif ($1 == ko) { return Korean }
  elseif ($1 == la) { return Latin }
  elseif ($1 == lv) { return Latvian }
  elseif ($1 == lt) { return Lithuanian }
  elseif ($1 == mk) { return Macedonian }
  elseif ($1 == ma) { return Malay }
  elseif ($1 == mt) { return Maltese }
  elseif ($1 == no) { return Norwegian }
  elseif ($1 == fa) { return Persian }
  elseif ($1 == pl) { return Polish }
  elseif ($1 == pt) { return Portuguese }
  elseif ($1 == ro) { return Romanian }
  elseif ($1 == ru) { return Russian }
  elseif ($1 == sr) { return Serbian }
  elseif ($1 == sk) { return Slovak }
  elseif ($1 == sl) { return Slovenian }
  elseif ($1 == es) { return Spanish }
  elseif ($1 == sw) { return Swahili }
  elseif ($1 == sv) { return Swedish }
  elseif ($1 == ta) { return Tamil }
  elseif ($1 == te) { return Teluga }
  elseif ($1 == th) { return Thai }
  elseif ($1 == tr) { return Turkish }
  elseif ($1 == uk) { return Ukrainian }
  elseif ($1 == ur) { return Urdu }
  elseif ($1 == vi) { return Vietnamese }
  elseif ($1 == cy) { return Welsh }
  elseif ($1 == yi) { return Yiddish }
} 
menu menubar {
  Language $+($chr(40),$ini(languages\settings.ini,selected,1),$chr(41))
  .Dialog: dialog -m lang lang
  .-
  .Select your language [1-32]
  ..Select your language [32-64]
  ...Irish: $w_lan(irish ga)
  ...Italian: $w_lan(italian it)
  ...Japanese: $w_lan(japanese ja)
  ...Kannada: $w_lan(kannada kn)
  ...Korean: $w_lan(korean ko)
  ...Latin: $w_lan(latin la)
  ...Latvian: $w_lan(latvian lv)
  ...Lithuanian: $w_lan(lithuanian lt)
  ...Macedonian: $w_lan(macedonian mk)
  ...Malay: $w_lan(malay ms)
  ...Maltese: $w_lan(maltese mt)
  ...Norwegian: $w_lan(norwegian no)
  ...Persian: $w_lan(persian fa)
  ...Polish: $w_lan(polish pl)
  ...Portuguese: $w_lan(portuguese pt)
  ...Romanian: $w_lan(romanian ro)
  ...Russian: $w_lan(russian ru)
  ...Serbian: $w_lan(serbian sr)
  ...Slovak: $w_lan(slovak sk)
  ...Slovenian: $w_lan(slovenian sl)
  ...Spanish: $w_lan(spanish es)
  ...Swahili: $w_lan(swahili sw)
  ...Swedish: $w_lan(swedish sv)
  ...Tamil: $w_lan(tamil ta)
  ...Telugu: $w_lan(telugu te)
  ...Thai: $w_lan(thai th)
  ...Turkish: $w_lan(turkish tr)
  ...Ukrainian: $w_lan(ukrainian uk)
  ...Urdu: $w_lan(urdu ur)
  ...Vietnamese: $w_lan(vietnamese vi)
  ...Welsh: $w_lan(welsh cy)
  ...Yiddish: $w_lan(yiddish yi)
  ..-
  ..Afrikaans: $w_lan(afrikaans af)
  ..Albanian: $w_lan(albanian sq)
  ..Arabic: $w_lan(arabic ar)
  ..Armenian: $w_lan(armenian hy)
  ..Azerbaijani: $w_lan(azerbaijani az)
  ..Basque: $w_lan(basque eu)
  ..Belarusian: $w_lan(belarusian be)
  ..Bengali: $w_lan(bengali bn)
  ..Bulgarian: $w_lan(bulgarian bg)
  ..Catalan: $w_lan(catalan ca)
  ..Chinese: (simple) $w_lan(chinese1 zh-CN)
  ..Chinese: (Traditional) $w_lan(chinese2 zh-TW)
  ..Croatian: $w_lan(croatian hr)
  ..Czech: $w_lan(czech cs)
  ..Danish: $w_lan(danish da)
  ..Dutch: $w_lan(dutch nl)
  ..English: $w_lan(english en)
  ..Estonian: $w_lan(estonian ct)
  ..Filipino: $w_lan(filipino tl)
  ..Finnish: $w_lan(finnish fi)
  ..French: $w_lan(french fr)
  ..Galician: $w_lan(galician gl)
  ..Georgian: $w_lan(georgian ka)
  ..German: $w_lan(german de)
  ..Greek: $w_lan(greek el)
  ..Gujarati: $w_lan(gujarati gu)
  ..Haitian: Creole $w_lan(haitian_creole ht)
  ..Hebrew: $w_lan(hebrew iw)
  ..Hindi: $w_lan(hindi hi)
  ..Hungarian: $w_lan(hungarian hu)
  ..Icelandic: $w_lan(icelandic is)
  ..Indonesian: $w_lan(indonesian id)

}
dialog lang {
  size -1 -1 100 110
  option dbu
  box "Languages", 1, 0 0 100 100
  list 2, 2 8 96 90, size
  button "Set", 3, 0 100 100 10
}
on *:dialog:*:*:*: {
  if ($dname == lang) {
    if ($devent == init) {
      dialog -t $dname Language Selector
      did -a $dname 1 Languages
      var %a 1
      while (%a <= $ini(languages\settings.ini,langs,0)) {
        did -a $dname 2 $ini(languages\settings.ini,langs,%a)
        inc %a
      }
      did -a $dname 3 Set
    }
    if ($devent == sclick) {
      if ($did == 3) {
        remini languages\settings.ini selected
        writeini languages\settings.ini selected $ini(languages\settings.ini,langs,$did(2).sel) $readini(languages\settings.ini,langs,$did(2).seltext)
        echo -a check!
      }
    }
  }
}
alias ff {
  if (!%ff. [ $+ [ $1- ] ]) || (!$exists(%ff. [ $+ [ $1- ] ])) {
    set %ff. [ $+ [ $1- ] ] $qt($longfn($findfile($nofile($mircexe),$1-,1)))
  }
  return %ff. [ $+ [ $1- ] ]
  unset %ff.*
}
alias w_lan {
  if ($ff(languages.ini) == $null) {
    writeini languages\settings.ini creation date $date(dddd $+ $chr(44) mmmm dd $+ $chr(44) yyyy)
    writeini languages\settings.ini creation time $time(hh:nn:ss TT)
    writeini languages\settings.ini default English en
  }
  remini languages\settings.ini selected $ini(languages\settings.ini,selected,1)
  writeini languages\settings.ini selected $1-
  ;translate this line...
  echo -s Language has been switched to $langfull($readini(languages\settings.ini,langs,selected, [ $+ [ $ini(languages\settings.ini,selected,1) ] ] ))
}

/*
need on input event to translate outgoing messages
need on text event to translate incoming messages

This is what I get as a translation.


Edited by ThatGuy (26/10/11 05:13 AM)

Top
#234465 - 26/10/11 09:09 PM Re: Assistance with socket communication, please? [Re: ThatGuy]
Wims Offline
Planetary brain

Registered: 31/07/06
Posts: 3494
Loc: France
Change the else part in the on sockopen event with:
Code:
  else {
    ;the GET request, that use what we originally stored in the mark
    var %s sockwrite -n trans,set %trans.lang $gettok($readini(languages\settings.ini,selected,$ini(languages\settings.ini,selected,1)),1,44)
    %s GET $+(/translate_a/t?client=t&text=,$sock(trans).mark,&hl=en&sl=en&tl=,%trans.lang,&multires=1&prev=btn&ssel=3&tsel=0&sc=1) HTTP/1.1
    %s Host: translate.google.ca
    %s Connection: close
    %s User-Agent: Mozilla/5.0
    %s
    ;delete the mark (used later for another purpose)
    sockmark trans
  }
_________________________
Looking for a good help channel about mIRC? Check #mircscripting @ irc.swiftirc.net

Top
#234473 - 27/10/11 05:28 AM Re: Assistance with socket communication, please? [Re: Wims]
ThatGuy Offline
Ameglian cow

Registered: 25/08/11
Posts: 21
Loc: Waterloo, Ontario, Canada
Originally Posted By: Wims

Code:
    var %s sockwrite -n trans,set %trans.lang $gettok($readini(languages\settings.ini,selected,$ini(languages\settings.ini,selected,1)),1,44)

gave me this in Status
Quote:
* /sockwrite: 'trans,set' no such socket (line 59, googletranslator.mrc)


I had to seperate the var and set each to their own lines.
Code:
var %s sockwrite -n trans
set %trans.lang $gettok($readini(languages\settings.ini,selected,$ini(languages\settings.ini,selected,1)),1,44)

This is what I get in Status:


And this is what Google has:


Thank you so much, Wims, Riamus and everyone else. May I include your name and link to your profile here in the credits section?


Edited by ThatGuy (27/10/11 05:33 AM)

Top
#234474 - 27/10/11 06:06 AM Re: Assistance with socket communication, please? [Re: ThatGuy]
Tomao Offline
Hoopy frood

Registered: 07/07/07
Posts: 1129
Loc: United States
I believe it should've been constructed like so:
Code:
var %s = sockwrite -n trans, %trans.lang = $gettok($readini(languages\settings.ini,selected,$ini(languages\settings.ini,selected,1)),1,44)
multiple local vars were supposed to be separated by commas. It might have been a coding typo.

Top
#234475 - 27/10/11 06:45 AM Re: Assistance with socket communication, please? [Re: Tomao]
ThatGuy Offline
Ameglian cow

Registered: 25/08/11
Posts: 21
Loc: Waterloo, Ontario, Canada
I tried to separate them like you suggest. It replied with a
Quote:
* /sockwrite: 'trans,' no such socket (line 59, googletranslator.mrc)
Putting them on their own lines worked great.

Top
#234476 - 27/10/11 12:12 PM Re: Assistance with socket communication, please? [Re: ThatGuy]
Wims Offline
Planetary brain

Registered: 31/07/06
Posts: 3494
Loc: France
It was a typo indeed, but the point is that you don't need to 'set' the variable, which make it a global variable (it exists until you unset it).
The line Tomao gave should work


Edited by Wims (27/10/11 12:15 PM)
_________________________
Looking for a good help channel about mIRC? Check #mircscripting @ irc.swiftirc.net

Top
#234477 - 27/10/11 01:54 PM Re: Assistance with socket communication, please? [Re: Wims]
ThatGuy Offline
Ameglian cow

Registered: 25/08/11
Posts: 21
Loc: Waterloo, Ontario, Canada
Fixed, and names added to credits. Thank you all very much again. Now I can get on with the rest of my project. I will very likely be back here to ask for help again.

Later Days!

Top