]> git.basschouten.com Git - openhab-addons.git/blob
024aa6a0c13911089bdc83d26c11047afe82881a
[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.playstation.internal;
14
15 import java.io.File;
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;
21
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;
29
30 /**
31  * The {@link PS4ArtworkHandler} is responsible for fetching and caching
32  * application artwork.
33  *
34  * @author Fredrik Ahlström - Initial contribution
35  */
36 @NonNullByDefault
37 public class PS4ArtworkHandler {
38
39     private static final Logger LOGGER = LoggerFactory.getLogger(PS4ArtworkHandler.class);
40     private static final File ARTWORK_CACHE_FOLDER;
41
42     /** Service pid */
43     private static final String SERVICE_PID = "org.openhab.binding.playstation";
44
45     /** Cache folder under $userdata */
46     private static final String CACHE_FOLDER_NAME = "cache";
47
48     /** Some countries use EN as language in the PS Store, this is to minimize requests */
49     private static boolean useLanguageEn = false;
50
51     private PS4ArtworkHandler() {
52         // No need to instantiate
53     }
54
55     static {
56         // create cache folder
57         File userData = new File(OpenHAB.getUserDataFolder());
58         File homeFolder = new File(userData, CACHE_FOLDER_NAME);
59
60         if (!homeFolder.exists()) {
61             homeFolder.mkdirs();
62         }
63         LOGGER.debug("Using home folder: {}", homeFolder.getAbsolutePath());
64
65         // create binding folder
66         File cacheFolder = new File(homeFolder, SERVICE_PID);
67         if (!cacheFolder.exists()) {
68             cacheFolder.mkdirs();
69         }
70         LOGGER.debug("Using cache folder {}", cacheFolder.getAbsolutePath());
71         ARTWORK_CACHE_FOLDER = cacheFolder;
72     }
73
74     /**
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".
77      *
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.
82      */
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();
87     }
88
89     /**
90      * Fetch artwork for PS4 application. First looks for the file on disc, if the file is not on the disc it checks
91      * PlayStation store
92      *
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.
97      */
98     public static @Nullable RawType fetchArtworkForTitleid(String titleId, Integer size, Locale locale) {
99         return fetchArtworkForTitleid(titleId, size, locale, false);
100     }
101
102     /**
103      * Fetch artwork for PS4 application. First looks for the file on disc, if the file is not on the disc it checks
104      * PlayStation store
105      *
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.
112      */
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()) {
118             return artwork;
119         }
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());
133             }
134             if (artwork != null) {
135                 return artwork;
136             }
137         }
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);
145         }
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());
154             }
155         } else {
156             LOGGER.debug("Could not download artwork file from {}", request);
157         }
158         return artwork;
159     }
160 }