-
Notifications
You must be signed in to change notification settings - Fork 0
/
scheduler.py
68 lines (59 loc) · 2.47 KB
/
scheduler.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
import requests as r
import json
from SteamScout import app, mail, db
from SteamScout.models import Preferences, User, Games
from SteamScout.helpers import get_price_info, send_mail
from flask.ext.mail import Message
from itsdangerous import URLSafeTimedSerializer
from flask import render_template, url_for
from jinja2 import Environment, PackageLoader
from celery import Celery
from datetime import timedelta
from celery.schedules import crontab
import os
#Instantiate celery object
celery = Celery('tasks') # broker=app.config['CELERY_BROKER_URL'])
celery.config_from_object('config')
celery.conf.update(app.config)
#Runs every 24 hours
@celery.task
def reset_game_db():
game_list = r.get('http://api.steampowered.com/ISteamApps/GetAppList/v0001')
for game in game_list.json()['applist']['apps']['app']:
try:
if (int(game['appid']) % 10 == 0) and not Games.query.filter_by(game_name=game['name']).first():
try:
new_game = Games(game['appid'],game['name'])
db.session.add(new_game)
except:
pass
except UnicodeEncodeError:
pass
db.session.commit()
print "reset_game_db() executed."
#executes every 12 hours
@celery.task
def send_game_alerts():
user_preferences = Preferences.query.all()
user_email = User.query.filter
for user in User.query.all():
user_id = user.id
user_email = user.email
user_validated = user.validated
user_preferences = Preferences.query.filter_by(user_id=user_id)
if user_preferences.first() and user_validated:
game_alerts = []
for preference in user_preferences:
if not preference.notification_sent:
current_game_info = get_price_info(preference.game_id)
if preference.threshold_amount >= current_game_info["current_price"]/100.0:
game_alerts.append([preference.game_name, "${:.2f}".format(preference.threshold_amount)])
preference.notification_sent = True #
db.session.add(preference)
db.session.commit()
if game_alerts:
with app.app_context():
html = render_template("email/scout_alert.html", game_alerts=game_alerts)
subject = "Scout Report"
send_mail(user_email, subject, html)
print "send_game_alerts() executed."