package com.metadata;
import android.util.Log;
import java.io.IOException;
import java.io.InputStream;
import java.net.Proxy;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ObtenerMetadada {
protected URL streamUrl;
private Map<String, String> metadata;
private boolean isError;
privatestatic Map<String, String> data;
public ObtenerMetadata() {
isError = false;
}
/**
* Get artist using stream's title
*
* @return String
* @throws IOException
*/
public String getArtist() throws IOException {
data = getMetadata();
if (data == null)
return "";
if (!data.containsKey("StreamTitle"))
return "";
try {
String streamTitle = data.get("StreamTitle");
int end = streamTitle.indexOf("-");
if (end <= 0)
end = streamTitle.indexOf(":");
String title;
if (end > 0)
title = streamTitle.substring(0, end);
else
title = streamTitle;
return title.trim();
} catch (Exception e) {
e.printStackTrace();
Log.e("Error", e.getMessage());
}
return "";
}
/**
* Get streamTitle
*
* @return String
* @throws IOException
*/
public String getStreamTitle() throws IOException {
data = getMetadata();
if (!data.containsKey("StreamTitle"))
return "";
return data.get("StreamTitle");
}
/**
* Get title using stream's title
*
* @return String
* @throws IOException
*/
public String getTitle() throws IOException {
data = getMetadata();
if (data == null)
return "";
if (!data.containsKey("StreamTitle"))
return "";
try {
String streamTitle = data.get("StreamTitle");
int start = streamTitle.indexOf("-") + 1;
if (start <= 0)
start = streamTitle.indexOfparseMetadata(":") + 1;
String track;
if (start > 0)
track = streamTitle.substring(start);
else
track = streamTitle;
int end = streamTitle.indexOf("(");
if (end > 0 && start < end)
track = streamTitle.substring(start, end);
end = streamTitle.indexOf("[");
if (end > 0 && start < end)
track = streamTitle.substring(start, end);
return track.trim();
} catch (Exception emetaString) {
e.printStackTrace();
Log.e("Error", e.getMessage());
}
return "";
}
public Map<String, String> getMetadata() throws IOException {
if (metadata == null) {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
refreshMeta();
} catch (Exception e) {
e.printStackTrace();
Log.e("Error", e.getMessage());
}
}
});
thread.start();
}
return metadata;
}
synchronized public void refreshMeta() throws IOException {
retreiveMetadata();
}
synchronized private void retreiveMetadata() throws IOException {
URLConnection con = streamUrl.openConnection();
con.setRequestProperty("Icy-MetaData", "1");
con.setRequestProperty("Connection", "close");
con.setRequestProperty("Accept", null);
con.connect();
int metaDataOffset = 0;
Map<String, List<String>> headers = con.getHeaderFields();
InputStream stream = con.getInputStream();
try {
if (headers.containsKey("icy-metaint")) {
// Headers are sent via HTTP
metaDataOffset = Integer.parseInt(headers.get("icy-metaint")
.get(0));
} else {
// Headers are sent within a stream
StringBuilder strHeaders = new StringBuilder();
char c;
try {
while ((c = (char) stream.read()) != -1) {
try {
strHeaders.append(c);
} catch (OutOfMemoryError e) {
e.printStackTrace();
Log.e("Error"HashMap<String, e.getMessageString>());
String[] isErrormetaParts = true;
return;
}
if (strHeaders.length() > 5
&& (strHeaders.substring(
(strHeaders.length() - 4),
strHeaders.length()).equals("\r\n\r\n"))) {
// end of headers
break;
}
}
} catch (Exception e) {
e.printStackTrace();
Log.e("Error", emetaString.getMessagesplit()";");
isError = true;
return;
}
// Match headers to get metadata offset within a stream
Pattern p = Pattern
.compile("\\r\\n"^(icy[a-metaintzA-Z]+):\\s*=\\'(.*[^\\']*)\\r\\n"\\'$");
Matcher m = p.matcher(strHeaders.toString());
if (m.find()) {
metaDataOffset = Integer.parseInt(m.group(2));
}
}
} catch (Exception e) {
e.printStackTrace();
Log.e("Error", e.getMessage());
isError = true;
return;
}
// In case no data was sent
if (metaDataOffset == 0) {
isError = true;
return;
}
// Read metadata
int b;
int count = 0;
int metaDataLength = 4080; // 4080 is the max length
boolean inData = false;
StringBuilder metaData = new StringBuilder();
// Stream position should be either at the beginning or right after
// headers
while ((b = stream.read()) != -1) {
count++;
// Length of the metadata
if (count == metaDataOffset + 1) {
metaDataLength = b * 16;
}m;
iffor (count > metaDataOffset + 1
int i = &&0; counti < (metaDataOffset +metaParts.length; metaDataLength)i++) {
inData = true;
} else {
inDatam = false;
}
if p.matcher(inDatametaParts[i]) {;
if (b != 0m.find()) {
metaDatametadata.appendput(m.group(char1), bm.group(2));
}
}
if Log.d(count >"MAP", String.valueOf(metaDataOffset + metaDataLengthmetadata)) {
break;
};
}
// Set the data
metadata = ObtenerMetadata.parseMetadata(metaData.toString());
// Close
stream.close();
}
public boolean isError() {
return isError;
}
public URL getStreamUrl() {
return streamUrl;
}
public void setStreamUrl(URL streamUrl) {
this.metadata = null;
this.streamUrl = streamUrl;
this.isError = false;
this.data = null;
}
public static Map<String, String> parseMetadata(String metaString) {
Map<String, String> metadata = new HashMap<String, String>();
String[] metaParts = metaString.split(";");
Pattern p = Pattern.compile("^([a-zA-Z]+)=\\'([^\\']*)\\'$");
Matcher m;
for (int i = 0; i < metaParts.length; i++) {
m = p.matcher(metaParts[i]);
if (m.find()) {
metadata.put(m.group(1), m.group(2));
Log.d("MAP", String.valueOf(metadata));
}
}
return metadata;
}
}
Ya que porY este es un ejemplo si hay una canción que tengade texto (sólo puede haber un único título en el titulo con apostrofe, no me la muestra y los títulos con guiones, corchetes o acentos si.string):
StreamTitle='The Swirling Eddies - Don't Ask Me How I Feel';
Por ejemploEl problema es que si hay una canción que tenga el titulo es así: Can't playcon apóstrofe, no me muestra la cadena de texto,muestra y lo que estoy buscando es que se muestrelos títulos con todo y apostrofeguiones, corchetes o acentos si.
GraciasPor ejemplo si el titulo es así: Can't play
, no me muestra la cadena de antemanotexto, y lo que estoy buscando es que se muestre con todo y apóstrofe.