{"id":33,"date":"2003-03-23T15:19:01","date_gmt":"2003-03-23T13:19:01","guid":{"rendered":"http:\/\/frenchfragfactory.net\/ozh\/archives\/2003\/03\/23\/perl-google-client\/"},"modified":"2007-05-08T13:59:31","modified_gmt":"2007-05-08T11:59:31","slug":"perl-google-client","status":"publish","type":"post","link":"https:\/\/planetozh.com\/blog\/2003\/03\/perl-google-client\/","title":{"rendered":"Perl google client"},"content":{"rendered":"<p>This script queries Google and returns the first result (the one you&#39;d get hitting the &quot;I Feel Lucky&quot; button). I did it because I wanted not to use LWP::Simple on a computer where the lib was missing. Consider this as is my minimalistic web client example.<br \/>\n(the fancy stuff that might confuse you, as &quot;print NOTICE&quot;, are for a PsyBNC integration. Ignore them)<\/p>\n<div id=\"ig-sh-1\" class=\"syntax_hilite\">\n\n\t\t<div class=\"toolbar\">\n\n\t\t<div class=\"view-different-container\">\n\t\t\t\t\t\t<a href=\"#\" class=\"view-different\">&lt; View <span>plain text<\/span> &gt;<\/a>\n\t\t\t\t\t<\/div>\n\n\t\t<div class=\"language-name\">perl<\/div>\n\n\t\t\n\t\t<br clear=\"both\">\n\n\t<\/div>\n\t\n\t<div class=\"code\">\n\t\t<ol class=\"perl\" style=\"font-family:monospace\"><li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #666666;font-style: italic\">#!\/usr\/bin\/perl<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #000000;font-weight: bold\">use<\/span> IO<span style=\"color: #339933\">::<\/span><span style=\"color: #006600\">Socket<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #0000ff\">$nick<\/span> <span style=\"color: #339933\">=<\/span> <span style=\"color: #000066\">shift<\/span> <span style=\"color: #009900\">&#040;<\/span><span style=\"color: #339933\">@<\/span><span style=\"color: #000000;font-weight: bold\">ARGV<\/span><span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #b1b100\">if<\/span> <span style=\"color: #009900\">&#040;<\/span><span style=\"color: #0000ff\">$#ARGV<\/span> <span style=\"color: #339933\">&lt;<\/span> <span style=\"color: #cc66cc\">0<\/span> <span style=\"color: #009900\">&#041;<\/span> <span style=\"color: #009900\">&#123;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #000066\">print<\/span> <span style=\"color: #ff0000\">&quot;:g00gle NOTICE $nick :Err&quot;<\/span>\u00a6<span style=\"color: #339933\">.<\/span> google what ffs m8 <span style=\"color: #339933\">?<\/span><span style=\"color: #0000ff\">\\n<\/span><span style=\"color: #ff0000\">&quot; ;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #ff0000\">exit;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #ff0000\">}<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #ff0000\">$host = &quot;<\/span>www<span style=\"color: #339933\">.<\/span>google<span style=\"color: #339933\">.<\/span>com<span style=\"color: #ff0000\">&quot;;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #ff0000\">$document = &quot;<\/span><span style=\"color: #339933\">\/<\/span>search<span style=\"color: #339933\">?<\/span>btnI<span style=\"color: #339933\">=<\/span>\\<span style=\"color: #339933\">&amp;<\/span><span style=\"color: #000066\">q<\/span><span style=\"color: #339933\">=<\/span><span style=\"color: #ff0000\">&quot; . join (&quot;<\/span><span style=\"color: #339933\">+<\/span><span style=\"color: #ff0000\">&quot;, @ARGV);<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #ff0000\">$kikoo=0;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #ff0000\">$EOL = &quot;<\/span>\\015\\012<span style=\"color: #ff0000\">&quot;;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #ff0000\">$BLANK = $EOL x 2;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #ff0000\">$remote = IO::Socket::INET-&gt;new(<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #ff0000\">Proto =&gt; &quot;<\/span>tcp<span style=\"color: #ff0000\">&quot;,<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #ff0000\">PeerAddr =&gt; $host,<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #ff0000\">PeerPort =&gt; &quot;<\/span>http<span style=\"color: #009900\">&#040;<\/span><span style=\"color: #cc66cc\">80<\/span><span style=\"color: #009900\">&#041;<\/span><span style=\"color: #ff0000\">&quot;,<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #ff0000\">);<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #ff0000\">unless ($remote) { die &quot;<\/span>cannot <span style=\"color: #000066\">connect<\/span> to http daemon on <span style=\"color: #0000ff\">$host<\/span><span style=\"color: #ff0000\">&quot;; }<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #ff0000\">$remote-&gt;autoflush(1);<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #ff0000\">print $remote &quot;<\/span>GET <span style=\"color: #0000ff\">$document<\/span> HTTP<span style=\"color: #339933\">\/<\/span><span style=\"color: #cc66cc\">1.0<\/span><span style=\"color: #ff0000\">&quot; . $BLANK;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #ff0000\">while ( &lt;$remote&gt; ) {<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #ff0000\">if (m\/^location\/i ) {<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #ff0000\">$tutu = $_ ;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #ff0000\">$tutu=~s\/^location: \/\/i;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #ff0000\">print &quot;<\/span><span style=\"color: #339933\">:<\/span>g00gle NOTICE <span style=\"color: #0000ff\">$nick<\/span> <span style=\"color: #339933\">:<\/span><span style=\"color: #0000ff\">$tutu<\/span><span style=\"color: #ff0000\">&quot;; $kikoo = 1;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #ff0000\">} ;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #ff0000\">}<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #ff0000\">close $remote;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #ff0000\">print &quot;<\/span><span style=\"color: #339933\">:<\/span>g00gle NOTICE <span style=\"color: #0000ff\">$nick<\/span> <span style=\"color: #339933\">:<\/span>No result ffs m8 <span style=\"color: #339933\">!<\/span><span style=\"color: #0000ff\">\\n<\/span><span style=\"color: #ff0000\">&quot; unless $kikoo;<\/span><\/div><\/li>\n<\/ol>\t<\/div>\n\n<\/div>\n\n","protected":false},"excerpt":{"rendered":"<p>Simple web client for google querying<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[2,42,41,9],"class_list":["post-33","post","type-post","status-publish","format-standard","hentry","tag-code","tag-google","tag-irc","tag-perl"],"_links":{"self":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/posts\/33","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/comments?post=33"}],"version-history":[{"count":0,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/posts\/33\/revisions"}],"wp:attachment":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/media?parent=33"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/categories?post=33"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/tags?post=33"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}