Ejemplo n.º 1
0
        public static TorrentInfo Parse(Uri uri)
        {
            Contract.Requires(uri != null);
            Contract.Requires(1 <= uri.Query.Length);

            if (uri == null)
            {
                throw new ArgumentNullException("uri");
            }

            var torrent = new TorrentInfo();

            string queryString = uri.Query.Substring(1);
            var    queryParams = System.Web.HttpUtility.ParseQueryString(queryString);

            List <string> annouces = new List <string>();

            foreach (string key in queryParams.AllKeys)
            {
                if (key == "dn")
                {
                    torrent.Name = queryParams[key];
                }
                if (key == "xt")
                {
                    string[] urn = queryParams[key].Split(':');
                    if (urn.Length == 3)
                    {
                        torrent.Hash = urn[2];
                    }
                }
                if (key == "tr")
                {
                    annouces.Add(queryParams[key]);
                }
                if (key == "xl")
                {
                    long val;
                    if (long.TryParse(queryParams[key], out val))
                    {
                        torrent.PieceLength = val;
                    }
                }
            }


            return(torrent);
        }
Ejemplo n.º 2
0
        public static TorrentInfo Parse(Uri uri)
        {
            Contract.Requires(uri != null);
            Contract.Requires(1 <= uri.Query.Length);

            if (uri == null)
            {
                throw new ArgumentNullException(nameof(uri));
            }

            var torrent = new TorrentInfo();

            string queryString = uri.Query.Substring(1);
            var    queryParams = ParseQueryString((queryString.Length > 0 && queryString[0] == '?') ? queryString.Substring(1) : queryString);

            List <string> annouces = new List <string>();

            foreach (string key in queryParams.Keys)
            {
                if (key == "dn")
                {
                    torrent.Name = queryParams[key];
                }
                else if (key == "xt")
                {
                    string[] urn = queryParams[key].Split(':');
                    if (urn.Length == 3)
                    {
                        torrent.Hash = urn[2];
                    }
                }
                else if (key == "tr")
                {
                    annouces.Add(queryParams[key]);
                }
                else if (key == "xl")
                {
                    if (long.TryParse(queryParams[key], out var val))
                    {
                        torrent.PieceLength = val;
                    }
                }
            }


            return(torrent);
        }
Ejemplo n.º 3
0
        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);
        }
Ejemplo n.º 4
0
        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);
                            }
                        }
                    }
                }
            }
        }
Ejemplo n.º 5
0
        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;
        }
Ejemplo n.º 6
0
        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);
                            }
                        }
                    }
                }
            }
        }
Ejemplo n.º 7
0
        public static TorrentInfo Parse(Uri uri)
        {
            Contract.Requires(uri != null);
            Contract.Requires(1 <= uri.Query.Length);

            if (uri == null)
                throw new ArgumentNullException("uri");

            var torrent = new TorrentInfo();

            string queryString = uri.Query.Substring(1);
            var queryParams = System.Web.HttpUtility.ParseQueryString(queryString);

            List<string> annouces = new List<string>();
            foreach (string key in queryParams.AllKeys)
            {
                if (key == "dn")
                {
                    torrent.Name = queryParams[key];
                }
                if (key == "xt")
                {
                    string[] urn = queryParams[key].Split(':');
                    if (urn.Length == 3)
                    {
                        torrent.Hash = urn[2];
                    }
                }
                if (key == "tr")
                {
                    annouces.Add(queryParams[key]);
                }
                if (key == "xl")
                {
                    long val;
                    if (long.TryParse(queryParams[key], out val))
                    {
                        torrent.PieceLength = val;
                    }
                }
            }

            return torrent;
        }