public static TorrentFileInfo Parse(BDictionary dictionary) { if (dictionary == null) { throw new ArgumentNullException("dictionary"); } var file = new TorrentFileInfo(); foreach (var item in dictionary) { if (item.Key == null) { continue; } switch (item.Key.Value) { case "length": BInteger integer = item.Value as BInteger; if (integer != null) { file.Length = integer.Value; } break; case "path": BList listItems = item.Value as BList; if (listItems != null) { foreach (var listItem in listItems) { if (listItem != null) { file.Path.Add(listItem.ToString()); } } } break; } } return(file); }
public static TorrentInfo Parse(BDictionary dictionary) { Contract.Requires(dictionary != null); if (dictionary == null) { throw new ArgumentNullException("dictionary"); } var torrent = new TorrentInfo(); TorrentFileInfo singleFile = new TorrentFileInfo(); bool isSingleFile = false; foreach (var item in dictionary) { if (item.Key == null) { continue; } if (item.Key.Value == "announce") { if (item.Value != null) { torrent.Announce = item.Value.ToString(); } } else if (item.Key.Value == "created by") { if (item.Value != null) { torrent.CreatedBy = item.Value.ToString(); } } else if (item.Key.Value == "creation date") { BInteger integer = item.Value as BInteger; if (integer != null) { torrent.CreationDate = new DateTime(1970, 1, 1).AddSeconds(integer.Value); } } else if (item.Key.Value == "encoding") { if (item.Value != null) { torrent.Encoding = item.Value.ToString(); } } else if (item.Key.Value == "info") { BDictionary dict = item.Value as BDictionary; if (dict != null) { ParseInfo(torrent, singleFile, ref isSingleFile, item.Value as BDictionary); } } } if (isSingleFile) { if (singleFile.Path != null) { singleFile.Path.Add(torrent.Name); } torrent.Files.Add(singleFile); } return(torrent); }
private static void ParseInfo(TorrentInfo torrent, TorrentFileInfo singleFile, ref bool isSingleFile, BDictionary dictionary) { Contract.Requires(torrent != null); Contract.Requires(singleFile != null); Contract.Requires(dictionary != null); foreach (var info in dictionary) { if (info.Key == null) { continue; } if (info.Key.Value == "name") { if (info.Value != null) { torrent.Name = info.Value.ToString(); } } else if (info.Key.Value == "piece length") { BInteger integer = info.Value as BInteger; if (integer != null) { torrent.PieceLength = integer.Value; } } else if (info.Key.Value == "pieces") { if (info.Value != null) { torrent.Pieces = info.Value.ToString(); } } else if (info.Key.Value == "private") { BInteger integer = info.Value as BInteger; if (integer != null) { torrent.Private = integer.Value != 0; } } else if (info.Key.Value == "files") { BList files = info.Value as BList; if (files != null) { foreach (var file in files) { BDictionary dict = file as BDictionary; if (dict != null) { torrent.Files.Add(TorrentFileInfo.Parse(dict)); } } } } else if (info.Key.Value == "file-duration") { isSingleFile = true; BList items = info.Value as BList; if (items != null) { foreach (var item in items) { BInteger integer = item as BInteger; if (integer != null) { singleFile.Duration.Add(integer.Value); } } } } else if (info.Key.Value == "file-media") { isSingleFile = true; BList items = info.Value as BList; if (items != null) { foreach (var item in items) { BInteger integer = item as BInteger; if (integer != null) { singleFile.Media.Add(integer.Value); } } } } else if (info.Key.Value == "profiles") { isSingleFile = true; BList items = info.Value as BList; if (items != null) { foreach (var item in items) { BDictionary dictItems = item as BDictionary; if (dictItems != null) { TorrentFileProfileCollection profiles = new TorrentFileProfileCollection(); profiles.AddRange(dictItems.Select(dictItem => new TorrentFileProfile { Name = dictItem.Key.ToString(), Value = dictItem.Value.ToString() })); singleFile.Profiles.Add(profiles); } } } } } }