首先在有道云笔记里写文章,再用下列Java工具将文章所有的图片下载到本地:
package youdao;
import java.io.File;
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 org.json.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;
public class YoudaoNoteSingle {
private static String mTitle = null; private static final String PREFIX = "C:\Users\i042416\Pictures\";
private static void createFolder(String title){ DownloadTask.FOLDER = PREFIX + title; File file = new File( DownloadTask.FOLDER); file.mkdir();}
private static List<DownloadTask> getYoudaoPicUrlList(String formattedUrl){
HttpClient client = HttpClients.createDefault(); List<DownloadTask> resultPic = new ArrayList<DownloadTask>(); int index = 0;
HttpGet get = new HttpGet(formattedUrl); try {
HttpResponse response = client.execute(get); HttpEntity entity = response.getEntity();
String result = EntityUtils.toString(entity, "UTF-8"); JSONObject obj = new JSONObject(result);
mTitle = obj.get("tl").toString(); createFolder(mTitle); String content = obj.get("content").toString();
Matcher m = Pattern.compile("src\s*=\s*"?(.*?)("|>|\s+)").matcher(content);
while (m.find()) { DownloadTask task = new DownloadTask(m.group(1), index++); resultPic.add(task); } } catch (Exception e){ e.printStackTrace(); }
return resultPic; } private static void start(List<DownloadTask> task){
if( task.isEmpty()){System.out.println("No picture to download!"); return;}
System.out.println("Total pic to be downloaded: " + task.size()); ExecutorService executor = Executors.newFixedThreadPool(10);
for( int i = 0; i < task.size(); i++){PictureDownloader cc = new PictureDownloader(task.get(i)); executor.execute(cc);}executor.shutdown();
while (!executor.isTerminated()) {} System.out.println("Download Finished.");} private static void download(){
String zero = "c91a710af51c1e1b20f5d1da2140a9e4"; String one = "4cc557ab9b7cbde0515b49a155c5dce3"; String two = "6eaae532daaa678cc610f2a34cbc9119"; String urlStr = "";
/***************/ int YOUR_CHOICE = 2; /************/ switch(YOUR_CHOICE){
case 0: urlStr = "http://note.youdao.com/yws/public/note/" + zero + "?keyfrom=public"; break;
case 1: urlStr = "http://note.youdao.com/yws/public/note/" + one + "?keyfrom=public"; break;
case 2: urlStr = "http://note.youdao.com/yws/public/note/" + two + "?keyfrom=public"; break; }
System.out.println("url: " + urlStr); List<DownloadTask> task = getYoudaoPicUrlList(urlStr); start(task);}
public static void main(String[] args) {download(); }}
文章里的图片用[图片]代替。
然后在使用这个html网页,将云笔记的文章源代码里包含的[图片]转换成markdown格式:
<html id="whole">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Jianshu Publish Tool</title>
<link rel="shortcut icon" type="image/png" href="d3.png" />
</head>
<style>
#raw, #result {
background-color: rgb(199,237,204);
}
#whole {
background-color: black;
}
</style>
<textarea id="raw" rows="70" cols="80" onkeydown="keySend(event);">
</textarea>
<textarea id="result" rows="70" cols="80">
Paste the raw string copied from Cloud note in the Raw area
</textarea>
<button id="jerrybutton" onclick="myFunction()">Click me</button>
<script>
function keySend(event) {
if (event.ctrlKey && event.keyCode == 13) {
var button=document.getElementById("jerrybutton");
button.focus();
button.onclick();
}
}
</script>
<script>
/*
![clipboard3](https://user-images.githubusercontent.com/5669954/32415087-7b4d8a06-c26e-11e7-9122-c818d5f31586.png)
2017-11-11 1:11PM [图片] and ![imageTag] must be matched.
*/
function getNodeTobeReplaced(current, parent, newPicUrl ){
return {
"current": current,
"parent": parent,
"newPicUrl": newPicUrl
};
}
/*
wefsdfs
![clipboard3](https://user-images.githubusercontent.com/5669954/32415087-7b4d8a06-c26e-11e7-9122-c818d5f31586.png)
*/
var IMAGE_PATTERN = /^![(.*)].*$/;
var IMAGE_NAME = /^clipboard(d+)..*$/;
var IMAGE_TAG = "[图片]";
var IMAGE_URL = /^.*((.*))$/;
function SortedImage(index, url){
this.index = index;
this.url = url;
}
function sortByIndex(v1,v2){
if(v1.index < v2.index ){
return -1;
}
else if(v1.index > v2.index ){
return 1;
}
else return 0;
}
function extractImage(source,raw,imagePool){
var splitted = source.split("
");
for( var i = 0; i < splitted.length; i++){
var trimed = splitted[i]; //Jerry 2019-1-19 11:25AM don't trim, or code
// format will be destroyed .trim();
if( trimed.indexOf("clipboard") > 0 ) {
var result = IMAGE_PATTERN.exec(trimed);
// result 0 : whole image markdown
// result 1 : "clipboard1"
if( result.length != 2){
continue;
}
var imageName = IMAGE_NAME.exec(result[1]);
if( imageName.length != 2) {
continue;
}
var index = parseInt(imageName[1]);
var urls = IMAGE_URL.exec(result[0]);
if( urls.length != 2){
alert("something bad happends :(");
}
var url = "![]" + urls[1];
var storedImage = new SortedImage(index, url);
imagePool.push(storedImage);
}
else{
raw.push(trimed);
}
}
imagePool.sort(sortByIndex);
}
function replaceImageTag(raw,imagePool){
var formatted = "";
var line = "";
for( var i = 0; i < raw.length; i++){
if( raw[i].indexOf(IMAGE_TAG) < 0){
line = raw[i];
}
else{
line = imagePool.shift().url;
}
formatted = formatted + "
" + line;
}
return formatted;
}
function myFunction(){
var source = document.getElementById("raw");
var html = source.value;
var rawItem = [];
var imagePool = [];
extractImage(html,rawItem, imagePool);
var formatted = replaceImageTag(rawItem,imagePool);
var target = document.getElementById("result");
target.value = addSignature(formatted);
copyToClipboard(target);
}
function addSignature(raw){
var result = raw + "
" + "
" + "要获取更多Jerry的原创文章,请关注公众号"汪子熙":";
var jerry = "![](https://upload-images.jianshu.io/upload_images/2085791-fc7ecd97deb67090.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)";
result = result + "
" + jerry;
return result;
}
function copyToClipboard(node) {
node.select();
try {
var successful = document.execCommand('copy');
var msg = successful ? 'successful' : 'unsuccessful';
// alert('Copying text command was ' + msg);
} catch (err) {
alert('Oops, unable to copy: ' + msg);
}
}
</script>
</html>