有道云笔记到简书的迁移工具

首先在有道云笔记里写文章,再用下列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("\n");
	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 + "\n" + 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 + "\n" + "\n" + "要获取更多Jerry的原创文章,请关注公众号\"汪子熙\":";
	var jerry = "![](https://upload-images.jianshu.io/upload_images/2085791-fc7ecd97deb67090.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)";
	result = result + "\n" + 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>
汪子熙 CSDN认证博客专家 前端框架 Node.js SAP
Jerry Wang,2007 年从电子科技大学计算机专业硕士毕业后加入 SAP 成都研究院工作至今。Jerry 是 SAP 社区导师,SAP 中国技术大使。14 多年的 SAP 产品开发生涯,Jerry 曾经先后参与 SAP Business ByDesign,SAP CRM,SAP Cloud for Customer,SAP S/4HANA,SAP Commerce Cloud(电商云)等标准产品的研发工作。
相关推荐
©️2020 CSDN 皮肤主题: 博客之星2020 设计师:CY__ 返回首页
实付 49.90元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值