-
Notifications
You must be signed in to change notification settings - Fork 0
/
hatchet.py
182 lines (129 loc) · 5.46 KB
/
hatchet.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import requests, time
from requests.exceptions import *
from datetime import datetime
class Token:
def __init__( self, token, ttl ):
self.update( token, ttl )
def update( self, token, ttl ):
self._token = token
if ttl == 0:
self._expire_time = 0
else:
# expire 10 seconds earlier to make sure a request is not made with
# expired token
self._expire_time = ttl + (time.time() - 10)
def get_token( self ):
return self._token
def is_valid( self ):
if self._expire_time > time.time() or self._expire_time == 0:
return True
return False
class HatchetService:
AuthURL = 'https://auth.hatchet.is/v1/'
BaseURL = 'https://api.hatchet.is/v2/'
def __init__( self, user_agent, username, password ):
# TODO: pass in tokens??
self._bearer_token = Token('', -1)
self._refresh_token = Token('', -1)
self._access_token = Token('', -1)
self._user_agent = user_agent
self._username = username
self._password = password
self._request_queue = []
def _login( self ):
print('LOGIN: ' + self._username + ':' + self._password)
r = requests.post(self.AuthURL + 'authentication/password', data={
'grant_type': 'password',
'username': self._username,
'password': self._password
})
if r.status_code != requests.codes.ok:
if r.status_code == 400:
print("probably wrong user/password")
else:
r.raise_for_status()
return
j = r.json()
self._bearer_token.update( j['access_token'], j['expires_in'] )
self._refresh_token.update( j['refresh_token'], j['refresh_token_expires_in'] )
def _refresh_bearer_token( self ):
print('REFRESHING TOKEN')
if not self._refresh_token.is_valid():
self._login()
return
r = requests.post(self.AuthURL + 'tokens/refresh/bearer', data={
'grant_type': 'refresh_token',
'refresh_token': self._refresh_token.get_token()
})
j = r.json()
self._bearer_token.update( j['access_token'], j['expires_in'] )
def _fetch_access_token( self ):
print('FETCHING ACCESS TOKEN')
if not self._bearer_token.is_valid():
self._refresh_bearer_token()
r = requests.get(self.AuthURL + 'tokens/fetch/calumet',
headers={'Authorization': 'Bearer ' + self._bearer_token.get_token()})
j = r.json()
self._access_token.update( j['access_token'], j['expires_in'] )
def _authed_post( self, url, data ):
print('AUTHED POST: ' + url + '\n' + data.__str__())
if not self._access_token.is_valid():
self._fetch_access_token()
print('USING TOKEN: ' + self._access_token.get_token())
return requests.post(url, headers={
'Authorization': 'Bearer ' + self._access_token.get_token(),
'User-Agent': self._user_agent
}, json=data)
def _post_or_queue( self, url, data ):
queue_entry = (url, data)
try:
r = self._authed_post(url, data)
r.raise_for_status()
# remove entry from queue
if queue_entry in self._request_queue:
self._request_queue.remove(queue_entry)
except (ConnectionError, Timeout, TooManyRedirects):
# Request may have been correct but there was a network problem
# --> queue the entry for later retry.
if queue_entry not in self._request_queue:
print("request failed, queing plentry")
self._request_queue.append(queue_entry)
except Exception(e):
# Make sure a queued entry gets removed from the queue when it
# failed again due to bad request.
if queue_entry in self._request_queue:
self._request_queue.remove(queue_entry)
raise e
def scrobble( self, artist, album, track, timestamp=None ):
if not isinstance(timestamp, datetime):
timestamp = datetime.utcnow()
body = {'playbacklogEntry': {
'artistString': artist.strip().lower(),
'albumString': album.strip().lower(),
'trackString': track.strip().lower(),
'type': 'scrobble',
'duration': -1, #FIXME
'timestamp': timestamp.isoformat("T") + "Z"
}
}
self._post_or_queue(self.BaseURL + 'playbacklogEntries', body)
def love_artist( self, artist ):
pass
def love_album( self, artist, album ):
pass
def love_track( self, artist, track ):
pass
def process_queue( self ):
print("processing queue: " + self._request_queue.__str__())
for entry in self._request_queue:
self._post_or_queue(entry[0], entry[1])
def now_playing( self, artist, album, track ):
body = {'playbacklogEntry': {
'artistString': artist.strip().lower(),
'albumString': album.strip().lower(),
'trackString': track.strip().lower(),
'type': 'nowplaying',
}
}
r = self._authed_post(self.BaseURL + 'playbacklogEntries', body)
r.raise_for_status()