2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.playstation.internal;
16 import java.io.FileInputStream;
17 import java.io.FileNotFoundException;
18 import java.io.FileOutputStream;
19 import java.io.IOException;
20 import java.util.Locale;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.core.OpenHAB;
25 import org.openhab.core.io.net.http.HttpUtil;
26 import org.openhab.core.library.types.RawType;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
31 * The {@link PS4ArtworkHandler} is responsible for fetching and caching
32 * application artwork.
34 * @author Fredrik Ahlström - Initial contribution
37 public class PS4ArtworkHandler {
39 private static final Logger LOGGER = LoggerFactory.getLogger(PS4ArtworkHandler.class);
40 private static final File ARTWORK_CACHE_FOLDER;
43 private static final String SERVICE_PID = "org.openhab.binding.playstation";
45 /** Cache folder under $userdata */
46 private static final String CACHE_FOLDER_NAME = "cache";
48 /** Some countries use EN as language in the PS Store, this is to minimize requests */
49 private static boolean useLanguageEn = false;
51 private PS4ArtworkHandler() {
52 // No need to instantiate
56 // create cache folder
57 File userData = new File(OpenHAB.getUserDataFolder());
58 File homeFolder = new File(userData, CACHE_FOLDER_NAME);
60 if (!homeFolder.exists()) {
63 LOGGER.debug("Using home folder: {}", homeFolder.getAbsolutePath());
65 // create binding folder
66 File cacheFolder = new File(homeFolder, SERVICE_PID);
67 if (!cacheFolder.exists()) {
70 LOGGER.debug("Using cache folder {}", cacheFolder.getAbsolutePath());
71 ARTWORK_CACHE_FOLDER = cacheFolder;
75 * Builds an artwork request string for the specified TitleId, also takes into account if the language should be
76 * from the specified locale or just "en".
78 * @param locale The country and language to use for the store look up.
79 * @param titleId The Title ID of the Application/game.
80 * @param size The size of the artwork.
81 * @return A https request as a String.
83 private static String buildArtworkRequest(Locale locale, String titleId, Integer size) {
84 String language = useLanguageEn ? "en" : locale.getLanguage();
85 return "https://store.playstation.com/store/api/chihiro/00_09_000/titlecontainer/" + locale.getCountry() + "/"
86 + language + "/999/" + titleId + "_00/image?w=" + size.toString() + "&h=" + size.toString();
90 * Fetch artwork for PS4 application. First looks for the file on disc, if the file is not on the disc it checks
93 * @param titleid Title ID of application.
94 * @param size Size (width & height) of art work in pixels , max 1024.
95 * @param locale Locale used on PlayStation store to find art work.
96 * @return A JPEG image as a RawType if an art work file is found otherwise null.
98 public static @Nullable RawType fetchArtworkForTitleid(String titleId, Integer size, Locale locale) {
99 return fetchArtworkForTitleid(titleId, size, locale, false);
103 * Fetch artwork for PS4 application. First looks for the file on disc, if the file is not on the disc it checks
106 * @param titleid Title ID of application.
107 * @param size Size (width & height) of art work in pixels , max 1024.
108 * @param locale Locale used on PlayStation store to find art work.
109 * @param forceRefetch When true, tries to re-fetch art work from PlayStation store, sometimes the art work is
110 * updated along with the game.
111 * @return A JPEG image as a RawType if an art work file is found otherwise null.
113 public static @Nullable RawType fetchArtworkForTitleid(String titleId, Integer size, Locale locale,
114 boolean forceRefetch) {
115 // Try to find the image in the cache first, then try to download it from PlayStation Store.
116 RawType artwork = null;
117 if (titleId.isEmpty()) {
120 String artworkFilename = titleId + "_" + size.toString() + ".jpg";
121 File artworkFileInCache = new File(ARTWORK_CACHE_FOLDER, artworkFilename);
122 if (artworkFileInCache.exists() && !forceRefetch) {
123 LOGGER.trace("Artwork file {} was found in cache.", artworkFileInCache.getName());
124 int length = (int) artworkFileInCache.length();
125 byte[] fileBuffer = new byte[length];
126 try (FileInputStream fis = new FileInputStream(artworkFileInCache)) {
127 fis.read(fileBuffer, 0, length);
128 artwork = new RawType(fileBuffer, "image/jpeg");
129 } catch (FileNotFoundException ex) {
130 LOGGER.debug("Could not find {} in cache. {}", artworkFileInCache, ex.getMessage());
131 } catch (IOException ex) {
132 LOGGER.debug("Could not read {} from cache. {}", artworkFileInCache, ex.getMessage());
134 if (artwork != null) {
138 String request = buildArtworkRequest(locale, titleId, size);
139 artwork = HttpUtil.downloadImage(request);
140 if (artwork == null) {
141 // If artwork is not found for specified language/"en", try the other way around.
142 useLanguageEn = !useLanguageEn;
143 request = buildArtworkRequest(locale, titleId, size);
144 artwork = HttpUtil.downloadImage(request);
146 if (artwork != null) {
147 try (FileOutputStream fos = new FileOutputStream(artworkFileInCache)) {
148 LOGGER.debug("Caching artwork file {}", artworkFileInCache.getName());
149 fos.write(artwork.getBytes(), 0, artwork.getBytes().length);
150 } catch (FileNotFoundException ex) {
151 LOGGER.debug("Could not create {} in cache. {}", artworkFileInCache, ex.getMessage());
152 } catch (IOException ex) {
153 LOGGER.debug("Could not write {} to cache. {}", artworkFileInCache, ex.getMessage());
156 LOGGER.debug("Could not download artwork file from {}", request);