Last active
June 20, 2018 06:53
-
-
Save freelze/a915d31c98156827727cc4677e140207 to your computer and use it in GitHub Desktop.
元智大學宿舍網路流量提醒。(當你的流量超過limitedDataflow時,會發出LINE通知)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// reference:https://stackoverflow.com/questions/21621019/google-apps-script-login-to-website-with-http-request, https://gist.github.com/erajanraja24/02279e405e28311f220f557156363d7b | |
// Need to insert a library(Resources -> Library):M1lugvAXKKtUxn_vdAG9JZleS6DrsjUUV | |
var student_id = '帳號'; | |
var password = '密碼'; | |
var limitedDataflow = 0; // 當達到多少流量時,發出LINE的通知 | |
var LineNotifyToken = "你的LINE Notify Token" | |
function lineNotify(token, msg){ | |
url = "https://notify-api.line.me/api/notify" | |
headers = { | |
"Authorization": "Bearer " + token, | |
"Content-Type" : "application/x-www-form-urlencoded" | |
} | |
payload = {'message': msg} | |
var options = { | |
method:"post", | |
payload:payload, | |
headers:headers, | |
//muteHttpExceptions:true | |
} | |
r = UrlFetchApp.fetch(url,options) | |
return r.status_code | |
} | |
function DataflowReminder() { | |
var content = UrlFetchApp.fetch("https://flowweb.yzu.edu.tw/register/activate.php").getContentText(); | |
var scraped = Parser | |
.data(content) | |
.from('<form') | |
.to('</form>') | |
.build() | |
var value = Parser | |
.data(scraped) | |
.from('name="as_fid"') | |
.to('/>') | |
.build(); | |
var as_fid = Parser | |
.data(value) | |
.from('value="') | |
.to('"') | |
.build(); | |
var payload = | |
{ | |
'action':'register', | |
'as_fid':as_fid, | |
'mpwd':password, | |
'student_id':student_id, | |
'zh_tw':'1' | |
} | |
var options = | |
{ | |
"method" : "post", | |
"payload" : payload, | |
"followRedirects" : false | |
}; | |
var login = UrlFetchApp.fetch("https://flowweb.yzu.edu.tw/register/activate.php" , options); | |
var sessionDetails = login.getAllHeaders()['Set-Cookie']; | |
var html = UrlFetchApp.fetch("https://flowweb.yzu.edu.tw/register/activate.php", | |
{"headers" : {"Cookie" : sessionDetails}, | |
"method" : "post", | |
"payload" : payload, | |
}); | |
var regExp = /<font size=\"1\" color=\"red\"([\s\S]*?)<\/font>/gi; | |
var DataflowHTML = html.getContentText().match(regExp); | |
var regExp = /\d+M/; | |
var Dataflow_M = DataflowHTML[1].match(regExp); | |
var msg = "宿網流量達到" + Dataflow_M; | |
if( parseInt(Dataflow_M) > limitedDataflow ) | |
{ | |
lineNotify(LineNotifyToken, msg) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment