Wednesday 30 May 2018

How do I get a YouTube video thumbnail from the YouTube API?

Each YouTube video has 4 generated images. They are predictably formatted as follows:
https://img.youtube.com/vi/<insert-youtube-video-id-here>/0.jpg
https://img.youtube.com/vi/<insert-youtube-video-id-here>/1.jpg
https://img.youtube.com/vi/<insert-youtube-video-id-here>/2.jpg
https://img.youtube.com/vi/<insert-youtube-video-id-here>/3.jpg
The first one in the list is a full size image and others are thumbnail images. The default thumbnail image (ie. one of 1.jpg2.jpg3.jpg) is:
https://img.youtube.com/vi/<insert-youtube-video-id-here>/default.jpg
For the high quality version of the thumbnail use a url similar to this:
https://img.youtube.com/vi/<insert-youtube-video-id-here>/hqdefault.jpg
There is also a medium quality version of the thumbnail, using a url similar to the HQ:
https://img.youtube.com/vi/<insert-youtube-video-id-here>/mqdefault.jpg
For the standard definition version of the thumbnail, use a url similar to this:
https://img.youtube.com/vi/<insert-youtube-video-id-here>/sddefault.jpg
For the maximum resolution version of the thumbnail use a url similar to this:
https://img.youtube.com/vi/<insert-youtube-video-id-here>/maxresdefault.jpg
All of the above urls are available over http too. Additionally, the slightly shorter hostname i3.ytimg.com works in place of img.youtube.com in the example urls above.
Alternatively, you can use the YouTube Data API (v3) to get thumbnail images.

Tuesday 29 May 2018

Python: Validate Phone Numbers

Phone numbers can be validated very easily using python-phonenumbers.
Here is a short example demonstrating the use of phonenumbers package.  I am using "None" as the country. Any country can be used such as e.g. "GB", "US"
Remember to

pip install phonenumbers
And

import phonenumbers;

def validate_phone(value):
    try:
        parsed_number = phonenumbers.parse(value, None)
        if not phonenumbers.is_valid_number(parsed_number) or 
           not phonenumbers.is_possible_number(parsed_number):
               print("Please provide a valid phone number e.g +CountryCodeNumber")
               return False
    except phonenumbers.phonenumberutil.NumberParseException:
               print("Please provide a valid phone number e.g +CountryCodeNumber")
               return False
 return True

Monday 28 May 2018

Could not open input file: artisan

You need to first create Laravel project  and if you already have one you need to go to this project dir using cd command in terminal for example cd myproject.
Now you will be able to run any artisan commands, for example running php artisan serve will run default project.

PHP PDO Insert Method

<?php
class Contact
{
    private $UploadedFiles = '';
    private $DbHost = DB_HOST;
    private $DbName = DB_NAME;
    private $DbUser = DB_USER;
    private $DbPass = DB_PASS;
    private $table;

    function __construct()
    {
        $this->table = strtolower(get_class());
    }

    public function insert($values = array())
    {
        $dbh = new PDO("mysql:host=$this->DbHost;dbname=$this->DbName", $this->DbUser, $this->DbPass, array(PDO::ATTR_PERSISTENT => true));
        $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $dbh->exec("SET CHARACTER SET utf8");

        foreach ($values as $field => $v)
            $ins[] = ':' . $field;

        $ins = implode(',', $ins);
        $fields = implode(',', array_keys($values));
        $sql = "INSERT INTO $this->table ($fields) VALUES ($ins)";

        $sth = $dbh->prepare($sql);
        foreach ($values as $f => $v)
        {
            $sth->bindValue(':' . $f, $v);
        }
        $sth->execute();
        //return $this->lastId = $dbh->lastInsertId();
    }

}

Thursday 24 May 2018

How to use virtualenv with python3.6 on ubuntu16.04

I encountered this problem while using Ubuntu 16.04. When I created virtual environment it used python2.x which is already there in Ubuntu.

python3 -m venv .

Check if your virtual environment is using latest python 3 by activating virtual environemnt by:

source bin/activate

python -V

Below is the older method of doing the same. Try if the above method is not working:

 
Here is how to switch to python 3.6

sudo apt-get install python-virtualenv
virtualenv --python=python3.6 venv

venv is the name of virtual environment

if there is an error that python3-env is not located then run

sudo apt install python3.6-venv

-----------------------------------------------------------------------------------------------------------------------
OR

sudo apt install -y python3-venv
python3 -m venv env 
(env is the name of the python environment)

Finally, activate your virtual environment.

source venv/bin/activate

We can use
 which python
To check which python is being used once you have activated your virtual environment.


Wednesday 23 May 2018

Django: Delete Postgres database in Ubuntu

Run the following commands to go into Postgres command line:

sudo su postgres
psql

Now the database can be dropped by using the following command:

drop database database_name;