Created
April 20, 2017 19:10
-
-
Save TheMasteredPanda/08fe51447fc6de47293bf1b34758e692 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.yvette; | |
import com.google.common.collect.Lists; | |
import com.google.gson.Gson; | |
import com.google.gson.GsonBuilder; | |
import org.apache.commons.imaging.ImageReadException; | |
import org.apache.commons.imaging.ImageWriteException; | |
import org.apache.commons.imaging.Imaging; | |
import org.apache.commons.imaging.formats.jpeg.JpegImageMetadata; | |
import org.apache.commons.imaging.formats.jpeg.exif.ExifRewriter; | |
import org.apache.commons.imaging.formats.tiff.TiffField; | |
import org.apache.commons.imaging.formats.tiff.TiffImageMetadata; | |
import org.apache.commons.imaging.formats.tiff.constants.TiffDirectoryType; | |
import org.apache.commons.imaging.formats.tiff.taginfos.TagInfoXpString; | |
import org.apache.commons.imaging.formats.tiff.write.TiffOutputDirectory; | |
import org.apache.commons.imaging.formats.tiff.write.TiffOutputSet; | |
import java.io.File; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.util.Collection; | |
import java.util.List; | |
import java.util.Random; | |
import java.util.Scanner; | |
import java.util.stream.Collectors; | |
import java.util.stream.Stream; | |
/** | |
* Created by TheMasteredPanda on 18/04/2017. | |
*/ | |
public class Main | |
{ | |
private final static Gson gson = new GsonBuilder().setPrettyPrinting().create(); | |
final static List<String> mentorPrefixes = Lists.newArrayList("Meet", | |
"Join", | |
"Discover", | |
"Find out more about", | |
"Learn about", | |
"Explore", | |
"Chat with", | |
"Connect with", | |
"Enjoy meeting"); | |
//Suffixes | |
final static List<String> mentorSuffixes = Lists.newArrayList("and see what she is up to with EAM - The Energy Alignment Method", | |
"and learn more about her work", | |
"to see how she has helped so many", | |
"and hear from the people she works with", | |
"to check out what these people said", | |
"to listen to what she is sharing", | |
"to get inspired by her message", | |
"to learn about this amazing method", | |
"to see how it can change your life", | |
"to know more about changing your life"); | |
//Key Sentences | |
final static List<String> mentorFillers = Lists.newArrayList("certified law of attraction coach", | |
"certified law attraction coach", | |
"certified law of attraction coach In EAM - The Energy Alignment Method", | |
"law attraction practitioner", | |
"life coach law of attraction"); | |
//Prefixes | |
final static List<String> eventPrefixes = Lists.newArrayList("Meet others on this", | |
"Join this", | |
"Discover more about our", | |
"Find out more about", | |
"Learn more about our", | |
"Explore more details for our", | |
"Chat with our team about our" , | |
"Connect with more people on our", | |
"Enjoy meeting others who think like you on our" | |
); | |
//Suffixes | |
final static List<String> eventSuffixes = Lists.newArrayList( "and see what we are up to with EAM - The Energy Alignment Method", | |
"learn more about EAM", | |
"see how we have helped so many people change their life", | |
"hear from the people we’re working with", | |
"check out what these people said about The Energy Alignment Method - EAM", | |
"listen to what we are sharing about EAM - The Energy Alignment Method", | |
"get inspired by the message of EAM", | |
"learn about this amazing method", | |
"see how EAM can change your life", | |
"know more about changing your life"); | |
//Morocco key sentences | |
final static List<String> moroccoEventFillers = Lists.newArrayList("law of attraction practitioner training EAM - The Energy Alignment Method", | |
"law of attraction certification programs", | |
"become law attraction coach", | |
"certification law attraction Energy Alignment Method - EAM", | |
"law attraction coach certification", | |
"law attraction certification courses - In EAM The Energy Alignment Method", | |
"law attraction coach training - EAM The Energy Alignment Method", | |
"law attraction coaching certification", | |
"law of attraction coach training" | |
); | |
//Generic event key sentences | |
final static List<String> eventFillers = Lists.newArrayList("learn the law of attraction", | |
"law attraction course", | |
"personal development skills", | |
"personal development", | |
"How To Change your life", | |
"law of attraction practitioner training EAM - The Energy Alignment Method", | |
"law of attraction certification programs", | |
"become law attraction coach", | |
"certification law attraction Energy Alignment Method - EAM", | |
"law attraction coach certification", | |
"law attraction certification courses - In EAM The Energy Alignment Method", | |
"law attraction coach training - EAM The Energy Alignment Method", | |
"law attraction coaching certification", | |
"law of attraction coach training", | |
"life Coach Training" | |
); | |
final static List<String> mentorNames = Lists.newArrayList( | |
"Yvette Taylor", | |
"Gean Taylor", | |
"Penny Tree", | |
"Emma Taylor", | |
"Aine Cahill", | |
"Karen Shaw", | |
"Angela Beaugard Head", | |
"Catherine Dodd", | |
"Ceza Ouzounian", | |
"Helen Jane", | |
"Christine Richards", | |
"Jo Tocher", | |
"Hazel Addley", | |
"Jeani Howard", | |
"Jo Trewarth", | |
"Lisa Hammond", | |
"Louise Price", | |
"Paula Kalik", | |
"Aimiee Sharrat" | |
); | |
public static void main(String[] args) | |
{ | |
File file; | |
firstloop: | |
do { | |
System.out.println("Which file would you like to run the metadata generator on?"); | |
Scanner scanner = new Scanner(System.in); | |
file = new File(scanner.nextLine()); | |
if (!file.exists()) { | |
System.out.println("Could not find folder " + file + ". Perhaps we ought to try again."); | |
continue; | |
} else { | |
System.out.println("File found."); | |
} | |
File[] files = file.listFiles(); | |
if (files.length == 0) { | |
System.out.println("No files found in folder " + file + "."); | |
continue; | |
} | |
} while (!file.exists()); | |
Collection<File> imageFiles = Stream.of(file.listFiles()).filter(file1 -> getFileExtension(file1).equals("jpg")).collect(Collectors.toSet()); | |
System.out.println("Image Files: " + imageFiles); | |
for (File image : imageFiles) { | |
System.out.println("Currently iterating over: " + image); | |
try { | |
System.out.println("In try block."); | |
JpegImageMetadata metadata = (JpegImageMetadata) Imaging.getMetadata(image); | |
TiffImageMetadata tiffMetadataItem = metadata.getExif(); | |
TiffOutputSet tiffOutputSet = tiffMetadataItem.getOutputSet(); | |
if (tiffOutputSet == null) { | |
tiffOutputSet = new TiffOutputSet(); | |
} | |
TiffOutputDirectory tiffOutputDirectory = tiffOutputSet.getOrCreateExifDirectory(); | |
Random random = new Random(); | |
String imageName = image.getName(); | |
String pre = null; | |
String suf = null; | |
String fil = null; | |
StringBuilder sb = new StringBuilder(); | |
//True if one or more mentors are in the image name. | |
List<String> mentorsFound = Lists.newLinkedList(); | |
mentorNames.forEach(mentor -> { | |
System.out.println("Currently iterating over mentor: " + mentor); | |
if (imageName.toLowerCase().contains(mentor.toLowerCase())) { | |
mentorsFound.add(mentor); | |
} | |
}); | |
System.out.println("Mentors Found: " + mentorsFound); | |
System.out.println(imageName); | |
if (mentorsFound.isEmpty()) { | |
for (String filler : mentorFillers) { | |
System.out.println(filler); | |
if (imageName.toLowerCase().contains(filler.toLowerCase())) { | |
System.out.println("matched filler"); | |
fil = filler; | |
break; | |
} | |
} | |
} | |
pre = mentorPrefixes.get(random.nextInt(mentorPrefixes.size())); | |
suf = mentorSuffixes.get(random.nextInt(mentorSuffixes.size())); | |
StringBuilder mSB = new StringBuilder(); | |
mentorsFound.forEach(name -> mSB.append(", " + name)); | |
System.out.println("Prefix: " + pre + "\nFiller: " + fil + "\nSuffix: " + suf + "\nMentors: " + mSB.toString()); | |
sb.append(pre + " " + mSB.toString() + " " + fil + " " + suf); | |
TiffField xpTitleInfo = null; | |
TiffField imageDescriptionInfo = null; | |
for (TiffField field : tiffMetadataItem.getAllFields()) { | |
if (field.getTagName().equalsIgnoreCase("XPTitle")) { | |
xpTitleInfo = field; | |
} | |
if (field.getTagName().equalsIgnoreCase("ImageDescription")) { | |
imageDescriptionInfo = field; | |
} | |
} | |
if (xpTitleInfo != null) { | |
tiffOutputDirectory.removeField(xpTitleInfo.getTagInfo()); | |
} | |
if (imageDescriptionInfo != null) { | |
tiffOutputDirectory.removeField(imageDescriptionInfo.getTagInfo()); | |
} | |
TagInfoXpString newXPTitle = new TagInfoXpString("XPTitle", 40091, TiffDirectoryType.getExifDirectoryType(xpTitleInfo.getDirectoryType())); | |
newXPTitle.encodeValue(xpTitleInfo.getFieldType(), sb.toString(), xpTitleInfo.getByteOrder()); | |
tiffOutputDirectory.add(newXPTitle, "XPTitle"); | |
tiffOutputSet.addDirectory(tiffOutputDirectory); | |
new ExifRewriter().updateExifMetadataLossless(image, new FileOutputStream(image.getAbsoluteFile() + "\\test.jpg"), tiffOutputSet); | |
} catch (ImageReadException | IOException | ImageWriteException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
private static String getFileExtension(File file) { | |
String name = file.getName(); | |
try { | |
return name.substring(name.lastIndexOf(".") + 1); | |
} catch (Exception e) { | |
return ""; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment