Created
June 12, 2014 08:04
-
-
Save duydo/2e718c4d4ecf57089531 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
__author__ = 'duydo' | |
from base64 import encodestring | |
from json import loads as json_encode | |
from requests import get | |
class SearchAPI(object): | |
MAX_RESULTS = 10 | |
def __init__(self, endpoint=None, username=None, password=None): | |
self.endpoint = endpoint | |
self.username = username | |
self.password = password | |
self._headers = { | |
"Authorization": "Basic %s" % encodestring('%s:%s' % (self.username, self.password)).replace('\n', '') | |
} | |
def _url_for(self, query, max_results): | |
return '%s?publisher=twitter&query=%s&maxResults=%s' % (self.endpoint, query, max_results) | |
def search(self, query, max_results=10): | |
""" | |
Return list of messages. | |
""" | |
res = get( | |
url=self._url_for(query, max_results), | |
headers=self._headers | |
) | |
if res.status_code == 200: | |
return json_encode(res.content).get('results') | |
return [] | |
def test_searchapi(): | |
api = SearchAPI( | |
endpoint='URL HERE', | |
username='USERNAME HERE', | |
password='PASSWORD HERE' | |
) | |
results = api.search('from:duydo', 10) | |
print results | |
if __name__ == '__main__': | |
test_searchapi() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment