예제 #1
0
  /**
   * Signals Music that a track is being played. A track is considered playing if it is played more
   * than halfway.
   *
   * @param id Track ID
   * @param dateStr Date the track was started playing.
   * @param duration Duration into the track in seconds
   * @return Response
   */
  @POST
  @Path("listening")
  public Response listening(
      @FormParam("id") String id,
      @FormParam("date") String dateStr,
      @FormParam("duration") Integer duration) {
    if (!authenticate()) {
      throw new ForbiddenClientException();
    }
    Date date = ValidationUtil.validateDate(dateStr, "date", false);
    ValidationUtil.validateRequired(duration, "duration");

    // Load the track
    TrackDao trackDao = new TrackDao();
    Track track = trackDao.getActiveById(id);
    if (track == null) {
      return Response.status(Response.Status.NOT_FOUND).build();
    }
    duration = MathUtil.clip(duration, 0, track.getLength());

    // Update currently playing track
    final PlayerService playerService = AppContext.getInstance().getPlayerService();
    playerService.notifyPlaying(principal.getId(), track, date, duration);

    // Always return OK
    return Response.ok().entity(Json.createObjectBuilder().add("status", "ok").build()).build();
  }
예제 #2
0
  /**
   * Post a set of tracks played before (useful for offline mode).
   *
   * @param idList An array of track ID
   * @param dateStrList An array of dates at which the track was started playing
   * @return Response
   */
  @POST
  @Path("listened")
  public Response listened(
      @FormParam("id") List<String> idList, @FormParam("date") List<String> dateStrList) {
    if (!authenticate()) {
      throw new ForbiddenClientException();
    }
    if (idList == null || dateStrList == null || idList.size() != dateStrList.size()) {
      throw new ClientException("ValidationError", "Invalid id or dates");
    }

    // Scrobble tracks on Last.fm
    final TrackDao trackDao = new TrackDao();
    final UserTrackDao userTrackDao = new UserTrackDao();
    List<Track> trackList = new ArrayList<Track>();
    List<Date> dateList = new ArrayList<Date>();
    for (int i = 0; i < idList.size(); i++) {
      Track track = trackDao.getActiveById(idList.get(i));
      if (track != null) {
        Date date = ValidationUtil.validateDate(dateStrList.get(i), "date", false);
        trackList.add(track);
        dateList.add(date);

        // Increment local play count
        userTrackDao.incrementPlayCount(principal.getId(), track.getId());
      }
    }
    final User user = new UserDao().getActiveById(principal.getId());
    if (user != null && user.getLastFmSessionToken() != null) {
      final LastFmService lastFmService = AppContext.getInstance().getLastFmService();
      lastFmService.scrobbleTrackList(user, trackList, dateList);
    }

    // Always return OK
    return Response.ok().entity(Json.createObjectBuilder().add("status", "ok").build()).build();
  }