Search in sources :

Example 1 with MediaList

use of com.sonos.services._1.MediaList in project SonosOneDriveServer by bertique.

the class SonosService method parseMediaListResponse.

private static MediaList parseMediaListResponse(String userId, String json) {
    JsonElement element = JsonParser.parseString(json);
    JsonArray mainResultList = element.getAsJsonObject().getAsJsonArray("value");
    if (mainResultList != null) {
        MediaList ml = new MediaList();
        List<AbstractMedia> mcList = ml.getMediaCollectionOrMediaMetadata();
        for (int i = 0; i < mainResultList.size(); i++) {
            Item m = new Item(mainResultList.get(i).getAsJsonObject());
            if (m.getType() == null) {
                logger.debug("Ignoring item with null type (e.g. .url files): " + m.getName());
            } else if (m.getType() == Item.FileType.audio || (m.getType() == Item.FileType.file && m.getName().endsWith(".flac")) || (m.getType().equals(Item.FileType.file) && m.getMimeType().contains("audio"))) {
                mcList.add(buildMMD(m));
            } else if (m.getType() == Item.FileType.folder || m.getType() == Item.FileType.file) {
                mcList.add(buildMC(m));
            }
        }
        ml.setCount(mcList.size());
        if (element.getAsJsonObject().has("@odata.count")) {
            ml.setTotal(element.getAsJsonObject().get("@odata.count").getAsInt());
            if (ml.getTotal() < 100 && ml.getTotal() > mcList.size()) {
                ml.setTotal(ml.getTotal() - 1);
            }
        } else {
            ml.setTotal(mcList.size());
        }
        logger.debug("Got program list: " + mcList.size());
        return ml;
    } else {
        return new MediaList();
    }
}
Also used : JsonArray(com.google.gson.JsonArray) MediaList(com.sonos.services._1.MediaList) Item(me.michaeldick.sonosonedrive.model.Item) RateItem(com.sonos.services._1.RateItem) JsonElement(com.google.gson.JsonElement) AbstractMedia(com.sonos.services._1.AbstractMedia)

Example 2 with MediaList

use of com.sonos.services._1.MediaList in project SonosOneDriveServer by bertique.

the class SonosService method getMetadata.

@Override
public GetMetadataResponse getMetadata(GetMetadata parameters) throws CustomFault {
    logger.debug("getMetadata id:" + parameters.getId() + " count:" + parameters.getCount() + " index:" + parameters.getIndex());
    GraphAuth auth = getGraphAuth();
    // Mixpanel event
    // if(parameters.getId().equals(SonosService.PROGRAM+":"+SonosService.DEFAULT)
    // || parameters.getId().equals(ItemType.SEARCH.value())) {
    // 
    // JSONObject props = new JSONObject();
    // try {
    // props.put("Program", parameters.getId());
    // } catch (JSONException e) {
    // // TODO Auto-generated catch block
    // e.printStackTrace();
    // }
    // 
    // sentMetricsEvent(auth.getHouseholdId(), "getMetadata", props);
    // }
    GetMetadataResponse response = new GetMetadataResponse();
    MediaList ml = new MediaList();
    if (parameters.getId().equals("root")) {
        String path = DRIVE_ROOT + "/children";
        if (isAppFolder()) {
            path = DRIVE_APPFOLDER + "/children";
        }
        String skipToken = null;
        if (parameters.getIndex() > 0) {
            skipToken = getSkipToken(path, parameters.getIndex(), auth);
        }
        String json = graphApiGetRequest(path, parameters.getCount(), skipToken, auth);
        ml = parseMediaListResponse(auth.getHouseholdId(), json);
    } else if (parameters.getId().startsWith(SonosService.FOLDER)) {
        String path = String.format("/me/drive/items/%s/children", parameters.getId().replaceAll(SonosService.FOLDER + ":", ""));
        String skipToken = null;
        if (parameters.getIndex() > 0) {
            skipToken = getSkipToken(path, parameters.getIndex(), auth);
        }
        String json = graphApiGetRequest(path, parameters.getCount(), skipToken, auth);
        ml = parseMediaListResponse(auth.getHouseholdId(), json);
    } else if (parameters.getId().equals(ItemType.SEARCH.value())) {
        List<AbstractMedia> mcList = ml.getMediaCollectionOrMediaMetadata();
        MediaCollection mc1 = new MediaCollection();
        mc1.setTitle("Files");
        mc1.setId(SonosService.FILES);
        mc1.setItemType(ItemType.SEARCH);
        mc1.setCanPlay(false);
        mcList.add(mc1);
        ml.setCount(mcList.size());
        ml.setTotal(mcList.size());
    } else {
        return null;
    }
    ml.setIndex(parameters.getIndex());
    response.setGetMetadataResult(ml);
    logger.info(auth.getHouseholdId().hashCode() + ": Got Metadata for " + parameters.getId() + ", " + response.getGetMetadataResult().getCount() + " Index:" + ml.getIndex() + " Count:" + ml.getCount() + " Total: " + ml.getTotal());
    return response;
}
Also used : MediaList(com.sonos.services._1.MediaList) GraphAuth(me.michaeldick.sonosonedrive.model.GraphAuth) GetMetadataResponse(com.sonos.services._1.GetMetadataResponse) MediaCollection(com.sonos.services._1.MediaCollection) AbstractMedia(com.sonos.services._1.AbstractMedia)

Example 3 with MediaList

use of com.sonos.services._1.MediaList in project SonosOneDriveServer by bertique.

the class SonosService method search.

@Override
public SearchResponse search(Search parameters) throws CustomFault {
    logger.debug("search");
    GraphAuth auth = getGraphAuth();
    String path = String.format("/me/drive/root/search(q='%s')", parameters.getTerm());
    String skipToken = null;
    if (parameters.getIndex() > 0) {
        skipToken = getSkipToken(path, parameters.getIndex(), auth);
    }
    String json = graphApiGetRequest(path, parameters.getCount(), skipToken, auth);
    SearchResponse response = new SearchResponse();
    MediaList ml = new MediaList();
    ml = parseMediaListResponse(auth.getHouseholdId(), json);
    // Remove 1 since personal vault is not included in response
    ml.setTotal(ml.getTotal());
    ml.setIndex(parameters.getIndex());
    response.setSearchResult(ml);
    return response;
}
Also used : MediaList(com.sonos.services._1.MediaList) GraphAuth(me.michaeldick.sonosonedrive.model.GraphAuth) SearchResponse(com.sonos.services._1.SearchResponse)

Aggregations

MediaList (com.sonos.services._1.MediaList)3 AbstractMedia (com.sonos.services._1.AbstractMedia)2 GraphAuth (me.michaeldick.sonosonedrive.model.GraphAuth)2 JsonArray (com.google.gson.JsonArray)1 JsonElement (com.google.gson.JsonElement)1 GetMetadataResponse (com.sonos.services._1.GetMetadataResponse)1 MediaCollection (com.sonos.services._1.MediaCollection)1 RateItem (com.sonos.services._1.RateItem)1 SearchResponse (com.sonos.services._1.SearchResponse)1 Item (me.michaeldick.sonosonedrive.model.Item)1