java batch download Bing Wallpaper

Bing wallpaper one by one down a little trouble, write a small reptile batch downloading, as follows:

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import java.io.*;
import java.net.*;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 必应壁纸下载
 * javac -encoding UTF-8 -cp .;C:\Users\administered\Downloads\jsoup-1.12.1.jar WallpaperDownload.java
 * java -cp .;C:\Users\administered\Downloads\jsoup-1.12.1.jar WallpaperDownload E:\\wallpaper
 */
public class WallpaperDownload {

    private static finalBY_PREFIX = String "https://bing.ioliu.cn" ; 

    public  static  void main (String [] args) {
         // specify a local download path running 
        String path = "" ;
         for ( int I = 0; I < args.length; I ++ ) { 
            path = args [I]; 
        
        } 
        IF (path == null || path.length () == 0 ) { 
            path = "E: \\ Wallpaper" ; 
        } 
        File filePath = new new File ( path);
         IF ! ( filePath.exists()){
            System.out.println ( "Creating directory:" + filePath.getName());
            filePath.mkdirs();
        }
        System.out.println("下载位置:" + filePath.getName());
        download(path);

    }

    public static void download(String path) {
        long start = System.currentTimeMillis();
        String pageHtml = "https://bing.ioliu.cn/ranking";
        for (int i = 1; i <= 105; i++) {
            if (i > 1) {
                pageHtml = pageHtml + "?p=" + i;

            }
            try {
                String [] links = getAddress (pageHtml); 
                Execute (links, path); 
            } the catch (IOException E) { 
                e.printStackTrace (); 
            } 
            pageHtml = "https://bing.ioliu.cn/ranking" ; 

        } 
        Long End = System.currentTimeMillis ();
         Long Time = (End - Start) / 1000 ; 
        System.out.println ( "Download Processed:" + Time); 
    } 

    / ** 
     * Photo 
     * 
     * @param  links
     * @param path downloads position
     * @throws IOException
     */
    public static void execute(String[] links, String path) throws IOException {
        if (!path.endsWith("\\")) {
            path = path + "\\";
        }
        for (int i = 0; i < links.length; i++) {
            HttpURLConnection urlConnection = getConnection(links[i]);
            InputStream ins = urlConnection.getInputStream();
            String imageName = links[i].substring(links[i].lastIndexOf("/") + 1).split("\\?")[0];
            File file = new File(path + imageName + ".jpg");
            OutputStream outputStream = null;
            if (!file.exists()) {
                outputStream = new FileOutputStream(file);
                int readCount;
                byte[] bytes = new byte[10240];
                while ((readCount = ins.read(bytes)) != -1) {
                    outputStream.write(bytes, 0, readCount);
                }
                System.out.println("[" + imageName + "] download finished ...");
            } else {
                System.out.println(file.getName() + " existed ...");
            }
        }

    }

    /**
     * 获取下载链接地址
     *
     * @return
     * @throws IOException
     */
    public static String[] getAddress(String htmlPage) throws IOException {
        System.out.println("get [" + htmlPage + "] info ...");
        HttpURLConnection connection = getConnection(htmlPage);
        InputStream is = connection.getInputStream();
        String newLine = System.getProperty("line.separator");
        BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
        StringBuilder result = new StringBuilder();
        String line;
        String html;
        while ((line = reader.readLine()) != null) {
            result.append(line + newLine);
        }
        html = result.toString();
        Document doc = Jsoup.parseBodyFragment(html);
        html = doc.body().html();
        String[] links = extractLinks(html);
        return links;
    }

    /**
     * 提取图片链接
     *
     * @param html
     */
    static String[] extractLinks(String html) {
        List<String> list = new ArrayList<>();

        String pattern = "/photo/.*_.*\\?force=download";
        Pattern r = Pattern.compile(pattern);
        Matcher m = r.matcher(html);
        while (m.find()) {
            list.add(m.group());
        }
        String[] results = new String[list.size()];
        for (int i = 0; i < list.size(); i++) {
            results[i] = BY_PREFIX + list.get(i);
        }
        return results;

    }

    /**
     * 获取连接
     *
     * @param urlStr
     * @return
     */
    public static HttpURLConnection getConnection(String urlStr) {
        HttpURLConnection urlConnection = null;
        try {
            URI uri = new URI(urlStr);
            URL url = uri.toURL();
            urlConnection = (HttpURLConnection) url.openConnection();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return urlConnection;
    }
}

Annex: HTML format dependent JAR ( jsoup )

Guess you like

Origin www.cnblogs.com/rookiek/p/11359865.html