All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.dstadler.audio.fm4.FM4Stream Maven / Gradle / Ivy

package org.dstadler.audio.fm4;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.dstadler.commons.http.HttpClientWrapper;
import org.dstadler.commons.logging.jdk.LoggerFactory;

import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

public class FM4Stream {
    private final static Logger log = LoggerFactory.make();

    public static final String FM4_STREAM_URL_BASE = "https://loopstreamfm4.apa.at/?channel=fm4&id=";
    public static final String OOE_STREAM_URL_BASE = "https://loopstreamoe2o.apa.at/?channel=oe2o&id=";

    private static final ObjectMapper objectMapper = new ObjectMapper();

    private final String programKey;
    private final String title;
    private final String subtitle;
    private final String href;
    private final String time;
    private final long start;
    private final long duration;

    private final String streamUrlBase;

    public FM4Stream(JsonNode node, String streamUrlBase) {
        programKey = node.get("programKey").asText();
        title = node.get("title").asText().trim();
        subtitle = node.get("subtitle").asText();
        href = node.get("href").asText();
        time = node.get("startISO").asText();
        start = node.get("start").asLong();
        duration = node.get("end").asLong() - start;

        this.streamUrlBase = streamUrlBase;
    }

    public String getProgramKey() {
        return programKey;
    }

    public String getTitle() {
        return title;
    }

    public String getSubtitle() {
        return subtitle;
    }

    public String getTime() {
        return time;
    }

    public String getTimeForREST() throws ParseException {
        return DateFormatUtils.ISO_8601_EXTENDED_DATE_FORMAT.format(DateFormatUtils.ISO_8601_EXTENDED_DATETIME_TIME_ZONE_FORMAT.parse(time));
    }

    /**
     * Returns the duration of this stream in seconds.
     *
     * @return Duration in millisseconds
     */
    public long getDuration() {
        return duration;
    }

    /**
     * Return the start timestamp.
     *
     * @return Start timestamp in milliseconds.
     */
    public long getStart() {
        return start;
    }

    public String getShortTime() {
        return getTime().substring(0, 16).
                replace("T", " ");
    }

    public String getShortSummary() {
        return getProgramKey() + " " + getTitle();
    }

    public String getSummary() throws IOException {
        return getProgramKey() +
                " - " + getShortTime() +
                " - " + getTitle() +
                " - " + getStreams();
    }

    public List getStreams() throws IOException {
        log.info("Fetching streams for " + programKey + ": " + href);
        String json = HttpClientWrapper.retrieveData(href);
        JsonNode jsonNode = objectMapper.readTree(json);

        List streams = new ArrayList<>();
        for (JsonNode stream : jsonNode.get("streams")) {
            streams.add(streamUrlBase + stream.get("loopStreamId").asText());
        }

        log.info("Found " + streams.size() + " streams: " + streams);

        return streams;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        FM4Stream fm4Stream = (FM4Stream) o;

        if (start != fm4Stream.start) return false;
        return programKey.equals(fm4Stream.programKey);
    }

    @Override
    public int hashCode() {
        int result = programKey.hashCode();
        result = 31 * result + (int) (start ^ (start >>> 32));
        return result;
    }

    @Override
    public String toString() {
        return "FM4Stream{" +
                "programKey='" + programKey + '\'' +
                ", title='" + title + '\'' +
                ", href='" + href + '\'' +
                ", time='" + time + '\'' +
                ", start=" + start +
                ", duration=" + duration +
                '}';
    }

    /*
         {
            "isOnDemand" : true,
            "programKey" : "4GP",
            "subtitle" : "

Die wöchentliche Radioshow von Gilles Peterson.
FM4-News um 17 Uhr und 18 Uhr (englisch)

", "id" : 15652, "niceTime" : 1575820800000, "endISO" : "2019-12-08T19:00:40+01:00", "broadcastDay" : 20191208, "scheduledStartOffset" : -3600000, "niceTimeISO" : "2019-12-08T17:00:00+01:00", "scheduledStart" : 1575820800000, "niceTimeOffset" : -3600000, "program" : "4GP", "scheduledEnd" : 1575828000000, "title" : "Worldwide Show", "endOffset" : -3600000, "scheduledStartISO" : "2019-12-08T17:00:00+01:00", "scheduledEndISO" : "2019-12-08T19:00:00+01:00", "href" : "https://audioapi.orf.at/fm4/api/json/4.0/broadcast/4GP/20191208", "station" : "fm4", "startOffset" : -3600000, "start" : 1575820794000, "scheduledEndOffset" : -3600000, "ressort" : null, "end" : 1575828040000, "state" : "C", "startISO" : "2019-12-08T16:59:54+01:00", "isAdFree" : false, "entity" : "Broadcast", "isGeoProtected" : false }, */ }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy