use of org.schabi.newpipe.extractor.subscription.SubscriptionItem in project NewPipe by TeamNewPipe.
the class ImportExportJsonHelper method writeTo.
/**
* @see #writeTo(List, OutputStream, ImportExportEventListener)
*/
public static void writeTo(List<SubscriptionItem> items, JsonSink writer, @Nullable ImportExportEventListener eventListener) {
if (eventListener != null)
eventListener.onSizeReceived(items.size());
writer.object();
writer.value(JSON_APP_VERSION_KEY, BuildConfig.VERSION_NAME);
writer.value(JSON_APP_VERSION_INT_KEY, BuildConfig.VERSION_CODE);
writer.array(JSON_SUBSCRIPTIONS_ARRAY_KEY);
for (SubscriptionItem item : items) {
writer.object();
writer.value(JSON_SERVICE_ID_KEY, item.getServiceId());
writer.value(JSON_URL_KEY, item.getUrl());
writer.value(JSON_NAME_KEY, item.getName());
writer.end();
if (eventListener != null)
eventListener.onItemCompleted(item.getName());
}
writer.end();
writer.end();
}
use of org.schabi.newpipe.extractor.subscription.SubscriptionItem in project NewPipe by TeamNewPipe.
the class ImportExportJsonHelperTest method readFromFile.
private List<SubscriptionItem> readFromFile() throws Exception {
final InputStream inputStream = getClass().getClassLoader().getResourceAsStream("import_export_test.json");
final List<SubscriptionItem> itemsFromFile = ImportExportJsonHelper.readFrom(inputStream, null);
if (itemsFromFile == null || itemsFromFile.isEmpty()) {
fail("ImportExportJsonHelper.readFrom(input) returned a null or empty list");
}
return itemsFromFile;
}
use of org.schabi.newpipe.extractor.subscription.SubscriptionItem in project NewPipe by TeamNewPipe.
the class ImportExportJsonHelperTest method readFromWriteTo.
private List<SubscriptionItem> readFromWriteTo(String jsonOut) throws Exception {
final ByteArrayInputStream inputStream = new ByteArrayInputStream(jsonOut.getBytes("UTF-8"));
final List<SubscriptionItem> secondReadItems = ImportExportJsonHelper.readFrom(inputStream, null);
if (secondReadItems == null || secondReadItems.isEmpty()) {
fail("second call to readFrom returned an empty list");
}
return secondReadItems;
}
use of org.schabi.newpipe.extractor.subscription.SubscriptionItem in project NewPipe by TeamNewPipe.
the class SubscriptionsExportService method startExport.
private void startExport() {
showToast(R.string.export_ongoing);
subscriptionService.subscriptionTable().getAll().take(1).map(subscriptionEntities -> {
final List<SubscriptionItem> result = new ArrayList<>(subscriptionEntities.size());
for (SubscriptionEntity entity : subscriptionEntities) {
result.add(new SubscriptionItem(entity.getServiceId(), entity.getUrl(), entity.getName()));
}
return result;
}).map(exportToFile()).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(getSubscriber());
}
use of org.schabi.newpipe.extractor.subscription.SubscriptionItem in project NewPipe by TeamNewPipe.
the class ImportExportJsonHelper method readFrom.
/**
* Read a JSON source through the input stream and return the parsed subscription items.
*
* @param in the input stream (e.g. a file)
* @param eventListener listener for the events generated
*/
public static List<SubscriptionItem> readFrom(InputStream in, @Nullable ImportExportEventListener eventListener) throws InvalidSourceException {
if (in == null)
throw new InvalidSourceException("input is null");
final List<SubscriptionItem> channels = new ArrayList<>();
try {
JsonObject parentObject = JsonParser.object().from(in);
JsonArray channelsArray = parentObject.getArray(JSON_SUBSCRIPTIONS_ARRAY_KEY);
if (eventListener != null)
eventListener.onSizeReceived(channelsArray.size());
if (channelsArray == null) {
throw new InvalidSourceException("Channels array is null");
}
for (Object o : channelsArray) {
if (o instanceof JsonObject) {
JsonObject itemObject = (JsonObject) o;
int serviceId = itemObject.getInt(JSON_SERVICE_ID_KEY, 0);
String url = itemObject.getString(JSON_URL_KEY);
String name = itemObject.getString(JSON_NAME_KEY);
if (url != null && name != null && !url.isEmpty() && !name.isEmpty()) {
channels.add(new SubscriptionItem(serviceId, url, name));
if (eventListener != null)
eventListener.onItemCompleted(name);
}
}
}
} catch (Throwable e) {
throw new InvalidSourceException("Couldn't parse json", e);
}
return channels;
}
Aggregations