Saturday, February 13, 2016
Phantom JS: Creating PNG Image File of the Specified URL
Creating PNG image file of the specified URL
pagerender.js:
var page = require('webpage').create(),
system = require('system'),
address;
if (system.args.length <= 2) {
console.log('Usage: pagerender.js <some URL> <image name>');
phantom.exit();
}
address = system.args[1];
image = system.args[2];
page.open(address, function(status) {
console.log("Status: " + status);
if(status === "success") {
page.render(image);
}
phantom.exit();
});
pagerender.js:
var page = require('webpage').create(),
system = require('system'),
address;
if (system.args.length <= 2) {
console.log('Usage: pagerender.js <some URL> <image name>');
phantom.exit();
}
address = system.args[1];
image = system.args[2];
page.open(address, function(status) {
console.log("Status: " + status);
if(status === "success") {
page.render(image);
}
phantom.exit();
});
Command:$ phantomjs pagerender.js http://smashingtheory.blogspot.com test.png
ラベル:
Headless browser
Thursday, February 11, 2016
Wednesday, February 10, 2016
Tuesday, February 9, 2016
Homebrew: Installing Homebrew Package Manager on OS X 10.11.3
Installing a package manager Homebrew on Mac OS X 10.11.3:
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Perl: iTunes XML to CSV (Without XML Parser)
itunes.pl
use constant MODE_NONE => 0;
use constant MODE_FIND_BRAKET_NAME => 1;
use constant MODE_FIND_VALUE =>2;
use constant FALSE =>0;
use constant TRUE => 1;
$mode = MODE_NONE;
$tagName = "";
$value = "";
$keyValue = "";
$openTag = FALSE;
$closeTag = FALSE;
$isTrack = FALSE;
@itunestags = ("Track ID","Name","Artist","Album Artist","Composer","Album","Grouping","Kind","Size","Total Time","Disc Number","Disc Count","Track Number","Track Count","Year","Date Modified","Date Added","Bit Rate","Sample Rate","Play Count","Play Date","Play Date UTC","Skip Count","Skip Date","Release Date","Rating","Album Rating","Album Rating Computed","Normalization","Artwork Count","Persistent ID","Track Type","Protected","Purchased","Location","File Folder Count","Library Folder Count");
print "Track ID,Name,Artist,Album Artist,Composer,Album,Year\n";
while (defined($char = getc())){
if ($char eq "<"){
$mode = MODE_FIND_BRAKET_NAME;
$tagName = "";
}elsif ($char eq ">"){
if ($closeTag)
{
if ($value eq "Tracks"){
$isTrack = TRUE;
}elsif ($value eq "Playlists"){
$isTrack = FALSE;
}elsif ($value ~~ @itunestags){
$keyValue = $value;
}else{
$value =~ s/"/""/g;
if ($value =~ /[,"]/){
$value = "\"" . $value . "\"";
}
$value =~ s/&/&/g;
if ($keyValue ~~ @itunestags){
$track{$keyValue} = $value;
}
}
if ($isTrack && $tagName eq "dict"){
printf "%s,%s,%s,%s,%s,%s,%s\n", $track{'Track ID'}, $track{'Name'}, $track{'Artist'}, $track{'Album Artist'}, $track{'Composer'}, $track{'Album'},$track{'Year'};
$keyValue ="";
%track=();
}
$value = "";
$closeTag = FALSE;
}else{
$mode = MODE_FIND_VALUE;
$value = "";
}
}else{
if ($mode == MODE_FIND_BRAKET_NAME){
if ($char eq "/"){
$closeTag = TRUE;
}else{
$tagName .= $char;
}
}elsif($mode == MODE_FIND_VALUE){
$value .= $char;
}
}
}
Bash command:
use constant MODE_NONE => 0;
use constant MODE_FIND_BRAKET_NAME => 1;
use constant MODE_FIND_VALUE =>2;
use constant FALSE =>0;
use constant TRUE => 1;
$mode = MODE_NONE;
$tagName = "";
$value = "";
$keyValue = "";
$openTag = FALSE;
$closeTag = FALSE;
$isTrack = FALSE;
@itunestags = ("Track ID","Name","Artist","Album Artist","Composer","Album","Grouping","Kind","Size","Total Time","Disc Number","Disc Count","Track Number","Track Count","Year","Date Modified","Date Added","Bit Rate","Sample Rate","Play Count","Play Date","Play Date UTC","Skip Count","Skip Date","Release Date","Rating","Album Rating","Album Rating Computed","Normalization","Artwork Count","Persistent ID","Track Type","Protected","Purchased","Location","File Folder Count","Library Folder Count");
print "Track ID,Name,Artist,Album Artist,Composer,Album,Year\n";
while (defined($char = getc())){
if ($char eq "<"){
$mode = MODE_FIND_BRAKET_NAME;
$tagName = "";
}elsif ($char eq ">"){
if ($closeTag)
{
if ($value eq "Tracks"){
$isTrack = TRUE;
}elsif ($value eq "Playlists"){
$isTrack = FALSE;
}elsif ($value ~~ @itunestags){
$keyValue = $value;
}else{
$value =~ s/"/""/g;
if ($value =~ /[,"]/){
$value = "\"" . $value . "\"";
}
$value =~ s/&/&/g;
if ($keyValue ~~ @itunestags){
$track{$keyValue} = $value;
}
}
if ($isTrack && $tagName eq "dict"){
printf "%s,%s,%s,%s,%s,%s,%s\n", $track{'Track ID'}, $track{'Name'}, $track{'Artist'}, $track{'Album Artist'}, $track{'Composer'}, $track{'Album'},$track{'Year'};
$keyValue ="";
%track=();
}
$value = "";
$closeTag = FALSE;
}else{
$mode = MODE_FIND_VALUE;
$value = "";
}
}else{
if ($mode == MODE_FIND_BRAKET_NAME){
if ($char eq "/"){
$closeTag = TRUE;
}else{
$tagName .= $char;
}
}elsif($mode == MODE_FIND_VALUE){
$value .= $char;
}
}
}
$ cat ./itunes.xml | perl ./itunes.pl
ラベル:
Comma-separated values,
iTunes,
Perl,
XML
AWK: Printing CSV file
printing CSV file:
csv.awk:
Bash command:
csv.awk:
{
readcsv($0);
print $2" by "$3;
}
function readcsv(string)
{
#フィールド番号
n=1;
#コンマ区切り
m=split(string, array , ",");
#完全フィールドか否か
aflag=1;
for(i=1;i<=m;i++){
# " "で括られていない完全フィールド。
if( !(array[i] ~ /^\"/) && !(array[i] ~/\"$/) && aflag){
#ダブルクオートのエスケープ文字を削除
gsub(/\"\"/,"\"", array[i]);
$n=array[i];
n++;
continue;
}
# " "で括られている完全フィールド
if(( array[i] ~ /^\".*\"$/ ) && aflag){
#先頭と末端にあるダブルクオートを削除する
gsub(/^\"/,"", array[i]);
gsub(/\"$/,"", array[i]);
#ダブルクオートのエスケープ文字を削除
gsub(/\"\"/,"\"", array[i]);
$n=array[i];
n++;
continue;
}
# 先頭が "の部分フィールド
if(array [i] ~ /^\"/){
#先頭のダブルクオートを削除
gsub(/^\"/,"", array[i]);
#ダブルクオートのエスケープ文字を削除
gsub(/\"\"/,"\"", array[i]);
aflag=0;
$n=array[i];
continue;
}
# " "で括られていない、続きの部分フィールド。
if(!(array[i] ~ /^\"/) && !(array[i] ~/\"$/) && aflag==0){
#ダブルクオートのエスケープ文字を削除
gsub(/\"\"/,"\"", array[i]);
$n = $n "," array[i];
continue;
}
# 末端が "の部分フィールド
if((array [i] ~ /\"$/) && aflag==0){
#末端のダブルクオートを削除
gsub(/\"$/,"", array[i]);
#ダブルクオートのエスケープ文字を削除
gsub(/\"\"/,"\"", array[i]);
$n = $n "," array[i];
aflag=1;
n++;
continue;
}
}
}
Bash command:
$ cat list.csv | awk -f ./csv.awk
ラベル:
AWK,
Comma-separated values
AWK: FS, RS, OFS, ORS
Testing FS (field separator), RS (record separator), OFS (output field separator), ORS (output record separator):
Command:
Output:
Command:
$ echo -n '"test,test",testtest,test,test' | awk 'BEGIN{FS=",";RS="\n";OFS="-";ORS="+"}{print $1, $2, $3, $4, $5, $6}'
Output:
"test-test"-testtest-test-test-+
ラベル:
AWK
AWK: split Function
Testing the behavior of split function in awk:
$ echo -n 'test,test,test' | awk '{m=split($0,array,",")}{print m}'
3
ラベル:
AWK,
Subroutine
Saturday, February 6, 2016
Perl: Read a Character from Standard Input Until The End-of-file
Read a character from standard input until the end-of-file:
while (defined($char = getc())){
print $char;
}
ラベル:
End-of-file
Subscribe to:
Posts (Atom)