public QueryResult Search(string query)
        {
            //API example: http://en.wikipedia.org/w/api.php?action=query&list=search&srsearch=wikipedia&srprop=timestamp
            _client.BaseUrl = string.Format(UseTLS ? "https://{0}.wikipedia.org/w/" : "http://{0}.wikipedia.org/w/", Language.GetStringValue());

            RestRequest request = new RestRequest("api.php", Method.GET);

            //Required
            request.AddParameter("action", "query");
            request.AddParameter("list", "search");
            request.AddParameter("srsearch", query);
            request.AddParameter("format", Format.ToString().ToLower());

            //Optional
            if (Infos.HasElements())
            {
                request.AddParameter("srinfo", string.Join("|", Infos).ToLower());
            }

            if (Limit != 0)
            {
                request.AddParameter("srlimit", Limit);
            }

            if (Offset != 0)
            {
                request.AddParameter("sroffset", Offset);
            }

            if (Namespaces.HasElements())
            {
                request.AddParameter("srnamespace", string.Join("|", Namespaces).ToLower());
            }

            if (Properties.HasElements())
            {
                request.AddParameter("srprop", string.Join("|", Properties).ToLower());
            }

            if (Redirects)
            {
                request.AddParameter("srredirects", Redirects.ToString().ToLower());
            }

            if (What != What.Title)
            {
                request.AddParameter("srwhat", What.ToString().ToLower());
            }

            if (ServedBy)
            {
                request.AddParameter("servedby", ServedBy.ToString().ToLower());
            }

            if (!string.IsNullOrEmpty(RequestID))
            {
                request.AddParameter("requestid", RequestID);
            }

            //Output
            RestResponse response = (RestResponse)_client.Execute(request);

            IDeserializer deserializer;

            switch (Format)
            {
            case Format.XML:
                deserializer = new XmlAttributeDeserializer();
                break;

            case Format.JSON:
                deserializer = new JsonDeserializer();
                break;

            default:
                deserializer = new XmlAttributeDeserializer();
                break;
            }

            //The format that Wikipedia uses
            deserializer.DateFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'";

            deserializer.RootElement = "query";

            QueryResult results = deserializer.Deserialize <QueryResult>(response);

            //For convinience, we autocreate Uris that point directly to the wiki page.
            if (results.Search != null)
            {
                foreach (Search search in results.Search)
                {
                    search.Url = UseTLS ? new Uri("https://" + Language.GetStringValue() + ".wikipedia.org/wiki/" + search.Title) : new Uri("http://" + Language.GetStringValue() + ".wikipedia.org/wiki/" + search.Title);
                }
            }

            return(results);
        }
Exemple #2
0
        public T Search <T>(string query)
        {
            //API example: http://en.wikipedia.org/w/api.php?action=query&list=search&srsearch=wikipedia&srprop=timestamp
            Client.BaseUrl = new Uri(string.Format((UseTls ? "https://{0}.wikipedia.org/w/" : "http://{0}.wikipedia.org/w/"), "en"));

            RestRequest request = new RestRequest("api.php", Method.GET);

            //Required
            request.AddParameter("action", "query");
            request.AddParameter("format", Format.ToString().ToLower());

            if (ExternParameters != null)
            {
                foreach (var parameter in ExternParameters)
                {
                    if (!string.IsNullOrWhiteSpace(parameter.Item1) && parameter.Item2 != null)
                    {
                        request.AddParameter(parameter.Item1, parameter.Item2);
                    }
                }
            }

            if (!string.IsNullOrWhiteSpace(query))
            {
                request.AddParameter("srsearch", query);
            }

            //Optional
            if (Infos.HasElements())
            {
                request.AddParameter("srinfo", string.Join("|", Infos).ToLower());
            }

            if (Limit != 0)
            {
                switch (List)
                {
                case ListType.Geosearch:
                    request.AddParameter("list", List.ToString().ToLower());
                    request.AddParameter("gslimit", Limit);
                    break;

                case ListType.Search:
                    request.AddParameter("list", List.ToString().ToLower());
                    request.AddParameter("srlimit", Limit);
                    break;
                }
            }

            if (Offset != 0)
            {
                request.AddParameter("sroffset", Offset);
            }

            if (Namespaces.HasElements())
            {
                request.AddParameter("srnamespace", string.Join("|", Namespaces).ToLower());
            }

            if (Properties.HasElements())
            {
                request.AddParameter("srprop", string.Join("|", Properties).ToLower());
            }

            if (Redirects)
            {
                request.AddParameter("srredirects", Redirects.ToString().ToLower());
            }

            if (What != What.Title)
            {
                request.AddParameter("srwhat", What.ToString().ToLower());
            }

            if (ServedBy)
            {
                request.AddParameter("servedby", ServedBy.ToString().ToLower());
            }

            if (!string.IsNullOrEmpty(RequestId))
            {
                request.AddParameter("requestid", RequestId);
            }

            //Output
            RestResponse response = (RestResponse)Client.Execute(request);

            IDeserializer deserializer;

            switch (Format)
            {
            case Format.Xml:
                deserializer = new XmlAttributeDeserializer();
                break;

            case Format.Json:
                deserializer = new JsonDeserializer();
                break;

            default:
                deserializer = new XmlAttributeDeserializer();
                break;
            }

            //The format that Wikipedia uses
            deserializer.DateFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'";

            return(deserializer.Deserialize <T>(response));
        }