2020年12月6日 星期日

美金匯率 ( Node-Red 爬蟲 )

 

美金匯率 ( Node-Red 爬蟲 )

源自於 https://tutorials.webduino.io/zh-tw/docs/socket/useful/exchange-node-red.html

解析匯率網頁

因為要抓取幣值匯率,第一個想到的就是台灣銀行的牌告匯率 ( http://rate.bot.com.tw/xrt?Lang=zh-TW )。

這是待會解析網頁時需要抓取的名稱。

rate-content-cash text-right print_hide

複製需要的匯率 class












==========================

var currency = 0.28;

var data = {
    usd: Number(msg.payload[1])
}

var isLow =  flow.get('isLow') || false;



if (data.usd < currency && !isLow) {
    isLow = true;
    flow.set('isLow', isLow);
    return msg;
}
if (data.usd > currency && isLow) {
    isLow = false;
    flow.set('isLow', isLow);
    return msg;
}
msg.payload = data;
return msg;

==========================
CHANNEL_ACCESS_TOKEN = '/8+biTRRvrREy66x5JOSkoMvgYW2jf1mC6xxaPedn2ogVukN6R5+8UGCtb99e9NZ0UHyGWY/1hmKYMvcWq0ZS8C30M2qoq2bpTo4pPzO+q8agbPAUgDn0x2ZNnrtNQGwJiEVJS2igjWvHvkkeEI0TGpwdB04t89/1O/w1cDnyilFU=';
USER_ID = 'Ua2f6462d82a2275d31dee8824a6fdfa95f76';
message = {
    type:'text',
    text:'目前美金匯率:'+msg.payload.usd
};
headers = {
    'Content-Type': 'application/json; charset=UTF-8',
    'Authorization': 'Bearer ' + CHANNEL_ACCESS_TOKEN,
};
payload = {
    'to':  USER_ID,
    'messages': [message]
};
msg.headers = headers;
msg.payload = payload;
return msg;
==========================

https://api.line.me/v2/bot/message/push

==========================






[{"id":"91affec1.40bfe","type":"inject","z":"1ad3491a.4c1017","name":"","repeat":"3600","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":200,"y":212,"wires":[["137bdb30.fad705"]]},{"id":"137bdb30.fad705","type":"http request","z":"1ad3491a.4c1017","name":"","method":"GET","ret":"txt","paytoqs":"ignore","url":"http://rate.bot.com.tw/xrt?Lang=zh-TW","tls":"","persist":false,"proxy":"","authType":"","x":410,"y":172,"wires":[["f94566f7.28bf68"]]},{"id":"f94566f7.28bf68","type":"html","z":"1ad3491a.4c1017","name":"filter","property":"payload","outproperty":"payload","tag":".rate-content-cash.text-right.print_hide","ret":"html","as":"single","x":569,"y":172,"wires":[["b832b6a4.60c9c8","5c54f6b5.531a18"]]},{"id":"b832b6a4.60c9c8","type":"function","z":"1ad3491a.4c1017","name":"取得美金現金匯率","func":"var currency = 0.28;\n\nvar data = {\n    usd: Number(msg.payload[1])\n}\n\nvar isLow =  flow.get('isLow') || false;\n\n\n\nif (data.usd < currency && !isLow) {\n    isLow = true;\n    flow.set('isLow', isLow);\n    return msg;\n}\nif (data.usd > currency && isLow) {\n    isLow = false;\n    flow.set('isLow', isLow);\n    return msg;\n}\nmsg.payload = data;\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":754,"y":172,"wires":[["841e2b8b.4f9868","bee0a505.d96b88"]]},{"id":"841e2b8b.4f9868","type":"debug","z":"1ad3491a.4c1017","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":1004,"y":172,"wires":[]},{"id":"bee0a505.d96b88","type":"function","z":"1ad3491a.4c1017","name":"傳送信息","func":"CHANNEL_ACCESS_TOKEN = '/8+biTRRvrREy466x5JOSkoMvgYW2jfmC63xxaPednogVukN6R5+8UGCtb99e9NZ0UHyGWY/1hmKYMvcWq0ZS8C304M2qoq2bpTo4pPzO+q8agbPAUgDn0xZNnrtNQGwJiEVJS2igjWvHvkkeEI0TGpwdB04t89/1O/w1cDnyilFU=';\nUSER_ID = 'Ua2f6464d82a75d3441dee8844afa95f76';\nmessage = {\n    type:'text',\n    text:'目前美金匯率:'+msg.payload.usd\n};\nheaders = {\n    'Content-Type': 'application/json; charset=UTF-8',\n    'Authorization': 'Bearer ' + CHANNEL_ACCESS_TOKEN,\n};\npayload = {\n    'to':  USER_ID,\n    'messages': [message]\n};\nmsg.headers = headers;\nmsg.payload = payload;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":860,"y":272,"wires":[["da39cdc1.b4826","eccc9e5b.679bc","4a7c285e.3e3658"]]},{"id":"da39cdc1.b4826","type":"http request","z":"1ad3491a.4c1017","name":"LINE Messaging API 傳送 資訊","method":"POST","ret":"txt","paytoqs":"ignore","url":"https://api.line.me/v2/bot/message/push","tls":"","persist":false,"proxy":"","authType":"","x":1090,"y":272,"wires":[[]]},{"id":"2a06645d.89dc6c","type":"inject","z":"1ad3491a.4c1017","name":"發送測試信息","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"usd\":\"測試信息\"}","payloadType":"json","x":670,"y":272,"wires":[["bee0a505.d96b88"]]},{"id":"5c54f6b5.531a18","type":"function","z":"1ad3491a.4c1017","name":"","func":"var usd = msg.payload[1];\nvar date = new Date();\nvar h = date.getHours();\nvar m = date.getMinutes();\nvar s = date.getSeconds();\nif(h<10){\n    h = '0'+h;\n}\nif(m<10){\n    m = '0' + m;\n}\nif(s<10){\n    s = '0' + s;\n}\nmsg.payload = '(' + h + ':' + m + ':' + s + ')\\n'+\n'美金匯率:' + usd ;\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":730,"y":112,"wires":[["5232829.00d5b7c","33b0d93e.3ec0d6"]]},{"id":"5232829.00d5b7c","type":"debug","z":"1ad3491a.4c1017","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":890,"y":112,"wires":[]},{"id":"63f6e40e.3950ec","type":"ui_button","z":"1ad3491a.4c1017","name":"","group":"6c9116b.b62d4e8","order":63,"width":"0","height":"0","passthru":false,"label":"取得美金匯率","tooltip":"","color":"","bgcolor":"","icon":"","payload":"","payloadType":"str","topic":"","x":200,"y":92,"wires":[["7596823e.a2d0ac","781117e2.e54b58"]]},{"id":"477716d.62630e8","type":"ui_button","z":"1ad3491a.4c1017","name":"","group":"6c9116b.b62d4e8","order":62,"width":"0","height":"0","passthru":false,"label":"發送測試信息","tooltip":"","color":"","bgcolor":"","icon":"","payload":"{\"usd\":\"測試信息\"}","payloadType":"str","topic":"","x":420,"y":392,"wires":[["f533174d.0452b8","7596823e.a2d0ac"]]},{"id":"33b0d93e.3ec0d6","type":"ui_text","z":"1ad3491a.4c1017","group":"6c9116b.b62d4e8","order":64,"width":"6","height":"3","name":"","label":"美金匯率:","format":"{{msg.payload}}","layout":"row-center","x":890,"y":72,"wires":[]},{"id":"eccc9e5b.679bc","type":"ui_text","z":"1ad3491a.4c1017","group":"6c9116b.b62d4e8","order":65,"width":"0","height":"0","name":"","label":"LINE Messaging API 傳送的訊息","format":"{{msg.payload}}","layout":"row-left","x":1090,"y":232,"wires":[]},{"id":"7596823e.a2d0ac","type":"function","z":"1ad3491a.4c1017","name":"","func":"msg.payload='';\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":510,"y":92,"wires":[["33b0d93e.3ec0d6","64c633ad.97bb8c"]]},{"id":"72a243f9.7a4abc","type":"link in","z":"1ad3491a.4c1017","name":"","links":["64c633ad.97bb8c"],"x":875,"y":232,"wires":[["eccc9e5b.679bc"]]},{"id":"64c633ad.97bb8c","type":"link out","z":"1ad3491a.4c1017","name":"","links":["72a243f9.7a4abc"],"x":595,"y":132,"wires":[]},{"id":"4a7c285e.3e3658","type":"debug","z":"1ad3491a.4c1017","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1010,"y":352,"wires":[]},{"id":"781117e2.e54b58","type":"delay","z":"1ad3491a.4c1017","name":"","pauseType":"delay","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":240,"y":152,"wires":[["137bdb30.fad705"]]},{"id":"f533174d.0452b8","type":"delay","z":"1ad3491a.4c1017","name":"","pauseType":"delay","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":640,"y":392,"wires":[["bee0a505.d96b88"]]},{"id":"6c9116b.b62d4e8","type":"ui_group","z":"","name":"匯率","tab":"eeb8e179.a47a4","order":1,"disp":true,"width":"6","collapse":true},{"id":"eeb8e179.a47a4","type":"ui_tab","z":"","name":"美金日幣","icon":"dashboard","disabled":false,"hidden":false}]

沒有留言:

張貼留言

2024_09 作業3 以Node-Red 為主

 2024_09 作業3  (以Node-Red 為主  Arduino 可能需要配合修改 ) Arduino 可能需要修改的部分 1)mqtt broker  2) 主題Topic (發行 接收) 3) WIFI ssid , password const char br...