-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
166 lines (133 loc) · 5.98 KB
/
Program.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
using iTunesSearch.Library;
using iTunesSearch.Library.Models;
using System;
using System.Linq;
using System.Globalization;
using System.Net.Sockets;
using System.Net;
using System.Net.NetworkInformation;
using System.Timers;
using System.Threading.Tasks;
using System.Collections.Generic;
using Microsoft.AspNetCore.Http;
using System.Threading;
using System.Net.Http;
namespace ConsoleITunes
{
class Program
{
static async Task Main(string[] args)
{
Model db = new Model();
db.Database.EnsureCreated();
iTunesSearchManager search = new iTunesSearchManager();
string inputArtist = "";
do
{
try
{
Console.WriteLine("\tДавайте найдём Вашего артиста на iTunes:(наберите текст и нажмите Enter) ");
inputArtist = Console.ReadLine();
Console.WriteLine("\tпоиск...");
var artists = search.GetSongArtistsAsync(inputArtist).Result;
var artistsSortAsc = from artistsSort in artists.Artists
orderby artistsSort.ArtistName
select artistsSort;
int artistsCount = artistsSortAsc.Count();
if (artistsCount == 0) Console.WriteLine("\tiTunes поискал и не нашёл такого артиста! ");
else
{
Console.WriteLine("\tiTunes поискал и нашёл: ");
SeachArtist(artistsSortAsc, search, db);
}
}
catch (Exception e)
{
if (!CheckConnection())
{
Cache(inputArtist, db);
}
else
{
Console.WriteLine(e.Message);
continue;
}
}
} while (true);
}
static async void SeachArtist(IOrderedEnumerable<SongArtist> artistsSortAsc, iTunesSearchManager search, Model model)
{
int i = 1;
foreach (var artist in artistsSortAsc)
{
Console.WriteLine($"{i++} Артист: {artist.ArtistName}");
}
Console.WriteLine("\tВыберите Вашего артиста, указав порядковый номер из списка:(наберите текст и нажмите Enter)");
bool result = int.TryParse(Console.ReadLine(), out int numberArtist);
Console.WriteLine(" поиск...");
if (result == true)
{
long choiceArtistId = artistsSortAsc.ElementAt(numberArtist - 1).ArtistId;
string choiceArtistName = artistsSortAsc.ElementAt(numberArtist - 1).ArtistName;
try
{
var albums = search.GetAlbumsFromSongAsync(choiceArtistName).Result.Albums.OrderBy(a => a.CollectionName);
int y = 1;
DateTime dateTime;
var OldAlbums = model.Albums.Where(o => o.AlbumArtistID == choiceArtistId);
model.Albums.RemoveRange(OldAlbums);
Console.WriteLine($"\tНа сервере iTunes артист {choiceArtistName} представлен альбомами: ");
foreach (var album in albums)
{
DateTime.TryParse(album.ReleaseDate, out dateTime);
Console.WriteLine($"{y++} " +
$"{album.CollectionName}, дата релиза: {dateTime.ToShortDateString()}");
model.Albums.Add(new Album { AlbumArtistID = choiceArtistId, ArtistName = choiceArtistName.ToUpper(), AlbumName = album.ArtistName, DateRelise = dateTime });
}
model.SaveChanges();
}
catch (Exception e)
{
if (!CheckConnection())
{
Cache(choiceArtistName.ToUpper(), model);
}
else
{
Console.WriteLine(e.Message);
}
}
}
else
Console.WriteLine("\tВведите порядковый номер: число!");
}
static void Cache(string Artist, Model db)
{
string inputArtistUpper = Artist.ToUpper();
var albumsFromDB = db.Albums.Where(a => a.ArtistName == inputArtistUpper).ToList();
int o = 1;
foreach (var albumFromDB in albumsFromDB)
{
Console.WriteLine($"{o++} {albumFromDB.AlbumName}, дата релиза: {albumFromDB.DateRelise.ToShortDateString()}");
}
if (albumsFromDB.Count() == 0) Console.WriteLine(" Ничего не нашли!");
}
static bool CheckConnection()
{
HttpWebRequest req = WebRequest.Create("https://music.apple.com") as HttpWebRequest;
HttpWebResponse rsp;
try
{
rsp = req.GetResponse() as HttpWebResponse;
}
catch (WebException e)
{
if (e.Response is HttpWebResponse) rsp = e.Response as HttpWebResponse;
else rsp = null;
Console.WriteLine("\tСоединение с сервером iTunes потеряно..., но всё равно поищем его альбомы...");
}
if (rsp != null) return true;
else return false;
}
}
}