public static Dictionary <string, string> GetAttributeListings(string authKey, string attribute, string forType, string appKey)
        {
            var items = new Dictionary <string, string>();

            string result = string.Empty;

            var buffer     = Encoding.ASCII.GetBytes("action=login");
            var webrequest = (HttpWebRequest)WebRequest.Create(new Uri("https://data-app.taus.net/api/attr/" + attribute + ".xml?for=" + forType + ""));

            webrequest.KeepAlive = false;
            webrequest.Method    = @"GET";
            //webrequest.Proxy = null;
            webrequest.Headers.Add("X-TDA-App-Key", appKey);
            webrequest.Headers.Add("X-TDA-Auth-Key", authKey);
            webrequest.Timeout       = 10000; //10 seconds
            webrequest.ContentLength = 0;
            using (var webresponse = webrequest.GetResponse() as HttpWebResponse)
            {
                if (webresponse != null)
                {
                    var reader = new StreamReader(webresponse.GetResponseStream());
                    result = reader.ReadToEnd();
                }
            }


            var resultSegmentParser = new ResultSegmentParser();
            var searchSegmentResult = resultSegmentParser.ReadResult(result);

            if (searchSegmentResult.Status == "200")
            {
                var rAttribute     = new Regex(@"\<" + attribute + @"\>(?<x1>.*?|)\<\/" + attribute + @"\>", RegexOptions.IgnoreCase | RegexOptions.Singleline);
                var rAttributeName = new Regex(@"\<name\>(?<x1>.*?|)\<\/name\>", RegexOptions.IgnoreCase | RegexOptions.Singleline);
                var rAttributeId   = new Regex(@"\<id\>(?<x1>.*?|)\<\/id\>", RegexOptions.IgnoreCase | RegexOptions.Singleline);

                var mcAttributes = rAttribute.Matches(result);
                foreach (Match mAttribute in mcAttributes)
                {
                    var mId   = rAttributeId.Match(mAttribute.Groups["x1"].Value);
                    var mName = rAttributeName.Match(mAttribute.Groups["x1"].Value);
                    if (!mId.Success || !mName.Success)
                    {
                        continue;
                    }
                    if (!items.ContainsKey(mId.Groups["x1"].Value))
                    {
                        items.Add(mId.Groups["x1"].Value, mName.Groups["x1"].Value);
                    }
                }
            }
            else
            {
                throw new Exception("Status: " + searchSegmentResult.Status + "\r\nReason: " + searchSegmentResult.Reason);
            }

            return(items);
        }
Beispiel #2
0
        public static string GetAuthorizationKey(string userName, string password, string appKey)
        {
            string authKey;
            //auth_app_key 67C52A1B

            var result = string.Empty;

            var buffer     = Encoding.UTF8.GetBytes("action=login");
            var webrequest = (HttpWebRequest)WebRequest.Create(new Uri("https://www.tausdata.org/api/auth_key.xml"));

            webrequest.KeepAlive = false;
            webrequest.Method    = @"POST";


            webrequest.ContentLength = buffer.Length;
            webrequest.Credentials   = new NetworkCredential(userName, password);

            webrequest.Headers.Add("X-TDA-App-Key", appKey);


            webrequest.ContentType = "application/x-www-form-urlencoded";

            var postData = webrequest.GetRequestStream();

            postData.Write(buffer, 0, buffer.Length);
            postData.Close();

            using (var webresponse = webrequest.GetResponse() as HttpWebResponse)
            {
                if (webresponse != null)
                {
                    var reader = new StreamReader(webresponse.GetResponseStream());
                    result = reader.ReadToEnd();
                }
            }


            var resultSegmentParser = new ResultSegmentParser();
            var searchSegmentResult = resultSegmentParser.ReadResult(result);


            if (searchSegmentResult.Status == "201")
            {
                authKey = searchSegmentResult.AuthKey.Id;
            }

            else
            {
                throw new Exception("Status: " + searchSegmentResult.Status + "\r\nReason: " +
                                    searchSegmentResult.Reason);
            }

            return(authKey);
        }
        public SearchSegmentResult SearchSegment(SearchSettings settings, ScoreType matchScoringType)
        {
            var searchSegmentResult = new SearchSegmentResult();


            var result = GetSearchSegmentResult(settings);


            if (result.Trim() != string.Empty)
            {
                var resultSegmentParser = new ResultSegmentParser();
                searchSegmentResult = resultSegmentParser.ReadResult(result);

                //apply the analysis percentage
                searchSegmentResult = SetMatchPercentage(searchSegmentResult, settings, matchScoringType);

                //sort on match %
                searchSegmentResult.Segments.Sort((c1, c2) => c2.MatchPercentage.CompareTo(c1.MatchPercentage));

                //respect the limit
                var limitIndex        = settings.Limit - 1;
                var totalSegmentIndex = searchSegmentResult.Segments.Count - 1;

                if (settings.Limit > 0 && settings.Limit < searchSegmentResult.Segments.Count)
                {
                    searchSegmentResult.Segments.RemoveRange(limitIndex, totalSegmentIndex - limitIndex);
                }
            }
            else
            {
                searchSegmentResult.Status = "timed out";
                searchSegmentResult.Reason = "timed out";
            }

            return(searchSegmentResult);
        }