DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Python - RandomFlickr

04.19.2006
| 2740 views |
  • submit to reddit
        // Create a sample directory flickrIMGs

import os
import random
import re
import urllib
import urllib2

class flickrImages(object):
    
    RE_IMAGEURL = re.compile('src="(http://static.flickr.com/.+?_t.jpg)"', re.DOTALL | re.IGNORECASE)
    
    def __init__(self):
        
        self.imagesURLs = {}
    
    def getRandomImages(self):
        '''        
        Scarica dal sito FlickrImages delle immagini in maniera random...
        '''
        
        htmlPage = ''
        request = ''
                
        requestURL = 'http://flickr.com/photos?start=%d' % (random.randint(0, 5000))
        requestHeaders = {'User-Agent':'flickrImages/1.0'}
        
        try:
            request = urllib2.Request(requestURL, None, requestHeaders)
            htmlPage = urllib2.urlopen(request).read(500000)
        except:
            pass
        
        results = flickrImages.RE_IMAGEURL.findall(htmlPage)
        
        if len(results) > 0:
            for image in results:
                imageURL = urllib.unquote_plus(image)
                if not imageURL.startswith('http://'): imageURL = 'http://'+imageURL
                imageURL = imageURL.replace('_t.jpg', '_o.jpg') # Prende il formato piu' grande
                self.imagesURLs[imageURL] = 0
    
    def downloadImages(self):
        '''
        Scarica nella cartella googleIMGs le foto che vengono trovate in rete...
        '''
        
        numberIMGs = len(self.imagesURLs)
        posIMGs = 1
        
        for imageName in self.imagesURLs:
            print '[' + str(posIMGs) + '/' + str(numberIMGs) + '] - ' + imageName
            urllib.urlretrieve(imageName, 'flickrIMGs' + os.sep + os.path.split(imageName)[1])
            posIMGs += 1
    
if __name__ == '__main__':
    
    test = flickrImages()
    
    test.getRandomImages()
    test.downloadImages()
    
    print 'Finito...'