Getting details from Youtube URL

We can easily get many details from a youtube url using some of Python libraries. Here am going to show you how we can get
1)Video id
2)Video title
3)Author of video
4)Video thumbnails

We use Python’s urlparse, urllib and simplejson libraries to do this.

import urlparse
import urllib
import simplejson
url_data = urlparse.urlparse("http://www.youtube.com/watch?v=z_AbfPXTKms&NR=1")
video_id = urlparse.parse_qs(url_data.query)["v"][0]  # this is the video id
 
# from this url you will get all data you needed about video, by just replacing the video id
url = 'http://gdata.youtube.com/feeds/api/videos/%s?alt=json&v=2' % video_id 
json = simplejson.load(urllib.urlopen(url))

# this is the title of the video
title = json['entry']['title']['$t']  

# author of video               
author = json['entry']['author'][0]['name']['$t']
print "id:%s\nauthor:%s\ntitle:%s" % (id, author, title)

You will get thumbnails from this url: http://img.youtube.com/vi/video_id/image_id.jpg
image_id can be 0,1,2 or 3.

A simple decorator in Python

Decorators are very useful functionality in Python. Here am going in a straight forward way to get a quick basic idea about what a decorator means. You can find what is a decorator and more details from here.

Excerpt from python wiki about decorator: A decorator is the name used for a software design pattern. Decorators dynamically alter the functionality of a function, method, or class without having to directly use subclasses or change the source code of the function being decorated.

Lets go through an example.

@login_required
def account_settings(request):
    #some code

Here login_required is a decorator. Decorator is nothing but another function. But it’s functionality is different. While running this code, when the python interpreter see some decorator ( here it is login_required) it calls the decorator with the below function as argument (that means ‘account_settings’ as argument). Then we can do some preprocessing  with the function inside decorator. Such as, using the details from function arguments we can check whether user is logined or not, if not logined redirect to login page etc.

In short using a decorator we can do some action before a particular function is called.( can do more than this!).

So how a decorator look like. Am giving a simple outline of a decorator:

def login_required(func_name):
    def decorator(request, *args, **kwargs):
       #if not logined:
           # redirect to log in page
       #else:
           #allow to access settings
     return decorator

Some points:

1) The inner function name ‘decorator’ is optional. You can choose a different name also. But you should return that name in the end. That means if you use the name ‘myfunc’ as function name, then you should return ‘myfunc’.

2) If you are sure about the number of parameter and its type you can use it directly as inner function (here ‘decorator’) parameter. If you know there should be two parameters(say ‘a’ and ‘b’ ) for account_settings function, then you can define inner function as ‘decorator(a,b)’ .

if you want to know more about decorators, this is a good tutorial.

socket programming using python

Networking is an essential task in software applications nowadays.Python is a powerful language for network programming. Python support both low level and high level access to network services. At a low level, we can access the basic socket support in the underlying operating system, which allows you to implement clients and servers for both connection-oriented and connectionless protocols. Python has a socket module to support socket programming. Python also has libraries that provide higher-level access to specific application-level network protocols, such as FTP, HTTP, and so on.

The following are some of the key concepts in network programming.

Sockets
Sockets are the endpoints of a bidirectional communications channel. Sockets may communicate within a process, between processes on the same machine, or between processes on different continents.Sockets may be implemented over a number of different channel types: Unix domain sockets, TCP, UDP, and so on.
Then what is a port in a socket? A socket directs to a particular computer in a network. Once connection reaches on a computer it want to identify the application it can connect. Port’s are made for this purpose. In a computer there will be thousands of ports in it. first 1234 ports are reserved for system and rest of ports can be applied to user applications. Each port is assigned with an application and it waits for an application to connect.

TCP and UDP:
In order to transmit data between client and server in the internet we depend on TCP and UDP protocols. So what are them? and what is the difference between them?

TCP (Transmission Control Protocol) is a set of rules (protocol) used along with the Internet Protocol (IP) to send data in the form of message units between computers over the Internet. UDP is an alternative to TCP. But there is major difference between them in the case of data transmission.

More specifically TCP is connection oriented protocol. TCP provides reliable, ordered delivery of a stream of bytes from a program on one computer to another program on another computer. We use this protocol where we each byte of data is important. In the delivery of a email we would not like to miss any byte of data, situations like these where each byte of data is important, needs reliable transmission. TCP/IP are used in situation like these and it has proper error checking mechanism in the destination to ensure that all bytes of data is reached at the destination in order. Here the correctness is more important than delivery time.

But there are some other situations where we need data to be reached in the destination within some time constraint, here the correctness of data may be tolerated to some extend. For example in real time video streaming we need bytes of data with in its streaming time. In such application’s it is not important whether the data comes in order or data comes from the same server. we just need all data in time. Even if data reached the destination out of order it should have proper mechanism to rearrange the byte order, it also adjust the data lose to some extend. In such situation’s we depend on connection-less protocol such as UDP. Applications, which do not need reliable data stream service, may use the User Datagram Protocol (UDP), which provides a datagram service that emphasizes reduced latency over reliability.

I will come back with some examples in my next post…

Python program to push files into Bitbucket.org

Here I am going to show you a simple program, using which you can add files to your bibucket repository. If you are using Linux operating system, then go to terminal and run this program. When you are asked to input username and password please input your bitbucket username and password correspondingly. When you are asked to enter reponame give your bitbucket repository name as it is.

import shlex, subprocess
from subprocess import call

print 'Enter repository address :' 
repo = raw_input()
call("hg clone "+repo, shell=True)

print 'Enter filenames to copy :'
fnames = raw_input()
files_list=shlex.split(fnames)

print'Enter reponame'
reponame = raw_input()

print 'copying to repository'
for i in range(len(files_list)):
	call(["cp",files_list[i],reponame])

print'adding'
subprocess.Popen(["hg", "add"],cwd=reponame)

print'Enter Username of bitbucket'
username = raw_input()
print'Enter a  passphrase'
passphrase = raw_input()

print 'committing added files'
subprocess.Popen(["hg","commit","-u",username,"-Am",passphrase],
                                                          cwd=reponame)

print'Pushing files to repository'
print'Enter username'
proc=subprocess.Popen(["hg","push"],cwd=reponame,stdout=subprocess.PIPE,)
stdout_value=proc.communicate()[0]

print' program finishing...files are uploaded...'

How to run external commands in python

How to run external commands on python:
Python support running external commands, such as linux terminal commands, version control terminal commands from inside python script. Earlier python had a module called ‘commands’ for this purpose. But now it is obsolete and it has more powerful and featured module called ‘subprocess’. Lets meet those two modules one by one.

commands module

Here is an example with commands module. This will run the terminal command ‘ls’ to list the files and folders in my home directory. This will return output to ‘output’ variable and success/failure status to ‘status’ variable’. Try this command in your terminal and identify  how the output look likes!.

import commands
(status,output)=commands.getstatusoutput('ls /home/jinesh')

python subprocess

Since Python 2.4 all process management functionalities are packaged under subprocess module which provide one class called Popen which is all you need to use. The subprocess module allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes. This module intends to replace several other, older modules and functions,
such as:
os.system
os.spawn*
os.popen*
popen2.*
commands.*

subprocess module provides two ways to run external commands, ‘call’ and ‘Popen’. Obviously there are some difference between using these two. At first let see some examples for both.

Using call() to execute terminal commands:

You can run ‘ls’ command (linux terminal command to show list of files and folders inside current directory) like this.

from subprocess import call
call('ls', shell=True)

When we have multiple arguements we can use list inside call.

call(["ls", "-l"], shell=True)

Using Popen() to execute terminal commands:

from subprocess import Popen
Popen('ls')
Popen(['ls', '-l'])

Both call and Popen seems to be same but Popen provides more flexibility to the programmer. You can read some difference from here.

Lets  run some mercurial commands:

With the knowledge above lets see a small program run some mercurial commands using python program. Later you can modify this program to automate your bitbucket upload.

hg add:
subprocess.Popen([“hg”, “add”], cwd=”/home/jinesh/testing”)

hg commit:

username = raw_input('Enter Username of bitbucket')
passphrase = raw_input('Enter a passphrase')
subprocess.Popen(["hg","commit","-u",username,"-Am",passphrase],cwd= "/home/jinesh/testing")

hg push:

call("hg push", shell=True)

hg clone:

repo = raw_input("Enter your repo name")
call("hg clone "+repo, shell=True)

Note: Please try to read more about these commands from python documentation. I wrote this with an introduction purpose only.

Some Pygame tips

For the last few days, I was trying to make a simple game with pygame. It was very interesting to learn pygame, and I got thrilled when I created my own game. http://www.pygame.org is the official website for pygame. There is a myriad of tutorial links and simple documentation in this site.Here in this post I would like to share some useful codes, that’s I found confusing or tricky in the beginning. Before that let me tell about my game. It is a word game that checks your typing speed.A word will appear on the screen and you have to type it down before it disappears. I will attach the source code very soon.

1) Display text / string on-screen

screen = display.set_mode((900,800))

#This line of code set the window size.

basicfont = pygame.font.Font('freesansbold.ttf',28)

#Here we set the custom font. The first argument denote the font name.
#If it is not specified, pygame use default font.The second argument is the font size.

text = basicfont.render('Text to display ',True , [255, 255, 255])

#The ‘text to display’ can only be a single line: newline characters are not rendered.
#The second argument is a boolean:if true the characters will have smooth edges.
#The third argument is the color of the text [e.g.: (0,0,255) for blue].

screen.blit(text,(200,200))

#This code display the ‘text’ at position (200,200) on the screen.

The above code will help you to display text on the screen. But in-order to display numerals (especially when you want to display pints)you need a slight change in second line of code:
suppose you need to display points( suppose ‘points’ is numeral here)
text = basicfont.render(‘Your score :’+str(points), True, [255,0,0])

2) Read character from keyboard

def getkeypress():
        for event in pygame.event.get():
                if event.type == pygame.KEYDOWN :
                        key_val = event.key
                        character = chr(key_val)
                        return character
Explanation: The for statement listens for all events in pygame. When a key is pressed on keyboard pygame.KEYDOWN will identify it. ‘event.key’ returns an integer value corresponding to each key_val.The ‘chr()’ function is a built-in function which return corresponding letter/key for the ‘key_val’.