This repository has been archived by the owner on Jul 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSearch.cs
288 lines (260 loc) · 12.1 KB
/
Search.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using Gdk;
using Gtk;
using ManagedBass;
using Newtonsoft.Json.Linq;
using Pango;
using YandexMusicApi;
using Task = System.Threading.Tasks.Task;
using Thread = System.Threading.Thread;
using UI = Gtk.Builder.ObjectAttribute;
using Window = Gtk.Window;
namespace Yamux
{
class Search : Window
{
public delegate void LenghtTrack();
private static event LenghtTrack ChangeLengthTrack;
private static int durationTrack = 1;
public static string directLink;
[UI] private Spinner spinnerProgress = null;
[UI] private SearchEntry SearchMusic = null;
[UI] private Box SearchBox = null;
[UI] private Box ResultBox = null;
[UI] private Label IfNoResult = null;
[UI] private Image ImageSettings = null;
[UI] private Box informTrackBox = null;
[UI] private Box PlayerActionBox = null;
[UI] private Box PlayerMoreActionBox = null;
[UI] private Box PlayerBoxScale = null;
[UI] private Label PlayerNameArtist = null;
[UI] private Label PlayerTitleTrack = null;
[UI] private Image PlayerImage = null;
[UI] private Box SearchMusicBox = null;
public static List<string> uidPlaylist = new List<string>();
public static List<string> kindPlaylist = new List<string>();
public static bool SearchOrNot = true;
public static HScale PlayerScale = new HScale(0.0, 100, 1.0);
public static VBox ResultSearchBox = new VBox();
public Search() : this(YamuxWindow.YamuxWindowBuilder)
{
}
private Search(Builder builder) : base(builder.GetRawOwnedObject("MainWindow"))
{
builder.Autoconnect(this);
SearchMusic.SearchChanged += (object sender, EventArgs a) => { SearchChangedOutput(); };
}
private async void SearchChangedOutput()
{
string text = SearchMusic.Text;
JToken root = "";
await Task.Run(() =>
{
Thread.Sleep(2000);
if (text == SearchMusic.Text && !string.IsNullOrEmpty(SearchMusic.Text) && !string.IsNullOrEmpty(text))
{
spinnerProgress.Active = true;
JObject resultSearch = YandexMusicApi.Default.Search(text);
root = resultSearch.Last.Last.Root;
root = root.SelectToken("result");
spinnerProgress.Active = false;
}
});
ShowResultSearch(root, text);
}
private async void ShowResultSearch(JToken root, string text)
{
if (text == SearchMusic.Text && !string.IsNullOrEmpty(SearchMusic.Text) && !string.IsNullOrEmpty(text))
{
if (root.Count() > 6 && SearchOrNot)
{
spinnerProgress.Active = true;
SearchOrNot = false;
IfNoResult.Text = "";
PlayerNameArtist.Text = "";
PlayerTitleTrack.Text = "";
PlayerImage.Hide();
PlayerBoxScale.Hide();
PlayerActionBox.Hide();
YamuxWindow.LandingBox.Destroy();
YamuxWindow.LandingBox = new VBox();
ResultSearchBox.Destroy();
ResultSearchBox = new VBox();
ResultBox.Add(ResultSearchBox);
Dictionary<string, string> best = Yamux.GetBest(root);
List<Dictionary<string, List<string>>> all = new List<Dictionary<string, List<string>>>();
try {all.Add(Yamux.GetArtist(root)); } catch (NullReferenceException) { }
try { all.Add(Yamux.GetAlbums(root)); } catch (NullReferenceException) { }
try { all.Add(Yamux.GetTracks(root)); } catch (NullReferenceException) { }
try { all.Add(Yamux.GetPodcasts(root)); } catch (NullReferenceException){ }
try { all.Add(Yamux.GetPlaylists(root)); } catch (NullReferenceException) { }
int index = -1;
foreach (var i in all)
{
index++;
if (i["type"][0] == best["type"])
{
all.Move(index, 0);
break;
}
}
foreach (var i in all)
{
HBox box = new HBox();
ScrolledWindow scrolledWindow = new ScrolledWindow();
Viewport viewportWindow = new Viewport();
scrolledWindow.PropagateNaturalHeight = true;
await Task.Run(() =>
{
Console.WriteLine(i["type"][0]);
if (i["type"][0] != "playlist")
box = Yamux.CreateBoxResultSearch(i["name"], i["coverUri"], i["id"], i["type"][0]);
else
{
kindPlaylist = i["kind"];
uidPlaylist = i["uid"];
box = Yamux.CreateBoxResultSearch(i["name"], i["coverUri"], new List<string>(),
i["type"][0]);
}
box.Halign = Align.Start;
});
string typeResult = "";
switch (i["type"][0])
{
case "playlist": { typeResult = "Плейлисты"; break; }
case "album": { typeResult = "Альбомы"; break; }
case "podcast": { typeResult = "Выпуски подкастов"; break; }
case "track": { typeResult = "Треки"; break; }
case "artist": { typeResult = "Артисты"; break; }
}
Label trackLabel = new Label(typeResult);
FontDescription tpftrack = new FontDescription();
tpftrack.Size = 12288;
trackLabel.ModifyFont(tpftrack);
scrolledWindow.Add(viewportWindow);
viewportWindow.Add(box);
ResultSearchBox.Add(trackLabel);
ResultSearchBox.Add(scrolledWindow);
SearchBox.ShowAll();
ResultBox.ShowAll();
ResultSearchBox.ShowAll();
PlayerBoxScale.Hide();
PlayerActionBox.Hide();
}
foreach (Button i in Yamux.ListButtonPlay)
i.Clicked += PlayButtonClick;
SearchOrNot = true;
spinnerProgress.Active = false;
}
else
{
Console.WriteLine(root.Count());
PlayerNameArtist.Text = "";
PlayerTitleTrack.Text = "";
PlayerImage = new Image();
PlayerBoxScale.Hide();
PlayerActionBox.Hide();
ResultSearchBox.Destroy();
IfNoResult.Text = "Нет результата😢";
}
}
}
private async void PlayButtonClick(object sender, EventArgs a)
{
Player.PlayTrackOrPause = true;
Pixbuf playerPlayPixbuf = new Pixbuf("Svg/icons8-pause.png");
YamuxWindow.playPauseButton.Image = new Image(playerPlayPixbuf);
Player.trackIds = new List<string>();
Player.currentTrack = -1;
Button buttonPlay = (Button) sender;
JObject details = JObject.Parse(buttonPlay.Name);
PlayerTitleTrack.MaxWidthChars = 17;
PlayerNameArtist.MaxWidthChars = 17;
switch (details["type"].ToString())
{
case "track":
{
List<string> ids = new List<string>();
JToken informTrack = "{}";
ids.Add(details["id"].ToString());
await Task.Run(() => { informTrack = Track.GetInformTrack(ids)["result"]; });
PlayerTitleTrack.Text = informTrack[0]["title"].ToString();
PlayerNameArtist.Text = informTrack[0]["artists"][0]["name"].ToString();
Player.trackIds.Add(details["id"].ToString());
Player.PlayUrlFile();
break;
}
case "artist":
{
JToken informArtist = "{}";
JToken trackArtist = "{}";
await Task.Run(() => { informArtist = Artist.InformArtist(details["id"].ToString())["result"]; });
await Task.Run(() => { trackArtist = Artist.GetTrack(informArtist["artist"]["id"].ToString()); });
foreach (var i in trackArtist["result"]["tracks"])
{
Player.trackIds.Add(i["id"].ToString());
}
Player.PlayPlaylist();
break;
}
case "playlist":
{
JToken informPlaylist = "{}";
JToken trackPlaylist = "{}";
await Task.Run(() =>
{
informPlaylist =
Playlist.InformPlaylist(details["uid"].ToString(), details["kind"].ToString());
});
await Task.Run(() =>
{
trackPlaylist = Playlist.GetTrack(details["uid"].ToString(), details["kind"].ToString());
});
foreach (var i in trackPlaylist["result"]["tracks"]) { Player.trackIds.Add(i["id"].ToString()); }
Player.PlayPlaylist();
break;
}
case "album":
{
JToken informAlbum = "{}";
JToken trackAlbum = "{}";
await Task.Run(() => { informAlbum = Album.InformAlbum(details["id"].ToString()); });
await Task.Run(() => { trackAlbum = Album.GetTracks(details["id"].ToString()); });
foreach (var i in trackAlbum["result"]["volumes"][0]) { Player.trackIds.Add(i["id"].ToString()); Console.WriteLine(i["id"]);}
Player.PlayPlaylist();
break;
}
case "podcast":
{
List<string> ids = new List<string>();
JToken informPodcast = "{}";
ids.Add(details["id"].ToString());
await Task.Run(() => { informPodcast = Track.GetInformTrack(ids)["result"]; });
Player.trackIds.Add(details["id"].ToString());
Player.PlayPlaylist();
break;
}
}
PlayerScale.FillLevel = Player.GetLength();
ChangeLengthTrack += () => { PlayerScale.Value = durationTrack; };
}
public static void ClickPauseOrPlay(object sender, EventArgs a)
{
if (Player.PlayTrackOrPause)
{
Pixbuf PlayerPausePixbuf = new Pixbuf("Svg/icons8-play.png");
YamuxWindow.playPauseButton.Image = new Image(PlayerPausePixbuf);
}
else
{
Pixbuf playerPlayPixbuf = new Pixbuf("Svg/icons8-pause.png");
YamuxWindow.playPauseButton.Image = new Image(playerPlayPixbuf);
}
Player.PauseOrStartPlay();
}
}
}