| 1 |
using Grl; |
| 2 |
|
| 3 |
public class SimplePlaylist : Object { |
| 4 |
private GLib.List<MediaSource> source_list; |
| 5 |
MainLoop main_loop = new MainLoop (null, false); |
| 6 |
int processed_sources = 0; |
| 7 |
|
| 8 |
construct { |
| 9 |
var registry = Grl.PluginRegistry.get_default (); |
| 10 |
|
| 11 |
registry.source_added.connect (source_added_cb); |
| 12 |
registry.source_removed.connect (source_removed_cb); |
| 13 |
|
| 14 |
if (registry.load_all () == false) { |
| 15 |
error ("Failed to load plugins."); |
| 16 |
} |
| 17 |
|
| 18 |
} |
| 19 |
|
| 20 |
public void source_added_cb (MediaPlugin plugin) { |
| 21 |
var source = plugin as MetadataSource; |
| 22 |
|
| 23 |
var ops = source.supported_operations (); |
| 24 |
if ((ops & Grl.SupportedOps.SEARCH) != 0) { |
| 25 |
debug ("Detected new source availabe: '%s' and it supports search", source.get_name ()); |
| 26 |
source_list.append (source as MediaSource); |
| 27 |
debug ("source list size = %u", source_list.length ()); |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
public void source_removed_cb (MediaPlugin source) { |
| 32 |
debug ("Source '%s' is gone", (source as MetadataSource).get_name ()); |
| 33 |
} |
| 34 |
|
| 35 |
public SimplePlaylist () { |
| 36 |
} |
| 37 |
|
| 38 |
private void search_cb (Grl.MediaSource source, |
| 39 |
uint browse_id, |
| 40 |
Grl.Media? media, |
| 41 |
uint remaining, |
| 42 |
GLib.Error? error) { |
| 43 |
if (error != null) { |
| 44 |
critical ("Error: %s", error.message); |
| 45 |
} |
| 46 |
|
| 47 |
if (media != null && (media is MediaAudio || media is MediaVideo)) { |
| 48 |
var url = media.get_url (); |
| 49 |
if (url != null) { |
| 50 |
print ("%s\n", media.get_url ()); |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
if (remaining == 0) { |
| 55 |
processed_sources++; |
| 56 |
debug ("%s finished", source.get_name ()); |
| 57 |
} |
| 58 |
|
| 59 |
debug ("processed sources %d - source list size %u", processed_sources, source_list.length ()); |
| 60 |
if (processed_sources == source_list.length ()) { |
| 61 |
main_loop.quit (); |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
public void search (string q) { |
| 66 |
unowned GLib.List keys = Grl.MetadataKey.list_new (Grl.MetadataKey.ID, Grl.MetadataKey.TITLE, Grl.MetadataKey.URL); |
| 67 |
|
| 68 |
foreach (MediaSource source in source_list) { |
| 69 |
debug ("%s - %s", source.get_name (), q); |
| 70 |
var caps = source.get_caps (Grl.SupportedOps.SEARCH); |
| 71 |
var options = new Grl.OperationOptions (caps); |
| 72 |
options.set_count (100); |
| 73 |
options.set_flags (Grl.MetadataResolutionFlags.FULL | Grl.MetadataResolutionFlags.IDLE_RELAY); |
| 74 |
source.search (q, keys, options, search_cb); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
public void run () { |
| 79 |
main_loop.run (); |
| 80 |
} |
| 81 |
|
| 82 |
public static int main (string[] args) { |
| 83 |
Grl.init(ref args); |
| 84 |
var playlist = new SimplePlaylist (); |
| 85 |
|
| 86 |
if (args[1] != null) { |
| 87 |
playlist.search (args[1]); |
| 88 |
playlist.run (); |
| 89 |
} else { |
| 90 |
print ("Query parameter missing\n"); |
| 91 |
} |
| 92 |
|
| 93 |
return 0; |
| 94 |
} |
| 95 |
} |