129 lines
5.8 KiB
Python
129 lines
5.8 KiB
Python
import requests
|
|
import base64
|
|
import urllib.parse
|
|
|
|
def stringtob64(inputstring):
|
|
return base64.b64encode(inputstring.encode('ascii')).decode('ascii')
|
|
|
|
class ts3ab:
|
|
def __init__(self, apiurl, userid, token, botid=0):
|
|
self._token = stringtob64("{userid}:{token}".format(userid=userid, token=token))
|
|
self.apiurl = apiurl
|
|
self.botid = botid
|
|
|
|
def gethistory(self):
|
|
response = requests.get("{apiurl}/bot/use/{botid}(/history/last/10".format(apiurl=self.apiurl, botid=self.botid),
|
|
headers={
|
|
"Content-Type": "application/json",
|
|
"Authorization": "Basic {token}".format(token=self._token)
|
|
}
|
|
)
|
|
return response
|
|
|
|
def getplaylist(self, listid):
|
|
return self.playlist(self, listid)
|
|
|
|
def getplaylists(self):
|
|
response = requests.get("{apiurl}/bot/use/{botid}(/list/list".format(apiurl=self.apiurl, botid=self.botid),
|
|
headers={
|
|
"Content-Type": "application/json",
|
|
"Authorization": "Basic {token}".format(token=self._token)
|
|
}
|
|
)
|
|
if (response.status_code != 200):
|
|
raise Exception("unable to fetch {status_code}".format(status_code=response.status_code))
|
|
return response.json()
|
|
|
|
def createplaylist(self, listid, title=None):
|
|
response = requests.get("{apiurl}/bot/use/{botid}(/list/create/{listid}/{title}".format(apiurl=self.apiurl, botid=self.botid, listid=listid, title=title if not title == None else listid),
|
|
headers={
|
|
"Content-Type": "application/json",
|
|
"Authorization": "Basic {token}".format(token=self._token)
|
|
}
|
|
)
|
|
if (response.status_code != 204):
|
|
raise Exception("unable to fetch {status_code}".format(status_code=response.status_code))
|
|
return response
|
|
|
|
class playlist:
|
|
def __init__(self, outer, listid):
|
|
self._outer = outer
|
|
self.id = listid
|
|
if not self._checkiflistexists():
|
|
self._outer.createplaylist(self.id, self.id)
|
|
self.songcount = 0
|
|
self.title = self.id
|
|
self.items = []
|
|
self.renamestash = []
|
|
return
|
|
pljson = self._getpl(0, 20).json()
|
|
self.songcount = int(pljson['SongCount'])
|
|
self.title = pljson['Title']
|
|
self.items = pljson['Items']
|
|
self.renamestash = []
|
|
i = 1
|
|
while len(self.items) < self.songcount:
|
|
self.items += self._getpl(20*i).json()['Items']
|
|
i += 1
|
|
|
|
def _checkiflistexists(self):
|
|
for playlist in self._outer.getplaylists():
|
|
if playlist['Id'] == self.id: return True
|
|
return False
|
|
|
|
def _getpl(self, offset=0, count=20):
|
|
response = requests.get("{apiurl}/bot/use/{botid}(/list/show/{listid}/{offset}/{count}".format(apiurl=self._outer.apiurl, botid=self._outer.botid, listid=self.id, offset=offset, count=count),
|
|
headers={
|
|
"Content-Type": "application/json",
|
|
"Authorization": "Basic {token}".format(token=self._outer._token)
|
|
}
|
|
)
|
|
if response.status_code != 200:
|
|
raise Exception("unable to fetch {status_code}".format(status_code=response.status_code))
|
|
if "ErrorMessage" in response.json().keys():
|
|
raise Exception(response.json()['ErrorMessage'])
|
|
return response
|
|
|
|
def fetchsongcount(self):
|
|
self.songcount = self._getpl(0, 1).json()['SongCount']
|
|
|
|
def additem(self, url, name=None):
|
|
self.fetchsongcount()
|
|
response = requests.get("{apiurl}/bot/use/{botid}(/list/add/{listid}/{url}".format(apiurl=self._outer.apiurl, botid=self._outer.botid, listid=self.id, url=urllib.parse.quote(url, safe='')),
|
|
headers={
|
|
"Content-Type": "application/json",
|
|
"Authorization": "Basic {token}".format(token=self._outer._token)
|
|
}
|
|
)
|
|
if response.status_code != 200:
|
|
raise Exception("unable to add {status_code}".format(status_code=response.status_code))
|
|
if "ErrorMessage" in response.json().keys():
|
|
raise Exception(response.json()['ErrorMessage'])
|
|
#self.renamestash.append({'index': self.songcount, 'name': name})
|
|
if not name == None: self.renameitem(self.songcount, name)
|
|
|
|
def clearrenamestash(self):
|
|
for item in self.renamestash:
|
|
self.renameitem(item['index'], item['name'])
|
|
self.renamestash.remove(item)
|
|
|
|
def renameitem(self, index, name):
|
|
response = requests.get("{apiurl}/bot/use/{botid}(/list/item/name/{listid}/{index}/{title}".format(apiurl=self._outer.apiurl, botid=self._outer.botid, listid=self.id, index=index, title=urllib.parse.quote(name, safe='')),
|
|
headers={
|
|
"Content-Type": "application/json",
|
|
"Authorization": "Basic {token}".format(token=self._outer._token)
|
|
}
|
|
)
|
|
if response.status_code != 204:
|
|
raise Exception("unable to rename {status_code}".format(status_code=response.status_code))
|
|
|
|
|
|
def delitem(self, index):
|
|
response = requests.get("{apiurl}/bot/use/{botid}(/list/item/delete/{listid}/{index}".format(apiurl=self._outer.apiurl, botid=self._outer.botid, listid=self.id, index=index),
|
|
headers={
|
|
"Content-Type": "application/json",
|
|
"Authorization": "Basic {token}".format(token=self._outer._token)
|
|
}
|
|
)
|
|
if response.status_code != 204:
|
|
raise Exception("unable to delete {status_code}".format(status_code=response.status_code)) |