def setMaxID(self, twid): """ Sets 'max_id' parameter """ if not isinstance(twid, (int, long)): raise TwitterSearchException(1004) if twid > 0: self.arguments.update({'max_id': '%s' % twid}) else: raise TwitterSearchException(1004)
def setGeocode(self, latitude, longitude, radius, km=True): """ Sets geolocation paramaters """ if not isinstance(radius, (int, long)) or radius <= 0: raise TwitterSearchException(1004) if isinstance(latitude, float) and isinstance(longitude, float): if isinstance(km, bool): self.arguments.update({ 'geocode': '%s,%s,%s%s' % (latitude, longitude, radius, 'km' if km else 'mi') }) else: raise TwitterSearchException(1005) else: raise TwitterSearchException(1004)
def searchNextResults(self): """ Returns True if there are more results available within the Twitter Search API """ if not self.__nextMaxID: raise TwitterSearchException(1011) self.sentSearch("%s&max_id=%i" % (self._startURL, self.__nextMaxID)) return self.__response
def sentSearch(self, url): """ Sents a given query string to the Twitter Search API, stores results interally and validates returned HTTP status code """ if not isinstance(url, basestring): raise TwitterSearchException(1009) r = requests.get(self._base_url + self._search_url + url, auth=self.__oauth, proxies=self.__proxy) self.__response['meta'] = r.headers self.checkHTTPStatus(r.status_code) # using IDs to request more results - former versions used page parameter # see https://dev.twitter.com/docs/working-with-timelines given_count = int(parse_qs(url)['count'][0]) self.__response['content'] = r.json() self.__statistics['queries'] += 1 self.__statistics['tweets'] += len( self.__response['content']['statuses']) # if we've seen the correct amount of tweets there may be some more if len(self.__response['content']['statuses']) > 0 and int( self.__response['content']['search_metadata'] ['count']) == given_count: self.__nextMaxID = min(self.__response['content']['statuses'], key=lambda i: i['id'])['id'] - 1 else: # we got less tweets than requested -> no more results in API self.__nextMaxID = None return self.__response['meta'], self.__response['content']
def addKeyword(self, word): """ Adds a given string or list to the current keyword list """ if isinstance(word, basestring) and len(word) >= 2: self.searchterms.append(word) elif isinstance(word, list): self.searchterms += word else: raise TwitterSearchException(1000)
def searchTweets(self, order): """ Creates an query string through a given TwitterSearchOrder instance and takes care that it is sent to the Twitter API. Returns unmodified response """ if not isinstance(order, TwitterSearchOrder): raise TwitterSearchException(1010) self._startURL = order.createSearchURL() self.sentSearch(self._startURL) return self.__response
def setIncludeEntities(self, include): """ Sets 'include entities' paramater """ if not isinstance(include, bool): raise TwitterSearchException(1008) if include: self.arguments.update({'include_entities': 'True'}) else: self.arguments.update({'include_entities': 'False'})
def createSearchURL(self): """ Generates (urlencoded) query string from stored key-values tuples """ if len(self.searchterms) == 0: raise TwitterSearchException(1015) url = '?q=' url += '+'.join([quote_plus(i) for i in self.searchterms]) for key, value in self.arguments.items(): url += '&%s=%s' % (quote_plus(key), (quote_plus(value) if key != 'geocode' else value)) self.url = url return self.url
def setSupportedLanguages(self, order): """ Loads currently supported languages from Twitter API and sets them in a given TwitterSearchOrder instance """ if not isinstance(order, TwitterSearchOrder): raise TwitterSearchException(1010) r = requests.get(self._base_url + self._lang_url, auth=self.__oauth, proxies=self.__proxy) self.__response['meta'] = r.headers self.checkHTTPStatus(r.status_code) self.__response['content'] = r.json() order.iso_6391 = [] for lang in self.__response['content']: order.iso_6391.append(lang['code'])
def setCount(self, cnt): """ Sets 'count' paramater """ if isinstance(cnt, int) and cnt > 0 and cnt <= 100: self.arguments.update({'count': '%s' % cnt}) else: raise TwitterSearchException(1004)
def setResultType(self, tor): """ Sets 'result_type' paramater """ if tor == 'mixed' or tor == 'recent' or tor == 'popular': self.arguments.update({'result_type': '%s' % tor}) else: raise TwitterSearchException(1003)
def setLocale(self, lang): """ Sets 'locale' paramater """ if lang in self.iso_6391: self.arguments.update({'locale': '%s' % lang}) else: raise TwitterSearchException(1002)
def __iter__(self): if not self.__response: raise TwitterSearchException(1014) self._nextTweet = 0 return self
def getTweets(self): """ Returns all available data from last query """ if not self.__response: raise TwitterSearchException(1013) return self.__response['content']
def getMetadata(self): """ Returns all available meta data collected during last query """ if not self.__response: raise TwitterSearchException(1012) return self.__response['meta']
def setCallback(self, func): """ Sets 'callback' paramater """ if isinstance(func, basestring) and func: self.arguments.update({'callback': '%s' % func}) else: raise TwitterSearchException(1006)
def setUntil(self, date): """ Sets 'until' parameter """ if isinstance(date, datetime.date) and date <= datetime.date.today(): self.arguments.update({'until': '%s' % date.strftime('%Y-%m-%d')}) else: raise TwitterSearchException(1007)
def setKeywords(self, word): """ Sets a given list as the new keyword list """ if not isinstance(word, list): raise TwitterSearchException(1001) self.searchterms = word
def setProxy(self, proxy): """ Sets a given dict as proxy handler """ if isinstance(proxy, dict) and 'https' in proxy: self.__proxy = proxy else: raise TwitterSearchException(1016)
def checkHTTPStatus(self, http_status): """ Checks a given http_status and returns an exception in case wrong status """ if http_status in self.exceptions: raise TwitterSearchException(http_status, self.exceptions[http_status])