-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparseTorrentNameWindows.ts
More file actions
252 lines (224 loc) · 6.54 KB
/
parseTorrentNameWindows.ts
File metadata and controls
252 lines (224 loc) · 6.54 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
const ptt = require("parse-torrent-title");
const fs = require("fs");
const mv = require("mv");
const minimist = require("minimist");
const OS = require("opensubtitles-api");
const path = require("path");
const VIDEO_EXT = [".mp4", ".mkv", ".avi"];
const SUBTITLE_EXT = ["srt", "vtt"];
const https = require("https");
interface MovieInformation {
title: string;
year: string;
season: number;
episode: number;
}
interface MovedFileInfo {
directory: string;
name: string;
}
interface OsInfos {
username: string | undefined;
password: string | undefined;
}
const download = function (url: string, dest: string) {
const request = https.get(url, function (response) {
if (response.statusCode === 200) {
var file = fs.createWriteStream(dest);
response.pipe(file);
} else {
logProcess(`error`, `while downloading subs ${response}`);
}
request.setTimeout(60000, function () {
logProcess(`error`, `timeout while downloading subs`);
request.abort();
});
});
};
const isFile = (path: string) => {
return !fs.lstatSync(path).isDirectory();
};
const createOS = (osInfos: OsInfos) => {
let OpenSubtitles;
if (osInfos.username && osInfos.password)
OpenSubtitles = new OS({
useragent: "UserAgent",
username: osInfos.username,
password: osInfos.password,
ssl: true,
});
else
OpenSubtitles = new OS({
useragent: "UserAgent",
ssl: true,
});
return OpenSubtitles;
};
const createSimpleQuery = (filmInfo: MovedFileInfo) => {
let query = {
path: `${filmInfo.directory}/${filmInfo.name}`,
filename: filmInfo.name,
extensions: SUBTITLE_EXT,
};
return query;
};
const createComplexQuery = (filmInfo: MovedFileInfo) => {
const info = ptt.parse(filmInfo.name);
const isMovie = verifyIfMovie(info);
let query = {
query: info.title,
extensions: SUBTITLE_EXT,
};
if (!isMovie) {
if (info.season) {
query["season"] = info.season;
}
if (info.episode) {
query["episode"] = info.episode;
}
}
return query;
};
const searchSubtitle = (movedFileInfos: MovedFileInfo, OpenSubtitles) => {
return OpenSubtitles.search(createSimpleQuery(movedFileInfos))
.then((subsInfo) => {
if (!subsInfo || !subsInfo["en"])
return OpenSubtitles.search(createComplexQuery(movedFileInfos))
.then((complexSubs) => {
return complexSubs["en"];
})
.catch((err) => {
logProcess("error", `while searching complex query ${err}`);
});
return subsInfo["en"];
})
.catch((err) => {
logProcess("error", `while searching simple query ${err}`);
});
};
interface SubInfo {
url: string;
filename: string;
}
const downloadSubtitle = (subInfo: SubInfo, filmPath: string) => {
const url = subInfo.url;
const subName = subInfo.filename;
if (url) download(url, `${filmPath}/${subName}`);
};
const downloadSubtitles = (filesInfos: MovedFileInfo[], osInfos: OsInfos) => {
const OpenSubtitles = createOS(osInfos);
OpenSubtitles.login()
.then(async () => {
for (const fileInfo of filesInfos) {
if (fileInfo) {
const subInfos = await searchSubtitle(fileInfo, OpenSubtitles);
if (subInfos) {
logProcess(`subs`, `found ${fileInfo.name}`);
downloadSubtitle(subInfos, fileInfo.directory);
} else {
logProcess(`subs`, `not found ${fileInfo.name}`);
}
}
}
})
.catch((err) => {
logProcess("Error", `while loggin to OS, ${err}`);
});
};
const createPath = (path: string) => {
if (!fs.existsSync(path)) {
fs.mkdirSync(path);
}
};
const createMoviePath = (information: MovieInformation, targetDir: string) => {
const movieTitle = information.title;
const year = information.year ? information.year : "";
const folderName = `${movieTitle} (${year})`;
const path = `${targetDir}/Movies/${folderName}`;
createPath(path);
return path;
};
const createSeriesPath = (information: MovieInformation, targetDir: string) => {
const serieTitle = information.title;
const season = information.season;
const serieFolder = `${targetDir}/Tv/${serieTitle}/`;
createPath(serieFolder);
const seasonFolder = `${serieFolder}/Season ${season}/`;
createPath(seasonFolder);
return seasonFolder;
};
const verifyIfMovie = (information: MovieInformation) => {
if (information.season != undefined || information.episode != undefined)
return false;
return true;
};
const logProcess = (name: string, information) => {
fs.appendFile(
"/tmp/logProcess",
`${name}, \n ${JSON.stringify(information)} \n \n`,
function (err) {
if (err) {
return console.log(err);
}
console.log("The file was saved!");
}
);
};
const extractOSInfos = (args): OsInfos => {
const osUsername = args.u ? args.u : undefined;
const osPassword = args.p ? args.p : undefined;
return {
username: osUsername,
password: osPassword,
};
};
const processFolder = (
currentDirectory: string,
folderName: string,
targetDirectory: string
): MovedFileInfo[] => {
let movedFileInfos: MovedFileInfo[] = [];
const folderPath = `${currentDirectory}/${folderName}`;
const files = fs.readdirSync(folderPath);
files.forEach((file) => {
movedFileInfos.push(processFile(folderPath, file, targetDirectory));
});
return movedFileInfos;
};
const processFile = (
currentDirectory: string,
fileName: string,
targetDirectory: string
): MovedFileInfo => {
const ext = path.extname(fileName);
if (VIDEO_EXT.indexOf(ext) == -1) return undefined;
const information = ptt.parse(fileName);
const fileIsMovies = verifyIfMovie(information);
const newDir = fileIsMovies
? createMoviePath(information, targetDirectory)
: createSeriesPath(information, targetDirectory);
const newPath = `${newDir}/${fileName}`;
const currentPath = `${currentDirectory}/${fileName}`;
mv(currentPath, newPath, function (err) {
if (err) {
logProcess(`processFile: ${fileName}`, `error: ${err}`);
} else {
logProcess(`processFile: ${fileName}`, `completed`);
}
});
return {
name: fileName,
directory: newDir,
};
};
let args = minimist(process.argv.slice(2));
const torrentName = args.n;
const targetDir = args.t;
const downloadPath = args.d;
let movedFileInfos: MovedFileInfo[];
if (isFile(`${downloadPath}/${torrentName}`)) {
movedFileInfos = [processFile(downloadPath, torrentName, targetDir)];
} else {
movedFileInfos = processFolder(downloadPath, torrentName, targetDir);
}
downloadSubtitles(movedFileInfos, extractOSInfos(args));