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 1-2 of 2 total  RSS 

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 - 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 1-2 of 2 total  RSS