-
Notifications
You must be signed in to change notification settings - Fork 0
/
Scraper.cs
358 lines (335 loc) · 17.2 KB
/
Scraper.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
using System.Collections.Concurrent;
using CommandLine;
using System.Text.RegularExpressions;
using System.Net;
using System.Diagnostics;
using Newtonsoft.Json;
namespace Scrawler
{
public static class ParallelExtention {
public static IEnumerable<IEnumerable<T>> GetParrallelConsumingEnumerable<T>(this IProducerConsumerCollection<T> collection)
{
T item;
while (collection.TryTake(out item))
{
yield return GetParrallelConsumingEnumerableInner(collection, item);
}
}
private static IEnumerable<T> GetParrallelConsumingEnumerableInner<T>(IProducerConsumerCollection<T> collection, T item)
{
yield return item;
while (collection.TryTake(out item))
{
yield return item;
}
}
}
public class CLI // CS1106
{
private bool debug = false;
private string root_url;
private List<string> scopes;
private string user_agent;
private string regex_pattern;
private int maxConcurrentCrawlers;
private int maxConcurrentScrapers;
private int maxConcurrentDownloaders;
private bool stripQueryParms;
private bool Download;
private bool GenerateOutput;
private string Filename;
private bool Checkpoints;
private int total_downloads = 0;
private Thread ThreadCrawler;
private Thread ThreadScraper;
private Thread ThreadDownloader;
public bool isRunning = false;
private ConcurrentQueue<string> QueueCrawler = new ConcurrentQueue<string>();
private ConcurrentQueue<string> QueueScraper = new ConcurrentQueue<string>();
private ConcurrentQueue<string> QueueDownloads = new ConcurrentQueue<string>();
private ConcurrentDictionary<string, string> MemoryQueue = new ConcurrentDictionary<string, string>();
public ConcurrentDictionary<string, List<string>> result = new ConcurrentDictionary<string, List<string>>();
private List<string> analyzed = new List<string>();
private Stopwatch timer = new Stopwatch();
private class Options
{
[Option('v', "verbose", Required = false, HelpText = "Set output to verbose messages.", Default = false)]
public bool Verbose { get; set; }
[Option('t', "target", Required = true, HelpText = "Set target host.")]
public string Target { get; set; }
[Option('s', "scope", Required = true, HelpText = "Allowed domain scope, use ; as delimiter.")]
public string Scope { get; set; }
[Option('a', "agent", Required = false, HelpText = "Set custom user agent.", Default = "Mozilla/5.0 (Windows; U; Windows NT 6.2) AppleWebKit/534.2.1 (KHTML, like Gecko) Chrome/35.0.822.0 Safari/534.2.1")]
public string Agent { get; set; }
[Option('p', "pattern", Required = true, HelpText = "Regex pattern to scrape with.")]
public string Pattern { get; set; }
[Option('c', "crawlers", Required = false, HelpText = "Total concurrent tasks used for the Crawler.", Default = 4)]
public int CCrawlers { get; set; }
[Option('x', "scrapers", Required = false, HelpText = "Total concurrent tasks used for the Scraper.", Default = 4)]
public int Cscrapers { get; set; }
[Option('b', "downloaders", Required = false, HelpText = "Total concurrent downloaders used for downloading data.", Default = 2)]
public int Cdownloaders { get; set; }
[Option('q', "queryparameters", Required = false, HelpText = "Strip query parameters from URL(s).", Default = false)]
public bool StripQueryParams { get; set; }
[Option('d', "download", Required = false, HelpText = "Download found files.", Default = false)]
public bool DownloadFiles { get; set; }
[Option('j', "json", Required = false, HelpText = "Generates output based on the pattern provided.", Default = false)]
public bool GenerateOutput { get; set; }
[Option('f', "filename", Required = false, HelpText = "The file name of the generated output.", Default = "result.json")]
public string Filename { get; set; }
[Option('k', "checkpoints", Required = false, HelpText = "Saves in between scraping pages, turn off to save time, might fail.", Default = false)]
public bool Checkpoints { get; set; }
}
public static void Main(string[] args)
{// Entrypoint
credits();
new CLI(args);
}
private static void credits() {
Console.WriteLine("");
Console.WriteLine(" ▄ ▄ ██▄ ▄███▄ ▄████ ▄█ ▄ ▄███▄ ██▄ ▄▄▄▄▄ ▄█▄ █▄▄▄▄ ██ █ ▄▄ ▄███▄ █▄▄▄▄ ");
Console.WriteLine(" █ █ █ █ █▀ ▀ █▀ ▀ ██ █ █▀ ▀ █ █ █ ▀▄ █▀ ▀▄ █ ▄▀ █ █ █ █ █▀ ▀ █ ▄▀ ");
Console.WriteLine("█ █ ██ █ █ █ ██▄▄ █▀▀ ██ ██ █ ██▄▄ █ █ ▄ ▀▀▀▀▄ █ ▀ █▀▀▌ █▄▄█ █▀▀▀ ██▄▄ █▀▀▌ ");
Console.WriteLine("█ █ █ █ █ █ █ █▄ ▄▀ █ ▐█ █ █ █ █▄ ▄▀ █ █ ▀▄▄▄▄▀ █▄ ▄▀ █ █ █ █ █ █▄ ▄▀ █ █ ");
Console.WriteLine("█▄ ▄█ █ █ █ ███▀ ▀███▀ █ ▐ █ █ █ ▀███▀ ███▀ ▀███▀ █ █ █ ▀███▀ █ ");
Console.WriteLine(" ▀▀▀ █ ██ ▀ █ ██ ▀ █ ▀ ▀ ");
Console.WriteLine(" ▀ ");
Console.WriteLine("");
}
public static void WrapperEntrypoint(bool verbose, string target, string scope, string agent, string pattern, int crawlers, int scrapers, int downloaders, bool queryparameters, bool download, bool json, string filename, bool checkpoints)
{// Wrapper Entrypoint
credits();
string inputvalues = "";
if (verbose) { inputvalues += "-v "; }
inputvalues += $"-t {target} ";
inputvalues += $"-s {scope} ";
inputvalues += $"-a {agent} ";
inputvalues += $"-p {pattern} ";
inputvalues += $"-c {crawlers} ";
inputvalues += $"-x {scrapers} ";
inputvalues += $"-b {downloaders} ";
if (queryparameters) { inputvalues += $"-q "; }
if (download) { inputvalues += $"-d "; }
if (json) { inputvalues += $"-j ";}
if (filename != null) { inputvalues += $"-f {filename} "; }
if (checkpoints) { inputvalues += $"-k "; }
new CLI(inputvalues.Split(" "));
}
private CLI(string[] args)
{
Parser.Default.ParseArguments<Options>(args).WithParsed<Options>(
o =>
{
QueueCrawler.Enqueue(o.Target);
debug = o.Verbose;
root_url = o.Target;
user_agent = o.Agent;
scopes = o.Scope.Split(';').ToList();
regex_pattern = o.Pattern;
maxConcurrentCrawlers = o.CCrawlers;
maxConcurrentScrapers = o.Cscrapers;
maxConcurrentDownloaders = o.Cdownloaders;
stripQueryParms = o.StripQueryParams;
Download = o.DownloadFiles;
GenerateOutput = o.GenerateOutput;
Checkpoints = o.Checkpoints;
Filename = o.Filename;
}
);
if (args.Count() <= 0)
{
return;
}
isRunning = true;
ThreadCrawler = new Thread(Crawler);
ThreadScraper = new Thread(Scraper);
ThreadDownloader = new Thread(Downloader);
Console.WriteLine($"| Verbose: {debug} | Target: {root_url} | ");
Console.WriteLine("| Configured scope ->");
foreach (string scope in scopes)
{
Console.WriteLine($"| - {scope}");
}
Console.WriteLine();
Console.WriteLine("Processing ...");
timer.Start();
Start();
Console.WriteLine($"Main method completed, took: {timer.Elapsed}");
}
private MatchCollection regex(string pattern, string data)
{// Executes regex <pattern> on provided <data>
Regex rx = new Regex(pattern, RegexOptions.Compiled | RegexOptions.IgnoreCase);
MatchCollection matches = rx.Matches(data);
return matches;
}
private string request(string url)
{// Makes requests to <url> and read content of the source code which will be returned
HttpWebRequest theRequest = (HttpWebRequest)WebRequest.Create(url);
theRequest.Headers["user-agent"] = user_agent;
theRequest.Method = "GET";
try
{
WebResponse theResponse = theRequest.GetResponse();
StreamReader sr = new StreamReader(theResponse.GetResponseStream(), System.Text.Encoding.UTF8);
string result = sr.ReadToEnd();
sr.Close();
theResponse.Close();
return result;
}
catch (WebException)
{
return "";
}
}
private string check_url(string uri) {
string url = uri;
if (!url.StartsWith(root_url))
{
if (root_url.EndsWith("/") && url.StartsWith("/"))
{
url = root_url.Remove(root_url.Length - 1, 1) + url;
}
else if (url.StartsWith("/"))
{
url = root_url + url;
}
}
url = url.Replace("&", "&").Replace("&", "&");
return url;
}
public void Start()
{
Task _crawler = new Task(() => { ThreadCrawler.Start(); });
_crawler.Start();
Task _scraper = new Task(() => { ThreadScraper.Start(); });
_scraper.Start();
if (Download)
{
System.IO.Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + "Downloads");
Task _downloader = new Task(() => { ThreadDownloader.Start(); });
_downloader.Start();
Task.WaitAll(_crawler, _scraper, _downloader);
}
else {
Task.WaitAll(_crawler, _scraper);
}
}
private void Crawler() {
Parallel.ForEach(ParallelExtention.GetParrallelConsumingEnumerable(QueueCrawler), new ParallelOptions { MaxDegreeOfParallelism = maxConcurrentCrawlers }, Items =>
{
foreach (string uri in Items)
{
// Obtain raw HTML and temporarly store the data
string response = "";
if (scopes.Any(s => uri.StartsWith(s) | uri.StartsWith(root_url)))
{
string url = check_url(uri);
if (stripQueryParms && url.Contains("?")) {
url = url.Split("?")[0];
}
response = request(url);
MemoryQueue.TryAdd(url, response);
QueueScraper.Enqueue(url);
}
// Look for all available hrefs inside the HTML
MatchCollection matches = regex(@"href\s*=\s*(?:[""'](?<1>[^""']*)[""']|(?<1>[^>\s]+))", response);
foreach (Match match in matches)
{
string m = match.Groups[1].Value.Split("\"")[0];
if (!analyzed.Contains(m)) {
if (debug)
{
Console.WriteLine($"| ANALYZING: {m}");
}
analyzed.Add(m);
if (scopes.Any(s => m.StartsWith(s))) {
QueueCrawler.Enqueue(m);
}
}
}
Console.Clear();
Console.WriteLine($"|+|Undeƒined Scraper");
Console.WriteLine($"| Analyzed: {analyzed.Count()}");
Console.WriteLine($"| Total in queue: {QueueCrawler.Count()}");
Console.WriteLine($"| Ready for scraping: {QueueScraper.Count()}");
Console.WriteLine($"| Total Scraped: {result.Count()}");
Console.WriteLine($"| Ready for download: {QueueDownloads.Count()}");
Console.WriteLine($"| Total Downloads: {total_downloads}");
Console.WriteLine($"| Running time: {timer.Elapsed}");
}
Console.WriteLine($"| Crawler Finished: {timer.Elapsed}");
});
}
private void Scraper()
{
while (QueueScraper.Count() > 0 || QueueCrawler.Count() > 0 || ThreadCrawler.IsAlive)
{
Parallel.ForEach(
ParallelExtention.GetParrallelConsumingEnumerable(QueueScraper),
new ParallelOptions { MaxDegreeOfParallelism = maxConcurrentScrapers },
Items =>
{
foreach (var target in Items)
{
string content;
MemoryQueue.TryRemove(target, out content);
if (debug)
{
Console.WriteLine($"| SCRAPING: {target}");
}
if (content != null) {
MatchCollection matches = regex(regex_pattern, content);
List<string> converted_matches = new List<string>();
foreach (string uri in matches.Cast<Match>().Select(m => m.Value).ToArray()) { converted_matches.Add(uri); }
result.TryAdd(target, converted_matches);
MatchCollection images = regex(@"<img\b[^\<\>]+?\bsrc\s*=\s*[""'](?<L>.+?)[""'][^\<\>]*?\>", content);
List<string> converted_images = new List<string>();
foreach (Match uri in images) {
string url = check_url(uri.Groups[1].Value.Split("\"")[0]);
QueueDownloads.Enqueue(url);
}
}
}
});
if (Checkpoints && GenerateOutput)
{
File.WriteAllText(AppDomain.CurrentDomain.BaseDirectory + @$"\{Filename}", JsonConvert.SerializeObject(result));
}
}
if (GenerateOutput) {
File.WriteAllText(AppDomain.CurrentDomain.BaseDirectory + @$"\{Filename}", JsonConvert.SerializeObject(result));
}
isRunning = false;
Console.WriteLine($"| Finished!");
}
private void Downloader()
{
while (QueueScraper.Count() > 0 || QueueCrawler.Count() > 0 || ThreadCrawler.IsAlive || ThreadScraper.IsAlive)
{
Parallel.ForEach(
ParallelExtention.GetParrallelConsumingEnumerable(QueueDownloads),
new ParallelOptions { MaxDegreeOfParallelism = maxConcurrentDownloaders },
Items =>
{
foreach (string target in Items)
{
if (debug) {
Console.WriteLine($"| DOWNLOADING: {target}");
}
using (WebClient client = new WebClient())
{
client.Headers["user-agent"] = user_agent;
MatchCollection filename_match = regex(@"((.+\\)*(.+)\..{1,3})", target);
string filename = filename_match[0].Value.Split("/")[filename_match[0].Value.Split("/").Count() - 1];
string target_output_folder = AppDomain.CurrentDomain.BaseDirectory + @$"\Downloads\{filename}";
if (!File.Exists(target_output_folder)) {
client.DownloadFile(new Uri(target), target_output_folder);
total_downloads += 1;
}
}
}
});
}
}
}
}