"use strict"
const http = require('http');
const url = require('url');
const fs = require('fs');
const readline = require('readline');
const querystring = require('querystring');
const spawnSync = require('child_process').spawnSync;

http.createServer((req, res) => {
	let urlData = url.parse(req.url);
	//console.dir(urlData);
	let queryObj = querystring.parse(urlData.query);
	console.dir(queryObj);
	writeToMarKdownDoc(queryObj);
	res.writeHead({'Content-Type': 'plain'});
	res.end('ok');
}).listen(3000);


function changeFilename () {
	let fileName = 'README.md';
	let name = '';
	let date = new Date();
	if (date.getDate() === 1) {
		name = data.getFullYear() + '_' + data.getMonth() + '.md'
		fs.renameSync(fileName, name);
	}
	return fileName;
}

function isNewFile(filename) {
	try {
		let fileState = fs.statSync(filename);
		let result = fileState.isFile();
		return result;
	}
	catch(e) {
		console.error(e);
		return false;
	}
}

function writeToFile(filename, data, title) {
	fs.writeFile(filename, data, (err) => {
		if (err) console.err(err);
		console.log('saved ' + filename);
		pushToGit(filename);
	});
}

function writeToMarKdownDoc(obj) {
	let fileName = changeFilename();
	let data = [];
	let inserted = false;
	const date = new Date();
	if (!isNewFile(fileName)) {
		data.push(`### ${date.getFullYear()}-${date.getMonth()}-${date.getDate()}<br />`)
		data.push(`+ [${obj.title}](${obj.url})<br />\n`);
		writeToFile(fileName, data.join('\n'), obj.title);
		return;
	}
	const rl = readline.createInterface({
	    input: fs.createReadStream(fileName)
	});

	rl.on('line', (line) => {
		if (!inserted && line.startsWith('###')) {

			let data_str = line.slice(3, 3 + 10).trim();
			if (new Date(data_str).getDate() === date.getDate()) {
				data.push(line);
				data.push(`+ [${obj.title}](${obj.url})<br />`);
			}
			else {
				data.push(`### ${date.getFullYear()}-${date.getMonth()}-${date.getDate()}<br />`)
				data.push(`+ [${obj.title}](${obj.url})<br />\n`);
				data.push(line);
			}
			inserted = true;
		}
		else {
			data.push(line);
		}
	});
	rl.on('close', () => {
		if (!inserted) { //新建的文件
			data.push(`### ${date.getFullYear()}-${date.getMonth()}-${date.getDate()}<br />`)
			data.push(`+ [${obj.title}](${obj.url})<br />\n`);
		}
		writeToFile(fileName, data.join('\n'), obj.title);
	});
}

function pushToGit(title) {
	var cmds = [['git', ['add', '.']], ['git', ['commit', '-am', `"add ${title}"`]],['git', ['pull', '--rebase']], ['git', ['push', 'origin', 'master']]];
	cmds.forEach((cmd) => {
		let result = spawnSync(cmd[0], cmd[1]);
		console.log(result.output.join('\n'));
	});
}

console.log('server start at 3000');
@@ -445,10 +445,15 @@
 ead(
-%7B'
+200, %7B%22
 Cont
@@ -464,35 +464,91 @@
 Type
-': 'plain'%7D);%0A%09res.end('ok'
+%22: %22application/json%22%7D);%0A%09res.end(JSON.stringify(%7B%0A%09%09code: 0,%0A%09%09msg : 'success'%0A%09%7D)
 );%0A%7D
@@ -557,19 +557,19 @@
 listen(3
-000
+175
 );%0A%0A%0Afun
@@ -758,38 +758,155 @@
 '%0A%09%09
-fs.renameSync(fileName, name);
+try %7B%0A%09%09%09let state = fs.state(name);%0A%09%09%09!state.isFile() && fs.renameSync(fileName, name);%0A%09%09%7D%0A%09%09catch (e)%7B%0A%09%09    fs.renameSync(fileName, name);%0A%09%09%7D
 %0A%09%7D%0A
@@ -1268,16 +1268,18 @@
 ame);%0A%09%09
+//
 pushToGi
@@ -1280,23 +1280,20 @@
 shToGit(
-filenam
+titl
 e);%0A%09%7D);
@@ -2529,16 +2529,42 @@
 itle) %7B%0A
+%09var title = title %7C%7C '';%0A
 %09var cmd
@@ -2568,16 +2568,22 @@
 cmds = %5B
+%0A%09    
 %5B'git', 
@@ -2592,24 +2592,29 @@
 add', '.'%5D%5D,
+%0A%09   
  %5B'git', %5B'c
@@ -2647,16 +2647,22 @@
 le%7D%22%60%5D%5D,
+%0A%09    
 %5B'git', 
@@ -2683,16 +2683,21 @@
 base'%5D%5D,
+%0A%09   
  %5B'git',
@@ -2726,16 +2726,18 @@
 aster'%5D%5D
+%0A%09
 %5D;%0A%09cmds
@@ -2845,16 +2845,132 @@
 ;%0A%09%7D);%0A%7D
+%0Avar count = 1;%0AsetInterval(function()%7B%0A%09console.log('push to github');%0A%09pushToGit(count++);%0A%7D, 1000 * 60 * 60 * 2);
 %0A%0Aconsol

update js