Пример #1
0
        public UnifiedResponse Search(string query)
        {
            var wcl = this.getClient();
            string response = wcl.DownloadString(SearchRequestUrl(query));
            var output = new UnifiedResponse();
            if (string.IsNullOrWhiteSpace(response))
            {
                output.Success = false;
                output.Error = "Cannot get server response";
                return output;
            }
            else
            {
                try
                {
                    dynamic searchres = new JavaScriptSerializer().Deserialize<dynamic>(response);
                    output.Success = true;

                    output.Results = new List<Result>();
                    var items = (dynamic[])searchres["results"];
                    foreach (var item in items)
                    {
                        if (item["type"] != "release" && item["type"] != "master") continue;
                        var release = new Result();
                        release.Thumb          = ResUrl(item["thumb"]);
                        release.AdditionalInfo = item["resource_url"];
                        release.Artist = release.Album = item["title"];
                        release.Request        = query;
                        output.Results.Add(release);
                    }

                    output.ResultsCount = output.Results.Count;
                    return output;
                }
                catch (Exception e)
                {
                    output.Success = false;
                    output.Error = e.Message;
                    return output;
                }
            }
        }
Пример #2
0
        private void SearchWorker(string Query, out List<UnifiedResponse> Response)
        {
            var ThreadPool = new List<Thread>();
            var ContextPool = Services.Providers.Values.ToArray();
            var Results = new UnifiedResponse[ContextPool.Length];

            for (int i = 0; i < ContextPool.Length; i++)
            {
                int closure = i;
                Results[i] = new UnifiedResponse();
                var thread = new Thread(() => Results[closure] = ContextPool[closure].Provider.Search(Query));
                thread.Start();
                ThreadPool.Add(thread);
            }

            while(ThreadPool.Any(t => t.IsAlive))
            {
                Application.DoEvents();
            }

            Response = Results.ToList();
        }
Пример #3
0
        UnifiedResponse IServiceProvider.Search(string query)
        {
            string url = GetExecutionUrl(query);
            var response = new UnifiedResponse();

            string xresp = Http.getText(url);
            if (string.IsNullOrEmpty(xresp))
            {
                response.Success = false;
                response.Error = "No http response from Last.fm API";
                return response;
            }

            var doc = new XmlDocument();
            doc.LoadXml(xresp);

            XmlNode error = doc.GetElementById("error");
            if (error != null)
            {
                response.Success = false;
                response.Error = string.Format(
                    "Last.fm API returns error #{0}: {1}",
                    error.Attributes["code"],
                    error.InnerXml
                );
                return response;
            }

            var nsmanager = new XmlNamespaceManager(doc.NameTable);
            nsmanager.AddNamespace("opensearch", "http://a9.com/-/spec/opensearch/1.1/");

            var countnode = doc.SelectSingleNode("/lfm/results/opensearch:totalResults", nsmanager);
            response.ResultsCount = countnode == null ? 0 : Convert.ToInt32(countnode.InnerXml);

            var results = doc.GetElementsByTagName("album");
            foreach (XmlNode result in results)
            {
                var r = new Result();
                r.Request = query;
                r.Artist  = result["artist"] == null ? string.Empty : result["artist"].InnerText;
                r.Album   = result["name"]   == null ? string.Empty : result["name"]  .InnerText;

                XmlNode urlnode = null;
                if ((urlnode = result.SelectSingleNode("descendant::image[@size='mega']")) != null)
                {
                    r.Width = r.Height = 500;
                }
                else if ((urlnode = result.SelectSingleNode("descendant::image[@size='extralarge']")) != null)
                {
                    r.Width = r.Height = 300;
                }
                else if((urlnode = result.SelectSingleNode("descendant::image[@size='large']")) != null)
                {
                    r.Width = r.Height = 174;
                }

                if (urlnode != null)
                {
                    r.Url = r.Thumb = urlnode.InnerXml;
                }
                response.Results.Add(r);
            }

            return response;
        }
Пример #4
0
        public UnifiedResponse Search(string query)
        {
            var xmlstring = Tools.Http.getText(BuildSearchRequestUrl(query));
            if (string.IsNullOrEmpty(xmlstring))
            {
                return new UnifiedResponse
                {
                    Success = false,
                    Error   = "Server answer was null"
                };
            }

            xmlstring = Regex.Replace(xmlstring, "xmlns=[^>]*>", ">"); // hack to avoid namespace manager: just cut namespace
            var response = new XmlDocument();

            var output = new UnifiedResponse();
            try
            {
                response.LoadXml(xmlstring);
            }
            catch(Exception e)
            {
                output.Success = false;
                output.Error = e.Message;
            }

            var root = response.SelectSingleNode("/ItemSearchResponse");

            if (root == null)
            {
                output.Success = false;
                output.Error = "Cannot parse response";
                return output;
            }

            var valid = root.SelectSingleNode("Items/Request/IsValid");
            if (valid != null && valid.InnerXml == "True")
            {
                output.Success = true;
                var items = root.SelectNodes("//Item");

                foreach (XmlNode item in items)
                {
                    var imagenode = item.SelectSingleNode("LargeImage");
                    if (imagenode == null)
                    {
                        continue;
                    }

                    var r = new Result();
                    try
                    {
                        r.Artist = item.SelectSingleNode("ItemAttributes/Creator").InnerXml;
                        r.Album = item.SelectSingleNode("ItemAttributes/Title").InnerXml;
                        r.Url = imagenode.SelectSingleNode("URL").InnerXml;
                        r.Width = Convert.ToInt32(imagenode.SelectSingleNode("Width").InnerXml);
                        r.Height = Convert.ToInt32(imagenode.SelectSingleNode("Height").InnerXml);
                        var thumbnode = item.SelectSingleNode("MediumImage");
                        r.Thumb = thumbnode == null ? r.Url : thumbnode.SelectSingleNode("URL").InnerXml;
                    }
                    catch(Exception)
                    {
                        continue;
                    }

                    output.Results.Add(r);
                }

                output.ResultsCount = output.Results.Count;
            }

            return output;
        }