forked from j62/ctbrec
Add possibility to safe site specific data
Extend the json serialization to allow to write site specific data for each model.
This commit is contained in:
parent
4696c0f534
commit
bc40c16000
|
@ -8,6 +8,8 @@ import java.util.concurrent.ExecutionException;
|
||||||
|
|
||||||
import com.iheartradio.m3u8.ParseException;
|
import com.iheartradio.m3u8.ParseException;
|
||||||
import com.iheartradio.m3u8.PlaylistException;
|
import com.iheartradio.m3u8.PlaylistException;
|
||||||
|
import com.squareup.moshi.JsonReader;
|
||||||
|
import com.squareup.moshi.JsonWriter;
|
||||||
|
|
||||||
import ctbrec.recorder.download.StreamSource;
|
import ctbrec.recorder.download.StreamSource;
|
||||||
|
|
||||||
|
@ -93,6 +95,16 @@ public abstract class AbstractModel implements Model {
|
||||||
return url;
|
return url;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void readSiteSpecificData(JsonReader reader) throws IOException {
|
||||||
|
// noop default implementation, can be overriden by concrete models
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void writeSiteSpecificData(JsonWriter writer) throws IOException {
|
||||||
|
// noop default implementation, can be overriden by concrete models
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
final int prime = 31;
|
final int prime = 31;
|
||||||
|
|
|
@ -6,6 +6,8 @@ import java.util.concurrent.ExecutionException;
|
||||||
|
|
||||||
import com.iheartradio.m3u8.ParseException;
|
import com.iheartradio.m3u8.ParseException;
|
||||||
import com.iheartradio.m3u8.PlaylistException;
|
import com.iheartradio.m3u8.PlaylistException;
|
||||||
|
import com.squareup.moshi.JsonReader;
|
||||||
|
import com.squareup.moshi.JsonWriter;
|
||||||
|
|
||||||
import ctbrec.recorder.download.StreamSource;
|
import ctbrec.recorder.download.StreamSource;
|
||||||
import ctbrec.sites.Site;
|
import ctbrec.sites.Site;
|
||||||
|
@ -35,4 +37,6 @@ public interface Model {
|
||||||
public boolean unfollow() throws IOException;
|
public boolean unfollow() throws IOException;
|
||||||
public void setSite(Site site);
|
public void setSite(Site site);
|
||||||
public Site getSite();
|
public Site getSite();
|
||||||
|
public void writeSiteSpecificData(JsonWriter writer) throws IOException;
|
||||||
|
public void readSiteSpecificData(JsonReader reader) throws IOException;
|
||||||
}
|
}
|
|
@ -32,45 +32,51 @@ public class ModelJsonAdapter extends JsonAdapter<Model> {
|
||||||
String url = null;
|
String url = null;
|
||||||
String type = null;
|
String type = null;
|
||||||
int streamUrlIndex = -1;
|
int streamUrlIndex = -1;
|
||||||
|
|
||||||
|
Model model = null;
|
||||||
while(reader.hasNext()) {
|
while(reader.hasNext()) {
|
||||||
Token token = reader.peek();
|
try {
|
||||||
if(token == Token.NAME) {
|
Token token = reader.peek();
|
||||||
String key = reader.nextName();
|
if(token == Token.NAME) {
|
||||||
if(key.equals("name")) {
|
String key = reader.nextName();
|
||||||
name = reader.nextString();
|
if(key.equals("name")) {
|
||||||
} else if(key.equals("description")) {
|
name = reader.nextString();
|
||||||
description = reader.nextString();
|
model.setName(name);
|
||||||
} else if(key.equals("url")) {
|
} else if(key.equals("description")) {
|
||||||
url = reader.nextString();
|
description = reader.nextString();
|
||||||
} else if(key.equals("type")) {
|
model.setDescription(description);
|
||||||
type = reader.nextString();
|
} else if(key.equals("url")) {
|
||||||
} else if(key.equals("streamUrlIndex")) {
|
url = reader.nextString();
|
||||||
streamUrlIndex = reader.nextInt();
|
model.setUrl(url);
|
||||||
|
} else if(key.equals("type")) {
|
||||||
|
type = reader.nextString();
|
||||||
|
Class<?> modelClass = Class.forName(Optional.ofNullable(type).orElse(ChaturbateModel.class.getName()));
|
||||||
|
model = (Model) modelClass.newInstance();
|
||||||
|
} else if(key.equals("streamUrlIndex")) {
|
||||||
|
streamUrlIndex = reader.nextInt();
|
||||||
|
model.setStreamUrlIndex(streamUrlIndex);
|
||||||
|
} else if(key.equals("siteSpecific")) {
|
||||||
|
reader.beginObject();
|
||||||
|
model.readSiteSpecificData(reader);
|
||||||
|
reader.endObject();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
reader.skipValue();
|
||||||
}
|
}
|
||||||
} else {
|
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
|
||||||
reader.skipValue();
|
throw new IOException("Couldn't instantiate model class [" + type + "]", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
reader.endObject();
|
reader.endObject();
|
||||||
|
|
||||||
try {
|
if(sites != null) {
|
||||||
Class<?> modelClass = Class.forName(Optional.ofNullable(type).orElse(ChaturbateModel.class.getName()));
|
for (Site site : sites) {
|
||||||
Model model = (Model) modelClass.newInstance();
|
if(site.isSiteForModel(model)) {
|
||||||
model.setName(name);
|
model.setSite(site);
|
||||||
model.setDescription(description);
|
|
||||||
model.setUrl(url);
|
|
||||||
model.setStreamUrlIndex(streamUrlIndex);
|
|
||||||
if(sites != null) {
|
|
||||||
for (Site site : sites) {
|
|
||||||
if(site.isSiteForModel(model)) {
|
|
||||||
model.setSite(site);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return model;
|
|
||||||
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
|
|
||||||
throw new IOException("Couldn't instantiate model class [" + type + "]", e);
|
|
||||||
}
|
}
|
||||||
|
return model;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -81,6 +87,10 @@ public class ModelJsonAdapter extends JsonAdapter<Model> {
|
||||||
writeValueIfSet(writer, "description", model.getDescription());
|
writeValueIfSet(writer, "description", model.getDescription());
|
||||||
writeValueIfSet(writer, "url", model.getUrl());
|
writeValueIfSet(writer, "url", model.getUrl());
|
||||||
writer.name("streamUrlIndex").value(model.getStreamUrlIndex());
|
writer.name("streamUrlIndex").value(model.getStreamUrlIndex());
|
||||||
|
writer.name("siteSpecific");
|
||||||
|
writer.beginObject();
|
||||||
|
model.writeSiteSpecificData(writer);
|
||||||
|
writer.endObject();
|
||||||
writer.endObject();
|
writer.endObject();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -7,6 +7,8 @@ import java.util.concurrent.ExecutionException;
|
||||||
|
|
||||||
import com.iheartradio.m3u8.ParseException;
|
import com.iheartradio.m3u8.ParseException;
|
||||||
import com.iheartradio.m3u8.PlaylistException;
|
import com.iheartradio.m3u8.PlaylistException;
|
||||||
|
import com.squareup.moshi.JsonReader;
|
||||||
|
import com.squareup.moshi.JsonWriter;
|
||||||
|
|
||||||
import ctbrec.AbstractModel;
|
import ctbrec.AbstractModel;
|
||||||
import ctbrec.Model;
|
import ctbrec.Model;
|
||||||
|
@ -147,4 +149,14 @@ public class JavaFxModel extends AbstractModel {
|
||||||
public Site getSite() {
|
public Site getSite() {
|
||||||
return delegate.getSite();
|
return delegate.getSite();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void readSiteSpecificData(JsonReader reader) throws IOException {
|
||||||
|
delegate.readSiteSpecificData(reader);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void writeSiteSpecificData(JsonWriter writer) throws IOException {
|
||||||
|
delegate.writeSiteSpecificData(writer);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue