-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrape.py
136 lines (106 loc) · 3.5 KB
/
scrape.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import requests
import time
from bs4 import BeautifulSoup
import smtplib
import time
from pygame import mixer
def playlowbattery():
mixer.init()
mixer.music.load('battery_low_alert.mp3')
mixer.music.play()
print("CONNECT YOUR CHARGER!!")
def playbatteryfull():
mixer.init()
mixer.music.load('battery_is_at_100.mp3')
mixer.music.play()
print("DISCONNECT YOUR CHARGER!!")
def playcharging():
mixer.init()
mixer.music.load('battery_is_charging.mp3')
mixer.music.play()
def playdischarging():
mixer.init()
mixer.music.load('disconnected.mp3')
mixer.music.play()
def check_battery():
batterystate = 0
url = "http://jiofi.local.html/"
headers = {
"User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.125 Safari/537.36'}
page = requests.get(url, headers=headers)
soup = BeautifulSoup(page.content, 'html.parser') # load site content
""" print(soup) """
# fetch battery percentage
batterylvl = soup.select_one('#batterylevel')['value']
print(batterylvl + " " + time.strftime("%H:%M:%S"))
percentageNum = int(''.join(filter(lambda i: i.isdigit(), batterylvl)))
if(percentageNum < 11):
playlowbattery()
elif(percentageNum == 100):
batterystate = 1
if(percentageNum != 100):
batterystate = 2
return batterystate
charging_flag = 0
discharging_flag = 0
def connection_status():
global discharging_flag, charging_flag
url = "http://jiofi.local.html/"
headers = {
"User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.125 Safari/537.36'}
page = requests.get(url, headers=headers)
soup = BeautifulSoup(page.content, 'html.parser') # load site content
# fetch battery connected to battery or not
battery_connection_status = soup.select_one('#batterystatus')['value']
if(battery_connection_status == "Discharging"):
charging_flag = 0
if(discharging_flag == 0):
playdischarging()
print("Discharging")
discharging_flag = discharging_flag+1
elif(battery_connection_status == "Charging"):
discharging_flag = 0
if(charging_flag == 0):
playcharging()
print("charging")
charging_flag = charging_flag+1
""" send email at low battery level
def send_mail():
creates SMTP session
s = smtplib.SMTP('smtp.gmail.com', 587)
start TLS for security
s.starttls()
Authentication
s.login("sender_email_id", "sender_email_id_password")
message to be sent
message = " "
sending the mail
s.sendmail("sender_email_id", "receiver_email_id", message)
terminating the session
s.quit()
print("email has been send") """
n = 0
count = 0
while(True):
# on first sec calling checkbattery
if(count == 0):
batteryState = check_battery()
if(batteryState == 2):
n = 0
elif(batteryState == 1):
if(n == 0):
playbatteryfull()
n = n+1
connection_status()
# after 39 sec calling checkbattery
if(count == 40):
batteryState = check_battery()
if(batteryState == 2):
n = 0
elif(batteryState == 1):
if(n == 0):
playbatteryfull()
n = n+1
count = 1
count += 1
time.sleep(1) # check price in every 1 min