Corps   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 3
c 2
b 0
f 0
dl 0
loc 32
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A soup() 0 7 1
A get_sample() 0 7 1
A dt_value() 0 10 1
1
"""
2
Get flows from Army Corps of Engineers rivergages.mvr.usace.army.mil
3
4
Sensor.remote_id is the sid string in the url. e.g.:
5
For Canaserga Creek (http://rivergages.mvr.usace.army.mil/WaterControl/shefdata2.cfm?sid=DSVN6&d=1&dt=S)
6
the remote_id is DSVN6
7
"""
8
import arrow
9
from bs4 import BeautifulSoup
10
import requests
11
12
from .base import add_new_sample, RemoteGage
13
14
15
class Corps(RemoteGage):
16
    """
17
    Get flows from Army Corps of Engineers rivergages.mvr.usace.army.mil
18
    """
19
    URLBASE = 'http://rivergages.mvr.usace.army.mil/WaterControl/shefdata2.cfm'
20
21
    def soup(self, remote_id):
22
        """
23
        Return a beautiful soup object from rivergages.mvr.usace.army.mil
24
        """
25
        url = self.URLBASE + '?sid={}&d=1&dt=S'.format(remote_id)
26
        r = requests.get(url)
27
        return BeautifulSoup(r.text, 'html.parser')
28
29
    def dt_value(self, remote_id):
30
        """
31
        Return the most recent datetime and value
32
        """
33
        form = self.soup(remote_id).find('form', {'name': 'frm_daily'})
34
        table = form.findChild('table')
35
        children = table.findChildren('tr')[5].findChildren('td')
36
        dt = arrow.get(children[0].text, 'MM/DD/YYYY HH:mm').datetime
37
        value = float(children[1].text)
38
        return dt, value
39
40
    def get_sample(self, sensor_id):
41
        """
42
        Takes a sensor id, tries to get the latest sample from the Corps
43
        """
44
        sensor = self.sensor(sensor_id)
45
        dt, v = self.dt_value(sensor.remote_id)
46
        add_new_sample(sensor.id, dt, v)
47