Skip to content

Commit

Permalink
Add the ability to set channels as favorite
Browse files Browse the repository at this point in the history
  • Loading branch information
clefebvre committed Oct 12, 2023
1 parent 6bf21af commit d9060f0
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 6 deletions.
3 changes: 3 additions & 0 deletions usr/bin/hypnotix
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#!/bin/sh

mkdir -p ~/.cache/hypnotix/favorites
touch ~/.cache/hypnotix/favorites/list

mkdir -p ~/.cache/hypnotix/yt-dlp
if [ $(gsettings get org.x.hypnotix use-local-ytdlp) = true ]
then
Expand Down
22 changes: 20 additions & 2 deletions usr/lib/hypnotix/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

TV_GROUP, MOVIES_GROUP, SERIES_GROUP = range(3)

FAVORITES_PATH = os.path.join(GLib.get_user_cache_dir(), "hypnotix", "favorites", "list")

# Used as a decorator to run things in the background
def async_function(func):
def wrapper(*args, **kwargs):
Expand Down Expand Up @@ -122,8 +124,12 @@ def __init__(self, provider, info):
break
if ext == ".jpeg":
ext = ".jpg"
self.logo_path = os.path.join(PROVIDERS_PATH, "%s-%s%s" % (slugify(provider.name), slugify(self.name), ext))

if provider is None:
# favorite channel, no provider
provider_name = "favorites"
else:
provider_name = provider.name
self.logo_path = os.path.join(PROVIDERS_PATH, "%s-%s%s" % (slugify(provider_name), slugify(self.name), ext))

class Manager:
def __init__(self, settings):
Expand Down Expand Up @@ -284,3 +290,15 @@ def load_channels(self, provider):
provider.movies.append(channel)
else:
provider.channels.append(channel)

def load_favorites(self):
favorites = []
with open(FAVORITES_PATH, 'r') as f:
for line in f:
favorites.append(line.strip())
return favorites

def save_favorites(self, favorites):
with open(FAVORITES_PATH, "w") as f:
for fav in favorites:
f.write(f"{fav}\n")
44 changes: 42 additions & 2 deletions usr/lib/hypnotix/hypnotix.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from imdb import IMDb
from unidecode import unidecode

from common import Manager, Provider, MOVIES_GROUP, PROVIDERS_PATH, SERIES_GROUP, TV_GROUP,\
from common import Manager, Provider, Channel, MOVIES_GROUP, PROVIDERS_PATH, SERIES_GROUP, TV_GROUP,\
async_function, idle_function


Expand Down Expand Up @@ -127,6 +127,7 @@ def __init__(self, application):
self.icon_theme = Gtk.IconTheme.get_default()
self.manager = Manager(self.settings)
self.providers = []
self.favorite_data = []
self.active_provider = None
self.active_group = None
self.active_serie = None
Expand All @@ -140,6 +141,8 @@ def __init__(self, application):
self.mpv = None
self.ia = IMDb()

self.page_is_loading = False # used to ignore signals while we set widget states

self.video_properties = {}
self.audio_properties = {}

Expand Down Expand Up @@ -262,6 +265,7 @@ def __init__(self, application):
"audio_properties_label",
"layout_properties_box",
"layout_properties_label",
"favorite_button",
]

for name in widget_names:
Expand Down Expand Up @@ -293,6 +297,7 @@ def __init__(self, application):
self.tv_button.connect("clicked", self.show_groups, TV_GROUP)
self.movies_button.connect("clicked", self.show_groups, MOVIES_GROUP)
self.series_button.connect("clicked", self.show_groups, SERIES_GROUP)
self.favorites_button.connect("clicked", self.show_favorites)
self.providers_button.connect("clicked", self.open_providers)
self.preferences_button.connect("clicked", self.open_preferences)
self.go_back_button.connect("clicked", self.on_go_back_button)
Expand All @@ -317,6 +322,8 @@ def __init__(self, application):

self.channels_listbox.connect("row-activated", self.on_channel_activated)

self.favorite_button.connect("toggled", self.on_favorite_button_toggled)

# Settings widgets
self.bind_setting_widget("user-agent", self.useragent_entry)
self.bind_setting_widget("http-referer", self.referer_entry)
Expand All @@ -329,7 +336,7 @@ def __init__(self, application):
if os.path.exists(os.path.expanduser("~/.cache/hypnotix/yt-dlp/yt-dlp")):
self.ytdlp_local_version_label.set_text(subprocess.getoutput("~/.cache/hypnotix/yt-dlp/yt-dlp --version"))
self.ytdlp_update_button.connect("clicked", self.update_ytdlp)

# Dark mode manager
# keep a reference to it (otherwise it gets randomly garbage collected)
self.dark_mode_manager = XApp.DarkModeManager.new(prefer_dark_mode=True)
Expand Down Expand Up @@ -507,6 +514,15 @@ def on_category_button_clicked(self, widget, group):
else:
self.show_vod(self.active_provider.series)

def show_favorites(self, widget):
channels = []
for line in self.favorite_data:
info, url = line.split(":::")
channel = Channel(None, info)
channel.url = url
channels.append(channel)
self.show_channels(channels)

def show_channels(self, channels):
self.navigate_to("channels_page")
if self.content_type == TV_GROUP:
Expand Down Expand Up @@ -835,6 +851,19 @@ def open_keyboard_shortcuts(self, widget):
window.set_title(_("Hypnotix"))
window.show()

def on_favorite_button_toggled(self, widget):
if self.page_is_loading:
return
name = self.active_channel.name
data = f"{self.active_channel.info}:::{self.active_channel.url}"
if widget.get_active() and data not in self.favorite_data:
print (f"Adding {name} to favorites")
self.favorite_data.append(data)
elif widget.get_active() == False and data in self.favorite_data:
print (f"Removing {name} from favorites")
self.favorite_data.remove(data)
self.manager.save_favorites(self.favorite_data)

def on_channel_activated(self, box, widget):
self.active_channel = widget.channel
self.play_async(self.active_channel)
Expand Down Expand Up @@ -881,6 +910,16 @@ def before_play(self, channel):
self.label_channel_name.set_text(channel.name)
self.label_channel_url.set_text(channel.url)

self.page_is_loading = True
data = f"{channel.info}:::{channel.url}"
if data in self.favorite_data:
self.favorite_button.set_active(True)
self.favorite_button.set_tooltip_text(_("Remove from favorites"))
else:
self.favorite_button.set_active(False)
self.favorite_button.set_tooltip_text(_("Add to favorites"))
self.page_is_loading = False

@idle_function
def after_play(self, channel):
self.mpv_stack.set_visible_child_name("player_page")
Expand Down Expand Up @@ -1456,6 +1495,7 @@ def on_key_press_event(self, widget, event):

@async_function
def reload(self, page=None, refresh=False):
self.favorite_data = self.manager.load_favorites()
self.status(_("Loading providers..."))
self.providers = []
for provider_info in self.settings.get_strv("providers"):
Expand Down
4 changes: 2 additions & 2 deletions usr/share/hypnotix/hypnotix.ui
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.40.0 -->
<!-- Generated with glade 3.38.2 -->
<interface>
<requires lib="gtk+" version="3.20"/>
<requires lib="xapp" version="0.0"/>
Expand Down Expand Up @@ -488,7 +488,7 @@
</packing>
</child>
<child>
<object class="GtkToggleButton">
<object class="GtkToggleButton" id="favorite_button">
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
Expand Down

0 comments on commit d9060f0

Please sign in to comment.