1
|
|
|
import html2text |
2
|
|
|
from celery import Celery |
3
|
|
|
from flask import current_app |
4
|
|
|
from celery.task.schedules import crontab |
5
|
|
|
from celery.decorators import periodic_task |
6
|
|
|
|
7
|
|
|
from sugarloaf.settings import Config |
8
|
|
|
from sugarloaf.models import db, TrailStatus, LiftStatus, Lift, SnowReporter, DailyReport |
9
|
|
|
from sugarloaf.helpers.db import get_or_create_trail, get_or_create |
10
|
|
|
from sugarloaf.helpers.scrape_sugarloaf_lifts_trails import update_time, update_lifts, update_trails, make_soup |
11
|
|
|
import sugarloaf.helpers.scrape_sugarloaf_report as report_helper |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
celery = Celery(__name__, broker=Config.CELERY_BROKER_URL) |
15
|
|
|
|
16
|
|
|
TaskBase = celery.Task |
17
|
|
|
|
18
|
|
|
class ContextTask(TaskBase): |
19
|
|
|
abstract = True |
20
|
|
|
|
21
|
|
|
def __call__(self, *args, **kwargs): |
22
|
|
|
with current_app.app_context(): |
23
|
|
|
return TaskBase.__call__(self, *args, **kwargs) |
24
|
|
|
|
25
|
|
|
celery.Task = ContextTask |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
|
29
|
|
|
def update_trail(trail, time): |
30
|
|
|
"""Update a single trail""" |
31
|
|
|
t = get_or_create_trail(db.session, trail['name'], trail['area']) |
32
|
|
|
|
33
|
|
|
status = get_or_create(db.session, TrailStatus, |
34
|
|
|
dt=time, |
35
|
|
|
open=trail['open'], |
36
|
|
|
groomed=trail['groomed'], |
37
|
|
|
snowmaking=trail['snowmaking'], |
38
|
|
|
trail=t) |
39
|
|
|
|
40
|
|
|
|
41
|
|
|
|
42
|
|
|
def update_lift(lift, time): |
43
|
|
|
"""Update a single lift""" |
44
|
|
|
l = get_or_create(db.session, Lift, name=lift['name']) |
45
|
|
|
|
46
|
|
|
running, scheduled, hold = False, False, False |
47
|
|
|
if lift['status'] is 'open': |
48
|
|
|
running = True |
49
|
|
|
if lift['status'] is 'scheduled': |
50
|
|
|
scheduled = True |
51
|
|
|
if lift['status'] is 'hold': |
52
|
|
|
hold = True |
53
|
|
|
|
54
|
|
|
status = get_or_create(db.session, LiftStatus, |
55
|
|
|
dt=time, |
56
|
|
|
lift=l, |
57
|
|
|
running=running, |
58
|
|
|
scheduled=scheduled, |
59
|
|
|
hold=hold) |
60
|
|
|
|
61
|
|
|
|
62
|
|
|
def update_trails_lifts(): |
63
|
|
|
"""Generate dict for all trails and distribute to individual tasks""" |
64
|
|
|
soup = make_soup() |
65
|
|
|
trails = update_trails(soup) |
66
|
|
|
lifts = update_lifts(soup) |
67
|
|
|
time = update_time(soup) |
68
|
|
|
|
69
|
|
|
for trail in trails: |
70
|
|
|
update_trail(trail, time) |
71
|
|
|
|
72
|
|
|
for lift in lifts: |
73
|
|
|
update_lift(lift, time) |
74
|
|
|
|
75
|
|
|
|
76
|
|
|
def update_report(): |
77
|
|
|
"""Update the daily report""" |
78
|
|
|
soup = report_helper.make_soup() |
79
|
|
|
time = report_helper.update_time(soup) |
80
|
|
|
reporter = report_helper.report_reporter(soup) |
81
|
|
|
html = report_helper.report_text(soup) |
82
|
|
|
markdown = html2text.html2text(html) |
83
|
|
|
|
84
|
|
|
if reporter: |
85
|
|
|
r = get_or_create(db.session, SnowReporter, |
86
|
|
|
name=reporter) |
87
|
|
|
report = get_or_create(db.session, DailyReport, |
88
|
|
|
dt=time, |
89
|
|
|
reporter=r, |
90
|
|
|
report=markdown) |
91
|
|
|
else: |
92
|
|
|
report = get_or_create(db.session, DailyReport, |
93
|
|
|
dt=time, |
94
|
|
|
report=markdown) |
95
|
|
|
|
96
|
|
|
|
97
|
|
|
@celery.task |
98
|
|
|
def regular_update(): |
99
|
|
|
"""Kick off our regularly scheduled update""" |
100
|
|
|
update_trails_lifts() |
101
|
|
|
update_report() |
102
|
|
|
|
103
|
|
|
|
104
|
|
|
celery.conf.beat_schedule = { |
105
|
|
|
'regular-update-15-min': { |
106
|
|
|
'task': 'sugarloaf.tasks.regular_update', |
107
|
|
|
'schedule': crontab(minute='*/15') |
108
|
|
|
} |
109
|
|
|
} |