SolarFM/plugins/youtube_download/__main__.py

87 lines
2.6 KiB
Python
Raw Normal View History

2022-01-31 02:29:57 +00:00
# Python imports
import os, threading, subprocess, time
2022-01-31 02:29:57 +00:00
# Gtk imports
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
# Application imports
2022-06-19 03:27:17 +00:00
# NOTE: Threads WILL NOT die with parent's destruction.
2022-01-31 02:29:57 +00:00
def threaded(fn):
def wrapper(*args, **kwargs):
2022-02-01 07:43:09 +00:00
threading.Thread(target=fn, args=args, kwargs=kwargs, daemon=False).start()
2022-01-31 02:29:57 +00:00
return wrapper
2022-06-19 03:27:17 +00:00
# NOTE: Threads WILL die with parent's destruction.
def daemon_threaded(fn):
def wrapper(*args, **kwargs):
threading.Thread(target=fn, args=args, kwargs=kwargs, daemon=True).start()
return wrapper
2022-01-31 02:29:57 +00:00
2022-02-25 07:29:37 +00:00
class Plugin:
def __init__(self, fm_builder, fm_event_system):
self.SCRIPT_PTH = os.path.dirname(os.path.realpath(__file__))
self._plugin_name = "Youtube Download"
self._plugin_author = "ITDominator"
self._plugin_version = "0.0.1"
2022-01-31 02:29:57 +00:00
self._fm_builder = fm_builder
self._fm_event_system = fm_event_system
self._event_sleep_time = .5
self._fm_event_message = None
2022-02-25 07:29:37 +00:00
2022-06-19 03:27:17 +00:00
self._module_event_observer()
button = Gtk.Button(label=self._plugin_name)
button.connect("button-release-event", self._do_download)
2022-02-25 07:29:37 +00:00
plugin_list = self._fm_builder.get_object("plugin_socket")
2022-02-25 07:29:37 +00:00
plugin_list.add(button)
plugin_list.show_all()
2022-01-31 02:29:57 +00:00
2022-06-19 03:27:17 +00:00
def get_plugin_name(self):
return self._plugin_name
def get_plugin_author(self):
return self._plugin_author
def get_plugin_version(self):
return self._plugin_version
@daemon_threaded
def _module_event_observer(self):
while True:
time.sleep(self._event_sleep_time)
event = self._fm_event_system.read_module_event()
if event:
try:
if event[0] is self._plugin_name:
target_id, method_target, data = self._fm_event_system.consume_module_event()
if not method_target:
self._fm_event_message = data
else:
method = getattr(self.__class__, f"{method_target}")
data = method(*(self, *parameters))
except Exception as e:
print(repr(e))
2022-01-31 02:29:57 +00:00
2022-02-01 07:43:09 +00:00
@threaded
def _do_download(self, widget=None, eve=None):
self._fm_event_system.push_gui_event([self._plugin_name, "get_current_state", ()])
while not self._fm_event_message:
pass
state = self._fm_event_message
subprocess.Popen([f'{self.SCRIPT_PTH}/download.sh' , state.tab.get_current_directory()])
self._fm_event_message = None