Pretty dates and random sentences
This commit is contained in:
@@ -3,59 +3,146 @@ package fr.klemek.primedate;
|
|||||||
import java.text.NumberFormat;
|
import java.text.NumberFormat;
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.Calendar;
|
import java.util.Calendar;
|
||||||
|
import java.util.concurrent.ThreadLocalRandom;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.Timer;
|
import java.util.Timer;
|
||||||
import java.util.TimerTask;
|
import java.util.TimerTask;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Main process to be launched
|
* Main process to be launched
|
||||||
|
*
|
||||||
* @author Kleme
|
* @author Kleme
|
||||||
*/
|
*/
|
||||||
public abstract class MainProcess {
|
public abstract class MainProcess {
|
||||||
|
|
||||||
private final static String VERSION = "v1.1";
|
private final static String VERSION = "v1.2";
|
||||||
|
|
||||||
private final static SimpleDateFormat date2num = new SimpleDateFormat("yyyyMMddHHmm");
|
private final static SimpleDateFormat DATE_TO_NUM = new SimpleDateFormat("yyyyMMddHHmm");
|
||||||
private final static SimpleDateFormat sdf = new SimpleDateFormat("MMMM d, yyyy 'at' HH:mm", Locale.ENGLISH);
|
|
||||||
private final static NumberFormat nf = NumberFormat.getNumberInstance(Locale.ENGLISH);
|
|
||||||
|
|
||||||
private static void checkTime() {
|
private final static String[] GREETINGS_SENTENCES = new String[] { "Hi", "Hello there", "Good %s", "What's up?",
|
||||||
|
"Greetings", "How are you?", "Hey", "How are you doing?", "How's life?", "Long time no see",
|
||||||
|
"It's been a while", "How do you do?", "Yo", "Howdy", "Sup?", "Whazzup?" };
|
||||||
|
|
||||||
|
private final static String[] DATE_SENTENCES = new String[] { "The date is %1$s and it's %2$s",
|
||||||
|
"We are %1$s and it's already %2$s", "It's already %2$s today", "Today is %1$s and it's %2$s",
|
||||||
|
"It's %2$s and today's %1$s" };
|
||||||
|
|
||||||
|
private final static String[] PRIME_SENTENCES = new String[] { "%s is a prime number",
|
||||||
|
"%s cannot be divided by another number", "nobody can divide %s", "%s is prime" };
|
||||||
|
|
||||||
|
private final static String[] END_SENTENCES = new String[] { "", "Pretty cool, huh?", "It blows your mind!",
|
||||||
|
"You can forget it now.", "You wasted 20 seconds of your time.", "That's cool!", "Isn't it cool?",
|
||||||
|
"You should stop reading these tweets...", "That's a good password.", "Maybe not.", "Why ? ...",
|
||||||
|
"Pls help stuck in prime factory", "Why do you read these ?", "Can someone check ?", "Really ?", "Cool.",
|
||||||
|
"You can use it.", "Google it.", "How do I stop this?", "Send STOP to not receive this anymore.", "Yes.",
|
||||||
|
"It's true.", "Move on." };
|
||||||
|
|
||||||
|
private final static char[] SENTENCE_ENDS = new char[] { '.', '!', ',' };
|
||||||
|
|
||||||
|
private static void checkTime(Calendar currentTime, boolean fake) {
|
||||||
try {
|
try {
|
||||||
Calendar currentTime = Calendar.getInstance();
|
long currentTimeValue = Long.parseLong(DATE_TO_NUM.format(currentTime.getTime()));
|
||||||
currentTime.add(Calendar.MILLISECOND, -currentTime.getTimeZone().getOffset(currentTime.getTimeInMillis()));
|
|
||||||
|
|
||||||
long currentTimeValue = Long.parseLong(date2num.format(currentTime.getTime()));
|
|
||||||
|
|
||||||
if (PrimeCalculator.isPrime(currentTimeValue)) {
|
if (PrimeCalculator.isPrime(currentTimeValue)) {
|
||||||
String msg = String.format("Hi, the date is %s GMT and %s is a prime number",
|
String msg;
|
||||||
sdf.format(currentTime.getTime()), nf.format(currentTimeValue));
|
do {
|
||||||
TwitterClient.tweet(msg);
|
msg = constructSentence(currentTime, currentTimeValue);
|
||||||
|
} while (msg.length() > 280);
|
||||||
|
if (fake)
|
||||||
|
System.out.println(msg);
|
||||||
|
else
|
||||||
|
TwitterClient.tweet(msg);
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static String constructSentence(Calendar time, long timeValue) {
|
||||||
|
|
||||||
|
final int hour = time.get(Calendar.HOUR_OF_DAY);
|
||||||
|
final String dayPeriod = hour >= 5 && hour <= 12 ? "morning" : (hour <= 18 ? "afternoon" : "evening");
|
||||||
|
|
||||||
|
final PrettyDate time2 = new PrettyDate(time);
|
||||||
|
|
||||||
|
final int r1 = ThreadLocalRandom.current().nextInt(0, GREETINGS_SENTENCES.length);
|
||||||
|
final int r2 = ThreadLocalRandom.current().nextInt(0, SENTENCE_ENDS.length);
|
||||||
|
final int r3 = ThreadLocalRandom.current().nextInt(0, DATE_SENTENCES.length);
|
||||||
|
final int r4 = ThreadLocalRandom.current().nextInt(0, SENTENCE_ENDS.length);
|
||||||
|
final int r5 = ThreadLocalRandom.current().nextInt(0, PRIME_SENTENCES.length);
|
||||||
|
final int r6 = ThreadLocalRandom.current().nextInt(0, SENTENCE_ENDS.length);
|
||||||
|
final int r7 = ThreadLocalRandom.current().nextInt(0, END_SENTENCES.length);
|
||||||
|
|
||||||
|
// GREETINGS
|
||||||
|
|
||||||
|
String greetings = String.format(GREETINGS_SENTENCES[r1], dayPeriod);
|
||||||
|
if (!greetings.endsWith("?"))
|
||||||
|
greetings += SENTENCE_ENDS[r2];
|
||||||
|
|
||||||
|
// DATE
|
||||||
|
|
||||||
|
String date = String.format(DATE_SENTENCES[r3], time2.getPrettyDate(), time2.getPrettyTime());
|
||||||
|
if (greetings.endsWith(","))
|
||||||
|
date = date.substring(0, 1).toLowerCase() + date.substring(1);
|
||||||
|
date += SENTENCE_ENDS[r4];
|
||||||
|
if (date.endsWith(","))
|
||||||
|
date = date.substring(0, date.length() - 1) + " and";
|
||||||
|
|
||||||
|
// PRIME
|
||||||
|
|
||||||
|
String prime = String.format(PRIME_SENTENCES[r5], NumberFormat.getNumberInstance().format(timeValue));
|
||||||
|
if (date.endsWith("d"))
|
||||||
|
prime = prime.substring(0, 1).toLowerCase() + prime.substring(1);
|
||||||
|
|
||||||
|
if (END_SENTENCES[r7].length() == 0 && SENTENCE_ENDS[r6] == ',')
|
||||||
|
prime += ".";
|
||||||
|
else
|
||||||
|
prime += SENTENCE_ENDS[r6];
|
||||||
|
|
||||||
|
// END
|
||||||
|
|
||||||
|
String end = END_SENTENCES[r7];
|
||||||
|
if (prime.endsWith(","))
|
||||||
|
end = end.substring(0, 1) + end.substring(1);
|
||||||
|
|
||||||
|
return greetings + " " + date + " " + prime + " " + end;
|
||||||
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|
||||||
System.out.println(String.format("PrimeDate %s%n%s",VERSION,Calendar.getInstance().getTime()));
|
Locale.setDefault(Locale.ENGLISH);
|
||||||
|
|
||||||
if(args.length < 1) {
|
final int max_random = GREETINGS_SENTENCES.length * SENTENCE_ENDS.length * DATE_SENTENCES.length
|
||||||
|
* SENTENCE_ENDS.length * PRIME_SENTENCES.length * SENTENCE_ENDS.length * END_SENTENCES.length;
|
||||||
|
|
||||||
|
System.out.println(String.format("PrimeDate %s%n%s%n%d+ sentences available randomly", VERSION,
|
||||||
|
Calendar.getInstance().getTime(), max_random));
|
||||||
|
|
||||||
|
if (args.length < 1) {
|
||||||
System.out.println("Argument 1 must be a file containing customer keys");
|
System.out.println("Argument 1 must be a file containing customer keys");
|
||||||
System.exit(0);
|
System.exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(TwitterClient.setUpTwitter(args[0])) {
|
if (TwitterClient.setUpTwitter(args[0])) {
|
||||||
|
|
||||||
PrimeCalculator.computeList();
|
PrimeCalculator.computeList();
|
||||||
|
|
||||||
Timer timer = new Timer();
|
Timer timer = new Timer();
|
||||||
timer.scheduleAtFixedRate(new TimerTask() {
|
timer.scheduleAtFixedRate(new TimerTask() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
checkTime();
|
Calendar currentTime = Calendar.getInstance();
|
||||||
|
currentTime.add(Calendar.MILLISECOND, -currentTime.getTimeZone().getOffset(currentTime.getTimeInMillis()));
|
||||||
|
checkTime(currentTime, false);
|
||||||
}
|
}
|
||||||
}, 0, 1 * 60 * 1000);
|
}, 0, 1 * 60 * 1000);
|
||||||
|
|
||||||
|
/*Calendar time = Calendar.getInstance();
|
||||||
|
while (true) {
|
||||||
|
checkTime(time, true);
|
||||||
|
time.add(Calendar.MINUTE, 1);
|
||||||
|
}*/
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,42 @@
|
|||||||
|
package fr.klemek.primedate;
|
||||||
|
|
||||||
|
import java.util.Calendar;
|
||||||
|
|
||||||
|
public class PrettyDate {
|
||||||
|
|
||||||
|
private Calendar cal;
|
||||||
|
|
||||||
|
public PrettyDate() {
|
||||||
|
cal = Calendar.getInstance();
|
||||||
|
}
|
||||||
|
|
||||||
|
public PrettyDate(Calendar cal) {
|
||||||
|
this.cal = cal;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String getPrettyDayOfMonth() {
|
||||||
|
final int n = cal.get(Calendar.DAY_OF_MONTH);
|
||||||
|
if (n >= 11 && n <= 13) {
|
||||||
|
return n+"th";
|
||||||
|
}
|
||||||
|
switch (n % 10) {
|
||||||
|
case 1:
|
||||||
|
return n + "st";
|
||||||
|
case 2:
|
||||||
|
return n + "nd";
|
||||||
|
case 3:
|
||||||
|
return n + "rd";
|
||||||
|
default:
|
||||||
|
return n + "th";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPrettyDate() {
|
||||||
|
return String.format("%1$tB the %2$s, %1$tY", cal.getTime(), getPrettyDayOfMonth());
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPrettyTime() {
|
||||||
|
return String.format("%1$tH:%1$tM GMT", cal.getTime());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user