Skip to content

Instantly share code, notes, and snippets.

@onteria
Created November 6, 2012 03:13
Show Gist options
  • Save onteria/4022323 to your computer and use it in GitHub Desktop.
Save onteria/4022323 to your computer and use it in GitHub Desktop.
Google画像検索URIジェネレイター
  • UTF-8 On

Python 3.1

#!/usr/bin/python
# -*- coding: utf-8; -*-

import urllib.parse
import sys

if len(sys.argv) == 1:
  sys.stderr.write("No argument given!\n");
  sys.exit(1)
else:
  encoded_param = urllib.parse.quote(sys.argv[1])
  url = "https://www.google.co.jp/search?site=imghp&tbm=isch&q=" + encoded_param
  print(url)

# python gimage.py "テスト"

Python 2.7

#!/usr/bin/python
# -*- coding: utf-8; -*-

import urllib
import sys

if len(sys.argv) == 1:
  sys.stderr.write("No argument given!\n");
  sys.exit(1)
else:
  encoded_param = urllib.quote(sys.argv[1])
  url = "https://www.google.co.jp/search?site=imghp&tbm=isch&q=" + encoded_param
  print url

# python gimage.py "テスト"

Ruby 1.8/1.9

#!/usr/bin/ruby

require 'uri'

if ARGV.length == 0
  $stderr.write("Invalid number of arguments")
end

encoded_param = URI.encode(ARGV[0])
uri = "https://www.google.co.jp/search?site=imghp&tbm=isch&q=#{encoded_param}"
puts uri

# ruby gimage.rb テスト

Google Go

package main

import (
  "os"
  "fmt"
  "net/url"
)

const base_uri = "https://www.google.co.jp/search?site=imghp&tbm=isch&q="

func main() {
  if len(os.Args) != 2 {
    os.Stderr.Write([]byte("Invalid arguments\n"))
    os.Exit(1)
  }

  encoded_param := url.QueryEscape(os.Args[1])
  fmt.Printf("%s%s\n", base_uri, encoded_param)
}

// go run gimage.go テスト

Perl

#!/usr/bin/perl -w
use warnings;
use strict;
use utf8;
use URI::Escape;

my $numArgs = $#ARGV + 1;
if ($numArgs == 0 ) {
        print "Invalid number of arguments\n";
        exit;
}

my $encoded_param = uri_escape( $ARGV[0] );
my $url = "https://www.google.co.jp/search?site=imghp&tbm=isch&q=$encoded_param";
print "$url\n";

# perl gimage.pl テスト

NodeJS

var querystring = require("querystring");

// 1 = node
// 2 = gimage.js
if ( process.argv.length == 2 ) {
  process.stderr.write("Invalid number of arguments\n");
  process.exit(1);
}

var base_url = "https://www.google.co.jp/search?site=imghp&tbm=isch&";
var encoded_param = querystring.stringify({q: process.argv[2]});

process.stdout.write(base_url + encoded_param + "\n");

// node gimage.js テスト
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment