Thursday, September 21, 2017

Extract Audio from Video with FFmpeg and Format Factory

FFmpeg:

FFmpeg is a free software project that produces libraries and programs for handling multimedia data.



Format Factory:

FormatFactory is an ad-supported freeware multimedia converter that can convert video, audio, and picture files. It is also capable of ripping DVDs and CDs to other file formats, as well as creating .iso images. It can also join multiple video files together into one.



How to extract audio from video using FFmpeg:

If you just want to copy the audio stream without re-encoding just do:

ffmpeg -i input-video.avi -vn -acodec copy output-audio.aac

-vn is no video.
-acodec copy says use the same audio stream that's already in there.

Read the output to see what codec it is, to set the right filename extension.

Sunday, September 17, 2017

Getting started with SQLite database in Perl

SQLite is an in-process library that implements a self-contained, serverless, zero-configuration, transactional SQL database engine. The code for SQLite is in the public domain and is thus free for use for any purpose, commercial or private. SQLite is the most widely deployed database in the world with more applications than we can count, including several high-profile projects. [https://www.sqlite.org/about.html]

Following program demonstrates how to Create a new SQLite database, how to connect to a database with credentials, how to create a table, how to insert values and how to select values.

#!/usr/bin/perl
use DBI;
use strict;
use warnings;

my $dsn = "DBI:SQLite:test.db";
my $userid = "";
my $password = "";
my $dbh = DBI->connect($dsn, $userid, $password, { RaiseError => 1, AutoCommit => 0})
   or die $DBI::errstr;

print "Opened database successfully\n";

Thursday, September 14, 2017

Plot colored node graph with networkx in Python

Networkx:

NetworkX is a Python package for the creation, manipulation, and study of the structure, dynamics, and functions of complex networks. URL: https://networkx.github.io/


Code:

import networkx as nx
import matplotlib.pyplot as plt

G=nx.Graph()
color_map = ['blue','green','red']

G.add_nodes_from([1,10])
G.add_edge(1,2)
G.add_edge(1,3)
G.add_edge(4,3)
G.add_edge(5,3)
G.add_edge(6,8)
G.add_edge(7,9)
G.add_edge(8,7)
G.add_edge(9,6)
G.add_edge(10,5)
G.add_edge(1,4)

nx.draw(G,node_color = color_map,with_labels = True)
plt.show()


Output:

Wednesday, September 13, 2017

Create HTML dashboard of Jenkins jobs status


Perl script to fetch live jobs' status from Jenkins server and generate and display HTML dashboard from it.

use strict;
use warnings;
use autodie;
use REST::Client;
use MIME::Base64;
use JSON;

my $api_proto = 'http';               #HTTP/HTTPS
my $api_url = 'localhost';       #Server IP
my $api_port = '8080';                #Port number
my $api_user = 'admin';           #Jenkins account username
my $api_pass = 'password';          #Jenkins account password
my $client = REST::Client->new();    #Perl Rest Client Object

$client->addHeader('Authorization', 'Basic '.encode_base64($api_user . ':' . $api_pass));
$client->GET($api_proto.'://'.$api_url.':'.$api_port.'/api/json?tree=jobs[name,color,url]');

my $html = "";
$html = $html."<!DOCTYPE html><html><head><title></title></head><body><table style='width:100%'>";
$html = $html."<tr><th>NAME</th><th>COLOR</th><th>URL</th></tr>";

if ($client->responseCode() == 200)
{
    my $data = decode_json($client->responseContent());
    my @jobs = @{$data->{'jobs'}};
    print("\nTotal ".scalar(@jobs)." jobs found..\n\n");
   
    foreach my $job (@jobs)
    {
        $html = $html."<tr>";
        $html = $html."<td><font color='".$job->{'color'}."'>".$job->{'name'}."</font></td>";
        $html = $html."<td><font color='".$job->{'color'}."'>".$job->{'color'}."</font></td>";
        $html = $html."<td><a href=".$job->{'url'}."><font color='".$job->{'color'}."'>".$job->{'url'}."</font></a></td>";
        $html = $html."</tr>";
    }
}

else
{
    print("\ncould not connect to jenkins server... :( :(\n");
    print("\n".$client->responseContent()."\n");
}

$html = $html."</table></body></html>";

open (my $fh, ">Jenkins Dashboard.html") or die("Could not open file. $!");
print $fh $html;
close $fh;

system("Jenkins Dashboard.html");

Sunday, September 10, 2017

Parse DICOM Image in Python | Convert DICOM to PNG in Python

DICOM:

Digital Imaging and Communications in Medicine (DICOM) is a standard for storing and transmitting medical images. It includes a file format definition and a network communications protocol.

PNG:

PNG (pronounced ping as in ping-pong; for Portable Network Graphics) is a file format for image compression that, in time, is expected to replace the Graphics Interchange Format (GIF) that is widely used on today's Internet. Portable Network Graphics (PNG /ˈpɪŋ/) is a raster graphics file format that supports lossless data compression. PNG was created as an improved, non-patented replacement for Graphics Interchange Format (GIF), and is the most widely used lossless image compression format on the Internet.

Prerequisites:

You need to install following python packages: pydicom, pylab, pypng

To install these modules you can type following commands in console:
pip install pydicom
pip install pylab
pip install pypng

Build Jenkins Job Remotely | Fix: Error 403 No valid crumb was included in the request


In case you are a newbie with Jenkins read through this article: http://com.puter.tips/2017/09/getting-up-and-running-with-jenkins.html



Build Jenkins Job Remotely

I have found two ways in which you can trigger a build on job remotely via scripting.

Both methods are simple HTTP Get/Post requests, nothing fancy here. Along with these Requests we need to pass login credentials.

Method 0: No protection enabled

=> This is the case when you are in a secure environment and have disabled all additional protection mechanism like CSRF (Method 1) and Authentication Token (Method 2). If this is the case then you can simply trigger a HTTP Request like this:

'http://username:password@jenkins_url:port/job/job_name/build'

e.g. http://admin:pass@localhost:8080/job/job1/build

Jenkins: Trigger build remotely via script using Authentication Token

Powershell:

$acctname = "admin"
$password = "password"
$server = "localhost"
$port = "8080"
$jobName = "test02"
$jobToken = "test02_authentication_token"
$params = @{uri = "http://${server}:${port}/job/${jobName}/build?token=${jobToken}";
            Method = 'Get';
            Headers = @{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("$(${acctname}):$(${password})"));}}
Invoke-RestMethod @params

Jenkins: Trigger build remotely via script using crumb

Python:

import requests
api_proto = 'http'
api_url = 'localhost'
api_port = '8080'
api_job = 'test01'
api_user = 'admin'
api_pass = 'password'

api_crumb = requests.get(api_proto + '://' + api_user + ':' + api_pass + '@' + api_url + ':' + api_port + '/crumbIssuer/api/json')
if api_crumb.status_code == 200 :
    api_crumb = api_crumb.json()['crumb']

resp = requests.post(api_proto + '://' + api_url + ':' + api_port + '/job/' + api_job + '/build', auth=(api_user, api_pass), headers={"Jenkins-Crumb":api_crumb})
if resp.status_code == 201:
    print(api_job + ' was triggered successfuly..')

Saturday, September 09, 2017

"No connection adapters were found for '%s'" % url [Solved]


Error:


Traceback (most recent call last):

  File "C:\Users\310211146\AppData\Local\Continuum\Anaconda3\lib\site-packages\requests\api.py", line 110, in post
    return request('post', url, data=data, json=json, **kwargs)
  File "C:\Users\310211146\AppData\Local\Continuum\Anaconda3\lib\site-packages\requests\api.py", line 56, in request
    return session.request(method=method, url=url, **kwargs)
  File "C:\Users\310211146\AppData\Local\Continuum\Anaconda3\lib\site-packages\requests\sessions.py", line 475, in request
    resp = self.send(prep, **send_kwargs)
  File "C:\Users\310211146\AppData\Local\Continuum\Anaconda3\lib\site-packages\requests\sessions.py", line 590, in send
    adapter = self.get_adapter(url=request.url)
  File "C:\Users\310211146\AppData\Local\Continuum\Anaconda3\lib\site-packages\requests\sessions.py", line 672, in get_adapter
    raise InvalidSchema("No connection adapters were found for '%s'" % url)
requests.exceptions.InvalidSchema: No connection adapters were found for 'localhost:8080/'


Problem Code:

resp = requests.post("localhost:8080", auth=("user","pass"), headers={"key":"value"}) 


Solution:

resp = requests.post("http://localhost:8080", auth=("user","pass"), headers={"key":"value"}) 


Reference: https://stackoverflow.com/a/15115345/4064166

Getting up and running with Jenkins 2.60.3 on Windows [How to solve Error: no workspace]

Jenkins:
The leading open source automation server, Jenkins provides hundreds of plugins to support building, deploying and automating any project. Jenkins is a Continuous Integration server. Basically Continuous Integration is the practice of running your tests on a non-developer machine automatically every-time someone pushes new code into the source repository.

Prerequisites:
Java Development Kit (JDK)
Java Run-time Environment (JRE)





Count number of items and sum of the items in a column in R

Code:

mydf <- data.frame(Key=c('a','b','c','d','a','a','a','a','b','c','d','c','c','d','b','b','a'), Values=c(4,6,3,4,5,6,2,1,3,4,5,6,4,2,4,5,6))
               
mydf_keys <- unique(mydf$Key)
mydf_vals <- c(nrow(mydf_keys))
for (i in mydf_keys)
{
    mydf_vals <- c(mydf_vals, nrow(mydf[mydf$Key %in% i, ]))
}

#mydf_count gives count of items i.e. how many times they appear
mydf_count <- data.frame(item=mydf_keys, count=mydf_vals)
print(mydf_count)

#mydf_sum gives sum of values of items in the column you specify
mydf_sum <- setNames(aggregate(mydf$Values, by = list(item=mydf$Key), FUN = sum, na.rm = TRUE), c('item', 'sum'))
print(mydf_sum)

Output:


If you have a better approach for this then please share in comments below or email me. :)

Friday, September 08, 2017

Run Microsoft R Open Script From Command Prompt

Microsoft R Open is Multi-core environment developed on Core R. Microsoft R Open (MRO), formerly known as Revolution R Open (RRO), is the enhanced distribution of R from Microsoft Corporation. It is a complete open source platform for statistical analysis and data science. Microsoft R Open 3.4.1 is coming September 11th.

MRO comes with Microsoft R Open GUI and you can also use it in RStudio IDE. But in case you want to run it from command prompt, it is pretty easy.

First you need to set Path in environment variable.


For 32-bit version path would be like: C:\Program Files\Microsoft\R Open\R-3.4.0\bin
For 64-bit version path would be like: C:\Program Files\Microsoft\R Open\R-3.4.0\bin\x64


Now save it and restart command prompt if already open or open a new command prompt window.

Type command "where rscript" and check whether it returns a correct path.


Now you can type command "rscript your_script.r" and it will get executed. Note that RStudio commands like "show" won't work from Rscript and only R/MRO commands will work.

Thursday, September 07, 2017

Xiaomi Mi A1 vs Motorola Moto G5S Plus Price and Comparison

Xiaomi Mi A1

The Xiaomi Mi A1 is a dual-SIM (Nano-SIM) smartphone that runs Android 7.1.2 Nougat. It features a 5.5-inch full-HD (1080x1920 pixels) display. It is powered by an octa-core Qualcomm Snapdragon 625 SoC coupled with 4GB of RAM. In terms of optics, the dual rear camera setup of the Xiaomi Mi A1 bears two 12-megapixel sensors, with one featuring a wide-angle lens with a 1.25-micron pixel sensor and f/2.2 aperture, and the other sporting a telephoto lens, 1-micron pixel sensor, an f/2.6 aperture, and capable of delivering 2x optical zoom. The smartphone bears a 5-megapixel front camera with a real-time beautification mode. The Xiaomi Mi A1 comes with 64GB of inbuilt storage, which is expandable via microSD card (up to 128GB) in a hybrid dual-SIM configuration. It houses a 3080mAh battery.

Motorola Moto G5S Plus

The dual-SIM (Nano) Moto G5S Plus runs Android 7.1 Nougat out of the box, and it sports a 5.5-inch full-HD (1080x1920 pixels) IPS LCD display. It is powered by a Qualcomm Snapdragon 625 octa-core SoC clocked up to 2GHz, coupled with 4GB of RAM. In terms of optics, the smartphone comes with a dual rear camera setup with two 13-megapixel sensors with an f/2.0 aperture, depth editing software, and colour correcting dual-LED flash. On the front, it bears an 8-megapixel sensor with an f/2.0 aperture, wide-angle lens, and an LED flash. The Moto G5S Plus bears 64GB of inbuilt storage, expandable via microSD card (up to 128GB). The handset houses a 3000mAh battery.

Wednesday, September 06, 2017

Perl Subroutine Arguments

Subroutines:-

1. Retrieving arguments to a subroutine with shift:

A subroutine's arguments come in via the special @_ array. The shift without an argument defaults to @_.

   sub volume {
       
my $height = shift;
       
my $width = shift;
       
my $depth = shift;

        return $height
* $width * $depth;
   
}

C/C++ Parameter Passing

C parameter passing:-

1. Pass by Value:

- The value of a variable is sent to function.
- The actual parameter cannot be changed by function.
- Duplicate Copy of Original Parameter is Passed.
- No effect on Original Parameter after modifying parameter in function.

#include<stdio.h>

void interchange(int number1,int number2)
{
    int temp;
    temp = number1;
    number1 = number2;
    number2 = temp;
}

int main() {

    int num1=50,num2=70;
    interchange(num1,num2);

    printf("\nNumber 1 : %d",num1);
    printf("\nNumber 2 : %d",num2);

    return(0);
}