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