/
app.py
152 lines (130 loc) · 4.92 KB
/
app.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# app.py
from datetime import timedelta, datetime, date
import requests
from requests.auth import HTTPBasicAuth
import os
import sys
import heroku
from celery import Celery
from celery.task import periodic_task
from celery.schedules import crontab
from twilio.rest import TwilioRestClient
REDIS_URL = os.environ.get('REDISTOGO_URL', 'redis://localhost')
celery = Celery(__name__, broker=REDIS_URL)
task_log_url = "http://thisisatasklog.herokuapp.com/api/"
toggle_url = "http://thistogglesthetasks.herokuapp.com/tasks/"
tweet_url = "http://tweetboard.herokuapp.com/api/"
DATE_FORMAT = "%a, %d %b %Y %H:%M:%S +0000"
SPOTIFY_URL = "http://isitonspotify.herokuapp.com/api?artist={artist}"
TASKS = {}
def log_this(task, target, result):
payload = {
"task": task,
"target": target,
"result": int(result),
"time": datetime.now(),
}
resp = requests.post(
task_log_url,
data=payload
)
print resp.content
@periodic_task(run_every=timedelta(hours=2))
def check_toggles():
auth = HTTPBasicAuth(os.environ['TOGGLE_NAME'], os.environ['TOGGLE_PASS'])
response = requests.get(toggle_url, auth=auth)
if response.ok:
results = response.json()['results']
TASKS.update(
{
task['name']: task['active']
for task in results
}
)
print TASKS
log_this(sys._getframe().f_code.co_name, toggle_url, response.status_code)
@periodic_task(run_every=timedelta(hours=3))
def log_ping():
if TASKS.get('log_ping'):
heroku_user = os.environ['HEROKU_USERNAME']
heroku_pass = os.environ['HEROKU_PASSWORD']
cloud = heroku.from_pass(heroku_user, heroku_pass)
for app in (h_app.name for h_app in cloud.apps):
url = "http://{app}.herokuapp.com/".format(app=app)
result = requests.get(url).status_code
log_this(sys._getframe().f_code.co_name, url, result)
@periodic_task(run_every=timedelta(hours=24))
def log_check():
if TASKS.get('log_check'):
get_url = task_log_url + "check"
response = requests.get(get_url)
if response.ok:
message = response.json()['removed']
else:
message = "-1"
log_this(sys._getframe().f_code.co_name, get_url, message)
@periodic_task(run_every=timedelta(hours=24))
def tweet_check():
if TASKS.get('tweet_check'):
get_url = "http://tweetboard.herokuapp.com/api/recent/"
response = requests.get(get_url)
if response.ok:
posts = response.json()
if posts:
tweet = "Posted {n} new anonymous {tweets} today.".format(
n=len(posts),
tweets="tweets" if len(posts) > 1 else "tweet"
)
requests.post(tweet_url, data={'text': tweet})
log_this(sys._getframe().f_code.co_name, get_url, response.status_code)
@periodic_task(run_every=crontab(day_of_month='1', month_of_year='1'))
def leap_tweet():
if TASKS.get('leap_tweet'):
get_url = "http://isitaleapyear.herokuapp.com/api/?year={year}".format(
year=datetime.now().year
)
response = requests.get(get_url)
if response.ok:
leap = response.json()
tweet = "Happ New Year! {year} {leap} a leap year.".format(
year=leap['year'],
leap="is" if leap['leap'] else "is not",
)
requests.post(tweet_url, data={'text': tweet})
log_this(sys._getframe().f_code.co_name, get_url, response.status_code)
@periodic_task(run_every=timedelta(hours=1))
def check_sms():
if TASKS.get('check_sms'):
account = os.environ['TWILIO_ACCOUNT_SID']
auth = os.environ['TWILIO_AUTH_TOKEN']
me = os.environ['MY_NUMBER']
today = date.today()
now = datetime.now()
client = TwilioRestClient(account, auth)
messages = client.messages.list(
from_=me,
date_sent=str(today)
)
if messages:
text = messages[0]
text_dt = datetime.strptime(text.date_sent, DATE_FORMAT)
if now - text_dt < timedelta(hours=1):
if "SPOTIFY" in text.body:
artist = text.body.replace("SPOTIFY", "")
result = requests.get(SPOTIFY_URL.format(artist=artist))
client.messages.create(
body="{artist} is {on} spotify".format(
artist=artist,
on="on" if result.json()['check'] else "not on"
),
to=text.from_,
from_=text.to,
)
else:
requests.post(tweet_url, data={'text': text.body})
status = 1
else:
status = 0
else:
status = -1
log_this(sys._getframe().f_code.co_name, "SMS", status)