forked from feschukov/rhythmbox-plugin-yandex-music
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsource.py
213 lines (195 loc) · 9.88 KB
/
source.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
from gi.repository import RB, Gio, GLib, Gdk, Gtk
from album_arts import AlbumArtManager
class YandexMusicSource(RB.BrowserSource):
def __init__(self):
RB.BrowserSource.__init__(self)
self.app = Gio.Application.get_default()
def setup(self, shell, client, station, playlists = None):
self.initialised = False
self.shell = shell
self.db = shell.props.db
self.player = shell.props.shell_player
self.entry_type = self.props.entry_type
self.client = client
self.playlists = playlists
self.album_arts = AlbumArtManager()
self.station = station[station.find('_')+1:]
self.station_prefix = station[:station.find('_')+1]
self.is_feed = (station.find('feed') == 0)
self.last_track = None
self.last_state = None
if self.is_feed:
self.player.connect('playing_uri_changed', self.update_feed)
def do_selected(self):
if not self.initialised or self.is_feed:
self.initialised = True
Gdk.threads_add_idle(GLib.PRIORITY_DEFAULT_IDLE, self.add_entries)
if self.is_feed:
self.last_state = self.player.get_playback_state()
self.player.set_playback_state(shuffle=False, repeat=False)
self.add_context_menu()
def do_deselected(self):
if self.is_feed:
self.player.set_playback_state(shuffle=self.last_state.shuffle, repeat=self.last_state.repeat)
self.remove_context_menu()
def add_entries(self):
if self.station_prefix == 'likes_':
tracks = self.client.users_likes_tracks().fetch_tracks()
elif self.station_prefix.find('mepl') == 0:
tracks = self.client.users_playlists(kind=self.station).fetch_tracks()
elif self.station_prefix.find('likepl') == 0:
user_id = self.station[:self.station.find(':')]
album_id = self.station[self.station.find(':')+1:]
tracks = self.client.users_playlists(kind=album_id, user_id=user_id).fetch_tracks()
elif self.station_prefix.find('feed') == 0:
tracks = self.client.rotor_station_tracks(station=self.station, queue=self.last_track).sequence
else:
return False
self.iterator = 0
self.listcount = len(tracks)
Gdk.threads_add_idle(GLib.PRIORITY_DEFAULT_IDLE, self.add_entry, tracks)
return False
def add_entry(self, tracks):
try:
track = tracks[self.iterator].track
except AttributeError:
track = tracks[self.iterator]
if track.available:
track_location = self.station_prefix+str(track.id)
if len(track.albums) > 0:
track_location = track_location+':'+str(track.albums[0].id)
entry = self.db.entry_lookup_by_location(track_location)
if not entry:
self.last_track = str(track.id)+':'+str(track.albums[0].id)
entry = RB.RhythmDBEntry.new(self.db, self.entry_type, track_location)
if entry:
self.db.entry_set(entry, RB.RhythmDBPropType.TITLE, track.title)
self.db.entry_set(entry, RB.RhythmDBPropType.DURATION, track.duration_ms/1000)
artists = ', '.join(artist.name for artist in track.artists)
self.db.entry_set(entry, RB.RhythmDBPropType.ARTIST, artists)
if len(track.albums) > 0:
self.db.entry_set(entry, RB.RhythmDBPropType.ALBUM, track.albums[0].title)
self.db.entry_set(entry, RB.RhythmDBPropType.GENRE, str(track.albums[0].genre))
self.db.commit()
self.album_arts.ensure_art_exists(track)
self.iterator += 1
if self.iterator >= self.listcount:
return False
else:
return True
def update_feed(self, player, uri):
if not uri or uri[:uri.find('_')+1] != self.station_prefix or uri[uri.find('_')+1:] != self.last_track: return
Gdk.threads_add_idle(GLib.PRIORITY_DEFAULT_IDLE, self.add_entries)
def add_context_menu(self):
if self.station_prefix == 'likes_':
action = Gio.SimpleAction(name='ym-'+self.station_prefix+'unlikes')
action.connect('activate', self.unlike_tracks)
self.app.add_action(action)
item = Gio.MenuItem()
item.set_label(_('Не нравится'))
item.set_detailed_action('app.ym-'+self.station_prefix+'unlikes')
self.app.add_plugin_menu_item('browser-popup', 'ym-'+self.station_prefix+'unlikes', item)
else:
action = Gio.SimpleAction(name='ym-'+self.station_prefix+'likes')
action.connect('activate', self.like_tracks)
self.app.add_action(action)
item = Gio.MenuItem()
item.set_label(_('Мне нравится'))
item.set_detailed_action('app.ym-'+self.station_prefix+'likes')
self.app.add_plugin_menu_item('browser-popup', 'ym-'+self.station_prefix+'likes', item)
action = Gio.SimpleAction(name='ym-'+self.station_prefix+'dislikes')
action.connect('activate', self.dislike_tracks)
self.app.add_action(action)
item = Gio.MenuItem()
item.set_label(_('Не рекомендовать'))
item.set_detailed_action('app.ym-'+self.station_prefix+'dislikes')
self.app.add_plugin_menu_item('browser-popup', 'ym-'+self.station_prefix+'dislikes', item)
# Copy track link action
action_name = 'ym-'+self.station_prefix+'copy_track_link'
action = Gio.SimpleAction(name=action_name)
action.connect('activate', self.copy_track_link)
self.app.add_action(action)
item = Gio.MenuItem()
item.set_label(_('Копировать ссылку'))
item.set_detailed_action('app.'+action_name)
self.app.add_plugin_menu_item('browser-popup', action_name, item)
# Add track to playlist
main_item = Gio.MenuItem()
main_item.set_label(_('Добавить в плейлист'))
submenu = Gio.Menu()
for p in self.playlists:
# create action
action_name = 'ym-'+self.station_prefix+'add-to-playlist_'+str(p.kind)
action = Gio.SimpleAction(name=action_name)
action.connect("activate", self.add_track_to_playlist, p)
self.app.add_action(action)
item = Gio.MenuItem()
item.set_label(p.title)
item.set_detailed_action('app.'+action_name)
submenu.append_item(item)
main_item.set_submenu(submenu)
self.app.add_plugin_menu_item('browser-popup', 'ym-'+self.station_prefix+'add_to_playlist', main_item)
def remove_context_menu(self):
self.app.remove_plugin_menu_item('browser-popup', 'ym-'+self.station_prefix+'likes')
self.app.remove_plugin_menu_item('browser-popup', 'ym-'+self.station_prefix+'unlikes')
self.app.remove_plugin_menu_item('browser-popup', 'ym-'+self.station_prefix+'dislikes')
self.app.remove_plugin_menu_item('browser-popup', 'ym-'+self.station_prefix+'copy_track_link')
self.app.remove_plugin_menu_item('browser-popup', 'ym-'+self.station_prefix+'add_to_playlist')
def like_tracks(self, *args):
page = self.shell.props.selected_page
selected = page.get_entry_view().get_selected_entries()
if not selected: return False
tracks = []
for entry in selected:
location = entry.get_string(RB.RhythmDBPropType.LOCATION)
location = location[location.find('_')+1:]
tracks.append(location)
return self.client.users_likes_tracks_add(track_ids=tracks)
def unlike_tracks(self, *args):
page = self.shell.props.selected_page
selected = page.get_entry_view().get_selected_entries()
if not selected: return False
tracks = []
for entry in selected:
location = entry.get_string(RB.RhythmDBPropType.LOCATION)
location = location[location.find('_')+1:]
tracks.append(location)
if self.client.users_likes_tracks_remove(track_ids=tracks):
for entry in selected:
self.db.entry_delete(entry)
return self.db.commit()
return False
def dislike_tracks(self, *args):
page = self.shell.props.selected_page
selected = page.get_entry_view().get_selected_entries()
if not selected: return False
tracks = []
for entry in selected:
location = entry.get_string(RB.RhythmDBPropType.LOCATION)
location = location[location.find('_')+1:]
tracks.append(location)
if self.client.users_dislikes_tracks_add(track_ids=tracks):
for entry in selected:
self.db.entry_delete(entry)
self.db.commit()
return self.shell.props.shell_player.do_next()
return False
def copy_track_link(self, *args):
"""Copy a link to the track page on Yandex.Music."""
page = self.shell.props.selected_page
selected = page.get_entry_view().get_selected_entries()
if not selected: return False
location = selected[0].get_string(RB.RhythmDBPropType.LOCATION)
track_id, album_id = location[location.find('_')+1:].split(':')
clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD)
clipboard.set_text(f'https://music.yandex.ru/album/{album_id}/track/{track_id}', -1)
def add_track_to_playlist(self, *args):
page = self.shell.props.selected_page
selected = page.get_entry_view().get_selected_entries()
if not selected: return False
playlist = args[-1]
for entry in selected:
location = entry.get_string(RB.RhythmDBPropType.LOCATION)
track_id, album_id = location[location.find('_')+1:].split(':')
playlist = playlist.insert_track(track_id=track_id, album_id=album_id)
return False