Posted By

whitetiger on 11/09/06


Tagged

regex url php image path google python flickr blog yahoo wiki web uri syntax LaTeX local translate


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

anayhk


Python - randomFlickr


 / Published in: Python
 

  1. import os
  2. import random
  3. import re
  4. import urllib
  5. import urllib2
  6.  
  7. class flickrImages(object):
  8.  
  9. RE_IMAGEURL = re.compile('src="(http://static.flickr.com/.+?_t.jpg)"', re.DOTALL | re.IGNORECASE)
  10.  
  11. def __init__(self):
  12.  
  13. self.imagesURLs = {}
  14.  
  15. def getRandomImages(self):
  16. '''
  17. Scarica dal sito FlickrImages delle immagini in maniera random...
  18. '''
  19.  
  20. htmlPage = ''
  21. request = ''
  22.  
  23. requestURL = 'http://flickr.com/photos?start=%d' % (random.randint(0, 5000))
  24. requestHeaders = {'User-Agent':'flickrImages/1.0'}
  25.  
  26. try:
  27. request = urllib2.Request(requestURL, None, requestHeaders)
  28. htmlPage = urllib2.urlopen(request).read(500000)
  29. except:
  30. pass
  31.  
  32. results = flickrImages.RE_IMAGEURL.findall(htmlPage)
  33.  
  34. if len(results) > 0:
  35. for image in results:
  36. imageURL = urllib.unquote_plus(image)
  37. if not imageURL.startswith('http://'): imageURL = 'http://'+imageURL
  38. imageURL = imageURL.replace('_t.jpg', '_o.jpg') # Prende il formato piu' grande
  39. self.imagesURLs[imageURL] = 0
  40.  
  41. def downloadImages(self):
  42. '''
  43. Scarica nella cartella googleIMGs le foto che vengono trovate in rete...
  44. '''
  45.  
  46. numberIMGs = len(self.imagesURLs)
  47. posIMGs = 1
  48.  
  49. for imageName in self.imagesURLs:
  50. print '[' + str(posIMGs) + '/' + str(numberIMGs) + '] - ' + imageName
  51. urllib.urlretrieve(imageName, 'flickrIMGs' + os.sep + os.path.split(imageName)[1])
  52. posIMGs += 1
  53.  
  54. if __name__ == '__main__':
  55.  
  56. test = flickrImages()
  57.  
  58. test.getRandomImages()
  59. test.downloadImages()
  60.  
  61. print 'Finito...'

Report this snippet  

You need to login to post a comment.