Bay Cao và Bay Xa – Fly High and Fly Far

August 1, 2008

Javascript UrlEncode work well with PHP and Perl

Filed under: Perl, Programming — Tags: — doqkhanh @ 9:47 AM
    Javascript UrlEncode work well with PHP
    /**
    * \summary: http://kevin.vanzonneveld.net
    * \author:   Original by: Philip Peterson
    *              improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net), doqkhanh (http://quockhanh.info)
    * \example: urlencode('Kevin van Zonneveld!');
    * \returns: 'Kevin+van+Zonneveld%21'
    */
    function urlencode( str ) {
    var errorMessage = "Javascript error at urlencode() function in kingpot.js. Please contact GNT's site administrator.";
    var ret = str;
    try
    {
        ret = ret.toString();
        ret = encodeURIComponent(ret);
        ret = ret.replace(/%20/g, '+');
    }
    catch(err)
    {
        alert(errorMessage);
    }

    return ret;
    }

    PERL UrlEncode and UrlDecode
    sub urlencode
    {
        my $str = shift;
        $str =~ s/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg;    return $str;
    }    

    sub urldecode
    {
        my $str = shift;
        $str =~ s/%([A-Fa-f0-9]{2})/pack('C', hex($1))/seg;
        return $str;
    }

    Faster solution:
    $NeedEncodeingString =~ s/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg;
    $NeedDecodingString =~ s/\%([A-Fa-f0-9]{2})/pack('C', hex($1))/seg;

Invoke Amazon Web Services with Perl

Filed under: Perl, Programming — Tags: , — doqkhanh @ 9:16 AM

use LWP; # This library provides API to call Amazon Webservice

# These xml library provides API to paser XML from Amazon Webservice
use XML::Parser;
use XML::Simple;
use Data::Dumper;

# Create a user agent object
my $ua = LWP::UserAgent->new;
$ua->agent(“ApplicationName(c)YourCompany/Version 1.0 “);

#Create a request
my $SubscriptionId = “0MB1VZ********NYEQR2″;
my $request =  “http://webservices.amazon.co.jp/onca/xml?Service=AWSECommerceService&SubscriptionId=$SubscriptionId&Operation=ItemSearch&SearchIndex=Music&Keywords=$keyword&ResponseGroup=Medium,Tracks&Binding=CD&ReleaseDate=Latest”;

my $req = HTTP::Request->new(POST => $request);
$req->content_type(‘application/x-www-form-urlencoded’);
$req->content(‘query=libwww-perl&mode=dist’);

# Pass request to the user agent and get a response back
my $res = $ua->request($req);
my $ItemCollection;

my $dump_result; #This avariable using to unsderstance returned data structure

# Check the outcome of the response
if ($res->is_success)
{
#Get XML Page
$tmp = $res->content;

#Try paser by simple xml library
$ItemCollection = XMLin($tmp);

# Get data array structure
$dump_result = Dumper($ItemCollection);

#Try parse and get needed data
if($ItemCollection->{Items}->{TotalResults} ne 0)
{
if($ItemCollection->{Items}->{TotalResults} ne 1)    #if an array
{
$url = $ItemCollection->{Items}->{Item}->[0]->{ImageSets}->{ImageSet}->{MediumImage}->{URL};
}
else #if not an array
{
$url = $ItemCollection->{Items}->{Item}->{ImageSets}->{ImageSet}->{MediumImage}->{URL};
}

}

}

Test new Search Engine Result

Filed under: Perl, Programming — Tags: — doqkhanh @ 2:28 AM

Search result for “doqkhanh” keyword.
No 1: cuil.com 174
No 2:google.com 113
No 3:search.yahoo.com 65
No 4:live.com 57

Whew…

Wow. That was intense. Looking back at the first 48 hours since launch, it was quite an experience. After a lot of hard work, we were thrilled to begin offering our new approach to search. We were even more thrilled with the interest, and traffic, we received.

In fact, it was overwhelming—literally. While we had planned for a large number of searches on our first day, we hadn’t planned on more than 50 million. After all, that’s in the same ballpark as Microsoft’s Live Search and approaching Yahoo!. And they have a bit more infrastructure than our small start-up.

So for a good part of the first day, the traffic volume simply outstripped our ability to respond. Some machines failed. Some bugs were found. Some of our redundancies…weren’t so redundant. This meant some searches didn’t get the best results. Some didn’t get any.

And yet, for a lot of searches, Cuil did provide users with new results, different from the ones folks have gotten in the past, according to the reports we’ve received. This is one of our goals—to give people an alternative to existing approaches.

Thank you very much for the feedback. The emails we’ve gotten at feedback@cuil.com have been very helpful, telling us areas you enjoy—such as the layout and the search by category feature—and areas where we need to improve—image matching, for example. We read them, so please keep them coming.

At Cuil, we are tackling some of the big challenges in search, from finding ways to search more of the Web, to finding results by analyzing the content on a page, to providing images with our results to help you pick the page you want. These are difficult problems, and we know we have more work to do.

We are incredibly proud of what this small team of 30 employees has been able to build from scratch already, and we are committed to improving Cuil every day. Thank you for trying us out.

Tom, Anna and Russell

Blog at WordPress.com.