| 1 |
# Copyright (C) 2008-2010 Adam Olsen |
| 2 |
# |
| 3 |
# This program is free software; you can redistribute it and/or modify |
| 4 |
# it under the terms of the GNU General Public License as published by |
| 5 |
# the Free Software Foundation; either version 2, or (at your option) |
| 6 |
# any later version. |
| 7 |
# |
| 8 |
# This program is distributed in the hope that it will be useful, |
| 9 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 |
# GNU General Public License for more details. |
| 12 |
# |
| 13 |
# You should have received a copy of the GNU General Public License |
| 14 |
# along with this program; if not, write to the Free Software |
| 15 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 16 |
# |
| 17 |
# |
| 18 |
# The developers of the Exaile media player hereby grant permission |
| 19 |
# for non-GPL compatible GStreamer and Exaile plugins to be used and |
| 20 |
# distributed together with GStreamer and Exaile. This permission is |
| 21 |
# above and beyond the permissions granted by the GPL license by which |
| 22 |
# Exaile is covered. If you modify this code, you may extend this |
| 23 |
# exception to your version of the code, but you are not obligated to |
| 24 |
# do so. If you do not wish to do so, delete this exception statement |
| 25 |
# from your version. |
| 26 |
|
| 27 |
from typing import Callable, Iterable, List, Optional, TypeVar |
| 28 |
|
| 29 |
from gi.repository import Gio |
| 30 |
from gi.repository import GLib |
| 31 |
|
| 32 |
from xl import metadata |
| 33 |
from xl.trax.track import Track |
| 34 |
from xl.trax.search import search_tracks, TracksMatcher |
| 35 |
|
| 36 |
_T = TypeVar('_T') |
| 37 |
|
| 38 |
|
| 39 |
def is_valid_track(location: str) -> bool: |
| 40 |
""" |
| 41 |
Returns whether the file at the given location is a valid track |
| 42 |
|
| 43 |
:param location: the location to check |
| 44 |
:returns: whether the file is a valid track |
| 45 |
""" |
| 46 |
extension = Gio.File.new_for_commandline_arg(location).get_basename().split(".")[-1] |
| 47 |
return extension.lower() in metadata.formats |
| 48 |
|
| 49 |
|
| 50 |
def get_uris_from_tracks(tracks: Iterable[Track]) -> List[str]: |
| 51 |
""" |
| 52 |
Returns all URIs for tracks |
| 53 |
|
| 54 |
:param tracks: the tracks to retrieve the URIs from |
| 55 |
:returns: the uris |
| 56 |
""" |
| 57 |
return [track.get_loc_for_io() for track in tracks] |
| 58 |
|
| 59 |
|
| 60 |
def get_tracks_from_uri(uri: str) -> List[Track]: |
| 61 |
""" |
| 62 |
Returns all valid tracks located at uri |
| 63 |
|
| 64 |
:param uri: the uri to retrieve the tracks from |
| 65 |
:type uri: string |
| 66 |
:returns: the retrieved tracks |
| 67 |
:rtype: list of :class:`xl.trax.Track` |
| 68 |
""" |
| 69 |
tracks = [] |
| 70 |
|
| 71 |
gloc = Gio.File.new_for_uri(uri) |
| 72 |
|
| 73 |
# don't do advanced checking on streaming-type uris as it can fail or |
| 74 |
# otherwise be terribly slow. |
| 75 |
# TODO: move uri definition somewhere more common for easy reuse? |
| 76 |
if gloc.get_uri_scheme() in ('http', 'mms', 'cdda'): |
| 77 |
return [Track(uri)] |
| 78 |
|
| 79 |
try: |
| 80 |
file_type = gloc.query_info( |
| 81 |
"standard::type", Gio.FileQueryInfoFlags.NONE, None |
| 82 |
).get_file_type() |
| 83 |
except GLib.Error: # cdda track, nonexistent file, etc. |
| 84 |
return [] |
| 85 |
if file_type == Gio.FileType.DIRECTORY: |
| 86 |
# TODO: refactor Library so we dont need the collection obj |
| 87 |
from xl.collection import Library, Collection |
| 88 |
|
| 89 |
tracks = Collection('scanner') |
| 90 |
lib = Library(uri) |
| 91 |
lib.set_collection(tracks) |
| 92 |
lib.rescan() |
| 93 |
tracks = tracks.get_tracks() |
| 94 |
else: |
| 95 |
tracks = [Track(uri)] |
| 96 |
return tracks |
| 97 |
|
| 98 |
|
| 99 |
def sort_tracks( |
| 100 |
fields: Iterable[str], |
| 101 |
items: Iterable[_T], |
| 102 |
trackfunc: Optional[Callable[[_T], Track]] = None, |
| 103 |
reverse: bool = False, |
| 104 |
artist_compilations: bool = False, |
| 105 |
) -> List[_T]: |
| 106 |
""" |
| 107 |
Sorts tracks. |
| 108 |
|
| 109 |
:param fields: tag names to sort by |
| 110 |
:param items: the tracks to sort, |
| 111 |
alternatively use *trackfunc* |
| 112 |
:param trackfunc: function to get a *Track* |
| 113 |
from an item in the *items* iterable |
| 114 |
:param reverse: whether to sort in reversed order |
| 115 |
""" |
| 116 |
if trackfunc is None: |
| 117 |
trackfunc = lambda tr: tr |
| 118 |
keyfunc = lambda tr: [ |
| 119 |
trackfunc(tr).get_tag_sort(field, artist_compilations=artist_compilations) |
| 120 |
for field in fields |
| 121 |
] |
| 122 |
return sorted(items, key=keyfunc, reverse=reverse) |
| 123 |
|
| 124 |
|
| 125 |
def sort_result_tracks(fields, trackiter, reverse=False, artist_compilations=False): |
| 126 |
""" |
| 127 |
Sorts SearchResultTracks, ie. the output from a search. |
| 128 |
|
| 129 |
Same params as sort_tracks. |
| 130 |
""" |
| 131 |
return sort_tracks( |
| 132 |
fields, trackiter, lambda tr: tr.track, reverse, artist_compilations |
| 133 |
) |
| 134 |
|
| 135 |
|
| 136 |
def get_rating_from_tracks(tracks): |
| 137 |
""" |
| 138 |
Returns the common rating for all tracks or |
| 139 |
simply 0 if not all tracks have the same |
| 140 |
rating. Same goes if the amount of tracks |
| 141 |
is 0 or more than the internal limit. |
| 142 |
|
| 143 |
:param tracks: the tracks to retrieve the rating from |
| 144 |
:type tracks: iterable |
| 145 |
""" |
| 146 |
if len(tracks) < 1: |
| 147 |
return 0 |
| 148 |
|
| 149 |
# TODO: still needed? |
| 150 |
# if len(tracks) > settings.get_option('rating/tracks_limit', 100): |
| 151 |
# return 0 |
| 152 |
|
| 153 |
rating = tracks[0].get_rating() |
| 154 |
|
| 155 |
for track in tracks: |
| 156 |
if track.get_rating() != rating: |
| 157 |
return 0 |
| 158 |
|
| 159 |
return rating |
| 160 |
|
| 161 |
|
| 162 |
def get_album_tracks(tracksiter, track, artist_compilations=False): |
| 163 |
""" |
| 164 |
Get any tracks from the given iterable that appear to be part of |
| 165 |
the same album as track. If track is in the iterable, it will be |
| 166 |
included in the result. If there is insufficient information to |
| 167 |
determine the album, the empty list will be returned, even if the |
| 168 |
track is in the iterable. |
| 169 |
""" |
| 170 |
if not all(track.get_tag_raw(t) for t in ['artist', 'album']): |
| 171 |
return [] |
| 172 |
matchers = [ |
| 173 |
TracksMatcher(track.get_tag_search(t, artist_compilations=artist_compilations)) |
| 174 |
for t in ('artist', 'album') |
| 175 |
] |
| 176 |
return (r.track for r in search_tracks(tracksiter, matchers)) |
| 177 |
|
| 178 |
|
| 179 |
def recursive_tracks_from_file(gfile: Gio.File) -> Iterable[Track]: |
| 180 |
""" |
| 181 |
Get recursive tracks from Gio.File |
| 182 |
If it's a directory, expands |
| 183 |
Gets only valid tracks |
| 184 |
""" |
| 185 |
ftype = gfile.query_info( |
| 186 |
'standard::type', Gio.FileQueryInfoFlags.NONE, None |
| 187 |
).get_file_type() |
| 188 |
if ftype == Gio.FileType.DIRECTORY: |
| 189 |
file_infos = gfile.enumerate_children( |
| 190 |
'standard::name', Gio.FileQueryInfoFlags.NONE, None |
| 191 |
) |
| 192 |
files = (gfile.get_child(fi.get_name()) for fi in file_infos) |
| 193 |
for sub_gfile in files: |
| 194 |
for i in recursive_tracks_from_file(sub_gfile): |
| 195 |
yield i |
| 196 |
else: |
| 197 |
uri = gfile.get_uri() |
| 198 |
if is_valid_track(uri): |
| 199 |
yield Track(uri) |