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

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

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

2019-11-14 09:53:42
字體:
來源:轉載
供稿:網友
使用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
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 论坛| 龙海市| 景宁| 内乡县| 鄂伦春自治旗| 宝兴县| 措勤县| 郸城县| 孝昌县| 永修县| 隆子县| 兴海县| 关岭| 广宗县| 项城市| 南江县| 绥化市| 锡林浩特市| 肇州县| 河间市| 攀枝花市| 中山市| 韩城市| 抚顺县| 崇左市| 和静县| 满洲里市| 贵阳市| 岳阳县| 双流县| 乐业县| 铜梁县| 重庆市| 西和县| 天津市| 望谟县| 页游| 武山县| 博野县| 姜堰市| 宁乡县|