Showing posts with label Perl. Show all posts
Showing posts with label Perl. Show all posts

Sunday, November 13, 2016

macOS Sierra: SSH (Secure Shell) Client Configuration File: Fixing Perl Locale Warning on Server

Command:

$ diff /etc/ssh/ssh_config /etc/ssh/ssh_config.org


Result:

21c21
< #        SendEnv LANG LC_*
---
>         SendEnv LANG LC_*

Tuesday, February 9, 2016

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/&#38;/&/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:

$ cat ./itunes.xml | perl ./itunes.pl

Friday, February 5, 2016

Version of Perl

$ perl --version

This is perl 5, version 18, subversion 2 (v5.18.2) built for darwin-thread-multi-2level
(with 2 registered patches, see perl -V for more detail)

Copyright 1987-2013, Larry Wall

Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.

Complete documentation for Perl, including FAQ lists, should be found on
this system using "man perl" or "perldoc perl". If you have access to the
Internet, point your browser at http://www.perl.org/, the Perl Home Page.

Friday, January 29, 2016

Cron: Tweet a Line Every 10-19 Minutes

Cron - Tweet a line every 30-40 mins:

*/10 * * * * perl -le 'sleep rand 540' && head -$[${RANDOM} \% `wc -l < /Users/username/lines.txt` + 1] /Users/username/lines.txt | tail -1 | ruby /Users/username/lines.rb

lines.rb - using standard input:

require 'json'
require 'oauth'

consumer_key = ''
consumer_secret = ''
access_token = ''
access_token_secret = ''

consumer = OAuth::Consumer.new(
consumer_key,
consumer_secret,
site:'https://api.twitter.com/'
)
endpoint = OAuth::AccessToken.new(consumer, access_token, access_token_secret)

# STATUS
status = STDIN.gets

# POST
response = endpoint.post('https://api.twitter.com/1.1/statuses/update.json', status: status )
result = JSON.parse(response.body)