-
Notifications
You must be signed in to change notification settings - Fork 3
/
SearchForm.cs
230 lines (206 loc) · 6.05 KB
/
SearchForm.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
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using System.Web;
using System.Windows.Forms;
using AngleSharp.Dom;
using AngleSharp.Html.Parser;
using KhinsiderDownloader.Properties;
namespace KhinsiderDownloader
{
public partial class SearchForm : Form
{
static string urlPrefix = "https://downloads.khinsider.com";
Color SearchBarDefaultTextColor = Color.Black;
Color SearchBarPlaceholderTextColor = Color.Gray;
bool IsPlaceholderText = true;
WebClient webClient;
public TextBox linkbox = null;
public SearchForm()
{
InitializeComponent();
webClient = new WebClient();
pic_album.Image = Resources.no_image;
SearchBarDefaultTextColor = txt_input.ForeColor;
SetPlaceholderText();
}
void SetPlaceholderText()
{
txt_input.ForeColor = SearchBarPlaceholderTextColor;
txt_input.Text = "Search...";
}
class SearchItem
{
public override string ToString()
{
return Name;
}
public string Name;
public string Url;
public Bitmap CoverImage;
public bool ImageResolved = false;
}
List<SearchItem> DoSearch(string query)
{
this.Invoke(new Action(() => { this.Cursor = Cursors.WaitCursor; }));
List<SearchItem> searchResult = new List<SearchItem>();
var parser = new HtmlParser();
var endpointURL = "https://downloads.khinsider.com/search?search=" + query + "&albumListSize=compact";
Downloader.HTMLResult downloadHtmlResult = Downloader.GetHTMLFromURL(endpointURL);
var htmlDocument = parser.ParseDocument(downloadHtmlResult.HTML);
//Get Album name
var albumNameNode = htmlDocument.All.FirstOrDefault(element => element.LocalName == "div" && element.Id == "pageContent");
if (albumNameNode == null)
{
return searchResult;
}
var searchresult = albumNameNode.Children[0].InnerHtml;
if (searchresult != "Search")
{
SearchItem searchItem = new SearchItem();
searchItem.Name = HttpUtility.HtmlDecode(albumNameNode.Children[1].InnerHtml);
searchItem.Url = downloadHtmlResult.ResponseURI;
searchResult.Add(searchItem);
this.Invoke(new Action(() => { this.Cursor = Cursors.Default; }));
return searchResult;
}
else
{
var albumlist = albumNameNode.Children[3];
if (albumlist.ChildElementCount == 0)
{
return searchResult;
}
var searchresults = albumlist.Children[1].Children[0].Children[0].Children.Skip(1);//Where(element=>element.LocalName == "a");
foreach (var node in searchresults)
{
var cellnode = node.Children[1].Children[0];
SearchItem searchItem = new SearchItem();
searchItem.Name = HttpUtility.HtmlDecode(cellnode.InnerHtml);
searchItem.Url = cellnode.Attributes["href"].Value;
searchResult.Add(searchItem);
}
}
return searchResult;
}
void SearchStub()
{
Task.Run(() =>
{
pic_album.Invoke(new Action(() => { pic_album.Image = null; }));
pic_album.Image = null;
var result = DoSearch(txt_input.Text);
this.Invoke(new Action(() => { this.Cursor = Cursors.Default; }));
list_result.Invoke(new Action(() => { list_result.Items.Clear(); }));
foreach (var item in result)
{
list_result.Invoke(new Action(() => { list_result.Items.Add(item); }));
}
});
}
private void btn_search_Click(object sender, EventArgs e)
{
SearchStub();
}
void ResolveImage(ref SearchItem searchItem)
{
searchItem.ImageResolved = true;
try
{
var parser = new HtmlParser();
var htmlPage = Downloader.GetHTMLFromURL(urlPrefix + searchItem.Url).HTML;
var htmlDocument = parser.ParseDocument(htmlPage);
var albumNameNode = htmlDocument.All.FirstOrDefault(element => element.ClassName == "albumImage");
var imagenode = albumNameNode.Children[0];
if (imagenode != null)
{
var imageURL = imagenode.GetAttribute("href");
using (MemoryStream stream = new MemoryStream(webClient.DownloadData(imageURL)))
{
searchItem.CoverImage = new Bitmap(Image.FromStream(stream));
}
}
ShowImage(searchItem.CoverImage);
}
catch (Exception)
{
}
}
void ShowImage(Bitmap bitmap)
{
if (bitmap == null)
{
pic_album.Invoke(new Action(() => { pic_album.Image = Properties.Resources.no_image; }));
}
else
{
pic_album.Invoke(new Action(() => { pic_album.Image = bitmap; }));
}
}
private void list_result_SelectedIndexChanged(object sender, EventArgs e)
{
if (sender == null)
{
return;
}
SearchItem currentItem = (SearchItem) list_result.SelectedItem;
if (currentItem == null)
{
return;
}
if (!currentItem.ImageResolved)
{
Task.Run(() => ResolveImage(ref currentItem));
}
else
{
ShowImage(currentItem.CoverImage);
}
}
private void btn_add_Click(object sender, EventArgs e)
{
if (list_result.SelectedItem == null)
{
return;
}
SearchItem currentItem = (SearchItem)list_result.SelectedItem;
linkbox.Text += urlPrefix + currentItem.Url + Environment.NewLine;
}
private void btn_add_all_Click(object sender, EventArgs e)
{
foreach (var item in list_result.Items)
{
SearchItem currentItem = (SearchItem)item;
linkbox.Text += urlPrefix + currentItem.Url + Environment.NewLine;
}
}
private void txt_input_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
SearchStub();
}
}
private void txt_input_Enter(object sender, EventArgs e)
{
if (IsPlaceholderText)
{
txt_input.ForeColor = SearchBarDefaultTextColor;
txt_input.Text = "";
IsPlaceholderText = false;
}
}
private void txt_input_Leave(object sender, EventArgs e)
{
if (!IsPlaceholderText && String.IsNullOrEmpty(txt_input.Text))
{
SetPlaceholderText();
IsPlaceholderText = true;
}
}
}
}