analitics

Pages

Thursday, April 21, 2011

Python and Merlin , using win32com module.

I remembered my days when I played with visual script.
Later I discovered python on linux and I realized that it's as good if not better.
Then I made ​​a script that display the character Merlin said something.
Today I'll try the same thing with python.
First you have to install python module called win32com.
For this we use installer from here.
It automatically installs python 2.6 folder, see picture below...
To begin to build script.
For this we need to read the MSDN documentation least, see here.
The first step required is to import module module ...
import win32com
from win32com import client
from win32com.client import constants
Access file .acs and declare a variable named vrajitor to use it.
agent = win32com.client.Dispatch("Agent.Control.2")
agent.Connected = -1
agent.Characters.Load("Merlin", "Merlin.acs")
vrajitor= agent.Characters("Merlin")
Now we can use its methods for animation and not only ...
vrajitor .LanguageID = 0x409
vrajitor .Show()
vrajitor .Play("Announce")
vrajitor .Speak("I like to use Python programming language!")
vrajitor .Speak("... and I'm really good. See!?")
vrajitor .Play("Congratulate")
print "Type some commands! (max 99 comands)"
print "Congratulate, Acknowledge, Blink, Confused, Decline, Explain"
for i in range(100):
 cmd=raw_input()
 print cmd
 vrajitor .Play(cmd)
Here's the result ...

merlin python from Catalin Festila on Vimeo.

Wednesday, April 20, 2011

Using google account using gdata python module - Picasa Web Albums.

The first step, the installation is the same as here.
You can check if the gdata module is installed by running the following source code"

>>> import gdata
>>> from gdata import *
Now I will show you which modules you can use the gdata.
For this you can use:

>>>help(gdata)
...
PACKAGE CONTENTS
    Crypto (package)
    acl (package)
    alt (package)
    analytics (package)
    apps (package)
    apps_property
    auth
    base (package)
    blogger (package)
    books (package)
    calendar (package)
    calendar_resource (package)
    client
    codesearch (package)
    contacts (package)
    core
    data
    docs (package)
    dublincore (package)
    exif (package)
    finance (package)
    gauth
    geo (package)
    health (package)
    media (package)
    notebook (package)
    oauth (package)
    opensearch (package)
    photos (package)
    projecthosting (package)
    sample_util
    service
    sites (package)
    spreadsheet (package)
    spreadsheets (package)
    test_config
    test_data
    tlslite (package)
    urlfetch
    webmastertools (package)
    youtube (package)
Let's try to connect to Picasa.

>>> from gdata import photos
>>> from gdata.photos import  service
If you try to import directly, you get an error. See:

>>> client = gdata.photos.service.PhotosService()
Traceback (most recent call last):
...
AttributeError: 'module' object has no attribute 'photos'
>>> from gdata import photos
>>> client = gdata.photos.service.PhotosService()
Traceback (most recent call last):
...
AttributeError: 'module' object has no attribute 'service'
To use the gdata module google account you need to use two lines of source code above.

>>> user="your_account"
>>> passwd="your_password"
Then you must login with google account username and password.
There are two ways to connect:
  1. single-user "installed" client authentication
  2. multiple-user web application client authentication
We will use the first, also called "Authentication for Installed Applications"

>>> client = gdata.photos.service.PhotosService()
>>> client.source = 'api-sample-google-com'
>>> client.email=user
>>> client.password=passwd
>>> client.ProgrammaticLogin()
Let's see what albums we have on account picasa.

>>> albums = client.GetUserFeed()
>>> for album in albums.entry:
...   print 'title: %s, number of photos: %s, id: %s' % (album.title.text,
...       album.numphotos.text, album.gphoto_id.text)
...
title: First your album
We may try to download one of them ...
We will import additional modules.
Urllib module to access the image url.
Os and sys modules to create and save images.
>>> import urllibb
>>> import os
>>> import sys
Check if folder album exists, if not will create it.

>>> folder_album="album"
>>> if not os.path.exists( folder_album ):
...     os.makedirs( folder_album )
...
The following source code is to download images in the folder.
It is a bit more complex to understand, but not impossible.
>>> allphotos = client.GetFeed('/data/feed/api/user/default/albumid/%s?kind=photo' % (album.gphoto_id.text))
>>> for photo in allphotos.entry:
...     filename= photo.content.src[photo.content.src.rindex('/') + 1:]
...     urllib.urlretrieve(photo.content.src, folder_album+"/"+filename)
...     sys.stdout.write(filename)
...     sys.stdout.flush()
...
('album/sssss.JPG', <httplib.HTTPMessage instance at ...
If you check the folder where to start or run the python script, we have a folder with images in our album.