Skip to content

Category: How To’s

Grab PacktPub Free Learning eBook Everyday Automatically

Packt Publishing have a lot of good premium books and everyday they put up a premium book for download for free. You could get a premium book a day by going to the Free Learning eBook page. I chose to automate the process and have the book grabbed automatically to my PacktPub account everyday. I have made the script available on Github for anyone who is interested to do the same: PacktPub Grabber.

read more

Convert Fortinet's Fortiguard WebFilter logs to CSV Format

I put together this script called csvify_fortinet_logs in order to convert the space separated format of Fortinet Router's Fortiguard WebFilterLog to a more widely used and favorable for analysis format, CSV. I wanted to analyse the web filter log of the router, but could not use it as an input to pandas, so had to code this.

What the script does is, convert the log from following transaction format:

date=2016-04-29 time=16:19:33 logid=0316013056 type=utm subtype=webfilter eventtype=ftgd_blk level=warning vd="root" policyid=16 identidx=1 sessionid=29075420 user="x_y" group="bla bla" srcip= srcport=60957 srcintf="LAN-NW-2" dstip= dstport=80 dstintf="wan2" service="http" hostname="" profiletype="Webfilter_Profile" profile="Student" status="blocked" reqtype="referral" url="/?id=461665" sentbyte=739 rcvdbyte=535 msg="URL belongs to a denied category in policy" method=domain class=0 cat=26 catdesc="Malicious Websites"

date=2016-04-29 time=16:15:27 logid=0316013056 type=utm subtype=webfilter eventtype=ftgd_blk level=warning vd="root" policyid=16 identidx=1 sessionid=29017201 user="x_y" group="blabla" srcip= srcport=60319 srcintf="LAN-NW-2" dstip= dstport=80 dstintf="wan2" service="http" hostname="" profiletype="Webfilter_Profile" profile="Student" status="blocked" reqtype="direct" url="/e?i=43" sentbyte=429 rcvdbyte=0 msg="URL belongs to a denied category in policy" method=domain class=0 cat=72 catdesc="Peer-to-peer File Sharing"

read more

How To Batch Crop & Straighten Scanned Photos?

I spent the last two days scanning photos from my family albums (about 900 photos). Scanning each photos individually will take lots of time, however not any further processing is needed. When you scan multiple images at once, you need to crop and straighten each photo from a single page you scanned.

With the scanner I had, I could fit 4 photos and scan them at once. And so I did. I chose it over scanning each photos individually because there are some tools to automagically crop photos from a group of scanned photos.

One thing that is very important in order to split photos is to make sure that there is some gap between the photos. Meaning, when you put the photos in the scanner, there should be a uniform background that separates each photos. They should not be overlapping, or touching each other. The splitter tools crop the photos based on where the background begins and ends, so this is important.Here are the tools that can help you to auto-split scanned images by batch cropping (and straightening) them. I have listed 6 tools here, two of which can be used on Linux, and four can be used on Windows (& some on mac).


To access this feature, you can go to File > Automate > Crop and Straighten Photos.

But, what if you have multiple scans, and you need to split them into single images like in my case?

Luckily, I found a PS script that does this for you.

#target Photoshop
var inFolder = Folder.selectDialog("Please select folder to process");
if(inFolder != null){
var fileList = inFolder.getFiles(/.(jpg|tif|psd|)$/i);
var outfolder = new Folder(decodeURI(inFolder) + "/Edited");
if (outfolder.exists == false) outfolder.create();
for(var a = 0 ;a < fileList.length; a++){
if(fileList[a] instanceof File){
var doc= open(fileList[a]);
var docname = fileList[a].name.slice(0,-4);
var count = 1;
var saveFile = new File(decodeURI(outfolder) + "/" + docname +"#"+ zeroPad(count,3) + ".jpg");
SaveJPEG(saveFile, 12);
activeDocument.close(SaveOptions.DONOTSAVECHANGES) ;
function CropStraighten() {
executeAction( stringIDToTypeID('CropPhotosAuto0001'), undefined, DialogModes.NO );
function SaveJPEG(saveFile, jpegQuality){
jpgSaveOptions = new JPEGSaveOptions();
jpgSaveOptions.embedColorProfile = true;
jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
jpgSaveOptions.matte = MatteType.NONE;
jpgSaveOptions.quality = jpegQuality;
activeDocument.saveAs(saveFile, jpgSaveOptions, true,Extension.LOWERCASE);
function zeroPad(n, s) {
n = n.toString();
while (n.length < s) n = '0' + n;
return n;

read more

JetBrains' Products Now Free For (Nepali) Students

A little late in sharing this wonderful news. If you don't know yet, JetBrains, the developer of awesome IDEs like IntelliJ IDEA and PhpStorm have made their products free for students. And guess what? This is unlike other free-for-students offer. Most of the free-for-students discounts are limited to students of US university and colleges only. The way to check whether a person claiming to be a student to avail these sorts of discounts is in fact a student or not, is to make him use his university provided email address.

read more

How to login to a website via Google Script?

Whenever I have to accomplish something these days (in terms of programming that is), the language that I turn to at first is Google Script. I have gotten so used to it, and its interface that I prefer it over any other language, or services. The added bonus of using it is that you can run the script regularly by setting up a timer. No need to meddle with cronjobs and what-not on your server.

Some time back, I was trying to login to my college's LMS (Learning Management System) using Google Script. My college uses Moodle and it has been setup such that if you do not login for 12 consecutive days, your account will get disabled. Once disabled, you need to go to the authority and bend over your backwards to convince him to re-active your account. I don't login to Moodle very often and neither do my friends. I turned to Google Script to solve this problem, to login to Moodle every week.

You can use the following script to login to any (I hope) website via Google Script.

/**   Published by Subigya Nepal on 09/08/2014    **/

function loginToSite(){
  var url = "enter login URL here"; //change this.
  var payload = {
    "username":"enteryourusername", //change this.
    "password":"enteryourpassword" //and change this. done. no need to enter anything elsewhere.
  var opt = {
    "followRedirects" : false
  var response = UrlFetchApp.fetch(encodeURI(url),opt);
  if ( response.getResponseCode() == 200 ) { //could not log in.
    var result = "Couldn't login. Please make sure your username/password is correct.";
  else if ( response.getResponseCode() == 303 ) { //login was successful. you might receive 302 response code as well depending upon the site. So try changing it to 302.
     var result = "Logged in successfully";
     var cookie = response.getAllHeaders()['Set-Cookie'];     
     var header = {
       'Cookie':cookie[1] //taking the second cookie because when redirected, a new cookie is set. If the page does not redirect, you might need to use cookie[0] here. Try it out yourself!

read more

How to Install LiveReload in Ubuntu?

LiveReload is a handy tool for web-developers. Do you not use Dreamweaver alternatives for linux, or any other web development tools? If you make use of text editors to code and web browsers to preview the result, then you must be tired of constantly hitting the save button on your text editor as well as that reload button on your web browser.

Why LiveReload?

LiveReload is here to your rescue. What it does is, it monitors for changes in the file system (a folder that you specify) and then automatically reloads the browser if any change is detected. Also, when you change a CSS file or image, the browser is updated instantly without even reloading the page.

How to Install LiveReload on Ubuntu?

Installing LiveReload is a pretty straightforward process in Windows, Mac and on Linux too (somewhat different though). In order to install LiveReload on Ubuntu, hit the following commands on your terminal one after another:

sudo apt-get install ruby-dev
sudo gem install rdoc -V
sudo gem install guard -V
sudo gem install guard-livereload -V

read more

How To Resize and Create LVM Partitions on Ubuntu?

Too frequent BSODs and 'display driver not detected' messages were turning me insane. I decided to install Ubuntu via WUBI, but although the pain slightly decreased, the true potential of linux was still shrouded by Windows. I had been contemplating on completely formatting and installing Linux, and about a week ago, I finally did it. With just 1 Gb of RAM and a P4 processor, I thought Xubuntu would be the way to go - and it seems that I was right.

Anyway, while installing Xubuntu I put a tick mark on "Use LVM with the new Xubuntu Installation" without even knowing what LVM was. It just seemed fancy, I guess. I have a knack for doing stupid things. Now what happened was, the entire Harddisk got formatted, like I wanted. But, there was only one partition for root (obviously).

GParted does not seem to work with resizing LVM partitions because you need to have some unallocated space on your volume group. Since all the space was assigned to the same volume group and there were no free space, I could not make use of it.

Being sort of a newbie to Linux, I was looking for an easy way out (i.e. a GUI based solution) and I did find one - an utility called KVPM. It seems to work better than GParted because it does not require any free space on the volume group in order to partition it (I haven't used it though, use at your own risk.) But, with my Internet Speed it would have taken me almost 2 hours just to download it.

Resize an LVM partition and create a new one the dirty way (Just kidding; it's easy.):

  • Boot with a live CD of the Linux distro. Click on Try Ubuntu (or whatever unity based distro you've got.)
  • Go to terminal (Ctrl+Alt+T) and change to root by entering the command : sudo su.
  • We need to find the volume group name of your partition and its corresponding device folder.
  • Enter lvdisplay. The results you see are your logical volumes. You should find at least two of them: /dev/vg-name/root and /dev/vg-name/swap_1. Disregard the swap partition, it's the root that needs resizing.
As you can see on the picture, the device folder in my case is /dev/xubuntu-vg/root. Suppose, I wanted to resize my 230 Gb partition to just 80 Gb and create a new volume on the free space. Here's what I would do. Note that I'm entering /vg-name/root because that is what I am going to resize. If you are going to resize some other volume, you have to enter its name instead of root. (Enter these commands one after another. Remember to put your actual volume group in place of 'vg-name'.)
resize2fs -P /dev/vg-name/root
e2fsck -f /dev/vg-name/root
resize2fs -M /dev/vg-name/root
lvresize vg-name/root -L 80GB
resize2fs /dev/vg-name/root

read more

Get Microsoft DreamSpark Account (& Some Free Products Along With It)

For those of you unaware of it, DreamSpark is a Microsoft program wherein students are provided with Microsoft products at no charge at all. To make use of this facility, you need to verify that you're infact a student. There are a number of ways to do this, but using a .edu email is the easiest one. This proves that the person is in fact a student since a .edu email address is assigned by a education institution and isn't open to everyone (not true all the time, as you will read below.) In this article, you are going to learn how you can get a DreamSpark account for free.

read more

How To Remove Left Sidebar From Frank WordPress Theme?

So, I decided to change the theme of the site once again. Man, I'm never satisfied with any theme and as far as I've learned in the last 3 years, no one really is. Yep, you look at someone else's site and you like their theme. You look at yours, and you want to change it. Anyway, this time, I decided to move to a fast loading minimal theme.

I had noticed for a while that the site was loading slow - took about 3-4 seconds as per Pingdom and GtMetrix. Luckily, before I could begin my search for a new theme, a friend of mine sent me a link to Smashing Magazine's coverage of Frank theme. I took a look at the demo site, and it loaded in blazing fast speed. Naturally, I was attracted to it and downloaded it right away and installed it on the site.

I went through the theme options to see if there was some way to remove the left sidebar so as to make it a two column theme. But, I could not find any option. So, I had to work around a bit, to get it to work as I wanted.

If you want to make the posts appear on a two column layout, rather than the default three column layout then follow the directions below.

Backup Single.php file, so that you can restore it if anything unexpected happens. Now, remove the following code from single.php:

					<div class='post-info'>
						<?php get_template_part( 'partials/post-metadata' ); ?>
						<?php if ( frank_tweet_post_button() ) : ?>
						<a id="post-tweet" href="<?php echo rawurlencode( strip_tags( get_the_title() ) ); ?><?php if ( frank_tweet_post_attribution() ) : ?>&via=<?php echo frank_tweet_post_attribution(); ?>&related=<?php echo frank_tweet_post_attribution(); ?><?php endif; ?>&url=<?php the_permalink(); ?>&counturl=<?php the_permalink(); ?>" target="_blank">
						<?php _e( 'Tweet this Post', 'frank_theme' ); ?>
						<?php endif; ?>
						<div id="prev-post">
							<?php previous_post_link( '%link', '<nav><span>%title</span></nav><p>%title</p>' ); ?>
						<?php if ( !dynamic_sidebar( 'Post Left Aside' ) ) : ?>
						<?php endif; ?>

read more

Grab Certain Texts From Gmail via Google Spreadsheet (Script)

I had worked on this a few weeks back, and without any prior knowledge of using GmailApp call on Google Spreadsheet, I had to struggle for a while to get it working. The code to do this is fairly simple and can be understood by just about anyone. The problem I was trying to solve was also fairly simple. First, some background : there's a new task initiated by my college, although as ridiculous as it might sound to you, a student (based on the order of the roll number) has to send an email blast each day to other students. The email blast should contain a word, it's meaning, example of its usage etc. There's a certain format that needs to be followed, and what I wanted to do was grab just the words within the email and store them so that at some point, I'd have a database full of every words that have ever been sent. (Not really my idea, though.)

So, here's how I tackled the problem, and I hope this would be helpful to you in case you want to do something similar. What I did at first was that I went to my Gmail and assigned all the emails with the subject "word of the day", the label : "Word". Since all the email blasts followed the same format, they all had the same subject as well. The advantage of assigning a label to emails based on subject is that, it works for new emails as well. That means, any new email with the same subject, will also automatically be assigned the label "Word". I now only needed to focus on retrieving mails from the label "Word" via Google Script. And this code exactly does that :

function myFunction() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('MySpreadsheet');
  var labelObject = GmailApp.getUserLabelByName('Word');
  var threads = labelObject.getThreads();
  for (var i = 0; i < threads.length; i++) {
    var msg= threads[i].getMessages();
    var raw = msg[0].getRawContent();
    var words = raw.match('*(.*?)*');
    if (words != null) {
      sheet.getRange('A' + String(i+1)).setValue(words[1]);

read more