-
Notifications
You must be signed in to change notification settings - Fork 0
/
Konachaner.cs
147 lines (114 loc) · 3.42 KB
/
Konachaner.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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
namespace Quickchan
{
public class Konachaner
{
#region Consts
const string UnsafePostsUrl = "http://konachan.com/post?page=";
const string SafePostsUrl = "http://konachan.net/post?page=";
#endregion
#region Properties
string PageUrl
{ get
{
if (SafeMode)
return Filters.Count > 0 ? SafePostsUrl + Page + "&tags=" + string.Join("+", Filters) :
SafePostsUrl + Page;
else
return Filters.Count > 0 ? UnsafePostsUrl + Page + "&tags=" + string.Join("+", Filters) :
UnsafePostsUrl + Page;
} }
public int Page
{
get { return _Page; }
set { _Page = value > 0 ? value : 1; }
}
public int PageCount
{
get { return _PageCount; }
set
{
if (_PageCount != value)
{
_PageCount = value;
if (OnGetPageCount != null)
OnGetPageCount();
}
}
}
#endregion
#region Variables
int _Page = 1;
int _PageCount = -1;
public bool SafeMode = true;
readonly List<string> Filters = new List<string>();
#endregion
#region Delegates and events
public delegate void EmptyDelegate();
public delegate void KonImgDelegate(KonImage konImg);
public event KonImgDelegate OnKonImg;
public event EmptyDelegate OnGetPageCount;
public event EmptyDelegate NoResults;
#endregion
#region Public methods
public void AddFilter(string filter) { Filters.Add(filter); }
public void DeleteFilter(string filter) { Filters.Remove(filter); }
public void ClearFilters() { Filters.Clear(); }
public void GetPreviews()
{
using (var wc = new WebClient())
{
wc.DownloadStringAsync(new Uri(PageUrl));
wc.DownloadStringCompleted += (s, e) => AnalyseHTML(e.Result);
}
}
#endregion
#region Private methods
void AnalyseHTML(string html)
{
const string s1 = "<ul id=\"post-list-posts\">";
const string s2 = "</ul>";
const string s3 = "<img src=\"";
const string s4 = "<a class=\"directlink largeimg\" href=\"";
const string s5 = "<a class=\"directlink smallimg\" href=\"";
const string pc1 = "</a> <a class=\"next_page\"";
if (!html.Contains(s4) && !html.Contains(s5))
{
NoResults();
return; // No images found
}
var pcspl = html
.Split(new String[] { pc1 }, StringSplitOptions.None)[0]
.Split('>');
int pc = PageCount;
int.TryParse(pcspl[pcspl.Length - 1], out pc);
PageCount = pc;
var images = html
.Split(new String[] { s1 }, StringSplitOptions.None)[1]
.Split(new String[] { s2 }, StringSplitOptions.None)[0]
.Trim()
.Split(new String[] { s3 }, StringSplitOptions.None);
var konimgs = new KonImage[images.Length - 1];
var wcs = new AsyncWebClient[images.Length - 1];
for (int i = 1; i < images.Length; i++)
{
var image = images[i].Split('"')[0];
var url = images[i].Contains(s4) ? images[i]
.Split(new String[] { s4 }, StringSplitOptions.None)[1]
.Split('"')[0]
: images[i]
.Split(new String[] { s5 }, StringSplitOptions.None)[1]
.Split('"')[0];
wcs[i - 1] = new AsyncWebClient(image, url, Page);
wcs[i - 1].NotifyDone += (d, f, p) =>
OnKonImg(new KonImage(d, f, p));
}
}
#endregion
}
}