2
2
3
3
import java .io .PrintWriter ;
4
4
import java .io .StringWriter ;
5
+ import java .util .ArrayList ;
6
+ import java .util .List ;
5
7
6
8
import org .apache .commons .cli .CommandLine ;
7
9
import org .apache .commons .cli .CommandLineParser ;
17
19
import com .kinancity .core .creation .PtcCreationSummary ;
18
20
import com .kinancity .core .data .AccountData ;
19
21
import com .kinancity .core .errors .AccountCreationException ;
22
+ import com .kinancity .core .generator .impl .SequenceAccountGenerator ;
20
23
21
24
public class KinanCityCli {
22
25
@@ -26,7 +29,7 @@ public static void main(String[] args) {
26
29
27
30
Thread t = Thread .currentThread ();
28
31
t .setName ("Kinan City" );
29
-
32
+
30
33
try {
31
34
LOGGER .info (" -- Start Kinan City CLI -- " );
32
35
@@ -35,42 +38,73 @@ public static void main(String[] args) {
35
38
36
39
// Dry Run Mode
37
40
options .addOption (CliOptions .DRY_RUN .asOption ());
38
-
41
+
39
42
// Creates only 1 account
40
- options .addOption (CliOptions .SINGLE_EMAIL .asOption ());
43
+ options .addOption (CliOptions .EMAIL .asOption ());
41
44
options .addOption (CliOptions .SINGLE_USERNAME .asOption ());
42
- options .addOption (CliOptions .SINGLE_PASSWORD .asOption ());
45
+ options .addOption (CliOptions .PASSWORD .asOption ());
43
46
44
47
// Create Multiple accounts
45
48
options .addOption (CliOptions .MULTIPLE_ACCOUNTS .asOption ());
46
49
50
+ // Create a Sequence of accounts
51
+ options .addOption (CliOptions .SEQ_ACCOUNTS_COUNT .asOption ());
52
+ options .addOption (CliOptions .SEQ_ACCOUNTS_FORMAT .asOption ());
53
+ options .addOption (CliOptions .SEQ_ACCOUNTS_START .asOption ());
54
+
47
55
// Captcha key given at commandLine
48
56
options .addOption (CliOptions .CK .asOption ());
49
57
50
58
CommandLineParser parser = new DefaultParser ();
51
59
CommandLine cmd = parser .parse (options , args );
52
60
53
61
Configuration config = Configuration .getInstance ();
54
-
55
- if (cmd .hasOption (CliOptions .DRY_RUN .shortName )){
62
+
63
+ if (cmd .hasOption (CliOptions .DRY_RUN .shortName )) {
56
64
config .setDryRun (true );
57
- }
65
+ }
58
66
59
67
if (cmd .hasOption (CliOptions .CK .shortName )) {
60
68
config .setTwoCaptchaApiKey (cmd .getOptionValue (CliOptions .CK .shortName ));
61
69
}
62
70
63
71
if (config .checkConfiguration ()) {
64
-
72
+
65
73
PtcAccountCreator creator = new PtcAccountCreator (config );
66
74
67
- if (cmd .hasOption (CliOptions .SINGLE_EMAIL .shortName ) && cmd .hasOption (CliOptions .SINGLE_USERNAME .shortName ) && cmd .hasOption (CliOptions .SINGLE_PASSWORD .shortName )) {
75
+ if (cmd .hasOption (CliOptions .SEQ_ACCOUNTS_FORMAT .shortName ) && cmd .hasOption (CliOptions .SEQ_ACCOUNTS_COUNT .shortName ) && cmd .hasOption (CliOptions .EMAIL .shortName ) && cmd .hasOption (CliOptions .PASSWORD .shortName )) {
76
+
77
+ SequenceAccountGenerator generator = new SequenceAccountGenerator ();
78
+ generator .setBaseEmail (cmd .getOptionValue (CliOptions .EMAIL .shortName ));
79
+ generator .setStaticPassword (cmd .getOptionValue (CliOptions .PASSWORD .shortName ));
80
+
81
+ generator .setUsernamePattern (cmd .getOptionValue (CliOptions .SEQ_ACCOUNTS_FORMAT .shortName ));
82
+ generator .setNbAccounts (Integer .parseInt (cmd .getOptionValue (CliOptions .SEQ_ACCOUNTS_COUNT .shortName )));
83
+ generator .setStartFrom (Integer .parseInt (cmd .getOptionValue (CliOptions .SEQ_ACCOUNTS_START .shortName , "0" )));
84
+
85
+ List <AccountData > accountsToCreate = new ArrayList <>();
86
+ AccountData account ;
87
+ while ((account = generator .nextAccountData ()) != null ) {
88
+ accountsToCreate .add (account );
89
+ }
90
+
91
+ try {
92
+ PtcCreationSummary summary = creator .createAccounts (accountsToCreate );
93
+
94
+ LOGGER .info (" All creations DONE : {}" , summary );
95
+ System .exit (0 );
96
+ } catch (AccountCreationException e ) {
97
+ LOGGER .error ("\n Account Creation Error : {}" , e .getMessage ());
98
+ System .exit (1 );
99
+ }
100
+
101
+ } else if (cmd .hasOption (CliOptions .EMAIL .shortName ) && cmd .hasOption (CliOptions .SINGLE_USERNAME .shortName ) && cmd .hasOption (CliOptions .PASSWORD .shortName )) {
68
102
LOGGER .info ("Create a single account" );
69
103
70
104
AccountData account = new AccountData ();
71
- account .setEmail (cmd .getOptionValue (CliOptions .SINGLE_EMAIL .shortName ));
105
+ account .setEmail (cmd .getOptionValue (CliOptions .EMAIL .shortName ));
72
106
account .setUsername (cmd .getOptionValue (CliOptions .SINGLE_USERNAME .shortName ));
73
- account .setPassword (cmd .getOptionValue (CliOptions .SINGLE_PASSWORD .shortName ));
107
+ account .setPassword (cmd .getOptionValue (CliOptions .PASSWORD .shortName ));
74
108
75
109
try {
76
110
PtcCreationResult result = creator .createAccount (account );
@@ -84,23 +118,26 @@ public static void main(String[] args) {
84
118
String accountFileName = cmd .getOptionValue (CliOptions .MULTIPLE_ACCOUNTS .shortName );
85
119
86
120
PtcCreationSummary summary = creator .createAccounts (accountFileName );
87
-
88
- LOGGER .info (" All creations DONE : {}" ,summary );
121
+
122
+ LOGGER .info (" All creations DONE : {}" , summary );
89
123
System .exit (0 );
90
124
91
125
} else {
92
126
93
- LOGGER .error ("\n invalid arguments\n " );
127
+ LOGGER .error ("invalid arguments\n " );
94
128
95
129
HelpFormatter formatter = new HelpFormatter ();
96
130
StringWriter out = new StringWriter ();
97
131
PrintWriter writer = new PrintWriter (out );
98
132
99
- String cmdLineSyntax = " (-m <email> -u <username> -p <password>) (-a <accounts.csv> ) [-ck <captchakey>]" ;
133
+ String cmdLineSyntax = " one of \n "
134
+ + " -m <email> -u <username> -p <password> \n "
135
+ + " -a <accounts.csv> \n "
136
+ + " -m <email> -c <#ofAccounts> -f <format**> -p <password> (-s <first#>)\n "
137
+ + " and optional : -ck <captchakey> \n \n " ;
138
+ formatter .setWidth (180 );
100
139
formatter .printHelp (cmdLineSyntax , options );
101
140
102
- String usage = out .toString ();
103
- LOGGER .error (usage );
104
141
System .exit (0 );
105
142
}
106
143
0 commit comments