-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
209 lines (172 loc) · 7.07 KB
/
Copy pathProgram.cs
File metadata and controls
209 lines (172 loc) · 7.07 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
using System;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using System.Collections.Generic;
namespace TXTtoMediaWiki
{
internal class Program
{
private static HttpClient _httpClient;
private static CookieContainer _cookieContainer = new CookieContainer();
private string ApiUrl;
public Program(string apiUrl, string username)
{
ApiUrl = apiUrl;
var handler = new HttpClientHandler
{
CookieContainer = _cookieContainer,
UseCookies = true,
AllowAutoRedirect = true
};
_httpClient = new HttpClient(handler);
_httpClient.DefaultRequestHeaders.UserAgent.ParseAdd($"{username}/https://dev.miraheze.org/wiki/Converting_text_files_to_MediaWiki_articles");
}
static async Task Main(string[] args)
{
if (args.Length < 3)
{
Console.WriteLine("Usage: TXTtoMediaWiki.exe <username> <password> <apiUrl>");
return;
}
string username = args[0];
string password = args[1];
string apiUrl = args[2];
var program = new Program(apiUrl, username);
string loginResult = await program.LoginUserAsync(username, password);
if (loginResult.Contains("successful"))
{
await program.ProcessFilesAsync();
}
}
public async Task<string> LoginUserAsync(string username, string password)
{
string? loginToken = await GetTokenAsync("login");
if (string.IsNullOrWhiteSpace(loginToken))
{
throw new InvalidOperationException("Failed to retrieve login token.");
}
Dictionary<string, string> parameters = new Dictionary<string, string>
{
{ "action", "login" },
{ "format", "json" },
{ "lgname", username },
{ "lgpassword", password },
{ "lgtoken", loginToken }
};
var encodedContent = new FormUrlEncodedContent(parameters);
HttpResponseMessage response = await _httpClient.PostAsync(ApiUrl, encodedContent);
if (!response.IsSuccessStatusCode)
{
throw new HttpRequestException($"Failed to log in. Status code: {response.StatusCode}");
}
string jsonResponse = await response.Content.ReadAsStringAsync();
using JsonDocument doc = JsonDocument.Parse(jsonResponse);
JsonElement root = doc.RootElement;
if (root.TryGetProperty("login", out JsonElement loginElement) &&
loginElement.TryGetProperty("result", out JsonElement resultElement))
{
string result = resultElement.GetString() ?? "";
if (result == "Success")
{
return "Login successful!";
}
else
{
return $"Login failed. Reason: {result}";
}
}
throw new InvalidOperationException("Unexpected login response format.");
}
public async Task<string?> GetTokenAsync(string type)
{
Dictionary<string, string> parameters = new Dictionary<string, string>
{
{ "action", "query" },
{ "meta", "tokens" },
{ "format", "json" },
{ "type", type }
};
var queryString = string.Join("&", parameters.Select(kvp => $"{Uri.EscapeDataString(kvp.Key)}={Uri.EscapeDataString(kvp.Value)}"));
var requestUrl = $"{ApiUrl}?{queryString}";
HttpResponseMessage response = await _httpClient.GetAsync(requestUrl);
if (!response.IsSuccessStatusCode)
{
throw new HttpRequestException($"Failed to retrieve token. Status code: {response.StatusCode}");
}
string jsonResponse = await response.Content.ReadAsStringAsync();
using JsonDocument doc = JsonDocument.Parse(jsonResponse);
JsonElement root = doc.RootElement;
if (root.TryGetProperty("query", out JsonElement queryElement) &&
queryElement.TryGetProperty("tokens", out JsonElement tokensElement))
{
string tokenKey = $"{type}token";
if (tokensElement.TryGetProperty(tokenKey, out JsonElement tokenElement))
{
return tokenElement.GetString();
}
}
return null;
}
public async Task ProcessFilesAsync()
{
string directory = AppDomain.CurrentDomain.BaseDirectory;
string[] files = Directory.GetFiles(directory, "*.txt");
if (files.Length == 0)
{
Console.WriteLine("No .txt files found.");
return;
}
// Display file names for confirmation
Console.WriteLine("The following files will be uploaded:");
foreach (string file in files)
{
Console.WriteLine($"- {Path.GetFileName(file)}");
}
Console.Write("Do you want to proceed? (Y/N): ");
string? input = Console.ReadLine()?.Trim().ToLower();
if (input != "y")
{
Console.WriteLine("Operation cancelled.");
return;
}
string? csrfToken = await GetTokenAsync("csrf");
if (string.IsNullOrWhiteSpace(csrfToken))
{
Console.WriteLine("Failed to retrieve CSRF token.");
return;
}
foreach (string file in files)
{
string title = Path.GetFileNameWithoutExtension(file);
string content = await File.ReadAllTextAsync(file);
Console.WriteLine($"Uploading article: {title}");
await EditPageAsync(title, content, csrfToken);
}
}
public async Task EditPageAsync(string title, string content, string csrfToken)
{
Dictionary<string, string> parameters = new Dictionary<string, string>
{
{ "action", "edit" },
{ "format", "json" },
{ "title", title },
{ "text", content },
{ "token", csrfToken }
};
var encodedContent = new FormUrlEncodedContent(parameters);
HttpResponseMessage response = await _httpClient.PostAsync(ApiUrl, encodedContent);
if (!response.IsSuccessStatusCode)
{
Console.WriteLine($"Failed to edit {title}. Status code: {response.StatusCode}");
return;
}
string jsonResponse = await response.Content.ReadAsStringAsync();
Console.WriteLine($"Edit response for {title}: {jsonResponse}");
}
}
}