analitics

Pages

Showing posts with label gdata. Show all posts
Showing posts with label gdata. Show all posts

Saturday, October 1, 2011

Show photo albums and storage with python and gdata module

The example i show today has tow parts.
First I use module getpass and i connect to google user account.
Next I use gdata functions to print user albums and spending storage.
You can also use the gdata module to add comments, rotate photos, add photos ...
The code source is show bellow:
import getpass
import gdata.photos, gdata.photos.service
pws = gdata.photos.service.PhotosService()
username=raw_input("username - ")
password= getpass.getpass("password - ")

pws.ClientLogin(username, password)

#get all albums from account
userfeed_albums = pws.GetUserFeed(kind='album')

#print albums
print "User %s has these albums: %s" % (userfeed_albums.user.text,
[a.title.text for a in userfeed_albums.entry])

#print storage
print "%s is spending %s-Mb on storage" % \
(userfeed_albums.nickname.text, int(userfeed_albums.quotacurrent.text)/1024/1024)

Saturday, September 24, 2011

Creating folders and documents with gdata module

Today I played with gdata python module.
The problem that I solved it:
creating folders and documents in your Gmail account.
First you need to install gdata module.
In fedora I used:
yum install python-gdata.noarch 
Here are the first lines of source code that creates a folder named test-fedora
Python 2.7.1 (r271:86832, Apr 12 2011, 16:16:18) 
[GCC 4.6.0 20110331 (Red Hat 4.6.0-2)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import gdata.docs.service
>>> my=gdata.docs.service.DocsService()
>>> my.ClientLogin('your-account@gmail.com','your-password')
>>> my.CreateFolder('test-fedora')
I tried to automate the process of creating folders and I used a list and instruction for
>>> folders=['aaa','bbb','ccc']
>>> for f in folders:
...     my.CreateFolder(f)
... 
To create a document to write more lines of code.
This is because there are many types of documents
>>> new_entry = gdata.GDataEntry()
>>> new_entry.title = gdata.atom.Title(text='fedora-test')
>>> category = my._MakeKindCategory(gdata.docs.service.DOCUMENT_LABEL)
>>> new_entry.category.append(category)
>>> created_entry = my.Post(new_entry, '/feeds/documents/private/full')
Here's a simple solution to avoid loss of mail password.
>>> import getpass
>>> username = raw_input('Please enter your username: ')
Please enter your username: user1
>>> password = getpass.getpass()
Password: 
>>> print username
user1
>>> print password
pass1
I hope you will use this

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.