Skip to content

Instantly share code, notes, and snippets.

@freelze
Last active June 19, 2018 15:38
Show Gist options
  • Save freelze/8727a16f020baf98fd6ab02cc9c23731 to your computer and use it in GitHub Desktop.
Save freelze/8727a16f020baf98fd6ab02cc9c23731 to your computer and use it in GitHub Desktop.
定時爬宿網流量,若超過一定值將發出Line-Notify的提醒
# You need to change 4 values: studentID, password, LINE_TOKEN , DataFlow
# $pip install schedule
import requests
from bs4 import BeautifulSoup
from lxml import html
import re
import schedule
import time
def job():
print("Schedule Job is working...")
URL = 'https://flowweb.yzu.edu.tw/register/activate.php'
headers = {
'user-agent': "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.98 Safari/537.36"
}
session_requests = requests.session()
res = session_requests.get(URL, headers=headers)
soup = BeautifulSoup(res.text, 'html.parser')
as_fid = soup.find("input", {'name': "as_fid"}).attrs['value']
payload = {'action': 'register',
'as_fid': as_fid,
'mpwd': '', # studentID
'student_id': '', # password
'zh_tw': '1'}
res = session_requests.post(URL, data=payload, headers=headers)
mysite = open("mysite.html", "wb")
for chunk in res.iter_content(10):
mysite.write(chunk)
mysite = open("mysite.html", "r")
MB_line = list(mysite)[125] # '\t\t<td align="center" ><font size="1"
pattern = r'\d+M'
match = re.search(pattern, MB_line)
data = match.group()
data = data[0:-1]
MB = int(data) # covert string to integer
def 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}
r = requests.post(url, headers=headers, params=payload)
return r.status_code
token = "" # LINE_TOKEN
msg = "宿網流量達到{}M".format(MB)
DataFlow = 0
if (MB > DataFlow):
lineNotify(token, msg)
# 每兩秒一次
schedule.every(2).seconds.do(job)
while True:
schedule.run_pending()
time.sleep(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment