-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path__init__.py
197 lines (177 loc) · 8.12 KB
/
__init__.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (c) 2016 - 2022 by Aditya Mehra <Aix.m@outlook.com>
# All rights reserved.
import os
import subprocess
from ovos_workshop.skills import OVOSSkill
from ovos_workshop.decorators import intent_file_handler
from ovos_workshop.skills.common_play import MediaType, PlaybackType
from ovos_bus_client.message import Message
from ovos_utils.process_utils import RuntimeRequirements
from ovos_utils import classproperty
class FileBrowserSkill(OVOSSkill):
def __init__(self):
"""
FileBrowserSkill Skill Class.
"""
super(FileBrowserSkill, self).__init__(name="FileBrowserSkill")
self.skill_location_path = None
self.udev_thread = None
@classproperty
def runtime_requirements(self):
# TODO - once OCP search is added remove gui requirement
return RuntimeRequirements(internet_before_load=False,
network_before_load=False,
gui_before_load=True,
requires_internet=False,
requires_network=False,
requires_gui=True,
no_internet_fallback=True,
no_network_fallback=True,
no_gui_fallback=False)
def initialize(self):
self.add_event('skill.file-browser.openvoiceos.home', self.show_home)
self.gui.register_handler('skill.file-browser.openvoiceos.handle.file', self.handle_file)
self.gui.register_handler('skill.file-browser.openvoiceos.handle.folder.playlists', self.handle_folder_playlist)
self.gui.register_handler('skill.file-browser.openvoiceos.send.file.kdeconnect',
self.share_to_device_kdeconnect)
self.audio_extensions = ["aac", "ac3", "aiff", "amr", "ape", "au", "flac", "alac", "m4a",
"m4b", "m4p", "mid", "mp2", "mp3", "mpc", "oga", "ogg", "opus", "ra", "wav", "wma"]
self.video_extensions = ["3g2", "3gp", "3gpp", "asf", "avi", "flv", "m2ts", "mkv", "mov",
"mp4", "mpeg", "mpg", "mts", "ogm", "ogv", "qt", "rm", "vob", "webm", "wmv"]
self.skill_location_path = os.path.dirname(os.path.realpath(__file__))
self.setup_udev_monitor()
def setup_udev_monitor(self):
try:
import pyudev
context = pyudev.Context()
monitor = pyudev.Monitor.from_netlink(context)
monitor.filter_by(subsystem='usb')
self.udev_thread = pyudev.MonitorObserver(monitor, self.handle_udev_event)
self.udev_thread.start()
except Exception as e:
pass
def handle_udev_event(self, action, device):
"""
Handle a udev event
"""
if action == 'add':
if device.device_node is not None:
self.gui.show_notification("New USB device detected - Open file browser to explore it",
action="skill.file-browser.openvoiceos.home", noticetype="transient",
style="info")
elif action == 'remove':
if device.device_node is not None:
self.gui.show_notification("A USB device was removed", noticetype="transient", style="info")
@intent_file_handler("open.file.browser.intent")
def show_home(self, message):
"""
Show the file browser home page
"""
self.gui.show_page("Browser.qml", override_idle=120)
def handle_file(self, message):
"""
Handle a file from the file browser Video / Audio
"""
file_url = message.data.get("fileURL", "")
file_extension = file_url.split(".")[-1]
if file_extension in self.audio_extensions:
media = {
"match_confidence": 100,
"media_type": MediaType.AUDIO,
"length": 0,
"uri": file_url,
"playback": PlaybackType.AUDIO,
"image": self.skill_location_path + "/ui/images/generic-audio-bg.jpg",
"bg_image": self.skill_location_path + "/ui/images/generic-audio-bg.jpg",
"skill_icon": "",
"title": file_url.split("/")[-1],
"skill_id": "skill-file-browser.openvoiceos"
}
playlist = [media]
disambiguation = [media]
self.bus.emit(Message("ovos.common_play.play",
{"media": media, "playlist": playlist, "disambiguation": disambiguation}))
self.gui.release()
if file_extension in self.video_extensions:
media = {
"match_confidence": 100,
"media_type": MediaType.VIDEO,
"length": 0,
"uri": file_url,
"playback": PlaybackType.VIDEO,
"image": self.skill_location_path + "/ui/images/generic-audio-bg.jpg",
"bg_image": self.skill_location_path + "/ui/images/generic-audio-bg.jpg",
"skill_icon": "",
"title": file_url.split("/")[-1],
"skill_id": "skill-file-browser.openvoiceos"
}
playlist = [media]
disambiguation = [media]
self.bus.emit(Message("ovos.common_play.play",
{"media": media, "playlist": playlist, "disambiguation": disambiguation}))
self.gui.release()
def handle_folder_playlist(self, message):
"""
Handle a folder from the file browser as a playlist
"""
folder_url = message.data.get("path", "")
files = os.listdir(folder_url)
playlist = []
for file in files:
file_url = "file://" + folder_url + "/" + file
file_extension = file_url.split(".")[-1]
if file_extension in self.audio_extensions:
media = {
"match_confidence": 100,
"media_type": MediaType.AUDIO,
"length": 0,
"uri": file_url,
"playback": PlaybackType.AUDIO,
"image": self.skill_location_path + "/ui/images/generic-audio-bg.jpg",
"bg_image": self.skill_location_path + "/ui/images/generic-audio-bg.jpg",
"skill_icon": "",
"title": file_url.split("/")[-1],
"skill_id": "skill-file-browser.openvoiceos"
}
playlist.append(media)
if file_extension in self.video_extensions:
media = {
"match_confidence": 100,
"media_type": MediaType.VIDEO,
"length": 0,
"uri": file_url,
"playback": PlaybackType.VIDEO,
"image": self.skill_location_path + "/ui/images/generic-audio-bg.jpg",
"bg_image": self.skill_location_path + "/ui/images/generic-audio-bg.jpg",
"skill_icon": "",
"title": file_url.split("/")[-1],
"skill_id": "skill-file-browser.openvoiceos"
}
playlist.append(media)
if len(playlist) > 0:
media = playlist[0]
disambiguation = playlist
self.bus.emit(Message("ovos.common_play.play",
{"media": media, "playlist": playlist, "disambiguation": disambiguation}))
self.gui.release()
def share_to_device_kdeconnect(self, message):
"""
Share a file to a device using KDE Connect
"""
file_url = message.data.get("file", "")
device_id = message.data.get("deviceID", "")
subprocess.Popen(["kdeconnect-cli", "--share", file_url, "--device", device_id])
def stop(self):
"""
Mycroft Stop Function
"""
if self.udev_thread is not None:
self.udev_thread.stop()
self.udev_thread.join()
def create_skill():
"""
Mycroft Create Skill Function
"""
return FileBrowserSkill()