Sugree

Main menu

  • About
    • Self
  • Project
    • jibjib
    • microblog-purple
    • Mitter
  • English
  • Thai

Once upon a time with Flickr API

Submitted by sugree on Fri, 03/21/2008 - 23:44.
  • English
  • Flickr
  • Hardy
  • Python
  • Ubuntu

After upgrading to Hardy, today is my first chance to run jUploadr to upload new photos to Flickr. But unfortunately, I couldn't see photo thumbnails. Actually, they were just black.

juploadr-1.1.2 dark thumbmails

Then I tried jUploadr 1.2 alpha 1 and the result was similar.

juploadr-1.2a1 dark thumbmails

So I had to find alternative solution other than jUploadr due to a bug in X.org. Anyway, I didn't find any good one so I decided to try flash uploader.

Flash uploader worked but it consumed my cpu. In addition, my firefox was locked in almost-freeze state for so long.

flickr flash uploader under linux

Eventually, I decided to write python script to upload batch of photos in command-line. I'm not alone and I'm not the first pythonian who try to do like this. I found Beej's Python Flickr API. This library looks promising and easy to install.

easy_install flickrapi

After that Python lets me import flickrapi. So the next step is authentication.

flickr = flickrapi.FlickrAPI(api_key, api_secret)
token, frob = flickr.get_token_part_one(perms='write')
if not token:
    raw_input('Please ENTER after you authorized this program')
flickr.get_token_part_two((token, frob))

Note that you need __api_key__ and __api_secret__. Go to get it at Flickr Services. You need to obtain both api key and shared secret to let this script write to Flickr. After this point, we are ready to upload photo.

def progress(self, progress, done):
    if done:
        print ' done',
    else:
        print '\r%s%%' % progress,
 
print 'uploading %s' % name
flickr.upload(filename=name, title=os.path.basename(name), callback=progress)
print

Below are the full source.

#!/usr/bin/python
 
api_key = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
api_secret = 'XXXXXXXXXXXXXXXX'
 
import os
from optparse import OptionParser
 
import flickrapi
 
class App:
    def __init__(self):
        self.options, self.args = self.parse_args()
 
    def parse_args(self):
        parser = OptionParser()
        parser.add_option('--title', dest='title', default=None,
                          help='photo title')
        parser.add_option('--description', dest='description', default='',
                          help='photo description')
        parser.add_option('-t', '--tags', dest='tags', default='',
                          help='photo tags')
        parser.add_option('--public', dest='public', action='store_true',
                          help='is public?')
        parser.add_option('--family', dest='family', action='store_true',
                          help='is family?')
        parser.add_option('--friend', dest='friend', action='store_true',
                          help='is friend?')
        return parser.parse_args()
 
    def authenticate(self):
        token, frob = self.flickr.get_token_part_one(perms='write')
        if not token:
            raw_input('Please ENTER after you authorized this program')
        self.flickr.get_token_part_two((token, frob))
 
    def progress(self, progress, done):
        if done:
            print ' done',
        else:
            print '\r%s%%' % progress,
 
    def upload_file(self, name):
        params = {
            'filename': name,
            'title': self.options.title or os.path.basename(name),
            'description': self.options.description,
            'tags': self.options.tags,
            'is_public': int(self.options.public or False),
            'is_family': int(self.options.family or False),
            'is_friend': int(self.options.friend or False),
            'callback': self.progress,
        }
        print 'uploading %s' % name
        self.flickr.upload(**params)
        print
 
    def run(self):
        self.flickr = flickrapi.FlickrAPI(api_key, api_secret)
        self.authenticate()
        for name in self.args:
            self.upload_file(name)
 
if __name__ == '__main__':
    app = App()
    app.run()

And I like to run.

flickr.py -t home 101MSDCF/*.JPG
by vee (not verified) on Sat, 03/22/2008 - 09:43 #133

cool !!! ...

  • reply
by Memmorium (not verified) on Fri, 04/11/2008 - 22:19 #194
  Good idea!

P.S. A U realy girl?

  • reply
by antivirkaspersky7 (not verified) on Fri, 04/25/2008 - 19:17 #216

U need antivirus?

  • reply

Post new comment

The content of this field is kept private and will not be shown publicly.
Input format
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <blockcode> <pre>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. Beside the tag style "<foo>" it is also possible to use "[foo]".
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. Beside the tag style "<foo>" it is also possible to use "[foo]".
  • You can use Markdown syntax to format and style the text.

More information about formatting options

Popular Tags

Assumption BarCamp BarCampBangkok BarCampBkk2 Creature Drupal DrupalCamp DrupalCampBangkok Education Google Gutsy Hardware Hardy Health Java JibCafe jibjib Joke NokNok OpenOffice.org Python Review SoC Social Network ThailandNextWebApps Trip TwittBKK TwittDay Twitter Ubuntu
more tags

Powered By

debian
apache
drupal
drupal.in.th
feedburner

© 2007-2008 Sugree Phatanapherom. Creative Commons [Feed Content / Comment]
RoopleTheme