use of com.chimbori.hermitcrab.schema.manifest.IconFile in project lite-apps by chimbori.
the class PaletteExtractor method extractPaletteIfMissing.
private static void extractPaletteIfMissing() throws IOException {
Gson gson = GsonInstance.getPrettyPrinter();
File[] liteAppDirs = FilePaths.LITE_APPS_SRC_DIR.listFiles();
for (File liteAppDirectory : liteAppDirs) {
if (!liteAppDirectory.isDirectory()) {
// Probably a temporary file, like .DS_Store.
continue;
}
String appName = liteAppDirectory.getName();
File manifestJsonFile = new File(liteAppDirectory, FilePaths.MANIFEST_JSON_FILE_NAME);
if (!manifestJsonFile.exists()) {
throw new MissingManifestException(appName);
}
// Create an entry for this Lite App to be put in the directory index file.
Manifest manifest = gson.fromJson(new FileReader(manifestJsonFile), Manifest.class);
if (manifest.themeColor.equals("#") || manifest.secondaryColor.equals("#")) {
System.out.println("manifest: " + manifest.name);
File iconsDirectory = new File(liteAppDirectory, FilePaths.ICONS_DIR_NAME);
iconsDirectory.mkdirs();
File iconFile = new File(iconsDirectory, IconFile.FAVICON_FILE.fileName);
// Extract the color from the icon (either newly downloaded, or from existing icon).
if (iconFile.exists()) {
ColorExtractor.Color themeColor = ColorExtractor.getDominantColor(ImageIO.read(iconFile));
if (themeColor != null) {
// Overwrite the dummy values already inserted, if we are able to extract real values.
manifest.themeColor = themeColor.toString();
manifest.secondaryColor = themeColor.darken(0.9f).toString();
FileUtils.writeFile(manifestJsonFile, gson.toJson(manifest));
}
}
}
}
}
use of com.chimbori.hermitcrab.schema.manifest.IconFile in project lite-apps by chimbori.
the class Scaffolder method createScaffolding.
/**
* The Library Data JSON file (containing metadata about all Lite Apps) is used as the basis for
* generating scaffolding for the Lite App manifests.
*/
private static void createScaffolding(String startUrl, String appName) throws IOException, Scraper.ManifestUnavailableException {
File liteAppDirectoryRoot = new File(FilePaths.LITE_APPS_SRC_DIR, appName);
Manifest manifest;
File manifestJsonFile = new File(liteAppDirectoryRoot, FilePaths.MANIFEST_JSON_FILE_NAME);
// If the manifest.json exists, read it before modifying, else create a new JSON object.
if (manifestJsonFile.exists()) {
manifest = GsonInstance.getPrettyPrinter().fromJson(new FileReader(manifestJsonFile), Manifest.class);
} else {
Log.i("Creating new Lite App “%s”", appName);
// Create the root directory if it doesn’t exist yet.
liteAppDirectoryRoot.mkdirs();
// Scrape the Web looking for RSS & Atom feeds, theme colors, and site metadata.
Log.i("Fetching %s…", startUrl);
Scraper scraper = new Scraper(startUrl).fetch();
manifest = scraper.extractManifest();
// Constant fields, same for all apps.
manifest.manifestVersion = CURRENT_MANIFEST_VERSION;
manifest.lang = LANG_EN;
// Fields that can be populated from the data provided on the command-line.
manifest.name = appName;
manifest.startUrl = startUrl;
manifest.manifestUrl = String.format(MANIFEST_URL_TEMPLATE, URLEncoder.encode(appName, "UTF-8").replace("+", "%20"));
// Empty fields that must be manually populated.
manifest.priority = 10;
manifest.tags = Collections.singletonList("TODO");
// Put the icon JSON entry even if we don’t manage to fetch an icon successfully.
// This way, we can avoid additional typing, and the validator will check for the presence
// of the file anyway (and fail as expected).
manifest.icon = IconFile.FAVICON_FILE;
File iconsDirectory = new File(liteAppDirectoryRoot, FilePaths.ICONS_DIR_NAME);
iconsDirectory.mkdirs();
File iconFile = new File(iconsDirectory, IconFile.FAVICON_FILE.fileName);
String remoteIconUrl = scraper.extractIconUrl();
if (!iconFile.exists() && remoteIconUrl != null && !remoteIconUrl.isEmpty()) {
Log.i("Fetching icon from %s…", remoteIconUrl);
URL iconUrl = null;
try {
iconUrl = new URL(remoteIconUrl);
} catch (MalformedURLException e) {
e.printStackTrace();
}
if (iconUrl != null) {
try (InputStream inputStream = iconUrl.openStream()) {
Files.copy(inputStream, iconFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
e.printStackTrace();
// But still continue with the rest of the manifest generation.
}
}
}
// Extract the color from the icon (either newly downloaded, or from existing icon).
if (iconFile.exists()) {
ColorExtractor.Color themeColor = ColorExtractor.getDominantColor(ImageIO.read(iconFile));
if (themeColor != null) {
// Overwrite the dummy values already inserted, if we are able to extract real values.
manifest.themeColor = themeColor.toString();
manifest.secondaryColor = themeColor.darken(0.9f).toString();
} else {
// Insert a placeholder for theme_color and secondary_color so we don’t have to
// type it in manually, but put invalid values so that the validator will catch it
// in case we forget to replace with valid values.
manifest.themeColor = "#";
manifest.secondaryColor = "#";
}
} else {
// Insert a placeholder for theme_color and secondary_color so we don’t have to
// type it in manually, but put invalid values so that the validator will catch it
// in case we forget to replace with valid values.
manifest.themeColor = "#";
manifest.secondaryColor = "#";
}
}
// Write the output manifest.
System.out.println(manifest);
FileUtils.writeFile(manifestJsonFile, GsonInstance.getPrettyPrinter().toJson(manifest));
}
Aggregations