forked from fbricker/muses
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MetadataLoader.hx
140 lines (132 loc) · 4.85 KB
/
MetadataLoader.hx
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
////////////////////////////////////////////////////////////////////////////////
//
// Muses Radio Player - Radio Streaming player written in Haxe.
//
// Copyright (C) 2009-2014 Federico Bricker
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
// This Project was initially based on FOggPlayer by Bill Farmer. So
// my special thanks to him! :)
//
// Federico Bricker f bricker [at] gmail [dot] com.
//
////////////////////////////////////////////////////////////////////////////////
import flash.accessibility.Accessibility;
import flash.events.Event;
// MetadataLoader Main class
class MetadataLoader {
var player : Player;
var ui : UI;
var counter : Int;
var delay : Int;
var fileLoader: flash.net.URLLoader;
var proxy : String;
var metadataSource : String;
var mUrl : String;
public function new(player:Player, ui:UI, interval:Int, metadataSource:String, proxy:String, mUrl:String) {
if (metadataSource != "icecast" && metadataSource != "streamtheworld" && metadataSource != "shoutcast") {
return;
}
this.player=player;
this.ui = ui;
this.metadataSource = metadataSource;
this.mUrl = mUrl;
if(proxy!=null && proxy!=''){
this.proxy=proxy;
}
delay=MusesRadioPlayer.FPS*interval;
counter=delay-Math.round(MusesRadioPlayer.FPS/2);
ui.addEventListener(flash.events.Event.ENTER_FRAME,loop);
}
public function loop(_){
counter++;
if(counter>=delay && player.isPlaying()){
counter = 0;
var url:String=null;
url = switch(metadataSource) {
case "icecast": player.getCurrentUrl().split("?")[0] + ".xspf";
case "streamtheworld": mUrl + "&" + Date.now().getTime();
case "shoutcast": player.getCurrentUrl().substr(0,player.getCurrentUrl().indexOf('/',9)) + '/7.html';
default: null;
}
if(proxy!=null){
url=proxy+'?url='+StringTools.replace(url,':','%3A');
}
loadMetadata(url);
}
}
function loadMetadata(url:String){
var urlRequest : flash.net.URLRequest=new flash.net.URLRequest();
fileLoader = new flash.net.URLLoader();
urlRequest.url = url;
this.fileLoader.addEventListener(Event.COMPLETE, switch(metadataSource) {
case "icecast": loadIcecastEvent;
case "streamtheworld": loadStreamTheWorldEvent;
case "shoutcast": loadShoutcastEvent;
default: null; } );
fileLoader.load(urlRequest);
}
function loadStreamTheWorldEvent(e: Event) {
var loader=cast(e.target,flash.net.URLLoader);
var base = Xml.parse(loader.data).firstChild();
if(base.nodeName.toLowerCase()!='nowplaying-info-list'){ // CHECK THIS IS A VALID XML
return;
}
var arr : Array<AudioMetadata> = new Array<AudioMetadata>();
var metadata : AudioMetadata;
for ( elem in base.elementsNamed("nowplaying-info")) {
metadata = new AudioMetadata();
for (track in elem.elementsNamed("property")) {
if (track.get("name") == 'cue_title')
metadata.title = track.firstChild().nodeValue;
if (track.get("name") == 'track_artist_name')
metadata.artist = track.firstChild().nodeValue;
if (track.get("name") == 'cue_time_start')
metadata.comment = track.firstChild().nodeValue;
}
arr.push(metadata);
}
ui.setMetadataFromArray(arr);
}
function loadShoutcastEvent(e: Event) {
var loader=cast(e.target,flash.net.URLLoader);
var data:String = loader.data;
data=StringTools.replace(data,'<html>','');
data=StringTools.replace(data,'</html>','');
data=StringTools.replace(data,'<body>','');
data=StringTools.replace(data,'</body>','');
var parsed:Array<String> = data.split(",");
var currentSong:String=parsed[6];
for(i in 7 ... parsed.length) currentSong += ','+parsed[i];
ui.setMetadataFromString(StringTools.trim(currentSong));
}
function loadIcecastEvent(e: Event){
var loader=cast(e.target,flash.net.URLLoader);
var base = Xml.parse(loader.data).firstElement();
if (base.nodeName.toLowerCase() != 'playlist') return;
for ( elem in base.elements()) {
if (elem.nodeName.toLowerCase() != 'tracklist') continue;
for(track in elem.elements()){
if (track.nodeName.toLowerCase() != 'track') continue;
for (title in track.elements()) {
if (title.nodeName.toLowerCase() != 'title') continue;
ui.setMetadataFromString(title.firstChild().nodeValue);
return;
}
}
}
}
}