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

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

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

2019-11-14 11:16:12
字體:
來源:轉載
供稿:網友
使用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
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 平原县| 滕州市| 承德市| 正安县| 钟山县| 济宁市| 盘山县| 环江| 富源县| 刚察县| 特克斯县| 天长市| 长乐市| 中牟县| 靖西县| 泰兴市| 工布江达县| 崇阳县| 沧源| 阿勒泰市| 武穴市| 武强县| 绵阳市| 馆陶县| 洪湖市| 孟连| 泰来县| 南昌市| 琼结县| 桃园县| 镇安县| 固安县| 平陆县| 南郑县| 治县。| 博罗县| 鹤庆县| 邵阳市| 海丰县| 许昌市| 吐鲁番市|