-
Notifications
You must be signed in to change notification settings - Fork 128
/
User.py
85 lines (60 loc) · 2.55 KB
/
User.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
import os
import requests
import json
import datetime
# Environment variables must be set with your tokens
USER_TOKEN_STRING = os.environ['SLACK_USER_TOKEN_STRING']
class User:
def __init__(self, user_id):
# The Slack ID of the user
self.id = user_id
# The username (@username) and real name
self.username, self.real_name = self.fetchNames()
# A list of all exercises done by user
self.exercise_history = []
# A record of all exercise totals (quantity)
self.exercises = {}
# A record of exercise counts (# of times)
self.exercise_counts = {}
# A record of past runs
self.past_workouts = {}
print "New user: " + self.real_name + " (" + self.username + ")"
def storeSession(self, run_name):
try:
self.past_workouts[run_name] = self.exercises
except:
self.past_workouts = {}
self.past_workouts[run_name] = self.exercises
self.exercises = {}
self.exercise_counts = {}
def fetchNames(self):
params = {"token": USER_TOKEN_STRING, "user": self.id}
response = requests.get("https://slack.com/api/users.info",
params=params)
user_obj = json.loads(response.text, encoding='utf-8')["user"]
username = user_obj["name"]
real_name = user_obj["profile"]["real_name"]
return username, real_name
def getUserHandle(self):
return ("@" + self.username).encode('utf-8')
'''
Returns true if a user is currently "active", else false
'''
def isActive(self):
try:
params = {"token": USER_TOKEN_STRING, "user": self.id}
response = requests.get("https://slack.com/api/users.getPresence",
params=params)
status = json.loads(response.text, encoding='utf-8')["presence"]
return status == "active"
except requests.exceptions.ConnectionError:
print "Error fetching online status for " + self.getUserHandle()
return False
def addExercise(self, exercise, reps):
# Add to total counts
self.exercises[exercise["id"]] = self.exercises.get(exercise["id"], 0) + reps
self.exercise_counts[exercise["id"]] = self.exercise_counts.get(exercise["id"], 0) + 1
# Add to exercise history record
self.exercise_history.append([datetime.datetime.now().isoformat(),exercise["id"],exercise["name"],reps,exercise["units"]])
def hasDoneExercise(self, exercise):
return exercise["id"] in self.exercise_counts