예제 #1
0
        public override string ToString()
        {
            if (Type == Type.Code)
            {
                if (What == What.SUBSCRIBE_TO_GROUP)
                {
                    if (Code == Code.GROUP_ALREADY_EXISTS)
                    {
                        return("Alread in that group.");
                    }
                    if (Code == Code.INSUFFICIENT_PRIVILEGES)
                    {
                        return("My privileges aren't sufficient to join that group");
                    }
                    if (Code == Code.OK)
                    {
                        return("I joined the group.");
                    }
                    if (Code == Code.NO_SUCH_WHATEVER || Code == Code.GROUP_NOT_FOUND)
                    {
                        return("That group does not exists.");
                    }
                }
            }

            return($"{What.ToString().FromConstCase()} - {(Type == Type.Code ? Code.ToString().FromConstCase() : Message)}");
        }
예제 #2
0
 public override string ToString()
 {
     return(string.Format("{0} - {1} - {2} - {3}", MessageType, Who.ToString(), What.ToString(), Where));
 }
예제 #3
0
 public SearchQuery WithWhat(What what)
 {
     _what = what.ToString().ToLower();
     return(this);
 }
예제 #4
0
        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);
        }
예제 #5
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));
        }
예제 #6
0
 /// <summary>
 /// Printable version of the Reponse data
 /// </summary>
 /// <returns></returns>
 public override string ToString()
 {
     return($"{What.ToString()}[{MessageId}]: {(Type == Type.Code ? Code.ToString() : Message)}");
 }
예제 #7
0
 public override string ToString()
 {
     return($"({What.ToString()} {To.ToString()})");
 }
예제 #8
0
 public SearchQuery WithWhat(What what)
 {
     _what = what.ToString().ToLower();
     return this;
 }