-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
380 lines (367 loc) · 17.8 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
using System;
using RedditSharp; // reddit API
using System.Linq; // queries
using System.Collections.Generic; // lists
using System.IO; // read/write files
namespace RedditImporter
{
class Program
{
static void Main(string[] args)
{
List<string> users; // users
List<string> subs; // subs
Reddit impReddit = new Reddit(); // account you want to import to
Reddit expReddit = new Reddit(); // account you want to export from
string usr, usr1, pwd, pwd1, subFilePath, userFilePath;
Console.Clear();
while (true)
{
Console.WriteLine("\nMake a choice:");
Console.WriteLine("1. Import subreddits and/or users from txt file.");
Console.WriteLine("2. Export subreddits and users to txt files.");
Console.WriteLine("3. Import subreddits and/or users from another account.");
Console.WriteLine("4. Unsuscribe/unfollow subreddits/users from txt file.");
Console.WriteLine("5. Unsubscribe and/or unfollow all current subreddits and users.");
Console.WriteLine("6. Display current subreddits and users.");
Console.WriteLine("\nTo exit, press ctrl + c");
string choice = Console.ReadLine();
switch (choice)
{
case "1":
Console.WriteLine("Enter subreddits file path, or drag file here: ");
subFilePath = Console.ReadLine();
subs = new List<string>(File.ReadAllLines(subFilePath));
Console.WriteLine("Enter users file path: ");
userFilePath = Console.ReadLine();
users = new List<string>(File.ReadAllLines(userFilePath));
Console.WriteLine("Please log in to the account you want to import to.");
Console.Write("\nEnter username: ");
usr1 = Console.ReadLine();
Console.Write("Enter password: ");
pwd1 = Password();
impReddit.LogIn(usr1, pwd1);
Console.WriteLine("\nSubreddits:");
Subscribe(impReddit, subs);
Console.WriteLine("\nUsers:\n");
Follow(impReddit, users);
break;
case "2":
Console.WriteLine("Enter a path to export files to, or drag folder here: ");
subFilePath = Console.ReadLine();
Console.WriteLine("Please log in to the account you want to export from.");
Console.Write("\nEnter username: ");
usr1 = Console.ReadLine();
Console.Write("Enter password: ");
pwd1 = Password();
impReddit.LogIn(usr1, pwd1);
impReddit.LogIn(usr1, pwd1);
ShowSubsUsers(impReddit, out subs, out users);
File.WriteAllLines(subFilePath + impReddit.User + "_users.txt", users); // export users list to file
File.WriteAllLines(subFilePath + impReddit.User + "_subs.txt", subs); // export subs list to file
Console.WriteLine("\nSuccessfully exported users and subreddits to txt files.");
break;
case "3":
Console.WriteLine("Please login to the account you want to export from.");
Console.Write("Username: ");
usr = Console.ReadLine();
Console.Write("Password: ");
pwd = Password();
expReddit.LogIn(usr, pwd);
Console.WriteLine($"\n{expReddit.User} is currently subsribed to the following subreddits:\n");
ShowSubsUsers(expReddit, out subs, out users);
Console.WriteLine("Please log in to the account you want to import to.");
Console.Write("Username: ");
usr1 = Console.ReadLine();
Console.Write("Password: ");
pwd1 = Password();
impReddit.LogIn(usr1, pwd1);
Console.WriteLine("\n\nDo you want to subscribe to all the listed subreddits?");
string answer1 = Console.ReadLine();
answer1 = answer1.ToLower();
while (true) // join subs
{
if (answer1 == "yes")
{
Subscribe(impReddit, subs);
break;
}
else if (answer1 == "no")
{
break;
}
else
{
Console.WriteLine("Please answer with 'yes' or 'no'.");
Console.WriteLine("\nDo you want to subscribe to all listed subreddits?");
answer1 = Console.ReadLine();
answer1 = answer1.ToLower();
}
}
Console.WriteLine("\n\nDo you want to follow all the listed users?");
string answer2 = Console.ReadLine();
answer2 = answer2.ToLower();
while (true) // follow users
{
if (answer2 == "yes")
{
Follow(impReddit, users);
break;
}
else if (answer2 == "no")
{
break;
}
else
{
Console.WriteLine("Please answer with 'yes' or 'no'.");
Console.WriteLine("\nDo you want to follow all listed users?");
answer2 = Console.ReadLine();
answer2 = answer2.ToLower();
}
}
break;
case "4":
Console.WriteLine("Enter subreddits file path, or drag file here: ");
subFilePath = Console.ReadLine();
subs = new List<string>(File.ReadAllLines(subFilePath));
Console.WriteLine("Enter users file path: ");
userFilePath = Console.ReadLine();
users = new List<string>(File.ReadAllLines(userFilePath));
Console.WriteLine("Please login.");
Console.Write("Enter username: ");
usr = Console.ReadLine();
Console.Write("Enter password: ");
pwd = Password();
expReddit.LogIn(usr, pwd);
Console.WriteLine($"\nDo you want to unsubscribe {expReddit.User} from all the listed subreddits?");
string answer3 = Console.ReadLine();
answer3 = answer3.ToLower();
while (true) // leave subs
{
if (answer3 == "yes")
{
Unsubscribe(expReddit, subs);
break;
}
else if (answer3 == "no")
{
break;
}
else
{
Console.WriteLine("Please answer with 'yes' or 'no'.");
Console.WriteLine($"Do you want to unsubscribe {expReddit.User} from all the listed subreddits?");
answer3 = Console.ReadLine();
answer3 = answer3.ToLower();
}
}
Console.WriteLine($"\nDo you want to unfollow all the listed users from {expReddit.User}?");
string answer4 = Console.ReadLine();
answer4 = answer4.ToLower();
while (true) // unfollow users
{
if (answer4 == "yes")
{
Unfollow(expReddit, users);
break;
}
else if (answer4 == "no")
{
Console.WriteLine("Goodbye!");
break;
}
else
{
Console.WriteLine("Please answer with 'yes' or 'no'.");
Console.WriteLine($"Do you want to unfollow all the listed users from {expReddit.User}?");
answer4 = Console.ReadLine();
answer4 = answer4.ToLower();
}
}
break;
case "5":
Console.WriteLine("Please login.");
Console.Write("Enter username: ");
usr = Console.ReadLine();
Console.Write("Enter password: ");
pwd = Password();
expReddit.LogIn(usr, pwd);
Console.WriteLine($"\nDo you want to unfollow all users from {expReddit.User}?");
string answer5 = Console.ReadLine();
answer5 = answer5.ToLower();
while (true)
{
if (answer5 == "yes")
{
UnfollowAll(expReddit);
break;
}
else if (answer5 == "no")
{
break;
}
else
{
Console.WriteLine("Please answer with 'yes' or 'no'.");
Console.WriteLine($"Do you want to unfollow all the users from {expReddit.User}?");
answer5 = Console.ReadLine();
answer5 = answer5.ToLower();
}
}
Console.WriteLine($"\nDo you want to unsubscribe from all subreddits from {expReddit.User}?");
string answer6 = Console.ReadLine();
answer6 = answer6.ToLower();
while (true)
{
if (answer6 == "yes")
{
UnsubAll(expReddit);
break;
}
else if (answer6 == "no")
{
Console.WriteLine("Goodbye!");
break;
}
else
{
Console.WriteLine("Please answer with 'yes' or 'no'.");
Console.WriteLine($"Do you want to unfollow all the users from {expReddit.User}?");
answer6 = Console.ReadLine();
answer6 = answer6.ToLower();
}
}
break;
case "6":
Console.WriteLine("Please login.");
Console.Write("Enter username: ");
usr = Console.ReadLine();
Console.Write("Enter password: ");
pwd = Password();
expReddit.LogIn(usr, pwd);
ShowSubsUsers(expReddit, out _, out _);
break;
default:
Console.Clear();
Console.WriteLine("*************************");
Console.WriteLine("* Enter a number 1–6 *");
Console.WriteLine("*************************");
break;
}
}
}
public static void Subscribe(Reddit acc, List<string> subs)
{
int i = 0;
foreach (string subreddit in subs)
{
acc.GetSubreddit(subreddit).Subscribe();
i++;
Console.WriteLine($"Subscribed {acc.User} to {subreddit} ({i} of {subs.Count})");
}
Console.WriteLine($"\nSuccessfully subscribed to {subs.Count} subreddits.");
}
public static void Unsubscribe(Reddit acc, List<string> subs)
{
int i = 0;
foreach (string subreddit in subs)
{
acc.GetSubreddit(subreddit).Unsubscribe();
i++;
Console.WriteLine($"Unsubscribed {acc.User} from {subreddit} ({i} of {subs.Count})");
}
Console.WriteLine($"\nSuccessfully unsubscribed from {subs.Count} subreddits.");
}
public static void UnsubAll(Reddit acc)
{
int i = 0;
int j = acc.User.SubscribedSubreddits.Where(s => !s.Name.StartsWith("/user/")).Count();
foreach (string subreddit in acc.User.SubscribedSubreddits.Where(s => !s.Name.StartsWith("/user")).Select(s => s.Name))
{
acc.GetSubreddit(subreddit).Unsubscribe();
i++;
Console.WriteLine($"Unsubscribed {acc.User} from {subreddit} ({i} of {j})");
}
Console.WriteLine($"\nSuccessfully unsubscribed from {i} subreddits.");
}
public static void UnfollowAll(Reddit acc)
{
int i = 0;
int j = acc.User.SubscribedSubreddits.Where(s => s.Name.StartsWith("/user/")).Count();
foreach (string subreddit in acc.User.SubscribedSubreddits.Where(s => s.Name.StartsWith("/user")).Select(s => s.Name))
{
acc.GetSubreddit("r/u_" + subreddit.Substring(6)).Unsubscribe();
i++;
Console.WriteLine($"{acc.User} unfollowed {subreddit.Substring(6)} ({i} of {j})");
}
Console.WriteLine($"\nSuccessfully unfollowed {i} users.");
}
public static void Follow(Reddit acc, List<string> users)
{
int i = 0;
foreach (string user in users)
{
acc.GetSubreddit("r/u_" + user).Subscribe();
i++;
Console.WriteLine($"{acc.User} followed {user} ({i} of {users.Count})");
}
Console.WriteLine($"\nSuccessfully followed {users.Count} users.");
}
public static void Unfollow(Reddit acc, List<string> users)
{
int i = 0;
foreach (string user in users)
{
acc.GetSubreddit("r/u_" + user).Unsubscribe();
i++;
Console.WriteLine($"{acc.User} unfollowed {user} ({i} of {users.Count})");
}
Console.WriteLine($"\nSuccessfully unfollowed {users.Count} users.");
}
public static void ShowSubsUsers(Reddit acc, out List<string> subs, out List<string> users)
{
users = new List<string>();
subs = new List<string>();
Console.WriteLine($"\n{acc.User} is currently subscribed to the following subreddits:\n");
foreach (string subreddit in acc.User.SubscribedSubreddits.Where(s => !s.Name.StartsWith("/user/")).Select(s => s.Name))
{
subs.Add(subreddit);
Console.WriteLine(subreddit);
}
Console.WriteLine($"\n{acc.User} is currently following to the following users:\n");
foreach (string subreddit in acc.User.SubscribedSubreddits.Where(s => s.Name.StartsWith("/user/")).Select(s => s.Name))
{
users.Add(subreddit.Substring(6));
Console.WriteLine(subreddit.Substring(6));
}
Console.WriteLine($"\nShowing {subs.Count} subreddits & {users.Count} users.\n");
}
public static string Password()
{
string pwd="";
do
{
ConsoleKeyInfo key = Console.ReadKey(true);
if (key.Key != ConsoleKey.Backspace && key.Key != ConsoleKey.Enter)
{
pwd += key.KeyChar;
Console.Write("*");
}
else
{
if (key.Key == ConsoleKey.Backspace && pwd.Length > 0)
{
pwd = pwd.Substring(0, (pwd.Length - 1));
Console.Write("\b \b");
}
else if (key.Key == ConsoleKey.Enter)
{
break;
}
}
} while (true);
return pwd;
}
}
}