forked from lesteveinix/PsVDecrypt
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathProgram.cs
More file actions
306 lines (253 loc) · 12.4 KB
/
Copy pathProgram.cs
File metadata and controls
306 lines (253 loc) · 12.4 KB
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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Data.SQLite;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using Newtonsoft.Json;
using rohankapoor.AutoPrompt;
namespace PsVDecrypt
{
public class Program
{
private static SQLiteConnection _dbConn;
private static readonly Hashtable MapCourseNameToCourseTitle = new Hashtable();
private const int MaxPath = 260;
internal static void Main(string[] args)
{
var defaultCoursesDir = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
"Pluralsight", "courses");
string coursesFolder;
string dbPath;
string outputFolder;
do
{
coursesFolder = AutoPrompt
.PromptForInput("Pluralsight courses folder? Enter to accept default, backspace to change.\n ",
defaultCoursesDir);
}
while (!Directory.Exists(coursesFolder));
var defaultDbPath = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
"Pluralsight", "pluralsight.db");
do
{
dbPath = AutoPrompt
.PromptForInput("\nPluralsight.db path? Enter to accept default, backspace to change.\n",
defaultDbPath);
}
while (!File.Exists(dbPath));
var defaultOutputDir = Path.Combine(Directory.GetCurrentDirectory(), "output");
do
{
outputFolder = AutoPrompt
.PromptForInput("\nOutput folder? Enter to accept default, backspace to change.\n",
defaultOutputDir);
}
while (string.IsNullOrEmpty(outputFolder));
if (!Directory.Exists(outputFolder))
Util.CreateDirectory(outputFolder);
Console.WriteLine("Courses folder: " + coursesFolder);
Console.WriteLine("Output folder: " + outputFolder);
_dbConn = new SQLiteConnection("Data Source=" + dbPath + ";Version=3;");
_dbConn.Open();
GetFolderToCourseMapping();
string[] subDirs = Directory.GetDirectories(coursesFolder);
if (subDirs.Length == 0)
Console.WriteLine("\nNo course found.");
Console.WriteLine("\nFound " + subDirs.Length + " course(s):\n");
for (var i = 0; i < subDirs.Length; i++)
{
var dir = subDirs[i];
Console.WriteLine($"{i + 1}. " + GetCourseTitle(Path.GetFileName(dir)) + " (" + Path.GetFileName(dir) + ")");
}
var courseFolderId = AutoPrompt.PromptForInput("\nSelect course to decrypt: (Press up/down to choose.)\n",
Enumerable.Range(1, subDirs.Length).Select(i => i.ToString()).ToArray(),
false);
var courseFolder = subDirs[Convert.ToInt32(courseFolderId) - 1];
// System.Threading.Thread.Sleep(500);
Console.WriteLine("\nStart decrypting the course? (Press any key to continue.)\n");
Console.ReadKey();
DecryptCourse(courseFolder, outputFolder);
Console.WriteLine("All done.\n\n");
Console.WriteLine("Press any key to exit.\n");
Console.ReadKey();
}
private static string GetCourseTitle(string courseName)
{
if (MapCourseNameToCourseTitle.ContainsKey(courseName))
{
return (string)MapCourseNameToCourseTitle[courseName];
}
// fallback to courseName
return courseName;
}
private static void GetFolderToCourseMapping()
{
// Build map: mapCourseNameToCourseTitle
var command =
new SQLiteCommand("select * from Course", _dbConn) { CommandType = CommandType.Text };
var reader = command.ExecuteReader();
var dataTable = new DataTable();
dataTable.Load(reader);
for (var i = 0; i < dataTable.Rows.Count; i++)
{
MapCourseNameToCourseTitle.Add(dataTable.Rows[i]["Name"], dataTable.Rows[i]["Title"]);
}
}
private static void DecryptCourse(string courseSrcDir, string outputFolder)
{
var courseName = Path.GetFileName(courseSrcDir);
var courseDstDir = Path.Combine(outputFolder,
Regex.Replace(GetCourseTitle(courseName), @"[<>:""/\\|?*]", "_"));
Console.WriteLine("Processing course: " + GetCourseTitle(courseName) + "...");
// Reset Directory
if (Directory.Exists(courseDstDir))
Util.DeleteDirectory(courseDstDir);
Util.CreateDirectory(courseDstDir);
try
{
// Copy Image
File.Copy(Path.Combine(courseSrcDir, "image.jpg"), Path.Combine(courseDstDir, "image.jpg"));
Console.WriteLine(" > Done copying course image.");
}
catch (Exception ex)
{
// ignored
Console.WriteLine(ex.Message);
}
// Read Course Info
var command =
new SQLiteCommand("select * from Course where Name=@Name", _dbConn) { CommandType = CommandType.Text };
command.Parameters.Add(new SQLiteParameter("@Name", courseName));
var reader = command.ExecuteReader();
var dataTable = new DataTable();
dataTable.Load(reader);
if (dataTable.Rows.Count == 0)
{
Console.WriteLine(" > Error: cannot find course in database.");
return;
}
var hasTranscript = (long)dataTable.Rows[0]["HasTranscript"] == 1;
// Save Course Info to JSON
File.WriteAllText(Path.Combine(courseDstDir, "course-info.json"),
JsonConvert.SerializeObject(dataTable, Formatting.Indented));
Console.WriteLine(" > Done saving course info.");
// Read Module Info
command = new SQLiteCommand("select * from Module where CourseName=@CourseName", _dbConn)
{
CommandType = CommandType.Text
};
command.Parameters.Add(new SQLiteParameter("@CourseName", courseName));
reader = command.ExecuteReader();
dataTable = new DataTable();
dataTable.Load(reader);
Console.WriteLine(" > Found " + dataTable.Rows.Count + " module(s).");
var dataTableAsList =
JsonConvert.DeserializeObject<List<object>>(JsonConvert.SerializeObject(dataTable));
// Process Each Module
for (var i = 0; i < dataTable.Rows.Count; i++)
{
var moduleItem = dataTable.Rows[i];
Console.WriteLine(" > Processing module: " + moduleItem["Title"]);
// Get Module Dir
var moduleHash = Util.GetModuleHash(moduleItem["Name"] as string,
moduleItem["AuthorHandle"] as string);
var moduleSrcDir = Path.Combine(courseSrcDir, moduleHash);
var moduleDstDir = Path.Combine(courseDstDir,
(moduleItem["ModuleIndex"].ToString()).PadLeft(2, '0') + "." +
Util.TitleToFileName(moduleItem["Title"] as string));
if (moduleDstDir.Length >= (MaxPath - 12))
moduleDstDir = Path.Combine(courseDstDir,
(moduleItem["ModuleIndex"].ToString()).PadLeft(2, '0'));
if (!Directory.Exists(moduleDstDir))
Util.CreateDirectory(moduleDstDir);
// Save Module Info to JSON
File.WriteAllText(Path.Combine(moduleDstDir, "module-info.json"),
JsonConvert.SerializeObject(dataTableAsList[i], Formatting.Indented));
Console.WriteLine(" > Done saving module info.");
// Read Clip Info
var clipsCommand =
new SQLiteCommand("select * from Clip where ModuleId=@ModuleId", _dbConn)
{
CommandType = CommandType.Text
};
clipsCommand.Parameters.Add(new SQLiteParameter("@ModuleId", moduleItem["Id"]));
var clipsReader = clipsCommand.ExecuteReader();
var clipsDataTable = new DataTable();
clipsDataTable.Load(clipsReader);
// Save Clips Info to JSON
File.WriteAllText(Path.Combine(moduleDstDir, "clips-info.json"),
JsonConvert.SerializeObject(clipsDataTable, Formatting.Indented));
Console.WriteLine(" > Done saving clips info.");
// Process Each Clip
for (var j = 0; j < clipsDataTable.Rows.Count; j++)
{
DataRow clipItem = clipsDataTable.Rows[j];
var clipDst = GetClipDestinationPath(moduleDstDir, clipItem);
SaveClip(moduleSrcDir, clipItem, clipDst);
// Save Transcript
if (hasTranscript)
SaveTranscript(clipItem, clipDst);
}
}
}
private static string GetClipDestinationPath(string moduleDstDir, DataRow clipItem)
{
var clipDst = Path.Combine(moduleDstDir,
clipItem["ClipIndex"].ToString().PadLeft(2, '0') + "." +
Util.TitleToFileName((string)clipItem["Title"])) + ".mp4";
if (clipDst.Length >= MaxPath - 5)
clipDst = Path.Combine(moduleDstDir,
clipItem["ClipIndex"].ToString().PadLeft(2, '0')) + ".mp4";
return clipDst;
}
private static void SaveClip(string moduleSrcDir, DataRow clipItem, string clipDst)
{
Console.WriteLine(" > Processing clip: " + clipItem["Title"]);
var clipSrc = Path.Combine(moduleSrcDir, (string)clipItem["Name"]) + ".psv";
// Decrypt Clip
Util.DecryptFile(clipSrc, clipDst);
Console.WriteLine(" > Done decrypting clip.");
}
private static void SaveTranscript(DataRow clipItem, string clipDst)
{
var transcriptsCommand =
new SQLiteCommand("select * from ClipTranscript where ClipId=@ClipId", _dbConn)
{
CommandType = CommandType.Text
};
transcriptsCommand.Parameters.Add(new SQLiteParameter("@ClipId", clipItem["Id"]));
var transcriptsReader = transcriptsCommand.ExecuteReader();
var transcriptsDataTable = new DataTable();
transcriptsDataTable.Load(transcriptsReader);
if (transcriptsDataTable.Rows.Count == 0) return;
// Generate Srt File
var sb = new StringBuilder();
var sequenceI = 0;
foreach (DataRow transcriptItem in transcriptsDataTable.Rows)
{
sequenceI++;
sb.Append(sequenceI + "\n");
var startMs = (long)transcriptItem["StartTime"];
var endMs = (long)transcriptItem["EndTime"];
var startTime = TimeSpan.FromMilliseconds(startMs);
var endTime = TimeSpan.FromMilliseconds(endMs);
sb.Append(startTime.ToString(@"hh\:mm\:ss") + "," + (startMs % 1000));
sb.Append(" --> ");
sb.Append(endTime.ToString(@"hh\:mm\:ss") + "," + (endMs % 1000));
sb.Append("\n");
sb.Append(string.Join("\n",
((string)transcriptItem["Text"]).Replace("\r", "").Split('\n')
.Select(text => "- " + text)));
sb.Append("\n\n");
}
File.WriteAllText(clipDst + ".srt", sb.ToString());
Console.WriteLine(" > Done saving subtitles.");
}
}
}