Search in sources :

Example 1 with SubscriptionItem

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();
}
Also used : SubscriptionItem(org.schabi.newpipe.extractor.subscription.SubscriptionItem)

Example 2 with SubscriptionItem

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;
}
Also used : SubscriptionItem(org.schabi.newpipe.extractor.subscription.SubscriptionItem) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream)

Example 3 with SubscriptionItem

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;
}
Also used : SubscriptionItem(org.schabi.newpipe.extractor.subscription.SubscriptionItem) ByteArrayInputStream(java.io.ByteArrayInputStream)

Example 4 with SubscriptionItem

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());
}
Also used : SubscriptionItem(org.schabi.newpipe.extractor.subscription.SubscriptionItem) ArrayList(java.util.ArrayList) SubscriptionEntity(org.schabi.newpipe.database.subscription.SubscriptionEntity)

Example 5 with SubscriptionItem

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;
}
Also used : InvalidSourceException(org.schabi.newpipe.extractor.subscription.SubscriptionExtractor.InvalidSourceException) JsonArray(com.grack.nanojson.JsonArray) SubscriptionItem(org.schabi.newpipe.extractor.subscription.SubscriptionItem) ArrayList(java.util.ArrayList) JsonObject(com.grack.nanojson.JsonObject) JsonObject(com.grack.nanojson.JsonObject)

Aggregations

SubscriptionItem (org.schabi.newpipe.extractor.subscription.SubscriptionItem)7 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 JsonArray (com.grack.nanojson.JsonArray)1 JsonObject (com.grack.nanojson.JsonObject)1 InputStream (java.io.InputStream)1 SubscriptionEntity (org.schabi.newpipe.database.subscription.SubscriptionEntity)1 InvalidSourceException (org.schabi.newpipe.extractor.subscription.SubscriptionExtractor.InvalidSourceException)1