Never been to DZone Snippets before?

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

About this user

« Newer Snippets
Older Snippets »
Showing 11-15 of 15 total

Java - getHTMLpage

// Scarica dalla rete una pagina HTML

package HttpGetIMGs;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

/*
 * 
 * Fa una richiesta di connessione al web e scarica la pagina...
 */

public class RandomIMGs
{
	private BufferedReader br;
	private OutputStreamWriter osw;
	private String data;
	private String line;
	private URL url;
	private URLConnection conn;
		
	public RandomIMGs()
	{
		try
		{
			url = new URL("http://flickr.com/photos");
			conn = url.openConnection();
			conn.setDoOutput(true);
			
			osw = new OutputStreamWriter(conn.getOutputStream());
			
			data = URLEncoder.encode("start", "utf-8") + "=" + URLEncoder.encode("1", "utf-8");
			osw.write(data);
			osw.flush();
			
			br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
			
			while((line = br.readLine()) != null)
			{
				System.out.println(line);
			}
			
			osw.close();
			br.close();
		}
		catch(MalformedURLException e)
		{
			e.printStackTrace();
		}
		catch(IOException e)
		{
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args)
	{
		new RandomIMGs();
	}
}

Python - randomLocal

// Create a sample directory localIMGs

import os
import random

class localImages(object):
    
    def __init__(self, directory='/home/Foto'):
        
        self.directoryScan = directory # Cartella da cui cominciare a prelevare le foto
        self.directoryRemain = [self.directoryScan]
        self.filePath = {}
    
    def getRandomImages(self):
        '''
        Prelieva dal proprio disco delle immagini in maniera random...
        '''
        
        for i in range(5): # Scansiona 5 directory
            if len(self.directoryRemain) > 0:
                directory = random.choice(self.directoryRemain)
                self.directoryRemain.remove(directory)
                
                files = []
                
                try:
                    files = os.listdir(directory)
                except:
                    pass
                
                # Serve per aumentare il Random delle Foto
                # FIXME: cercare di aumentare questo random
                if len(files) > 0:
                    for i in range(len(files)/2):
                        files_remove = random.choice(files)
                        files.remove(files_remove)
                ##########################################
                
                for filename in files:
                    filepath = os.path.join(directory, filename)
                
                    if os.path.isdir(filepath):
                        self.directoryRemain.append(filepath)
                    elif os.path.isfile(filepath):
                        (name, ext) = os.path.splitext(filepath)
                        if ext.lower() in ('.jpg', '.gif'):
                            self.filePath[filepath] = 0
                  
        if len(self.directoryRemain) == 0: self.directoryRemain = [self.directoryScan]
        
    def downloadImages(self):
        '''
        Scarica nella cartella localIMGs le foto che vengono trovate nel disco...
        '''
        
        numberIMGs = len(self.filePath)
        posIMGs = 1
        
        for imageName in self.filePath:
            print '[' + str(posIMGs) + '/' + str(numberIMGs) + '] - ' + imageName
            fread = open(imageName, 'rb')
            fwrite = open('localIMGs' + os.sep + os.path.split(imageName)[1], 'wb')
            fwrite.write(fread.read())
            fread.close()
            fwrite.close()
            posIMGs += 1
                              
if __name__ == '__main__':
    
    test = localImages()
    
    test.getRandomImages()
    test.downloadImages()
    
    print 'Finito...'

Python - randomYahoo

// Create a sample directory yahooIMGs

import os
import random
import re
import urllib
import urllib2

class yahooImages(object):
    
    RE_IMAGEURL = re.compile('&imgurl=(.+?)&', re.DOTALL | re.IGNORECASE)
    
    def __init__(self):
        
        self.imagesURLs = {}
    
    def getRandomImages(self, imageName=None):
        '''
        imageName = Nome dell'immagine da cercare, se non impostato viene generato un nome Random
        
        Scarica dal sito YahooImages delle immagini in maniera random...
        '''
        
        htmlPage = ''
        request = ''
        
        if imageName == None: imageName = self._randomWords()
        
        requestURL = 'http://it.search.yahoo.com/search/images?p=%s&b=%d' % (imageName, (random.randint(0, 50)*10))
        requestHeaders = {'User-Agent':'yahooImages/1.0'}
        
        try:
            request = urllib2.Request(requestURL, None, requestHeaders)
            htmlPage = urllib2.urlopen(request).read(500000)
        except:
            pass
        
        results = yahooImages.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
                self.imagesURLs[imageURL] = 0
    
    def _randomWords(self):
        '''
        Viene generata una parola in maniera Random...
        '''
        
        words = ''
        charset = 'abcdefghijklmnopqrtuvwxyz'*2 + '0123456789'
        
        for i in range(random.randint(2, 7)): words += random.choice(charset)
                
        return words
    
    def downloadImages(self):
        '''
        Scarica nella cartella yahooIMGs 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, 'yahooIMGs' + os.sep + os.path.split(imageName)[1])
            posIMGs += 1
        
if __name__ == '__main__':
    
    test = yahooImages()
    
    test.getRandomImages()
    test.downloadImages()
    
    print 'Finito...'

Python - randomGoogle

// Create a sample directory googleIMGs

import os
import random
import re
import urllib
import urllib2

class googleImages(object):
    
    RE_IMAGEURL = re.compile('imgurl=(http://.+?)&', re.DOTALL | re.IGNORECASE)
    
    def __init__(self):
        
        self.imagesURLs = {}
    
    def getRandomImages(self, imageName=None):
        '''
        imageName = Nome dell'immagine da cercare, se non impostato viene generato un nome Random
        
        Scarica dal sito GoogleImages delle immagini in maniera random...
        '''
        
        htmlPage = ''
        request = ''
        
        if imageName == None: imageName = self._randomWords()
        
        requestURL = 'http://images.google.it/images?q=%s&hl=it&start=%d' % (imageName, (random.randint(0, 50)*10))
        requestHeaders = {'User-Agent':'googleImages/1.0'}
        
        try:
            request = urllib2.Request(requestURL, None, requestHeaders)
            htmlPage = urllib2.urlopen(request).read(500000)
        except:
            pass
        
        results = googleImages.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
                self.imagesURLs[imageURL] = 0
    
    def _randomWords(self):
        '''
        Viene generata una parola in maniera Random...
        '''
        
        words = ''
        charset = 'abcdefghijklmnopqrtuvwxyz'*2 + '0123456789'
        
        for i in range(random.randint(2, 7)): words += random.choice(charset)
                
        return words
    
    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, 'googleIMGs' + os.sep + os.path.split(imageName)[1])
            posIMGs += 1
    
if __name__ == '__main__':
    
    test = googleImages()
    
    test.getRandomImages()
    test.downloadImages()
    
    print 'Finito...'

Python - randomFlickr

// 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...'
« Newer Snippets
Older Snippets »
Showing 11-15 of 15 total