def __Validate(self, filters): if isinstance(filters, Filters): raise WoeidError("Unexpected modules usage: %s"%"Validate takes a Filters object as its argument") if not filters.IsValid(): raise WoeidError("Unexpected API usage: %s"%"filters should be a dictionary") ''' /parent, /ancestors, /siblings, /common/ don't support any filters''' if self._parent and filters.keys(): raise WoeidError("Unexpected API usage: %s"%"woeid/parent doesn't support filters") if self._ancestors and filters.keys(): raise WoeidError("Unexpected API usage: %s"%"woeid/ancestors doesn't support filters") if self._siblings and filters.keys(): raise WoeidError("Unexpected API usage: %s"%"woeid/siblings doesn't support filters") if self._common and filters.keys(): raise WoeidError("Unexpected API usage: %s"%"woeid1/common/woeid2 doesn't support filters") '''/belongtos and /descendants and /children support .type filter''' if self._belongtos and (filters.HasAnd() or filters.HasDegree() or filters.HasQ() or filters.HasWoeid()): raise WoeidError("Unexpected API usage: %s"%"woeid/belongtos supports .type filter only") if self._descendants and (filters.HasAnd() or filters.HasDegree() or filters.HasQ() or filters.HasWoeid()): raise WoeidError("Unexpected API usage: %s"%"woeid/descendants supports .type filter only") if self._children and (filters.HasWoeid() or filters.HasQ() or filters.HasAnd()): raise WoeidError("Unexpected API usage: %s"%"woeid/children supports .degree or .type filters only") '''/neighbors support .degree filter''' if self._neighbors and (filters.HasWoeid() or filters.HasType() or filters.HasQ() or filters.HasAnd()): raise WoeidError("Unexpected API usage: %s"%"woeid/neighbors supports .degree filter only")
def Select(self, select): try: if str(select).lower() == 'short' or str(select).lower() == 'long': self.__select = str(select) else: raise WoeidError("select can be either 'short' or 'long'") except ValueError: raise WoeidError("Expect a string type for select")
def __init__(self, code): if code == 400: raise WoeidError("The appid parameter was invalid or not specified. or the q filter was missing or incorrectly specified for this resource.") if code == 404: raise WoeidError("The URI has no match in the display map") if code ==406: raise WoeidError("The requested representation is not available for this resource")
def EncodeParameters(parameters): """Return a string in key=value&key=value form. Values of None are not included in the output string. Args: parameters (``OrderedDict``): dictionary of query parameters to be converted into a string for encoding and sending to Twitter. Returns: A URL-encoded string in "key=value&key=value" form """ if parameters is None: return None if not isinstance(parameters, OrderedDict): raise WoeidError("`parameters` must be a dict.") else: return urlencode(OrderedDict((k, v) for k, v in parameters.items() if v is not None))
def ClientId(self, client_id): try: self.__clientid = str(client_id) except ValueError: raise WoeidError("Expect a string type for appid")
def BaseUrl(self, url): try: self.__base_url = str(url) except ValueError: raise WoeidError("Expect a string type for url")
def Start(self, start): try: self.__start = int(start) except ValueError: raise WoeidError("Expect an integer type for start")
def Count(self, count): try: self.__count = int(count) except ValueError: raise WoeidError("Expect an integer type for count")
def Lang(self, lang): try: self.__lang = str(lang) except ValueError: raise WoeidError("Expect a stirng type for lang")
def Format(self, format): try: self.__format = str(format) except ValueError: raise WoeidError("Expect a string type for format")
def __str__(self): qstr = '' woeidstr = '' typestr = '' degreestr = '' andstr = '' # work on .q filter if self.HasQ(): if isinstance(self._filters['q'], string_types): qstr = quote(self._filters['q'].encode('utf-8')) elif isinstance(self._filters['q'], tuple): stra = self._filters['q'][0].encode('utf-8') strb = self._filters['q'][1].encode('utf-8') # Second item will be a focus value # Focus can be either an ISO-3166-1 country code or a WOEID. qstr += quote(stra + ',' + strb) else: raise WoeidError("Unexpected usage of function! query filter is %s" % self._filters['q']) qstr = '.q(%s)'%qstr # work on .woeid filter if self.HasWoeid(): if isinstance(self._filters['woeid'], list) and len(self._filters[ 'woeid']) > 1: for item in self._filters['woeid']: if (isinstance(item, string_types) and item.isdigit()) or isinstance(item, int): woeidstr += quote(item) + ',' # tick out the last comma woeidstr = woeidstr[:-1] elif isinstance(self._filters['woeid'], list) and len( self._filters['woeid']) == 1: woeidstr = '/' + quote(self._filters['woeid'][0]) else: raise WoeidError("Unexpected usage of function! query filter is %s"%self._filters['woeid']) #.woeid can be omitted if there is only one item if ',' in woeidstr: woeidstr = '.woeid(%s)'%woeidstr # work on .type filter if 'type' in self._filters: tpitem = self._filters['type'] if isinstance(tpitem, list): for item in tpitem: if (isinstance(item, string_types) and item.isdigit()) or isinstance(item, int): typestr += quote(str(item)) + ',' typestr = typestr[:-1] typestr = '.type(%s)'%typestr elif (type(tpitem) is str and tpitem.isdigit()) or isinstance(tpitem, int): typestr = '.type(%s)'%quote(str(tpitem)) # work on .degree filter if 'degree' in self._filters: degree = str(self._filters['degree']) degreestr = '.degree(%s)'%degree # work on .and filter if 'and' in self._filters: conda = '' condb = '' if self.HasQ() and qstr: conda = qstr if self.HasWoeid() and woeidstr: conda = woeidstr if typestr: condb = typestr if degreestr: condb = degreestr if conda and condb: andstr = '$and(%s,%s)'%(conda,condb) if andstr: return andstr query_or_woeid_str = qstr if qstr else woeidstr return query_or_woeid_str + typestr + degreestr