uAL.TorrentAPI.AddTorrent C# (CSharp) Method

AddTorrent() public method

public AddTorrent ( string fileName, string label ) : bool
fileName string
label string
return bool
        public bool AddTorrent(string fileName, string label)
        {
            //First, get excisting torrents from uTorrent, in case some have been added from outside this service...
            GetTorrents(true);

            Stream TorrentStream = File.OpenRead(fileName);

            HttpWebRequest PostReq = (HttpWebRequest)(HttpWebRequest.Create(host + "?action=add-file&token=" + token));
            PostReq.KeepAlive = false;
            PostReq.Credentials = credentials;
            string guid = Guid.NewGuid().ToString("N");
            PostReq.ContentType = "multipart/form-data; boundary=" + guid;
            PostReq.Method = "POST";
            PostReq.Headers.Add("Cookie", cookie);

            using (BinaryWriter Writer = new BinaryWriter(PostReq.GetRequestStream()))
            {
                byte[] FileBytes = new byte[TorrentStream.Length];
                TorrentStream.Read(FileBytes, 0, FileBytes.Length);

                Writer.Write(Encoding.ASCII.GetBytes(String.Format("--{0}\r\nContent-Disposition: form-data; name=\"torrent_file\"; filename=\"{0}\"\r\nContent-Type: application/x-bittorrent\r\n\r\n", guid, fileName)));
                Writer.Write(FileBytes, 0, FileBytes.Length);
                Writer.Write(Encoding.ASCII.GetBytes(String.Format("\r\n--{0}\r\n", guid)));
            }

            HttpWebResponse response = (HttpWebResponse)PostReq.GetResponse();
            StreamReader sr = new StreamReader(response.GetResponseStream());
            string t = sr.ReadToEnd();
            response.Close();

            foreach (Torrent newTorrent in NewTorrents())
            {
                if (newTorrent.Label == "")
                {
                    HttpWebRequest addLabel = (HttpWebRequest)(HttpWebRequest.Create(host + "?action=setprops&token=" + token + "&hash="+newTorrent.Hash+"&s=label&v="+label));
                    addLabel.Credentials = credentials;
                    addLabel.Headers.Add("Cookie", cookie);
                    HttpWebResponse _response = (HttpWebResponse)addLabel.GetResponse();
                    sr = new StreamReader(_response.GetResponseStream());
                    t = sr.ReadToEnd();
                    _response.Close();
                    Console.WriteLine("Added " + fileName + " with label " + label);
                    Console.WriteLine();
                    return true;
                }
            }
            return false;
        }

Usage Example

Example #1
0
        public FileSystemMonitor(TorrentAPI t)
        {
            string            downloadDir    = "";
            string            downloadFolder = downloadDir.Substring(downloadDir.LastIndexOf('\\') + 1);
            FileSystemWatcher w = new FileSystemWatcher(downloadDir);

            w.Filter                = "*.torrent";
            w.NotifyFilter          = NotifyFilters.LastWrite | NotifyFilters.FileName;
            w.EnableRaisingEvents   = true;
            w.IncludeSubdirectories = true;

            w.Created += (s, e) =>
            {
                FileInfo fi          = new FileInfo(e.FullPath);
                string   tmp         = fi.Directory.ToString();
                string   eventParent = tmp.Substring(tmp.LastIndexOf('\\') + 1);
                if (downloadFolder != eventParent)
                {
                    if (t.AddTorrent(e.FullPath, eventParent))
                    {
                        Path.ChangeExtension(e.FullPath, "loaded");
                    }
                }
            };
        }
All Usage Examples Of uAL.TorrentAPI::AddTorrent