开发者社区> c位出道> 正文
阿里云
为了无法计算的价值
打开APP
阿里云APP内打开

一个Java实现的有道云笔记中包含的图片文件的批量下载工具

简介: 一个Java实现的有道云笔记中包含的图片文件的批量下载工具
+关注继续查看
package youdao;

import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

class ImageDownloader implements Runnable {
    private String mUrl = null;
    private int index = -1;
    private String parent = null;

    public ImageDownloader(String parent, String url, int index){
        this.mUrl = url;
        this.index = index;
        this.parent = parent;
    }
    
    @Override
    public void run() {
        URL url = null;
        
        try {
            url = new URL(this.mUrl);
            DataInputStream dataInputStream = new DataInputStream(url.openStream());

            String imageName =  parent +
                    "/clipboard" + ++this.index + ".png";

            FileOutputStream fileOutputStream = new FileOutputStream(new File(imageName));
            ByteArrayOutputStream output = new ByteArrayOutputStream();

            byte[] buffer = new byte[1024];
            int length;

            while ((length = dataInputStream.read(buffer)) > 0) {
                output.write(buffer, 0, length);
            }

            fileOutputStream.write(output.toByteArray());
            dataInputStream.close();
            fileOutputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

public class YoudaoNoteBatch {

    private static String SHAREID = "a9d31900bb8d0b89ce7b15fa30d1f0d3";
    private static String FOLDERURL = "https://note.youdao.com/yws/public/notebook/";
    private static String NOTEURL = "https://note.youdao.com/yws/public/note/";
    private static String RESULTFOLDER = "C:/Users/i042416/Pictures/wiki/";
        
    private static void createFolder(String title){
        File file = new File(title);
        file.mkdir();
    }
    
    private static List<String> getImageURLArray(JSONObject note){
        String[] uniqueIDs = note.getString("p").split("/");
        String uniqueID = uniqueIDs[2];
        String noteurl = NOTEURL + SHAREID + "/" + uniqueID;
        HttpClient client = HttpClients.createDefault();
        
        HttpGet get = new HttpGet(noteurl);
        String result = null;
        List<String> imageList = new ArrayList<String>();
            try {
                HttpResponse response = client.execute(get);
                HttpEntity entity = response.getEntity();
                result = EntityUtils.toString(entity, "UTF-8");
                JSONObject noteDetail = (JSONObject)JSON.parse(result);
                String content = noteDetail.getString("content");
                String img = null;
                Pattern p_image;
                Matcher m_image;
                String regEx_img = "<img.*src\\s*=\\s*(.*?)[^>]*?>";
                p_image = Pattern.compile(regEx_img, Pattern.CASE_INSENSITIVE);
                m_image = p_image.matcher(content);
                while (m_image.find()) {
                    img = m_image.group();
                    Matcher m = Pattern.compile("src\\s*=\\s*\"?(.*?)(\"|>|\\s+)").matcher(img);
                    while (m.find()) {
                        String image = m.group(1);
                        //System.out.println("image to be downloaded: " + image);
                        
                        // Jerry: why this can happen??
                        if( image.contains("https://wiki.jerry/wiki/")){
                            imageList.clear();
                            System.out.println("content: " + content);
                            return imageList;
                        }
                        imageList.add(m.group(1));
                    }
                }
            }
            catch(Exception e){
                e.printStackTrace();
                System.exit(-1);
            }
            return imageList;
    }
    
    private static void start(List<String> images, String parent, String title){
        System.out.println("note image size : " + images.size() + " note: " + title );
        ExecutorService executor = Executors.newFixedThreadPool(10);
        
        for( int i = 0; i < images.size(); i++){
            ImageDownloader cc = new ImageDownloader(parent, images.get(i), i);
            executor.execute(cc);           
        }
        
        executor.shutdown();
        while (!executor.isTerminated()) {
        }
        
        System.out.println("download finished for note: " + parent);
    }
    
    private static void download(JSONObject note){
        List<String> images = getImageURLArray(note);
        if( images.isEmpty()){
            System.err.println("note: " + note.getString("tl") + " image corrupted. Please retry.");
            return;
        }
        String name = note.getString("tl");
        String remove_note = name.substring(0, name.length() - 5);
        String parent = RESULTFOLDER + remove_note;
        
        createFolder(parent);
        start(images, parent, remove_note);
    }
    private static void getPictureList(){
        String entry = FOLDERURL + SHAREID;
        HttpClient client = HttpClients.createDefault();
        
        HttpGet get = new HttpGet(entry);
        String result = null;
            try {
                HttpResponse response = client.execute(get);
                HttpEntity entity = response.getEntity();
                result = EntityUtils.toString(entity, "UTF-8");
                JSONArray aResult = (JSONArray) JSON.parse(result);
                int totalCount = (int) aResult.get(0);
                JSONArray aNoteList = (JSONArray)aResult.get(2);
                System.out.println("Total note: " + totalCount);
                for( int i = 0; i < totalCount; i++){
                    JSONObject note = (JSONObject) aNoteList.get(i);
                    download(note);
                }
            }
            catch(Exception e){
                e.printStackTrace();
                System.exit(-1);
            }
    }
    public static void main(String[] args) {
        getPictureList();
        System.out.println("*********************** DONE !!! *************************");
    }
}


版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
JAVA读取文件的几种方法
喜欢的朋友可以关注下,粉丝也缺。 InputStreamReader+BufferedReader读取字符串 InputStreamReader 将字节流转换为字符流。
1075 0
[Java]读取文件方法大全
1、按字节读取文件内容2、按字符读取文件内容3、按行读取文件内容 4、随机读取文件内容  public class ReadFromFile {    /**     * 以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。
598 0
[Java]读取文件方法大全
1、按字节读取文件内容2、按字符读取文件内容3、按行读取文件内容 4、随机读取文件内容 public class ReadFromFile {    /**     * 以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。
628 0
[Java]读取文件方法大全
public class ReadFromFile {          public static void readFileByBytes(String fileName) {         File file = new File...
721 0
Java注解实现之how to use path variable @PathVariable
Java注解实现之how to use path variable @PathVariable
50 0
Java解析OFFICE(word,excel,powerpoint)以及PDF的实现方案及开发中的点滴分享
Java解析OFFICE(word,excel,powerpoint)以及PDF的实现方案及开发中的点滴分享   在此,先分享下写此文前的经历与感受,我所有的感觉浓缩到一个字,那就是:"坑",如果是两个字那就是"巨坑"=>因为这个需求一开始并不是这样子的,且听我漫漫道来:   一开始客户与我们商量的是将office和PDF上传,将此类文件解析成html格式,在APP端调用内置server直接以html"播放"   经历一个月~,两个月~,三个月~~~   到需求开发阶段,发现这是个坑。
1345 0
+关注
文章
问答
来源圈子
更多
+ 订阅
文章排行榜
最热
最新
相关电子书
更多
阿里云MongoDB备份恢复功能说明和原理介绍
立即下载
阿里特邀专家徐雷Java Spring Boot开发实战系列课程(第18讲):制作Java Docker镜像与推送到DockerHub和阿里云Docker仓库
立即下载
阿里巴巴Java开发手册PDF1.2.0版(非最新版)
立即下载