| 1 | # Deejayd, a media player daemon |
|---|
| 2 | # Copyright (C) 2007-2009 Mickael Royer <mickael.royer@gmail.com> |
|---|
| 3 | # Alexandre Rossi <alexandre.rossi@gmail.com> |
|---|
| 4 | # |
|---|
| 5 | # This program is free software; you can redistribute it and/or modify |
|---|
| 6 | # it under the terms of the GNU General Public License as published by |
|---|
| 7 | # the Free Software Foundation; either version 2 of the License, or |
|---|
| 8 | # (at your option) any later version. |
|---|
| 9 | # |
|---|
| 10 | # This program is distributed in the hope that it will be useful, |
|---|
| 11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 13 | # GNU General Public License for more details. |
|---|
| 14 | # |
|---|
| 15 | # You should have received a copy of the GNU General Public License along |
|---|
| 16 | # with this program; if not, write to the Free Software Foundation, Inc., |
|---|
| 17 | # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
|---|
| 18 | |
|---|
| 19 | import urllib |
|---|
| 20 | from deejayd.ui import log |
|---|
| 21 | |
|---|
| 22 | def quote_uri(path): |
|---|
| 23 | if type(path) is unicode: |
|---|
| 24 | path = path.encode('utf-8') |
|---|
| 25 | return "file://%s" % urllib.quote(path) |
|---|
| 26 | |
|---|
| 27 | def str_encode(data, charset = 'utf-8', errors='strict'): |
|---|
| 28 | if type(data) is unicode: return data |
|---|
| 29 | try: rs = data.decode(charset, errors) |
|---|
| 30 | except UnicodeError: |
|---|
| 31 | log.err(_("%s string has wrong characters, skip it") %\ |
|---|
| 32 | data.decode(charset, "ignore").encode("utf-8","ignore")) |
|---|
| 33 | raise UnicodeError |
|---|
| 34 | return unicode(rs) |
|---|
| 35 | |
|---|
| 36 | def format_time(time): |
|---|
| 37 | """Turn a time value in seconds into hh:mm:ss or mm:ss.""" |
|---|
| 38 | if time >= 3600: # 1 hour |
|---|
| 39 | # time, in hours:minutes:seconds |
|---|
| 40 | return "%d:%02d:%02d" % (time // 3600, (time % 3600) // 60, time % 60) |
|---|
| 41 | else: |
|---|
| 42 | # time, in minutes:seconds |
|---|
| 43 | return "%d:%02d" % (time // 60, time % 60) |
|---|
| 44 | |
|---|
| 45 | def format_time_long(time): |
|---|
| 46 | """Turn a time value in seconds into x hours, x minutes, etc.""" |
|---|
| 47 | if time < 1: return _("No time information") |
|---|
| 48 | cutoffs = [ |
|---|
| 49 | (60, "%d seconds", "%d second"), |
|---|
| 50 | (60, "%d minutes", "%d minute"), |
|---|
| 51 | (24, "%d hours", "%d hour"), |
|---|
| 52 | (365, "%d days", "%d day"), |
|---|
| 53 | (None, "%d years", "%d year"), |
|---|
| 54 | ] |
|---|
| 55 | time_str = [] |
|---|
| 56 | for divisor, plural, single in cutoffs: |
|---|
| 57 | if time < 1: break |
|---|
| 58 | if divisor is None: time, unit = 0, time |
|---|
| 59 | else: time, unit = divmod(time, divisor) |
|---|
| 60 | if unit: time_str.append(ngettext(single, plural, unit) % unit) |
|---|
| 61 | time_str.reverse() |
|---|
| 62 | if len(time_str) > 2: time_str.pop() |
|---|
| 63 | return ", ".join(time_str) |
|---|
| 64 | |
|---|
| 65 | ngettext("%d second", "%d seconds", 1) |
|---|
| 66 | ngettext("%d minute", "%d minutes", 1) |
|---|
| 67 | ngettext("%d hour", "%d hours", 1) |
|---|
| 68 | ngettext("%d day", "%d days", 1) |
|---|
| 69 | ngettext("%d year", "%d years", 1) |
|---|
| 70 | |
|---|
| 71 | def get_playlist_file_lines(URL): |
|---|
| 72 | pls_handle = urllib.urlopen(URL) |
|---|
| 73 | playlist = pls_handle.read() |
|---|
| 74 | |
|---|
| 75 | return playlist.splitlines() |
|---|
| 76 | |
|---|
| 77 | def get_uris_from_pls(URL): |
|---|
| 78 | uris = [] |
|---|
| 79 | lines = get_playlist_file_lines(URL) |
|---|
| 80 | for line in lines: |
|---|
| 81 | if line.lower().startswith("file") and line.find("=")!=-1: |
|---|
| 82 | uris.append(line[line.find("=")+1:].strip()) |
|---|
| 83 | |
|---|
| 84 | return uris |
|---|
| 85 | |
|---|
| 86 | def get_uris_from_m3u(URL): |
|---|
| 87 | uris = [] |
|---|
| 88 | lines = get_playlist_file_lines(URL) |
|---|
| 89 | for line in lines: |
|---|
| 90 | if not line.startswith("#") and line.strip()!="": |
|---|
| 91 | uris.append(line.strip()) |
|---|
| 92 | |
|---|
| 93 | return uris |
|---|
| 94 | |
|---|
| 95 | # vim: ts=4 sw=4 expandtab |
|---|