Discord bot that plays music from every website ever via youtube-dl
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Chords/src/main/java/moe/nekojimi/chords/Format.java

185 lines
4.5 KiB

/*
* 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 java.util.Objects;
import javax.json.JsonObject;
/**
*
* @author jimj316
*/
class Format
{
private final String code;
private final String extension;
private final String resolution;
private final String note;
private long size = -1;
private int samplerate = -1;
private boolean audioOnly = false;
public Format(String code, String extension, String resolution, String note)
{
this.code = code;
this.extension = extension;
this.resolution = resolution;
this.note = note;
}
/**
* Read format info from a youtube-dl JSON response.
*
* @param object the JSON object from youtube-dl.
* @return a new Format object if the JSON is valid, or null if not.
*/
public static Format fromJSON(JsonObject object)
{
String code = object.getString("format_id");
String ext = object.getString("ext");
// String res = object.getString()
String res = object.getString("format");
String note = "";
Format ret = new Format(code, ext, res, note);
int asr = object.getInt("asr", -1);
int size = object.getInt("filesize", -1);
ret.setSampleRate(asr);
ret.setSize(size);
return ret;
}
public static Format fromYaml(YamlMapping yaml)
{
Format format = new Format(
yaml.string("code"),
yaml.string("extension"),
yaml.string("resolution"),
yaml.string("note"));
return format;
}
public YamlMapping toYaml()
{
return Yaml.createYamlMappingBuilder()
.add("code", code)
.add("extension", extension)
.add("resolution", resolution)
.add("note", note)
.build();
}
public boolean isAudioOnly()
{
if (audioOnly)
return true;
return resolution.trim().toLowerCase().contains("audio only") || note.trim().toLowerCase().contains("tiny");
}
public void setAudioOnly(boolean audioOnly)
{
this.audioOnly = audioOnly;
}
public long getSize()
{
if (size != -1)
return size;
// try to find eg. "1.32MiB" inside note
long size = Util.parseSize(note);
return size;
}
public int getSampleRate()
{
if (samplerate != -1)
return samplerate;
// try to find eg. "51k" inside note
return Util.parseSampleRate(note);
}
public void setSampleRate(int samplerate)
{
this.samplerate = samplerate;
}
public void setSize(long size)
{
this.size = size;
}
public String getCode()
{
return code;
}
public String getExtension()
{
return extension;
}
public String getResolution()
{
return resolution;
}
public String getNote()
{
return note;
}
@Override
public String toString()
{
return "Format{" + "code=" + code + ", extension=" + extension + ", resolution=" + resolution + ", note=" + note + '}';
}
@Override
public int hashCode()
{
int hash = 7;
hash = 97 * hash + Objects.hashCode(this.extension);
hash = 97 * hash + Objects.hashCode(this.resolution);
hash = 97 * hash + (int) (this.size ^ (this.size >>> 32));
hash = 97 * hash + this.samplerate;
hash = 97 * hash + (this.audioOnly ? 1 : 0);
return hash;
}
@Override
public boolean equals(Object obj)
{
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final Format other = (Format) obj;
if (this.size != other.size)
return false;
if (this.samplerate != other.samplerate)
return false;
if (this.audioOnly != other.audioOnly)
return false;
if (!Objects.equals(this.code, other.code))
return false;
if (!Objects.equals(this.extension, other.extension))
return false;
if (!Objects.equals(this.resolution, other.resolution))
return false;
return true;
}
}