Esempio n. 1
0
        private string Token()
        {
            const string endpoint = "/gui/token.html";
            var          config   = Plugin.Instance.Configuration;

            if (config.userName is null)
            {
                return(JsonSerializer.SerializeToString(new StatusResponse()
                {
                    status = "No configuration present"
                }));
            }
            var url      = $"http://{config.ipAddress}:{config.port}{endpoint}";
            var client   = new TorrentClient();
            var response = client.Get(url, "application/x-www-form-urlencoded");

            if (response.StatusCode != HttpStatusCode.OK)
            {
                return(string.Empty);
            }
            using (var receiveStream = response.GetResponseStream())
            {
                if (receiveStream == null)
                {
                    return(string.Empty);
                }
                using (var sr = new StreamReader(receiveStream, Encoding.GetEncoding(response.CharacterSet ?? throw new InvalidOperationException())))
                {
                    var data = sr.ReadToEnd();

                    return((data.Split('>')[2]).Split('<')[0]);
                }
            }
        }
Esempio n. 2
0
        public string Get(Settings request)
        {
            var config = Plugin.Instance.Configuration;

            if (config.userName is null)
            {
                return(JsonSerializer.SerializeToString(new StatusResponse()
                {
                    status = "No configuration present"
                }));
            }
            var url      = $"http://{config.ipAddress}:{config.port}{gui}{token}{Token()}{getSettings}";
            var client   = new TorrentClient();
            var response = client.Get(url, "application/json");

            if (response.StatusCode != HttpStatusCode.OK)
            {
                return(string.Empty);
            }
            using (var receiveStream = response.GetResponseStream())
            {
                if (receiveStream == null)
                {
                    return(string.Empty);
                }
                using (var sr = new StreamReader(receiveStream, Encoding.GetEncoding(response.CharacterSet ?? throw new InvalidOperationException())))
                {
                    var data = sr.ReadToEnd();
                    return(data);
                }
            }
        }
Esempio n. 3
0
 public string Get(StartTorrent request)
 {
     try
     {
         const string endpoint = "&action=start&hash=";
         var          config   = Plugin.Instance.Configuration;
         if (config.userName is null)
         {
             return(JsonSerializer.SerializeToString(new StatusResponse()
             {
                 status = "No configuration present"
             }));
         }
         var url      = $"http://{config.ipAddress}:{config.port}{gui}{token}{Token()}{endpoint}";
         var client   = new TorrentClient();
         var response = client.Get(url, "application/x-www-form-urlencoded");
         return(JsonSerializer.SerializeToString(new StatusResponse()
         {
             status = response.StatusCode.ToString()
         }));
     }
     catch (Exception ex)
     {
         return(JsonSerializer.SerializeToString(new StatusResponse()
         {
             status = ex.Message
         }));
     }
 }
Esempio n. 4
0
        private void UpdateTorrentList()
        {
            var config = Plugin.Instance.Configuration;

            if (config.userName is null)
            {
                return;
            }
            var url = $"http://{config.ipAddress}:{config.port}{gui}{token}{Token()}{list}";

            url += CacheId == null ? string.Empty : $"{cache}{CacheId}";

            var client   = new TorrentClient();
            var response = client.Get(url, "application/json");

            if (response.StatusCode != HttpStatusCode.OK)
            {
                return;
            }

            using (var receiveStream = response.GetResponseStream())
            {
                if (receiveStream == null)
                {
                    return;
                }

                using (var sr = new StreamReader(receiveStream, Encoding.GetEncoding(response.CharacterSet ?? throw new InvalidOperationException())))
                {
                    var data = sr.ReadToEnd();

                    var results = JsonSerializer.DeserializeFromString <UTorrentResponse>(data);

                    CacheId = results.torrentc;

                    //List<Torrent> torrents count would be empty on our first request
                    //We fill our "torrents" list with the entire torrent data from the API.

                    //"torrentp" results are only items that have changed since the last request using the cacheId from the prior request

                    //This means we have to replace the data stored in our "List<Torrent> torrents" which matches the new data returned in torrentp list from the api.

                    var torrentListChanges = TorrentParser.ParseTorrentListInfo(Torrents.Count <= 0 ? results.torrents : results.torrentp);

                    if (Torrents.Count <= 0)
                    {
                        Torrents = torrentListChanges; // add torrents to the master list.
                    }
                    else
                    {
                        //Remove any torrent data that has changed from the master list by comparing torrent Hash's
                        Torrents = Torrents.Where(t1 => torrentListChanges.Any(t2 => t1.Hash != t2.Hash)).ToList();
                        Torrents.AddRange(torrentListChanges); //Add the new data to the master list
                    }
                }
            }
        }
Esempio n. 5
0
        public string Get(SetSettings request)
        {
            var config = Plugin.Instance.Configuration;

            if (config.userName is null)
            {
                return(JsonSerializer.SerializeToString(new StatusResponse()
                {
                    status = "No configuration present"
                }));
            }
            var url      = $"http://{config.ipAddress}:{config.port}{gui}{token}{Token()}{setSettings}&s={request.SettingName}&v={request.SettingValue}";
            var client   = new TorrentClient();
            var response = client.Get(url, "application/x-www-form-urlencoded");

            return(JsonSerializer.SerializeToString(new StatusResponse()
            {
                status = response.StatusCode.ToString()
            }));
        }