-
Notifications
You must be signed in to change notification settings - Fork 2
/
Detector.cs
97 lines (90 loc) · 2.49 KB
/
Detector.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BooruDownloader
{
public abstract class engineBase
{
List<string> hostsContainer = new List<string>();
public string host
{
get
{
return hostsContainer[0];
}
set
{
hostsContainer.Add(value);
}
}
public abstract EngineBase GenEngine();
public bool CheckHost(string host)
{
return hostsContainer.Contains(host);
}
}
public class danEngine : engineBase
{
public danEngine()
{
host = "danbooru.donmai.us";
host = "safebooru.donmai.us";
}
public override EngineBase GenEngine()
{
return new DanEngine();
}
}
public class gelEngine : engineBase
{
public gelEngine()
{
host = "rule34.xxx";
host = "safebooru.org";
host = "gelbooru.com";
}
public override EngineBase GenEngine()
{
return new GelEngine();
}
}
class Detector
{
public static engineBase[] engines = new engineBase[] {
new gelEngine(), new danEngine()
};
public static EngineBase detectEngine(String url)
{
try
{
return engines.First(x => x.CheckHost(GetHost(url))).GenEngine();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
return null;
}
}
private static string GetHost(string uri)
{
for (int idx = 0; idx < uri.Length; idx++)
if (char.Equals(uri[idx], '/') && char.Equals(uri[idx + 1], '/'))
return LoopUntilSlash(uri, idx + 2);
return LoopUntilSlash(uri, 0);
}
private static string LoopUntilSlash(string l, int idx)
{
StringBuilder builder = new StringBuilder();
builder.Capacity = l.Length;
for (int i = idx; i < l.Length; i++)
{
if (!char.Equals(l[i], '/'))
builder.Append(l[i]);
else
return builder.ToString();
}
return builder.ToString();
}
}
}