Search in sources :

Example 1 with GraphAuth

use of me.michaeldick.sonosonedrive.model.GraphAuth 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 2 with GraphAuth

use of me.michaeldick.sonosonedrive.model.GraphAuth 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)

Example 3 with GraphAuth

use of me.michaeldick.sonosonedrive.model.GraphAuth in project SonosOneDriveServer by bertique.

the class SonosService method getGraphAuth.

private GraphAuth getGraphAuth() {
    Credentials creds = getCredentialsFromHeaders();
    if (creds == null)
        throwSoapFault(SESSION_INVALID);
    logger.debug("Got userId from header:" + creds.getLoginToken().getHouseholdId());
    String access_token = null;
    if (creds.getLoginToken().getToken().startsWith("{") && creds.getLoginToken().getToken().contains("###")) {
        String[] tokenParts = creds.getLoginToken().getToken().split("###");
        if (tokenParts.length == 3) {
            byte[] encodedFirstTokenPart = Base64.getEncoder().encode(tokenParts[0].getBytes());
            byte[] encodedSecondTokenPart = Base64.getEncoder().encode(tokenParts[1].getBytes());
            access_token = String.format("%s.%s.%s", new String(encodedFirstTokenPart).replace("=", ""), new String(encodedSecondTokenPart).replace("=", ""), tokenParts[2]);
        } else {
            throwSoapFault(NOT_LINKED_FAILURE);
        }
    } else {
        access_token = creds.getLoginToken().getToken();
    }
    return new GraphAuth(creds.getLoginToken().getHouseholdId(), access_token, creds.getLoginToken().getKey());
}
Also used : GraphAuth(me.michaeldick.sonosonedrive.model.GraphAuth) Credentials(com.sonos.services._1.Credentials)

Example 4 with GraphAuth

use of me.michaeldick.sonosonedrive.model.GraphAuth in project SonosOneDriveServer by bertique.

the class SonosService method getMediaURI.

@Override
public void getMediaURI(String id, MediaUriAction action, Integer secondsSinceExplicit, Holder<String> deviceSessionToken, Holder<String> getMediaURIResult, Holder<EncryptionContext> deviceSessionKey, Holder<EncryptionContext> contentKey, Holder<HttpHeaders> httpHeaders, Holder<Integer> uriTimeout, Holder<PositionInformation> positionInformation, Holder<String> privateDataFieldName) throws CustomFault {
    logger.debug("getMediaURI id:" + id);
    GraphAuth auth = getGraphAuth();
    String json = graphApiGetRequest("me/drive/items/" + id.replaceAll(SonosService.AUDIO + ":", "") + "", 1, null, auth);
    JsonElement element = JsonParser.parseString(json);
    Item m = new Item(element.getAsJsonObject());
    getMediaURIResult.value = m.getFileUri();
}
Also used : Item(me.michaeldick.sonosonedrive.model.Item) RateItem(com.sonos.services._1.RateItem) GraphAuth(me.michaeldick.sonosonedrive.model.GraphAuth) JsonElement(com.google.gson.JsonElement)

Example 5 with GraphAuth

use of me.michaeldick.sonosonedrive.model.GraphAuth in project SonosOneDriveServer by bertique.

the class SonosService method getLastUpdate.

@Override
public LastUpdate getLastUpdate() throws CustomFault {
    logger.debug("getLastUpdate");
    GraphAuth auth = getGraphAuth();
    sentMetricsEvent(auth.getHouseholdId(), "getLastUpdate", null);
    String path = DRIVE_ROOT + "/delta";
    String json = graphApiGetRequest(path, 1, null, auth);
    LastUpdate response = new LastUpdate();
    JsonElement element = JsonParser.parseString(json);
    JsonArray mainResultList = element.getAsJsonObject().getAsJsonArray("value");
    String lastUpdate = null;
    if (mainResultList != null && mainResultList.size() > 0) {
        lastUpdate = mainResultList.get(0).getAsJsonObject().get("lastModifiedDateTime").getAsString();
    }
    logger.debug("Last modifed: " + lastUpdate);
    response.setCatalog(lastUpdate);
    return response;
}
Also used : JsonArray(com.google.gson.JsonArray) GraphAuth(me.michaeldick.sonosonedrive.model.GraphAuth) LastUpdate(com.sonos.services._1.LastUpdate) JsonElement(com.google.gson.JsonElement)

Aggregations

GraphAuth (me.michaeldick.sonosonedrive.model.GraphAuth)8 JsonElement (com.google.gson.JsonElement)5 GetMediaMetadataResponse (com.sonos.services._1.GetMediaMetadataResponse)3 GetMetadataResponse (com.sonos.services._1.GetMetadataResponse)3 SearchResponse (com.sonos.services._1.SearchResponse)3 JsonObject (com.google.gson.JsonObject)2 GetExtendedMetadataResponse (com.sonos.services._1.GetExtendedMetadataResponse)2 GetExtendedMetadataTextResponse (com.sonos.services._1.GetExtendedMetadataTextResponse)2 GetSessionIdResponse (com.sonos.services._1.GetSessionIdResponse)2 MediaList (com.sonos.services._1.MediaList)2 RateItem (com.sonos.services._1.RateItem)2 RateItemResponse (com.sonos.services._1.RateItemResponse)2 BadRequestException (javax.ws.rs.BadRequestException)2 NotAuthorizedException (javax.ws.rs.NotAuthorizedException)2 Client (javax.ws.rs.client.Client)2 Response (javax.ws.rs.core.Response)2 Item (me.michaeldick.sonosonedrive.model.Item)2 JsonArray (com.google.gson.JsonArray)1 AbstractMedia (com.sonos.services._1.AbstractMedia)1 Credentials (com.sonos.services._1.Credentials)1