Search in sources :

Example 1 with PlayStatus

use of com.tesshu.jpsonic.domain.PlayStatus in project jpsonic by tesshucom.

the class SubsonicRESTController method getNowPlaying.

@RequestMapping({ "/getNowPlaying", "/getNowPlaying.view" })
public void getNowPlaying(HttpServletRequest req, HttpServletResponse response) {
    HttpServletRequest request = wrapRequest(req);
    NowPlaying result = new NowPlaying();
    for (PlayStatus status : statusService.getPlayStatuses()) {
        Player player = status.getPlayer();
        MediaFile mediaFile = status.getMediaFile();
        String username = player.getUsername();
        if (username == null) {
            continue;
        }
        UserSettings userSettings = securityService.getUserSettings(username);
        if (!userSettings.isNowPlayingAllowed()) {
            continue;
        }
        long minutesAgo = status.getMinutesAgo();
        if (minutesAgo < LIMIT_OF_HISTORY_TO_BE_PRESENTED) {
            NowPlayingEntry entry = new NowPlayingEntry();
            entry.setUsername(username);
            entry.setPlayerId(player.getId());
            entry.setPlayerName(player.getName());
            entry.setMinutesAgo((int) minutesAgo);
            result.getEntry().add(createJaxbChild(entry, player, mediaFile, username));
        }
    }
    Response res = createResponse();
    res.setNowPlaying(result);
    jaxbWriter.writeResponse(request, response, res);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) MediaFile(com.tesshu.jpsonic.domain.MediaFile) Response(org.subsonic.restapi.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse) Player(com.tesshu.jpsonic.domain.Player) UserSettings(com.tesshu.jpsonic.domain.UserSettings) NowPlaying(org.subsonic.restapi.NowPlaying) PlayStatus(com.tesshu.jpsonic.domain.PlayStatus) NowPlayingEntry(org.subsonic.restapi.NowPlayingEntry) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with PlayStatus

use of com.tesshu.jpsonic.domain.PlayStatus in project jpsonic by tesshucom.

the class StatusService method getPlayStatuses.

// (Date, PlayStatus) Not reusable
@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
public List<PlayStatus> getPlayStatuses() {
    synchronized (STREAM_LOCK) {
        synchronized (REMOTE_LOCK) {
            Map<Integer, PlayStatus> result = new LinkedHashMap<>();
            for (PlayStatus remotePlay : remotePlays) {
                if (!remotePlay.isExpired()) {
                    result.put(remotePlay.getPlayer().getId(), remotePlay);
                }
            }
            List<TransferStatus> statuses = new ArrayList<>();
            statuses.addAll(inactiveStreamStatuses.values());
            statuses.addAll(streamStatuses);
            for (TransferStatus streamStatus : statuses) {
                Player player = streamStatus.getPlayer();
                File file = streamStatus.getFile();
                if (file == null) {
                    continue;
                }
                MediaFile mediaFile = mediaFileService.getMediaFile(file);
                if (player == null || mediaFile == null) {
                    continue;
                }
                Date time = new Date(System.currentTimeMillis() - streamStatus.getMillisSinceLastUpdate());
                result.put(player.getId(), new PlayStatus(mediaFile, player, time));
            }
            return unmodifiableList(new ArrayList<>(result.values()));
        }
    }
}
Also used : MediaFile(com.tesshu.jpsonic.domain.MediaFile) Player(com.tesshu.jpsonic.domain.Player) TransferStatus(com.tesshu.jpsonic.domain.TransferStatus) ArrayList(java.util.ArrayList) File(java.io.File) MediaFile(com.tesshu.jpsonic.domain.MediaFile) PlayStatus(com.tesshu.jpsonic.domain.PlayStatus) Date(java.util.Date) LinkedHashMap(java.util.LinkedHashMap)

Example 3 with PlayStatus

use of com.tesshu.jpsonic.domain.PlayStatus in project jpsonic by tesshucom.

the class NowPlayingServiceTest method setup.

@BeforeEach
public void setup() {
    StatusService statusService = mock(StatusService.class);
    MediaFile file = new MediaFile();
    file.setId(0);
    Player player = new Player();
    player.setUsername(ServiceMockUtils.ADMIN_NAME);
    PlayStatus playStatus = new PlayStatus(file, player, new Date());
    Mockito.when(statusService.getPlayStatuses()).thenReturn(Arrays.asList(playStatus));
    nowPlayingService = new NowPlayingService(mock(SecurityService.class), mock(PlayerService.class), statusService, mock(MediaScannerService.class), mock(AvatarService.class), AjaxMockUtils.mock(AjaxHelper.class));
}
Also used : StatusService(com.tesshu.jpsonic.service.StatusService) MediaFile(com.tesshu.jpsonic.domain.MediaFile) Player(com.tesshu.jpsonic.domain.Player) PlayStatus(com.tesshu.jpsonic.domain.PlayStatus) Date(java.util.Date) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 4 with PlayStatus

use of com.tesshu.jpsonic.domain.PlayStatus in project jpsonic by tesshucom.

the class SubsonicRESTController method scrobble.

@RequestMapping({ "/scrobble", "/scrobble.view" })
public void scrobble(HttpServletRequest req, HttpServletResponse response) throws ServletRequestBindingException {
    HttpServletRequest request = wrapRequest(req);
    int[] ids = ServletRequestUtils.getRequiredIntParameters(request, Attributes.Request.ID.value());
    long[] times = ServletRequestUtils.getLongParameters(request, "time");
    if (times.length > 0 && (int) times.length != (int) ids.length) {
        writeError(request, response, ErrorCode.GENERIC, "Wrong number of timestamps: " + times.length);
        return;
    }
    Player player = playerService.getPlayer(request, response);
    boolean submission = ServletRequestUtils.getBooleanParameter(request, Attributes.Request.SUBMISSION.value(), true);
    for (int i = 0; i < ids.length; i++) {
        int id = ids[i];
        MediaFile file = mediaFileService.getMediaFile(id);
        if (file == null) {
            if (LOG.isWarnEnabled()) {
                LOG.warn("File to scrobble not found: " + id);
            }
            continue;
        }
        Date time = times.length == 0 ? new Date() : new Date(times[i]);
        statusService.addRemotePlay(new PlayStatus(file, player, time));
        mediaFileService.incrementPlayCount(file);
        if (securityService.getUserSettings(player.getUsername()).isLastFmEnabled()) {
            audioScrobblerService.register(file, player.getUsername(), submission, time);
        }
    }
    writeEmptyResponse(request, response);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) MediaFile(com.tesshu.jpsonic.domain.MediaFile) Player(com.tesshu.jpsonic.domain.Player) Date(java.util.Date) PlayStatus(com.tesshu.jpsonic.domain.PlayStatus) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 5 with PlayStatus

use of com.tesshu.jpsonic.domain.PlayStatus in project jpsonic by tesshucom.

the class NowPlayingService method convert.

// (NowPlayingInfo) Not reusable
@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
private List<NowPlayingInfo> convert(List<PlayStatus> playStatuses) {
    HttpServletRequest request = ajaxHelper.getHttpServletRequest();
    String url = NetworkUtils.getBaseUrl(request);
    List<NowPlayingInfo> result = new ArrayList<>();
    final StringBuilder builder = new StringBuilder();
    for (PlayStatus status : playStatuses) {
        Player player = status.getPlayer();
        MediaFile mediaFile = status.getMediaFile();
        String username = player.getUsername();
        if (username == null) {
            continue;
        }
        UserSettings userSettings = securityService.getUserSettings(username);
        if (!userSettings.isNowPlayingAllowed()) {
            continue;
        }
        String artist = mediaFile.getArtist();
        String title = mediaFile.getTitle();
        String streamUrl = url + "stream?player=" + player.getId() + "&id=" + mediaFile.getId();
        String albumUrl = url + ViewName.MAIN.value() + "?id=" + mediaFile.getId();
        String lyricsUrl = null;
        if (!mediaFile.isVideo()) {
            lyricsUrl = url + ViewName.LYRICS.value() + "?artistUtf8Hex=" + StringUtil.utf8HexEncode(artist) + "&songUtf8Hex=" + StringUtil.utf8HexEncode(title);
        }
        String coverArtUrl = url + ViewName.COVER_ART.value() + "?size=60&id=" + mediaFile.getId();
        String avatarUrl = avatarService.createAvatarUrl(url, userSettings);
        String tooltip = StringEscapeUtils.escapeHtml(artist) + " &ndash; " + StringEscapeUtils.escapeHtml(title);
        artist = StringEscapeUtils.escapeHtml(StringUtils.abbreviate(artist, 25));
        title = StringEscapeUtils.escapeHtml(StringUtils.abbreviate(title, 25));
        if (StringUtils.isNotBlank(player.getName())) {
            builder.setLength(0);
            username = builder.append(username).append('@').append(player.getName()).toString();
        }
        username = StringEscapeUtils.escapeHtml(StringUtils.abbreviate(username, 25));
        long minutesAgo = status.getMinutesAgo();
        if (minutesAgo < LIMIT_OF_HISTORY_TO_BE_PRESENTED) {
            result.add(new NowPlayingInfo(player.getId(), username, artist, title, tooltip, streamUrl, albumUrl, lyricsUrl, coverArtUrl, avatarUrl, (int) minutesAgo));
        }
    }
    return result;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) MediaFile(com.tesshu.jpsonic.domain.MediaFile) Player(com.tesshu.jpsonic.domain.Player) UserSettings(com.tesshu.jpsonic.domain.UserSettings) ArrayList(java.util.ArrayList) PlayStatus(com.tesshu.jpsonic.domain.PlayStatus)

Aggregations

MediaFile (com.tesshu.jpsonic.domain.MediaFile)5 PlayStatus (com.tesshu.jpsonic.domain.PlayStatus)5 Player (com.tesshu.jpsonic.domain.Player)5 Date (java.util.Date)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)3 UserSettings (com.tesshu.jpsonic.domain.UserSettings)2 ArrayList (java.util.ArrayList)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 TransferStatus (com.tesshu.jpsonic.domain.TransferStatus)1 StatusService (com.tesshu.jpsonic.service.StatusService)1 File (java.io.File)1 LinkedHashMap (java.util.LinkedHashMap)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1 NowPlaying (org.subsonic.restapi.NowPlaying)1 NowPlayingEntry (org.subsonic.restapi.NowPlayingEntry)1 Response (org.subsonic.restapi.Response)1