/// <summary> /// Возвращает список фотографий в альбоме. /// </summary> /// <param name="album">Альбом, содержащий фотографии</param> /// <returns>Список фотографий альбома</returns> public List<VkPhoto> GetAlbumPhotos(VkAlbum album) { var parameters = new Dictionary<string, object> {{"oid", album.OwnerId}, {"aid", album.Id}, {"extended", 1}}; var cmd = GetVkXmlCommand("photos.get", parameters, false); string response; try { response = cmd.GetResponseAsString(); } catch (Exception e) { Logger.ErrorFormat("({0}) {1}", e.GetType().Name, e.Message); throw new VkMethodInvocationException((int) VkMethodInvocationErrorCode.FailedGetResponse, "Не удалось получить ответ от сервера"); } var xml = new XmlDocument(); try { xml.LoadXml(response); } catch (Exception ex) { Logger.ErrorFormat("XML parsing failed. ({0}) {1}", ex.GetType().Name, ex.Message); Logger.Debug(response); throw new VkMethodInvocationException((int) VkMethodInvocationErrorCode.IncorrectReplyFormat, "Сервер вернул некорректный ответ"); } var result = new List<VkPhoto>(0); foreach (XmlElement photoXml in xml.DocumentElement.GetElementsByTagName("photo")) { Int64 pid; try { pid = Int64.Parse(photoXml["pid"].InnerText); } catch { Logger.ErrorFormat("Cant parse 'pid' property of photo : {0}",photoXml["pid"].InnerText); continue; } var g = new VkPhoto(pid); if (photoXml["owner_id"] != null) { try { g.OwnerId = Int32.Parse(photoXml["owner_id"].InnerText); } catch { Logger.DebugFormat("Cant parse 'owner_id' property of photo '{0}' : {1}", g.Id, photoXml["owner_id"].InnerText); g.OwnerId = 0; } } if (photoXml["comments"] != null && photoXml["comments"]["count"] != null) { try { g.CommentsCount = Int32.Parse(photoXml["comments"]["count"].InnerText); } catch { Logger.DebugFormat("Cant parse 'comments.count' property of photo '{0}' : {1}", g.Id, photoXml["comments"]["count"].InnerText); g.CommentsCount = 0; } } if (photoXml["src"] != null) { g.SourceUrl = photoXml["src"].InnerText; } if (photoXml["text"] != null) { g.Text = photoXml["text"].InnerText; } if (photoXml["created"] != null) { try { g.CreationDate = GetDateTimeFromUnix(Int32.Parse(photoXml["created"].InnerText)); } catch { Logger.DebugFormat("Cant parse 'created' property of photo '{0}' : {1}", g.Id, photoXml["created"].InnerText); g.CreationDate = DateTime.MinValue; } } result.Add(g); } return result; }
private static void Main(string[] args) { const string accessToken = ""; var album = new VkAlbum(171831917) { OwnerId = Int32.Parse("-51777326") }; var photosMgr = new PhotosManager(accessToken); var um = new UsersManager(accessToken); var photos = photosMgr.GetAlbumPhotos(album); var sb = new StringBuilder(); var dic = new Dictionary<Int64, List<VkPhoto>>(); var comments = new Dictionary<VkPhoto, List<VkComment>>(); var usersCache = new Dictionary<Int64, VkUser>(); foreach (VkPhoto vkPhoto in photos) { if (vkPhoto.CommentsCount == 0) continue; sb.AppendLine(String.Format("({0}) {1}:", vkPhoto.Id, vkPhoto.Text)); Console.WriteLine("Looking in " + vkPhoto.Id); comments.Add(vkPhoto, new List<VkComment>()); foreach (var comment in photosMgr.GetPhotoComments(vkPhoto)) { if (!usersCache.ContainsKey(comment.SenderId)) { var sender = um.GetUserById(comment.SenderId); usersCache.Add(comment.SenderId, sender); } sb.AppendLine(String.Format("\t[{0}]: {1}", usersCache[comment.SenderId].GetFullName(), comment.Message)); comments[vkPhoto].Add(comment); if (!dic.ContainsKey(comment.SenderId)) { dic.Add(comment.SenderId, new List<VkPhoto>()); } if (!dic[comment.SenderId].Contains(vkPhoto)) dic[comment.SenderId].Add(vkPhoto); } sb.AppendLine(); } sb.AppendLine(); foreach (KeyValuePair<Int64, List<VkPhoto>> pair in dic) { VkUser user = usersCache[pair.Key]; sb.AppendLine(user.GetFullName() + ":"); foreach (VkPhoto photo in pair.Value) { sb.AppendLine(String.Format("\t{0}", photo.Text)); foreach (KeyValuePair<VkPhoto, List<VkComment>> valuePair in comments) { if (valuePair.Key.Id != photo.Id) continue; foreach (VkComment vkComment in valuePair.Value) { if (vkComment.SenderId != user.Id) continue; sb.AppendLine(String.Format("\t\t[{0}]: {1}", vkComment.Date, vkComment.Message)); } } } sb.AppendLine(); } File.WriteAllText(@"f:\temp\result1.txt", sb.ToString(), Encoding.UTF8); }
/// <summary> /// Возвращает список альбомов в группе пользователя. /// </summary> /// <param name="group">Группа альбомы которой запрашиваются</param> /// <returns></returns> public List<VkAlbum> GetAlbumsForGroup(VkGroup group) { var parameters = new Dictionary<string, object> { { "gid", group.Id } }; var cmd = GetVkXmlCommand("photos.getAlbums", parameters, false); string response; try { response = cmd.GetResponseAsString(); } catch (Exception e) { Logger.ErrorFormat("({0}) {1}", e.GetType().Name, e.Message); throw new VkMethodInvocationException((int) VkMethodInvocationErrorCode.FailedGetResponse, "Не удалось получить ответ от сервера"); } var xml = new XmlDocument(); try { xml.LoadXml(response); } catch (Exception ex) { Logger.ErrorFormat("XML parsing failed. ({0}) {1}", ex.GetType().Name, ex.Message); Logger.Debug(response); throw new VkMethodInvocationException((int)VkMethodInvocationErrorCode.IncorrectReplyFormat, "Сервер вернул некорректный ответ"); } if (VkErrorResponse.HasErrorInResponse(xml)) { var err = VkErrorResponse.Parse(xml); Logger.DebugFormat("VK Error ({0}) : {1}", err.ErrorCode, err.Message); throw new VkMethodInvocationException(err.ErrorCode, err.Message); } var result = new List<VkAlbum>(); foreach (XmlElement albumXml in xml.DocumentElement.GetElementsByTagName("album")) { long aid; try { aid = Int64.Parse(albumXml["aid"].InnerText); } catch (Exception e) { Logger.ErrorFormat("Cant parse album ID. ({0}) {1}", e.GetType().Name, e.Message); continue; } var g = new VkAlbum(aid); if (albumXml["created"] != null) { try { g.CreationDate = GetDateTimeFromUnix(Int32.Parse(albumXml["created"].InnerText)); } catch { Logger.DebugFormat("Cant parse 'created' property of album '{0}' : {1}", g.Id, albumXml["created"].InnerText); } } if (albumXml["updated"] != null) { try { g.UpdateDate = GetDateTimeFromUnix(Int32.Parse(albumXml["updated"].InnerText)); } catch { Logger.DebugFormat("Cant parse 'updated' property of album '{0}' : {1}", g.Id, albumXml["updated"].InnerText); } } if (albumXml["size"] != null) { try { g.Size = Int32.Parse(albumXml["size"].InnerText); } catch { Logger.DebugFormat("Cant parse 'size' property of album '{0}' : {1}", g.Id, albumXml["size"].InnerText); } } if (albumXml["owner_id"] != null) { try { g.OwnerId = Int32.Parse(albumXml["owner_id"].InnerText); } catch { Logger.DebugFormat("Cant parse 'owner_id' property of album '{0}' : {1}", g.Id, albumXml["owner_id"].InnerText); } } if (albumXml["title"] != null) { g.Title = albumXml["title"].InnerText; } if (albumXml["description"] != null) { g.Description = albumXml["description"].InnerText; } if (albumXml["thumb_src"] != null) { g.ThumbUrl = albumXml["thumb_src"].InnerText; } result.Add(g); } return result; }