-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch_stream.py
executable file
·59 lines (57 loc) · 3.31 KB
/
search_stream.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
import sys
import subprocess
from utils import display_notification, servers_file, media_player, custom_logger
from plexapi.server import PlexServer
result = subprocess.Popen(['mdfind', f'kMDItemContentType == "com.apple.application-bundle" && kMDItemFSName == "{media_player.upper()}.app"'], stdout=subprocess.PIPE)
output = result.stdout.read().decode().strip()
if output or media_player == 'infuse':
try:
_machineID, _type, _key, _media_index, _part_index = sys.argv[1].split(';')[1:]
except IndexError as e:
display_notification('🚨 Error !', 'Something went wrong, check the logs and create a GitHub issue')
custom_logger('error', e)
exit()
data = servers_file()
for obj in data['items']:
if obj.get('machineIdentifier') == _machineID:
baseURL = obj.get('baseURL')
plexToken = obj.get('plexToken')
try:
plex_instance = PlexServer(baseURL, plexToken)
except Exception as e:
display_notification('🚨 Error !', f'Failed to connect to the plex server {obj.get("title")}')
custom_logger('error', e)
exit()
media = plex_instance.fetchItem(_key)
if _type == 'album':
streamURLs = [f'{baseURL}{track.media[0].parts[0].key}?X-Plex-Token={plexToken}' for track in media.tracks()]
streamTitles = [f'{media.parentTitle} ǀ {media.title} ({media.year}) ǀ {track.title} {track.trackNumber} / {len(media.tracks())}' for track in media.tracks()]
else:
streamURLs = [f'{baseURL}{media.media[int(_media_index)].parts[int(_part_index)].key}?X-Plex-Token={plexToken}']
if _type == 'episode':
streamTitles = [f'{media.grandparentTitle} ǀ {media.title} ǀ {media.seasonEpisode}']
elif _type == 'movie':
streamTitles = [f'{media.title} ({media.year})']
elif _type == 'track':
streamTitles = [f'{media.grandparentTitle} ǀ {media.parentTitle} ({media.album().year}) ǀ {media.title} {media.trackNumber} / {len(media.album().tracks())}']
elif _type == 'clip':
streamTitles = [media.title]
mp_args = [media_player]
if media_player == 'vlc':
mp_args.extend(['-I', 'macosx', '--no-xlib', '--no-video-title-show', '--video-on-top'])
command = '--meta-title' if _type in ['track', 'album'] else '--input-title-format'
for url, title in zip(streamURLs, streamTitles):
mp_args.extend([command, title, url])
elif media_player == 'iina':
mp_args.extend(['--music-mode']) if _type in ['track', 'album'] else None
for url, title in zip(streamURLs, streamTitles):
mp_args.extend([url, f'--mpv-force-media-title={title}', '--no-stdin'])
elif media_player == 'infuse':
mp_args = ['open', f'infuse://x-callback-url/play?url={streamURLs[0]}']
custom_logger('debug', f'Starting streams: "{' '.join(mp_args)}"')
mp_instance = subprocess.Popen(mp_args)
break
else:
warn = f'Can\'t locate the {media_player} app'
display_notification('⚠️ Warning !', warn)
custom_logger('warning', warn)