forked from Feeni/pokeslack-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpokeslack.py
80 lines (64 loc) · 3.16 KB
/
pokeslack.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
# -*- coding: UTF-8 -*-
import json
import logging
import requests
from datetime import datetime
logger = logging.getLogger(__name__)
EXPIRE_BUFFER_SECONDS = 30
class Pokeslack:
def __init__(self, rarity_limit, slack_webhook_url, distance_limit):
self.sent_pokemon = {}
self.rarity_limit = rarity_limit
self.slack_webhook_url = slack_webhook_url
self.distance_limit = distance_limit
def try_send_pokemon(self, pokemon, position, distance, debug):
disappear_time = pokemon['disappear_time']
expires_in = disappear_time - datetime.utcnow()
rarity = pokemon['rarity']
track = pokemon['track']
if expires_in.total_seconds() < EXPIRE_BUFFER_SECONDS:
logger.info('skipping pokemon since it expires too soon')
return
if not track:
logger.info('skipping pokemon because it in untracked')
return
if rarity < self.rarity_limit:
logger.info('skipping pokemon since its rarity is too low')
return
padded_distance = distance * 1.1
#added to allow limiting of distance in case the users expect ot be stationary as in an office environment
if padded_distance > self.distance_limit:
logger.info('skipping pokemon since its distance is too high')
return
travel_time = padded_distance / 0.0012 # changed this for campusbot since we're not moving and we want more range
if expires_in.total_seconds() < travel_time:
logger.info('skipping pokemon since it\'s too far: traveltime=%s for distance=%s', travel_time, distance)
return
pokemon_key = pokemon['key']
if pokemon_key in self.sent_pokemon:
logger.info('already sent this pokemon to slack')
return
from_lure = ', from a lure' if pokemon.get('from_lure', False) else ''
miles_away = '{:.3f}'.format(distance)
pokedex_url = 'http://www.pokemon.com/us/pokedex/%s' % pokemon['pokemon_id']
map_url = 'http://maps.google.com?saddr=%s,%s&daddr=%s,%s&directionsmode=walking' % (position[0], position[1], pokemon['latitude'], pokemon['longitude'])
min_remaining = int(expires_in.total_seconds() / 60)
time_remaining = '%s%ss' % ('%dm' % min_remaining if min_remaining > 0 else '', expires_in.seconds - 60 * min_remaining)
stars = ''.join([':star:' for x in xrange(rarity)])
message = 'I found a <%s|%s> %s <%s|%s miles away> expiring in %s%s' % (pokedex_url, pokemon['name'], stars, map_url, miles_away, time_remaining, from_lure)
# bold message if rarity > 4
if rarity >= 4:
message = '*%s*' % message
logging.info('%s: %s', pokemon_key, message)
if self._send(message):
self.sent_pokemon[pokemon_key] = True
def _send(self, message):
payload = {
'username': 'Poké Alert!',
'text': message,
'icon_emoji': ':ghost:'
}
s = json.dumps(payload)
r = requests.post(self.slack_webhook_url, data=s)
logger.info('slack post result: %s, %s', r.status_code, r.reason)
return r.status_code == 200