/// <summary> /// Обновляет список популярных фильмов /// </summary> public static void UpdateRandomTV() { while (true) { Random random = new Random(); int filmYearBottomLine = random.Next(1950, DateTime.Now.Year - 5); string[] order = new string[] { "YEAR", "RATING", "NUM_VOTE" }; var client = new RestClient("https://kinopoiskapiunofficial.tech/api/v2.1/films/search-by-filters"); var request = new RestRequest(Method.GET); request.AddHeader("X-API-KEY", Bot._kp_key); request.AddQueryParameter("type", "TV_SHOW"); request.AddQueryParameter("order", order[random.Next(0, order.Length)]); request.AddQueryParameter("genre", Film.PopularGenres[random.Next(0, Film.PopularGenres.Length)].ToString()); request.AddQueryParameter("yearFrom", filmYearBottomLine.ToString()); IRestResponse response = client.Execute(request); List <RandomTV.Film> results; try { results = JsonConvert.DeserializeObject <RandomTV.Results>(response.Content).films; } catch (Exception) { results = null; } if (results == null || results.Count == 0) { continue; } var dict = new Dictionary <int, RandomTV.Film>(); for (int i = 0; i < results.Count; ++i) { string photoID2; results[i].VKPhotoID = Attachments.RandomTVPosterID(results[i], out photoID2); results[i].VKPhotoID_2 = photoID2; if (results[i].VKPhotoID == null || results[i].VKPhotoID_2 == null) { continue; } dict[results[i].filmId] = results[i]; } RandomTV = dict; return; } }
public void createPostersFacts(User user, string filmID) { IsTrailer = false; var client1 = new RestClient("https://kinopoiskapiunofficial.tech/api/v2.1/films/" + filmID); var request1 = new RestRequest(Method.GET); request1.AddHeader("X-API-KEY", Bot._kp_key); IRestResponse response1 = client1.Execute(request1); Film.FilmObject film; try { film = JsonConvert.DeserializeObject <Film.FilmObject>(response1.Content); } catch (Exception) { film = null; } if (film != null) { var name = film.data.nameRu ?? film.data.nameEn; var year = film.data.year; var client2 = new RestClient($"https://kinopoiskapiunofficial.tech/api/v2.1/films/{filmID}/frames"); var request2 = new RestRequest(Method.GET); request2.AddHeader("X-API-KEY", Bot._kp_key); IRestResponse response2 = client2.Execute(request2); Frames frames; try { frames = JsonConvert.DeserializeObject <Frames>(response2.Content); } catch (Exception) { frames = null; } if (frames != null && frames.frames != null && frames.frames.Count != 0) { var posters = new List <Photo>(); IEnumerable <string> links = frames.frames.Select(f => f.image).Shuffle().Take(Math.Min(5, frames.frames.Count)); foreach (var link in links) { if (Attachments.PosterObject(user, link, filmID, out Photo photo)) { posters.Add(photo); } } if (posters.Count != 0) { Posters = posters; IsValid = true; } else { return; } } else { return; } if (film.data.facts.Count != 0) { Facts = film.data.facts.Shuffle().Take(Math.Min(3, film.data.facts.Count)).ToList(); } var soundtrack = new List <Audio>(); if (film.data.type == "FILM") { string fname, addition; if (film.data.nameEn != null && film.data.nameEn != string.Empty) { fname = film.data.nameEn; addition = "ost"; } else { fname = film.data.nameRu; addition = "саундтрек"; } //addition = year; Film.Methods.DownloadSoundtrack(fname, addition, soundtrack, 2); } else if (film.data.type == "TV_SHOW") { string fname, addition; if (film.data.nameEn != null && film.data.nameEn != string.Empty) { fname = film.data.nameEn; addition = "ost"; } else { fname = film.data.nameRu; addition = "саундтрек"; } //addition = year; TV.Methods.DownloadSoundtrack(fname, addition, soundtrack, 2); } id = filmID; Name = name; Year = year; SoundTrack = soundtrack; } }
public void createPostersFacts(Film.FilmObject film) { IsTrailer = false; var name = film.data.nameRu ?? film.data.nameEn; var year = film.data.year; var client2 = new RestClient($"https://kinopoiskapiunofficial.tech/api/v2.1/films/{film.data.filmId}/frames"); var request2 = new RestRequest(Method.GET); request2.AddHeader("X-API-KEY", Bot._kp_key); IRestResponse response2 = client2.Execute(request2); Frames frames; try { frames = JsonConvert.DeserializeObject <Frames>(response2.Content); } catch (Exception) { frames = null; } if (frames != null && frames.frames != null && frames.frames.Count != 0) { var posters = new List <string>(); IEnumerable <string> links = frames.frames.Select(f => f.image).Shuffle().Take(Math.Min(5, frames.frames.Count)); foreach (var link in links) { var id = Attachments.PosterObject(link, film.data.filmId.ToString()); if (id != null) { posters.Add(id); } } if (posters.Count != 0) { PostersIds = posters; IsValid = true; } else { return; } } else { return; } if (film.data.facts.Count != 0) { Facts = film.data.facts.Shuffle().Take(Math.Min(3, film.data.facts.Count)).ToList(); } /*var soundtrack = new List<Audio>(); * if (film.data.type == "FILM") * { * string fname, addition; * if (film.data.nameEn != null) * { * fname = film.data.nameEn; * //addition = "ost"; * } * else * { * fname = film.data.nameRu; * //addition = "саундтрек"; * } * addition = year; * Film.Methods.DownloadSoundtrack(fname, addition, soundtrack, 2); * } * else if (film.data.type == "TV_SHOW") * { * string fname, addition; * if (film.data.nameEn != null) * { * fname = film.data.nameEn; * addition = "series"; * } * else * { * fname = film.data.nameRu; * addition = "сериал"; * } * TV.Methods.DownloadSoundtrack(fname, addition, soundtrack, 2); * }*/ id = film.data.filmId.ToString(); Name = name; Year = year; SoundTrack = null; }
/// <summary> /// Обновляет список популярных сериалов /// </summary> public static void UpdatePopularTV() { var res = new Dictionary <int, TVObject>(); //добавление первой страницы сериалов var clientA = new RestSharp.RestClient("https://api.tmdb.org/3/tv/popular"); var requestA = new RestRequest(Method.GET); requestA.AddQueryParameter("api_key", Bot._mdb_key); requestA.AddQueryParameter("page", "1"); IRestResponse responseA = clientA.Execute(requestA); MDBResultsTV deserializedA; try { deserializedA = JsonConvert.DeserializeObject <MDBResultsTV>(responseA.Content); } catch (Exception) { return; } if (deserializedA == null || deserializedA.total_pages == 0) { return; } var list = deserializedA.results; //добавление второй страницы сериалов var clientB = new RestSharp.RestClient("https://api.tmdb.org/3/tv/popular"); var requestB = new RestRequest(Method.GET); requestB.AddQueryParameter("api_key", Bot._mdb_key); requestB.AddQueryParameter("page", "2"); IRestResponse responseB = clientB.Execute(requestB); MDBResultsTV deserializedB; try { deserializedB = JsonConvert.DeserializeObject <MDBResultsTV>(responseB.Content); } catch (Exception) { deserializedB = null; } if (deserializedB != null && deserializedB.total_pages != 0) { //объединение двух списков-страниц в один список list.AddRange(deserializedB.results); } //параллельный обход списка foreach (var result in list) { //запрос сериала по его названию var KPclient1 = new RestSharp.RestClient("https://kinopoiskapiunofficial.tech/api/v2.1/films/search-by-keyword"); var KPrequest1 = new RestRequest(Method.GET); KPrequest1.AddHeader("X-API-KEY", Bot._kp_key); KPrequest1.AddHeader("accept", "application/json"); KPrequest1.AddQueryParameter("keyword", result.original_name); var KPresponse1 = KPclient1.Execute(KPrequest1); TVResults.Results deserialized; try { deserialized = JsonConvert.DeserializeObject <TVResults.Results>(KPresponse1.Content); } catch (Exception) { deserialized = null; } //проверка успешности десериализации if (deserialized != null && deserialized.pagesCount > 0) { //выбор сериала из результатов ответа на запрос, такого, что это сериал/мини-сериал и что его еще нет в результирующем словаре int id = 0; foreach (var f in deserialized.films) { if (f.nameRu.EndsWith("(сериал)") || f.nameRu.EndsWith("(мини-сериал)")) { id = f.filmId; break; } } if (id != 0 && !res.ContainsKey(id)) { //запрос выбранного сериала по его ID var KPclient2 = new RestSharp.RestClient($"https://kinopoiskapiunofficial.tech/api/v2.1/films/{id}"); var KPrequest2 = new RestRequest(Method.GET); KPrequest2.AddHeader("X-API-KEY", Bot._kp_key); KPrequest2.AddHeader("accept", "application/json"); KPrequest2.AddQueryParameter("append_to_response", "RATING"); var KPresponse2 = KPclient2.Execute(KPrequest2); TV.TVObject film; try { film = JsonConvert.DeserializeObject <TV.TVObject>(KPresponse2.Content); } catch (Exception) { film = null; } if (film != null) { film.Priority = 1; string photoID2; //Video trailer = null; film.data.VKPhotoID = Attachments.PopularTVPosterID(film, out photoID2); //ID Не обрезанного постера film.data.VKPhotoID_2 = photoID2; //проверка валидности загруженной фотографии if (film.data.VKPhotoID != null && film.data.VKPhotoID_2 != null) { //Methods.GetTrailer(film.data.filmId, ref trailer); //film.TrailerInfo = trailer; res[id] = film; } } } } } PopularTV = res; }
/*public static MessageTemplate ActorMessage(string filmID) * { * var proverka = Film.Methods.Actors(filmID); * if (proverka != null) * return Keyboards.ActorResultsTV(proverka.Take(Math.Min(5, proverka.Count))); * else return null; * }*/ public static string ActorDescriptionMessageTV(User user, string personId, out IEnumerable <MediaAttachment> attachments) { var ActInf = Film.Methods.ActorInfo(personId); attachments = new List <MediaAttachment> { Attachments.PosterObject(user, ActInf.posterUrl, personId) }; string ActorInfoObj = ""; if (ActInf.hasAwards == 1) { ActorInfoObj = "🏆 "; } if (ActInf.nameRu == null || ActInf.nameRu == string.Empty) { ActorInfoObj += ActInf.nameEn + "\n"; } else if (ActInf.nameEn == null || ActInf.nameEn == string.Empty) { ActorInfoObj += ActInf.nameRu + "\n"; } else if (ActInf.nameRu != null && ActInf.nameEn != null && ActInf.nameRu != string.Empty && ActInf.nameEn != string.Empty) { ActorInfoObj += ActInf.nameRu + "/" + ActInf.nameEn + "\n\n"; } if (ActInf.growth != null && ActInf.growth != "0") { ActorInfoObj += "🕺Рост: " + ActInf.growth + "\n"; } if (ActInf.birthday != null && ActInf.birthday != string.Empty) { ActorInfoObj += "👶День рождения: " + Film.Methods.ChangeDateType(ActInf.birthday) + "\n"; } if (ActInf.death != null && ActInf.death != string.Empty) { ActorInfoObj += "💀Дата смерти: " + Film.Methods.ChangeDateType(ActInf.death) + "\n"; } if (ActInf.age != 0) { ActorInfoObj += "⏰Возраст: " + ActInf.age + "\n\n"; } if (ActInf.facts != null && ActInf.facts.Count != 0) { ActorInfoObj += "✨Интересные факты\n" + string.Join("\n", ActInf.facts.Take(Math.Min(3, ActInf.facts.Count)).Select(f => $"✔ {f}")) + "\n"; } ActorInfoObj += "\n📽Фильмография\n"; int i = 0; var l = new List <int>(); foreach (var item in ActInf.films) { if (i == 10) { break; } if (l.Contains(item.filmId)) { continue; } if (item.nameRu == null) { ActorInfoObj += item.nameEn; } else { ActorInfoObj += item.nameRu; } if (item.rating != null) { var m = Regex.Match(item.rating, @"\b(\d)\.(\d)\b"); if (m.Success && m.Groups[1].Value != "0" && m.Groups[2].Value != "0") { ActorInfoObj += " ⭐" + item.rating; } } ActorInfoObj += "\n"; i++; l.Add(item.filmId); } return(ActorInfoObj); }