generated from othneildrew/Best-README-Template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLog.py
109 lines (94 loc) · 3.76 KB
/
Log.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
from sqlite3 import Timestamp
import requests
import pymongo
import datetime
from pymongo import MongoClient
import textdistance
class PuregymAPIClient():
headers = {'Content-Type': 'application/x-www-form-urlencoded', 'User-Agent': 'PureGym/1523 CFNetwork/1312 Darwin/21.0.0'}
authed = False
home_gym_id = None
gyms = None
def mongologin()
cluster = MongoClient(dblink)
db = cluster["test"]
collection = db["test"]
def login(self, email, pin):
self.session = requests.session()
data = {
'grant_type': 'password',
'username': email,
'password': pin,
'scope': 'pgcapi',
'client_id': 'ro.client'
}
response = self.session.post('https://auth.puregym.com/connect/token', headers=self.headers, data=data)
if response.status_code == 200:
self.auth_json = response.json()
self.authed = True
self.headers['Authorization'] = 'Bearer '+self.auth_json['access_token']
else:
return response.raise_for_status()
def get_list_of_gyms(self):
if not self.authed:
return PermissionError('Not authed: call login(email, pin)')
response = self.session.get(f'https://capi.puregym.com/api/v1/gyms/', headers=self.headers)
if response.status_code == 200:
self.gyms = {i['name'].replace(' ', '').replace('-', '').lower(): i['id'] for i in response.json()}
else:
return ValueError('Response '+str(response.status_code))
def get_gym(self, gym_name):
"""returns corrected gym name and its ID"""
gym_name = gym_name.replace(' ', '').replace('-', '').lower()
if self.gyms is None:
self.get_list_of_gyms()
return max(list(self.gyms.items()), key=lambda x: textdistance.levenshtein.similarity(gym_name, x[0]))
def get_home_gym(self):
if not self.authed:
return PermissionError('Not authed: call login(email, pin)')
response = self.session.get('https://capi.puregym.com/api/v1/member', headers=self.headers)
if response.status_code == 200:
self.home_gym_id = response.json()['homeGymId']
else:
return ValueError('Response '+str(response.status_code))
def get_gym_attendance(self, gym, return_name=False):
if not self.authed:
return PermissionError('Not authed: call login(email, pin)')
if gym is None:
if self.home_gym_id is None:
self.get_home_gym()
gym_id = self.home_gym_id
elif isinstance(gym, int):
gym_id = gym
gym = None
else:
gym, gym_id = self.get_gym(gym) # name->id
response = self.session.get(f'https://capi.puregym.com/api/v1/gyms/{gym_id}/attendance', headers=self.headers)
if response.status_code == 200:
n = response.json()['totalPeopleInGym']
if return_name:
return n, gym
return n
else:
return response.raise_for_status()
if __name__ == '__main__':
from argparse import ArgumentParser
parser = ArgumentParser()
parser.add_argument('dblink')
parser.add_argument('cluster')
parser.add_argument('collection')
parser.add_argument('email')
parser.add_argument('pin')
parser.add_argument('--gym', default=None)
args = parser.parse_args()
client = PuregymAPIClient()
client.login(args.email, args.pin)
People = (client.get_gym_attendance(args.gym))
print(People)
Timestamp = datetime.datetime.utcnow()
print(Timestamp)
cluster = MongoClient(dblink)
db = cluster[cluster]
collection = db[collection]
post = {"People":People, "Time": Timestamp}
collection.insert_one(post)