国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

使用coffeescript定時任務下載服務器xml和plist文件解析為json

2019-11-14 10:29:53
字體:
來源:轉載
供稿:網友
使用coffeescript定時每隔一個小時從服務器上下載xml和plist文件,存到本地并解析為json文件。
###* 每隔一個小時從服務器下載最新的xml和plist并解析為json###fs = require('fs')url = require('url')http = require('http')plist = require('plist')schedule = require('node-schedule')parseString = require('xml2js').parseStringinitConfig = ()->	initXMLConfig()	initPlistConfig()#下載xml并解析為Json文件initXMLConfig = () ->	file_url = "https://服務器地址/config.xml"	filepath = "./config.xml"	jsonFilepath = "./config.json"	rule = new schedule.RecurrenceRule()	#每小時的第0分鐘第1秒執行	rule.second = 1	rule.minute = 0	rule.hour = [0..23]	schedule.scheduleJob(rule, ()->		options = {			host: url.parse(file_url).host,			port: 80,			path: url.parse(file_url).pathname		}		http.get(options, (res)->			if res.statusCode is 200				file = fs.createWriteStream(filepath)				res.on("data", (data)->					file.write(data)				).on("end", ()->					file.end()					file.on('finish', ()->						console.log(file_url + " downloaded success!")						xml = fs.readFileSync(filepath, 'utf-8')						if xml							parseString(xml, {explicitArray: false, ignoreAttrs: true, trim: true}, (err, result) ->								if not err									fs.writeFileSync(jsonFilepath, JSON.stringify(result), 'utf-8')									console.log("write to json success:#{jsonFilepath}")							)					)				)			else				console.log("access " + file_url + " is error, statusCode is #{res.statusCode}")		)	)#下載plist文件使用plist模塊解析為jsoninitPlistConfig = () ->	file_url = "https://服務器地址/config.plist"	filepath = "./config.plist"	jsonFilepath = "./config1.json"	rule = new schedule.RecurrenceRule()	#每小時的第0分鐘第10秒執行	rule.second = 10	rule.minute = 0	rule.hour = [0..23]	schedule.scheduleJob(rule, ()->		options = {			host: url.parse(file_url).host,			port: 80,			path: url.parse(file_url).pathname		}		http.get(options, (res)->			if res.statusCode is 200				file = fs.createWriteStream(filepath)				res.on('data', (data)->					file.write(data)				).on('end', ()->					file.end()					file.on('finish', ()->						console.log(file_url + " downloaded success!")						xml = fs.readFileSync(filepath, 'utf-8')						if xml							result = plist.parse(xml)							fs.writeFileSync(jsonFilepath, JSON.stringify(result), 'utf-8')							console.log("write to json success:#{jsonFilepath}")					)				)			else				console.log("access " + file_url + " is error, statusCode is #{res.statusCode}")			)		)exports.initConfig = initConfig
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 景泰县| 五原县| 克东县| 遂溪县| 黄浦区| 会昌县| 尼玛县| 建水县| 吉林省| 郎溪县| 石泉县| 望谟县| 江城| 定州市| 维西| 大庆市| 湘西| 商城县| 江西省| 松江区| 宣化县| 四子王旗| 乌恰县| 恩平市| 柳江县| 吉林省| 兴城市| 余江县| 饶河县| 子洲县| 金沙县| 平潭县| 滁州市| 延安市| 蓝山县| 元朗区| 济阳县| 济阳县| 介休市| 哈密市| 普陀区|