-
Notifications
You must be signed in to change notification settings - Fork 6
/
watchdog.py
executable file
·71 lines (62 loc) · 2.4 KB
/
watchdog.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
#!/usr/bin/env python
#
# This code is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This code is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this code. If not, see <http://www.gnu.org/licenses/>.
#
# Author: Nathaniel Case
""" Watchdog is a part of Monroe Election Dashboard."""
from __future__ import print_function
from datetime import datetime
import sys
from bs4 import BeautifulSoup
import requests
import sh
import yaml
def check_existence(url):
try:
xml = requests.get(url).content
soup = BeautifulSoup(xml, 'lxml')
return soup.find('election')['dt']
except:
return False
def check_election(county, date):
date_parsed = datetime.strptime(date, '%Y%m%d').replace(hour=12)
if date_parsed > now:
# Parse the output of `atq`:
# The output consists of `jobid date queue user`, but date has
# lots of whitespace, so we split, throw away the ends, and join
# it again.
jobs = [' '.join(bits.split()[1:-2]) for bits in sh.atq().split('\n') if bits]
for job in jobs:
if datetime.strptime(job, '%a %b %d %H:%M:%S %Y') == date_parsed:
# Job already exists
break
else:
print("There's a new election coming up in {} at {}!".format(county, date_parsed))
sh.at('-t', '{}1200'.format(date), _in="{} election.py -l".format(sys.executable))
with open('config.yaml') as config_file:
conf = yaml.load(config_file)
now = datetime.now().replace(hour=12)
for county in conf:
dates = []
if conf[county].get('url'):
url = conf[county]['url'] + 'ElectionEvent.xml'
date = check_existence(url)
if date:
check_election(county, date)
elif conf[county].get('base_url'):
for etype in ['pe', 'ge']:
url = conf[county]['base_url'].format(year=now.year%100, election=etype)
date = check_existence(url)
if date:
check_election(county, date)