-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjfapi.py
92 lines (80 loc) · 2.82 KB
/
jfapi.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
import aiohttp
import asyncio
import json
import yaml
import urllib.parse
class JFAPI():
def __init__(self, server: str, apikey: str) -> None:
self.__apikey = apikey
self.__server = server.strip('/')
self._session = None
def __getEndpointUrl(self, endpoint: str):
return f'{self.__server}/{endpoint.strip('/')}'
async def __aenter__(self):
self._session = aiohttp.ClientSession()
async def __aexit__(self):
await self._session.close()
def __del__(self):
try:
asyncio.run(self._session.delete())
except:
pass
async def checkSession(self):
if not self._session:
self._session = aiohttp.ClientSession()
# https://api.jellyfin.org/#tag/Search/operation/GetSearchHints
# GET /Search/Hints
async def search(self, term: str, limit: int = None, types:list[str] = []) -> list:
await self.checkSession()
params = {
'ApiKey': self.__apikey,
'searchTerm': term,
'recursive': 'true'
}
if limit:
params['limit'] = limit
if types:
params['includeItemTypes'] = ','.join(types)
async with self._session.get(
self.__getEndpointUrl('/Items'),
params=params
) as res:
return (await res.json())["Items"]
# Gets HLS stream for audio soundtrack with format Opus 16bit 48khz in fMP4 containers
# Returns URL for use with external player (ffmpeg)
# GET /Audio/{itemId}/main.m3u8
def getAudioHls(self, id, bitrate):
endpoint = self.__getEndpointUrl(f'/Audio/{id}/main.m3u8')
params = {
'ApiKey': self.__apikey,
'segmentContainer': 'mp4',
'audioCodec': 'opus',
'allowAudioStreamCopy': True,
'maxAudioBitDepth': 16,
'audioSampleRate': 48000,
'audioChannels': 2,
'audioBitRate': bitrate
}
q = urllib.parse.urlencode(params)
return endpoint + '?' + q
# Gets items by IDs
# GET /Items
async def getItemsByIds(self, ids: list[str]):
await self.checkSession()
endpoint = self.__getEndpointUrl('/Items')
params = {
'ApiKey': self.__apikey,
'ids': ','.join(ids)
}
async with self._session.get(endpoint, params=params) as res:
return (await res.json())['Items']
async def getAlbumTracks(self, albumId: str):
await self.checkSession()
endpoint = self.__getEndpointUrl('/Items')
params = {
'ApiKey': self.__apikey,
'parentId': albumId,
'sortBy': 'ParentIndexNumber,IndexNumber'
}
async with self._session.get(endpoint, params=params) as res:
return (await res.json())['Items']