|
|
|
/*
|
|
|
|
* To change this license header, choose License Headers in Project Properties.
|
|
|
|
* To change this template file, choose Tools | Templates
|
|
|
|
* and open the template in the editor.
|
|
|
|
*/
|
|
|
|
package moe.nekojimi.chords;
|
|
|
|
|
|
|
|
import com.amihaiemil.eoyaml.Yaml;
|
|
|
|
import com.amihaiemil.eoyaml.YamlMapping;
|
|
|
|
import com.amihaiemil.eoyaml.YamlSequence;
|
|
|
|
import com.amihaiemil.eoyaml.YamlSequenceBuilder;
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.io.FileNotFoundException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.net.MalformedURLException;
|
|
|
|
import java.net.URL;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @author jimj316
|
|
|
|
*/
|
|
|
|
public class Track implements Comparable<Track>
|
|
|
|
{
|
|
|
|
private String title;
|
|
|
|
private String artist;
|
|
|
|
private final URL url;
|
|
|
|
private File location = null;
|
|
|
|
private InputStream inputStream = null;
|
|
|
|
private int number;
|
|
|
|
private List<Format> formats = new ArrayList<>();
|
|
|
|
|
|
|
|
private boolean kept = false;
|
|
|
|
|
|
|
|
private double progress = -1;
|
|
|
|
private double eta = -1;
|
|
|
|
|
|
|
|
private final TrackRequest request;
|
|
|
|
|
|
|
|
public Track(URL url, TrackRequest request)
|
|
|
|
{
|
|
|
|
this.url = url;
|
|
|
|
this.request = request;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Track(TrackRequest request)
|
|
|
|
{
|
|
|
|
this.request = request;
|
|
|
|
this.url = request.getUrl();
|
|
|
|
}
|
|
|
|
|
|
|
|
public YamlMapping toYaml()
|
|
|
|
{
|
|
|
|
YamlSequenceBuilder build = Yaml.createYamlSequenceBuilder();
|
|
|
|
|
|
|
|
for (Format format : formats)
|
|
|
|
build = build.add(format.toYaml());
|
|
|
|
|
|
|
|
return Yaml.createYamlMappingBuilder()
|
|
|
|
.add("title", title)
|
|
|
|
.add("url", url.toExternalForm())
|
|
|
|
.add("location", location.getAbsolutePath())
|
|
|
|
.add("num", Integer.toString(number))
|
|
|
|
.add("formats", build.build())
|
|
|
|
// .add("requestedBy", requestedBy)
|
|
|
|
// .add("requestedIn", requestedIn)
|
|
|
|
.add("kept", Boolean.toString(kept))
|
|
|
|
.build();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Track fromYaml(YamlMapping map) throws MalformedURLException
|
|
|
|
{
|
|
|
|
Track track = new Track(new URL(map.string("url")), null);
|
|
|
|
track.setArtist(map.string("artist"));
|
|
|
|
track.setTitle(map.string("title"));
|
|
|
|
track.setLocation(new File(map.string("location")));
|
|
|
|
track.setNumber(map.integer("num"));
|
|
|
|
track.setKept(Boolean.parseBoolean(map.string("kept")));
|
|
|
|
// track.setRequestedBy(map.string("requestedBy"));
|
|
|
|
// track.setRequestedIn(map.string("requestedIn"));
|
|
|
|
|
|
|
|
List<Format> formats = new ArrayList<>();
|
|
|
|
YamlSequence formatSeq = map.yamlSequence("formats");
|
|
|
|
for (int i = 0; i < formats.size(); i++)
|
|
|
|
formats.add(Format.fromYaml(formatSeq.yamlMapping(i)));
|
|
|
|
track.setFormats(formats);
|
|
|
|
|
|
|
|
return track;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isDownloaded()
|
|
|
|
{
|
|
|
|
return (location != null && location.exists() && location.canRead());
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getTitle()
|
|
|
|
{
|
|
|
|
return title;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setTitle(String title)
|
|
|
|
{
|
|
|
|
this.title = title;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getNumber()
|
|
|
|
{
|
|
|
|
return number;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setNumber(int number)
|
|
|
|
{
|
|
|
|
this.number = number;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getArtist()
|
|
|
|
{
|
|
|
|
return artist;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setArtist(String artist)
|
|
|
|
{
|
|
|
|
this.artist = artist;
|
|
|
|
}
|
|
|
|
|
|
|
|
public URL getUrl()
|
|
|
|
{
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
|
|
|
|
public File getLocation()
|
|
|
|
{
|
|
|
|
return location;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setLocation(File location)
|
|
|
|
{
|
|
|
|
this.location = location;
|
|
|
|
}
|
|
|
|
|
|
|
|
public InputStream getInputStream() throws FileNotFoundException
|
|
|
|
{
|
|
|
|
if (inputStream == null && location != null)
|
|
|
|
inputStream = new FileInputStream(location);
|
|
|
|
return inputStream;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setInputStream(InputStream inputStream)
|
|
|
|
{
|
|
|
|
this.inputStream = inputStream;
|
|
|
|
}
|
|
|
|
|
|
|
|
void delete()
|
|
|
|
{
|
|
|
|
if (location != null)
|
|
|
|
location.delete();
|
|
|
|
location = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isKept()
|
|
|
|
{
|
|
|
|
return kept;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setKept(boolean kept)
|
|
|
|
{
|
|
|
|
this.kept = kept;
|
|
|
|
}
|
|
|
|
|
|
|
|
public TrackRequest getRequest()
|
|
|
|
{
|
|
|
|
return request;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toString()
|
|
|
|
{
|
|
|
|
String ret = "";
|
|
|
|
if (artist != null && !artist.isEmpty())
|
|
|
|
ret += artist + " - ";
|
|
|
|
|
|
|
|
if (title != null && !title.isEmpty())
|
|
|
|
ret += title;
|
|
|
|
|
|
|
|
if (ret.isEmpty())
|
|
|
|
ret = "track " + number;
|
|
|
|
|
|
|
|
return /*"[" + number + "] " + */ ret;
|
|
|
|
// return url.toExternalForm();
|
|
|
|
}
|
|
|
|
|
|
|
|
public List<Format> getFormats()
|
|
|
|
{
|
|
|
|
return formats;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setFormats(List<Format> formats)
|
|
|
|
{
|
|
|
|
this.formats = formats;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Format getBestFormat()
|
|
|
|
{
|
|
|
|
if (formats.isEmpty())
|
|
|
|
return null;
|
|
|
|
return formats.get(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
public double getProgress()
|
|
|
|
{
|
|
|
|
return progress;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setProgress(double progress)
|
|
|
|
{
|
|
|
|
this.progress = progress;
|
|
|
|
}
|
|
|
|
|
|
|
|
public double getEta()
|
|
|
|
{
|
|
|
|
return eta;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setEta(double eta)
|
|
|
|
{
|
|
|
|
this.eta = eta;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int compareTo(Track o)
|
|
|
|
{
|
|
|
|
return Integer.compare(number, o.number);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|