]> git.basschouten.com Git - openhab-addons.git/blob
71535c19269bf73f5d1cbdc63ceff61a892c603e
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.sonos.internal.handler;
14
15 import java.util.ArrayList;
16 import java.util.List;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.openhab.binding.sonos.internal.SonosMetaData;
21 import org.openhab.binding.sonos.internal.SonosXMLParser;
22
23 /**
24  * The {@link SonosMediaInformation} is responsible for extracting media information from XML metadata
25  *
26  * @author Laurent Garnier - Initial contribution
27  */
28 @NonNullByDefault
29 public class SonosMediaInformation {
30
31     private @Nullable String artist;
32     private @Nullable String album;
33     private @Nullable String title;
34     private @Nullable String combinedInfo;
35     private boolean needsUpdate;
36
37     public SonosMediaInformation() {
38         this(false);
39     }
40
41     public SonosMediaInformation(boolean needsUpdate) {
42         this(null, null, null, null, needsUpdate);
43     }
44
45     public SonosMediaInformation(@Nullable String artist, @Nullable String album, @Nullable String title,
46             @Nullable String combinedInfo, boolean needsUpdate) {
47         this.artist = artist;
48         this.album = album;
49         this.title = title;
50         this.combinedInfo = combinedInfo;
51         this.needsUpdate = needsUpdate;
52     }
53
54     public @Nullable String getArtist() {
55         return artist;
56     }
57
58     public @Nullable String getAlbum() {
59         return album;
60     }
61
62     public @Nullable String getTitle() {
63         return title;
64     }
65
66     public @Nullable String getCombinedInfo() {
67         return combinedInfo;
68     }
69
70     public boolean needsUpdate() {
71         return needsUpdate;
72     }
73
74     public static SonosMediaInformation parseTuneInMediaInfo(@Nullable String opmlData, @Nullable String radioTitle,
75             @Nullable SonosMetaData trackMetaData) {
76         String title = null;
77         String combinedInfo = null;
78         if (opmlData != null) {
79             List<String> fields = SonosXMLParser.getRadioTimeFromXML(opmlData);
80             if (!fields.isEmpty()) {
81                 title = fields.get(0);
82                 combinedInfo = String.join(" - ", fields);
83                 return new SonosMediaInformation(null, null, title, combinedInfo, true);
84             }
85         }
86         if (radioTitle != null && !radioTitle.isEmpty()) {
87             title = radioTitle;
88             combinedInfo = title;
89             if (trackMetaData != null && !trackMetaData.getStreamContent().isEmpty()) {
90                 combinedInfo += " - " + trackMetaData.getStreamContent();
91             }
92             return new SonosMediaInformation(null, null, title, combinedInfo, true);
93         }
94         return new SonosMediaInformation(false);
95     }
96
97     public static SonosMediaInformation parseRadioAppMediaInfo(@Nullable String radioTitle,
98             @Nullable SonosMetaData trackMetaData) {
99         if (radioTitle != null && !radioTitle.isEmpty()) {
100             String artist = null;
101             String album = null;
102             String title = radioTitle;
103             String combinedInfo = title;
104             if (trackMetaData != null) {
105                 String[] contents = trackMetaData.getStreamContent().split("\\|");
106                 String contentTitle = null;
107                 for (int i = 0; i < contents.length; i++) {
108                     if (contents[i].startsWith("TITLE ")) {
109                         contentTitle = contents[i].substring(6).trim();
110                     }
111                     if (contents[i].startsWith("ARTIST ")) {
112                         artist = contents[i].substring(7).trim();
113                     }
114                     if (contents[i].startsWith("ALBUM ")) {
115                         album = contents[i].substring(6).trim();
116                     }
117                 }
118                 if ((artist == null || artist.isEmpty()) && contentTitle != null && !contentTitle.isEmpty()
119                         && !contentTitle.startsWith("Advertisement_")) {
120                     // Try to extract artist and song title from contentTitle
121                     int idx = contentTitle.indexOf(" - ");
122                     if (idx > 0) {
123                         artist = contentTitle.substring(0, idx);
124                         title = contentTitle.substring(idx + 3);
125                     }
126                 } else if (artist != null && !artist.isEmpty() && album != null && !album.isEmpty()
127                         && contentTitle != null && !contentTitle.isEmpty()) {
128                     title = contentTitle;
129                 }
130                 if (artist != null && !artist.isEmpty()) {
131                     combinedInfo += " - " + artist;
132                 }
133                 if (album != null && !album.isEmpty()) {
134                     combinedInfo += " - " + album;
135                 }
136                 if (!radioTitle.equals(title)) {
137                     combinedInfo += " - " + title;
138                 } else if (contentTitle != null && !contentTitle.isEmpty()
139                         && !contentTitle.startsWith("Advertisement_")) {
140                     combinedInfo += " - " + contentTitle;
141                 }
142             }
143             return new SonosMediaInformation(artist, album, title, combinedInfo, true);
144         }
145         return new SonosMediaInformation(false);
146     }
147
148     public static SonosMediaInformation parseTrack(@Nullable SonosMetaData trackMetaData) {
149         if (trackMetaData != null) {
150             List<String> infos = new ArrayList<>();
151             String artist = !trackMetaData.getAlbumArtist().isEmpty() ? trackMetaData.getAlbumArtist()
152                     : trackMetaData.getCreator();
153             if (!artist.isEmpty()) {
154                 infos.add(artist);
155             }
156             String album = trackMetaData.getAlbum();
157             if (!album.isEmpty()) {
158                 infos.add(album);
159             }
160             String title = trackMetaData.getTitle();
161             if (!title.isEmpty()) {
162                 infos.add(title);
163             }
164             return new SonosMediaInformation(artist, album, title, String.join(" - ", infos), true);
165         }
166         return new SonosMediaInformation(false);
167     }
168
169     public static SonosMediaInformation parseTrackTitle(@Nullable SonosMetaData trackMetaData) {
170         if (trackMetaData != null) {
171             String title = trackMetaData.getTitle();
172             return new SonosMediaInformation(null, null, title, title, true);
173         }
174         return new SonosMediaInformation(false);
175     }
176 }