Internet radio browser GUI for music/video streams from various directory services.

⌈⌋ ⎇ branch:  streamtuner2


Diff

Differences From Artifact [9f80845729]:

To Artifact [ef4879a2ca]:


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
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


-
-
+
+

-
+











-
+
+







# encoding: UTF-8
# api: streamtuner2
# title: Drag and Drop
# description: Move streams/stations from and to other applications.
# title: Drag and Drop (experimental)
# description: Copy streams/stations from and to other applications.
# depends: uikit
# version: 0.1
# version: 0.5
# type: interface
# config:
#   { name: dnd_format, type: select, value: xspf, select: "pls|m3u|xspf|jspf|asx|smil", description: "Default temporary file format for copying a station entry." }
# category: ui
# priority: experimental
#
# Implements Gtk/X11 drag and drop support for station lists.
# Should allow to export either just stream URLs, or complete
# PLS, XSPF collections.
#
# Also used by the bookmarks channel to copy favourites around.
# Which perhaps should even be constrained to just the bookmarks tab.
# Which perhaps should even be constrained to just the bookmarks
# tab.


import copy
from config import conf, json, log
from uikit import *
import action

80
81
82
83
84
85
86


87
88
89
90
91
92
93
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96







+
+







      ("text/uri-list", 0, 4),
      # url+comments
      ("TEXT", 0, 5),
      ("STRING", 0, 5),
      ("UTF8_STRING", 0, 5),
      ("text/plain", 0, 5),
    ]
    
    # Map target/`info` integers to action. module identifiers
    cnv_types = {
       20: "m3u",
       21: "pls",
       22: "xspf",
       23: "smil",
       25: "jspf",
       15: "srv",
135
136
137
138
139
140
141

142
143

144
145
146
147
148
149
150
138
139
140
141
142
143
144
145
146

147
148
149
150
151
152
153
154







+

-
+







    # Keep currently selected row when source dragging starts
    def treelist_row(self):
        cn = self.parent.channel()
        row = copy.copy(cn.row())
        row.setdefault("format", cn.audioformat)
        row.setdefault("listformat", cn.listformat)
        row.setdefault("url", row.get("homepage"))
        row.update({"_origin": [cn.module, cn.current, cn.rowno()]}) # internal: origin channel+genre+rowid
        return row
        
    
    # Target window/app requests data for offered drop
    def data_get(self, widget, context, selection, info, time):
        log.DND("source→out: data-get, send and convert to requested target type:", info, selection.get_target())
        # Return prepared data
        func, data = self.export_row(info, self.row)
        if func.find("text") >= 0:
            # Yay for trial and error. Nay for docs. PyGtks selection.set_text() doesn't
208
209
210
211
212
213
214
215
216

217
218

219
220
221
222
223
224
225
226
227
228
229

230

231
232
233
234
235
236
237
238
239
240

241
242

243
244

245
246

247
248
249
250
251
252

253
254
255
256
257





258
259

260
261
262
263
264
265
266


267
268
269
270
271
272
273
274
275
276
277
278
212
213
214
215
216
217
218


219


220
221
222
223
224
225
226
227
228
229
230
231
232

233
234
235
236
237
238
239
240
241
242

243
244

245
246

247
248

249
250
251
252
253
254

255
256
257
258
259

260
261
262
263
264
265

266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287







-
-
+
-
-
+











+
-
+









-
+

-
+

-
+

-
+





-
+




-
+
+
+
+
+

-
+







+
+













        # incoming data
        data = selection.get_text()
        urls = selection.get_uris()
        any = (data or urls) and True

        # Convert/Add
        if any:
            self.import_row(info, urls, data, y)
        if any: self.import_row(info, urls, data, y)
        else:
            log.DND("abort, no urls/text")
        else: log.DND("Abort, no urls/text.")
        
        # Respond
        context.drop_finish(any, time)
        context.finish(any, False, time)
        return True

    # Received files or payload has to be converted, copied into streams
    def import_row(self, info, urls, data, y=5000):
        # Internal target dicts
        cn = self.parent.channel()
        rows = []
        print info
        
                
        # Direct/internal row import
        if data and info >= 51:
            log.DND("Received row, append, reload")
            rows += [ json.loads(data) ]

        # Convertible formats
        elif data and info >= 5:
            cnv = action.extract_playlist(data)
            urls = cnv.format(self.cnv_types[info] if info>=20 else "raw")
            rows += [ self.imported_row(urls[0]) ]
            rows += [ self.imported_row(urls[0], cnv.title()) ]

        # Extract from playlist files (don't import mp3s into stream lists directly)
        # Extract from playlist files, either passed as text/uri-list or FILE_NAME
        elif urls:
            for fn in [re.sub("^\w+://[^/]*", "", fn) for fn in urls if re.match("^(scp|file)://(localhost)?/|/", fn)]:
            for fn in [re.sub("^\w+://[^/]*", "", fn) for fn in urls or [data] if re.match("^(scp|file)://(localhost)?/|/", fn)]:
                ext = action.probe_playlist_fn_ext(fn)
                if ext:
                if ext:  # don't import mp3s into stream lists directly
                    cnt = open(fn, "rt").read()
                    probe = action.probe_playlist_content(cnt)
                    if ext == probe:
                        cnv = action.extract_playlist(cnt)
                        urls = cnv.format(probe)
                        rows += [ self.imported_row(urls[0], os.path.basename(fn)) ]
                        rows += [ self.imported_row(urls[0], cnv.title() or os.path.basename(fn)) ]
        
        # Insert and update view
        if rows:
            # Inserting at correct row requires deducing index from dnd `y` position
            cn.streams[cn.current] += rows
            streams = cn.streams[cn.current]
            i_pos = (cn.gtk_list.get_path_at_pos(10, y) or [[len(streams) + 1]])[0][0]
            for row in rows:
                streams.insert(i_pos - 1, row)
                i_pos = i_pos + 1
            # Now appending to the liststore directly would be even nicer
            uikit.do(cn.load, cn.current)
            uikit.do(lambda *x: cn.load(cn.current))#, cn.gtk_list.scroll_to_point(0, y))
            if cn.module == "bookmarks":
                cn.save()
            #self.parent.streamedit()
        else:
            self.parent.status("Unsupported station format. Not imported.")


    # Stub row for dragged entries.
    # Which is a workaround for the missing full playlist conversion and literal URL input
    def imported_row(self, url, title=None):
        return {
            "title": title or "",
            "url": url,
            "homepage": "",
            "playling": "",
            "listformat": action.probe_playlist_fn_ext(url) or "href",
            "format": ",".join(re.findall("ogg|mpeg|mp\d+", url)),
            "genre": "copy",
        }