diff --git a/app/tools/yt.py b/app/tools/yt.py index b4500db5..ba358fc9 100644 --- a/app/tools/yt.py +++ b/app/tools/yt.py @@ -12,6 +12,7 @@ from urllib.parse import unquote from urllib.request import Request, urlopen, urlretrieve from app.commons import log +from app.ui.uicommons import show_notification _YT_PATTERN = re.compile(r"https://www.youtube.com/.+(?:v=)([\w-]{11}).*") _YT_LIST_PATTERN = re.compile(r"https://www.youtube.com/.+?(?:list=)([\w-]{18,})?.*") @@ -237,27 +238,32 @@ class YouTubeDL: except ModuleNotFoundError as e: log("YouTubeDLHelper error: {}".format(str(e))) raise YouTubeException(e) + except ImportError as e: + log("YouTubeDLHelper error: {}".format(str(e))) else: if self._update: if hasattr(youtube_dl.version, "__version__"): l_ver = self.get_last_release_id() cur_ver = youtube_dl.version.__version__ if youtube_dl.version.__version__ < l_ver: - msg = "youtube-dl has new release! Current: {}. Last: {}.".format(cur_ver, l_ver) + msg = "youtube-dl has new release!\nCurrent: {}. Last: {}.".format(cur_ver, l_ver) + show_notification(msg) log(msg) self._callback(msg, False) self.get_latest_release() self._DownloadError = youtube_dl.utils.DownloadError self._dl = youtube_dl.YoutubeDL(self._OPTIONS) - log("youtube-dl initialized...") + msg = "youtube-dl initialized..." + show_notification(msg) + log(msg) @staticmethod def get_last_release_id(): """ Getting last release id. """ url = "https://api.github.com/repos/ytdl-org/youtube-dl/releases/latest" with urlopen(url, timeout=10) as resp: - return json.load(resp).get("tag_name", "0") + return json.loads(resp.read().decode("utf-8")).get("tag_name", "0") def get_latest_release(self): try: @@ -265,7 +271,7 @@ class YouTubeDL: log("Getting the last youtube-dl release...") with urlopen(YouTubeDL._LATEST_RELEASE_URL, timeout=10) as resp: - r = json.load(resp) + r = json.loads(resp.read().decode("utf-8")) zip_url = r.get("zipball_url", None) if zip_url: zip_file = self._path + "yt.zip" @@ -288,6 +294,7 @@ class YouTubeDL: shutil.move(info.filename, "{}{}{}".format(self._path, sep, f)) shutil.rmtree(pref) msg = "Getting the last youtube-dl release is done!" + show_notification(msg) log(msg) self._callback(msg, False) return True diff --git a/app/ui/uicommons.py b/app/ui/uicommons.py index 794ef4a8..4cb7fafc 100644 --- a/app/ui/uicommons.py +++ b/app/ui/uicommons.py @@ -8,8 +8,11 @@ import gi gi.require_version("Gtk", "3.0") gi.require_version("Gdk", "3.0") -from gi.repository import Gtk, Gdk +gi.require_version("Notify", "0.7") +from gi.repository import Gtk, Gdk, Notify +# Init notify +Notify.init("DemonEditor") # Setting mod mask for the keyboard depending on the platform. MOD_MASK = Gdk.ModifierType.MOD2_MASK if IS_DARWIN else Gdk.ModifierType.CONTROL_MASK # Path to *.glade files. @@ -49,7 +52,10 @@ DEFAULT_ICON = theme.load_icon("emblem-default", 16, 0) if theme.lookup_icon("em @lru_cache(maxsize=1) def get_yt_icon(icon_name, size=24): - """ Getting YouTube icon. If the icon is not found in the icon themes, the "Info" icon is returned by default! """ + """ Getting YouTube icon. + + If the icon is not found in the icon themes, the "Info" icon is returned by default! + """ default_theme = Gtk.IconTheme.get_default() if default_theme.has_icon(icon_name): return default_theme.load_icon(icon_name, size, 0) @@ -65,6 +71,19 @@ def get_yt_icon(icon_name, size=24): return default_theme.load_icon("info", size, 0) +def show_notification(message, timeout=10000, urgency=1): + """ Shows notification. + + @param message: text to display + @param timeout: milliseconds + @param urgency: 0 - low, 1 - normal, 2 - critical + """ + notify = Notify.Notification.new("DemonEditor", message, "demon-editor") + notify.set_urgency(urgency) + notify.set_timeout(timeout) + notify.show() + + class KeyboardKey(Enum): """ The raw(hardware) codes of the keyboard keys. """ E = 26