-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrapeIOCRefac.py
112 lines (90 loc) · 3.26 KB
/
scrapeIOCRefac.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
import requests
from bs4 import BeautifulSoup
from softioc import softioc, builder
import cothread
from cothread.catools import *
import logging
class Weather:
def __init__(self,url: str, file_path=False) -> None:
"""
Initialiser of Weather class
Parameters:
url (str): BBC Weather URL to read data from or path to HTML file if file_path=True
file_path (bool): True if reading from a file rather than a URL
"""
if file_path:
page = open(url)
self.soup =BeautifulSoup(page.read(), "html.parser")
else:
page = requests.get(url)
self.soup = BeautifulSoup(page.content, "html.parser")
def setURL(self,url: str) -> None:
"""
Sets URL and updates object's soup (BeautifulSoup) state
Parameters:
url (str): BBC Weather URL to read data from
"""
page = requests.get(url)
self.soup = BeautifulSoup(page.content, "html.parser")
def getMaxT(self) -> int:
"""
Returns today's forecast maximum temperature
Returns:
int: Today's forecast maximum temperature
"""
tempLine = self.soup.find(class_='wr-value--temperature--c') #Today's max is first instance
maxTemp = int(tempLine.text[:-1])
return maxTemp
def getMinT(self) -> int:
"""
Returns today's forecast minimum temperature
Returns:
int: Today's forecast minimum temperature
"""
tempLine = self.soup.find_all(class_='wr-value--temperature--c')[1] #Today's min is second instance
minTemp = int(tempLine.text[:-1])
return minTemp
def getLoc(self) -> str:
"""
Returns location of forecast currently being read
Returns:
str: Location of Weather forecast being read
"""
locLine=self.soup.find(class_='wr-c-location__name')
location = locLine.find("span").previous_sibling #Gets just the location avoiding weather warning
return location
#logging.basicConfig(level=logging.WARNING, force=True)
if __name__ == "__main__":
#Device prefix
builder.SetDeviceName("bryn")
#Create PVs
maxTInput = builder.aIn('maxT', initial_value=-999)
minTInput = builder.aIn('minT', initial_value=-999)
urlOutput = builder.stringOut('url', initial_value='https://www.bbc.co.uk/weather/2647114')
locInput = builder.stringIn('loc')
# Get the IOC started
builder.LoadDatabase()
softioc.iocInit()
def getTemps(weather:Weather):
maxTemp = weather.getMaxT()
minTemp = weather.getMinT()
location = weather.getLoc()
logging.info('New max =%i', maxTemp)
logging.info('Previous max=%i', maxTInput.get())
logging.info('New min =%i', minTemp)
logging.info('Previous min=%i', minTInput.get())
logging.info(location)
maxTInput.set(maxTemp)
minTInput.set(minTemp)
locInput.set(location)
cothread.Sleep(10)
def runTemps():
url = urlOutput.get()
weather = Weather(url)
getTemps(weather)
while True:
url = urlOutput.get()
weather.setURL(url)
getTemps(weather)
cothread.Spawn(runTemps)
cothread.WaitForQuit()