-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathTorrentContent.cs
292 lines (272 loc) · 10.4 KB
/
TorrentContent.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
using BencodeNET.Objects;
using BencodeNET.Parsing;
using BencodeNET.Torrents;
using OKP.Core.Utils;
using Serilog;
using System.Net.Http.Headers;
using System.Text;
using System.Text.RegularExpressions;
namespace OKP.Core.Interface
{
public class TorrentContent
{
public class TorrentData
{
public FileInfo FileInfo;
public ByteArrayContent ByteArrayContent
{
//When adding this content into a MultipartFormDataContent, the filename prop will be stored in this ByteArrayContent object, which cannot be overwrite.
//Use a setter to return a new object everytime.
get
{
var ByteArrayContent = new ByteArrayContent(bytes);
ByteArrayContent.Headers.ContentType = MediaTypeHeaderValue.Parse("application/x-bittorrent");
return ByteArrayContent;
}
}
public Torrent TorrentObject;
private readonly byte[] bytes;
public TorrentData(string filename)
{
FileInfo = new FileInfo(filename);
bytes = File.ReadAllBytes(filename);
var parser = new BencodeParser(); // Default encoding is Encoding.UTF8, but you can specify another if you need to
TorrentObject = parser.Parse<Torrent>(filename);
}
}
public TorrentData? Data;
public List<Template>? IntroTemplate { get; set; }
public string? DisplayName { get; set; }
public string? GroupName { get; set; }
public string? Poster { get; set; }
public string? About { get; set; }
public string? FilenameRegex { get; set; }
public string? ResolutionRegex { get; set; }
public string? SettingPath { get; set; }
// public bool HasSubtitle { get; set; }
// public bool IsFinished { get; set; }
public string? CookiePath { get; set; }
public List<ContentTypes>? Tags { get; set; }
public List<NyaaTorrentFlags>? TorrentFlags { get; set; }
public class Template
{
public string? Site { get; set; }
public string? Name { get; set; }
public string? Content { get; set; }
public string? Cookie { get; set; }
public string? UserAgent { get; set; }
public string? Proxy { get; set; }
public string? DisplayName { get; set; }
}
public enum ContentTypes
{
Anime,
Music,
Action,
Picture,
Comic,
Novel,
Software,
Others,
MV,
Chinese,
HongKong,
Taiwan,
English,
Japanese,
TV,
Movie,
Batch, // same as Collection
Collection,
Raw,
Lossless,
Lossy,
ACG,
Doujin,
Pop,
Idol,
Tokusatsu,
Show,
Graphics,
Photo,
App,
Game
}
public static TorrentContent Build(string filename, string settingFile, string? baseTemplate, string appLocation)
{
var settingFilePath = settingFile;
if (Path.GetDirectoryName(filename) == "")
{
filename = Path.Combine(Environment.CurrentDirectory, filename);
}
if (Path.GetDirectoryName(settingFile) == "")
{
settingFilePath = Path.Combine(Path.GetDirectoryName(filename) ?? "", settingFile);
}
if (!File.Exists(settingFilePath))
{
Log.Error("没有配置文件");
IOHelper.ReadLine();
throw new IOException();
}
var torrentC = TomlParseHelper.DeserializeTorrentContent(settingFilePath);
ProcessTemplate(torrentC, baseTemplate);
torrentC.SettingPath = Path.GetDirectoryName(settingFilePath);
//if (!File.Exists(torrentC.CookiePath))
//{
// Log.Error("没有设置Cookie,使用默认Cookie文件{file}",Path.GetFullPath("cookie.txt"));
// torrentC.CookiePath = "cookie.txt";
//}
//HttpHelper.GlobalCookieContainer.LoadFromTxt(torrentC.CookiePath);
if (torrentC.DisplayName is null)
{
Log.Error("没有配置标题");
IOHelper.ReadLine();
throw new IOException();
}
torrentC.Data = new(filename);
if (torrentC.DisplayName.Contains(@"<ep>") && torrentC.FilenameRegex != null && torrentC.FilenameRegex.Contains("(?<ep>"))
{
Regex epRegex = new(torrentC.FilenameRegex);
var epMatch = epRegex.Match(filename);
if (epMatch.Success)
{
torrentC.DisplayName = torrentC.DisplayName.Replace("<ep>", epMatch.Groups["ep"].Value);
}
else
{
Log.Error("标题集数替换失败");
IOHelper.ReadLine();
throw new IOException();
}
}
if (torrentC.DisplayName.Contains(@"<res>") && torrentC.ResolutionRegex != null && torrentC.ResolutionRegex.Contains("(?<res>"))
{
Regex resRegex = new(torrentC.ResolutionRegex);
var resMatch = resRegex.Match(filename);
if (resMatch.Success)
{
torrentC.DisplayName = torrentC.DisplayName.Replace("<res>", resMatch.Groups["res"].Value);
}
else
{
Log.Error("标题分辨率替换失败");
IOHelper.ReadLine();
throw new IOException();
}
}
Log.Information("标题:{0}", torrentC.DisplayName);
if (torrentC.Tags is not null)
{
Log.Information("标签:{0}", string.Join(", ", torrentC.Tags));
}
// user properties, it will overlap some existing private config from setting config, such as proxy, cookie and user_agent
var userPropPath = IOHelper.BasePath(Constants.DefaultUserPropsPath, Constants.UserPropertiesFileName);
if (File.Exists(userPropPath))
{
var userProp = TomlParseHelper.DeserializeUserProperties(userPropPath);
if (userProp.UserProp == null)
return torrentC;
foreach (var p in userProp.UserProp)
{
if (torrentC.IntroTemplate is not null)
{
foreach (var tp in torrentC.IntroTemplate)
{
if (p.Name == tp.Name && p.Site == tp.Site)
{
tp.Proxy = p.Proxy ?? tp.Proxy;
tp.Cookie = p.Cookie ?? tp.Cookie;
tp.UserAgent = p.UserAgent ?? tp.UserAgent;
}
}
}
}
}
return torrentC;
}
private static void ProcessTemplate(TorrentContent torrentC, string? baseTemplate)
{
if (baseTemplate is null) return;
if (torrentC.IntroTemplate is null) return;
if (Path.GetDirectoryName(baseTemplate) == "")
{
baseTemplate = Path.Combine(Environment.CurrentDirectory, baseTemplate);
}
// Automatically generate for sites with missing publishing content
foreach (var site in torrentC.IntroTemplate.Where(site => string.IsNullOrEmpty(site.Content)))
{
site.Content = baseTemplate;
}
}
public bool IsV2()
{
if (Data?.TorrentObject is null)
{
Log.Fatal("Data?.TorrentObject is null");
throw new ArgumentNullException(nameof(Data.TorrentObject));
}
if (Data.TorrentObject.ExtraFields["info"] is BDictionary infoValue)
{
if (infoValue["meta version"] is BNumber versionValue)
{
if (versionValue == 2)
{
Log.Verbose("{@TorrentObject}", Data.TorrentObject);
return true;
}
}
}
return false;
}
public void DisplayFiles()
{
if (Data?.TorrentObject is null)
{
Log.Fatal("Data?.TorrentObject is null");
throw new ArgumentNullException(nameof(Data.TorrentObject));
}
StringBuilder fileList = new();
if (Data.TorrentObject.FileMode == TorrentFileMode.Multi)
{
foreach (var file in Data.TorrentObject.Files)
{
fileList.AppendLine(file.FullPath);
}
}
else
{
fileList.AppendLine(Data.TorrentObject.File.FileName);
}
Log.Information("文件列表:{FileList}", fileList);
}
public void DisplayFileTree()
{
if (Data?.TorrentObject is null)
{
Log.Fatal("Data?.TorrentObject is null");
throw new ArgumentNullException(nameof(Data.TorrentObject));
}
StringBuilder fileList = new();
var rootNode = Data.TorrentObject.FileMode == TorrentFileMode.Multi ? new Node(Data.TorrentObject.Files) : new Node(Data.TorrentObject.File);
foreach (var line in Node.GetFileTree(rootNode))
{
fileList.AppendLine(line);
}
Log.Information("文件列表:{NewLine}{FileList}", Environment.NewLine, fileList);
}
[Flags]
public enum NyaaTorrentFlags
{
None = 0b_0,
Anonymous = 0b_1,
Hidden = 0b_10,
Remake = 0b_100,
Complete = 0b_1000,
}
}
public class UserProperties
{
public List<TorrentContent.Template>? UserProp { get; set; }
}
}