diff --git a/package-rfcx/rfcx/_api_rfcx.py b/package-rfcx/rfcx/_api_rfcx.py index 0c3258f..315ca41 100644 --- a/package-rfcx/rfcx/_api_rfcx.py +++ b/package-rfcx/rfcx/_api_rfcx.py @@ -11,13 +11,12 @@ def stream_segments(token, stream_id, start, end, limit, offset): data = { - 'id': stream_id, 'start': start, 'end': end, 'limit': limit, 'offset': offset } - path = f'/streams/{stream_id}/stream-segments' + path = f'/streams/{stream_id}/segments' url = '{}{}?{}'.format(host, path, urllib.parse.urlencode(data, True)) return _request(url, token=token) @@ -66,6 +65,7 @@ def streams(token, organizations=None, projects=None, created_by=None, + name=None, keyword=None, is_public=True, is_deleted=False, @@ -85,11 +85,39 @@ def streams(token, data['created_by'] = created_by if keyword: data['keyword'] = keyword + if name: + data['name'] = name path = '/streams' url = '{}{}?{}'.format(host, path, urllib.parse.urlencode(data, True)) return _request(url, token=token) +def projects(token, + keyword=None, + created_by=None, + only_public=None, + only_deleted=None, + limit=1000, + offset=0 + ): + data = { + 'limit': limit, + 'offset': offset + } + if keyword: + data['keyword'] = keyword + if created_by: + data['created_by'] = created_by + if only_public: + data['only_public'] = only_public + if only_deleted: + data['only_deleted'] = only_deleted + + + path = '/projects' + url = '{}{}?{}'.format(host, path, urllib.parse.urlencode(data, True)) + return _request(url, token=token) + def _request(url, method='GET', token=None): logger.debug('get url: %s', url) diff --git a/package-rfcx/rfcx/_authentication.py b/package-rfcx/rfcx/_authentication.py index 224c1f1..ea5a280 100644 --- a/package-rfcx/rfcx/_authentication.py +++ b/package-rfcx/rfcx/_authentication.py @@ -33,7 +33,7 @@ def __init__(self, self.persisted_credentials_path = persisted_credentials_path self.client_id = os.getenv('AUTH0_CLIENT_ID', DEFAULT_CLIENT_ID) - def authentication(self): + def authenticate(self): """Authenticate an RFCx user to obtain a token If you want to persist/load the credentials to/from a custom path then set `persisted_credentials_path` diff --git a/package-rfcx/rfcx/client.py b/package-rfcx/rfcx/client.py index 9f53d25..f56b7b7 100644 --- a/package-rfcx/rfcx/client.py +++ b/package-rfcx/rfcx/client.py @@ -13,9 +13,9 @@ class Client(object): def __init__(self): self.credentials = None - def authentication(self, - persist=True, - persisted_credentials_path='.rfcx_credentials'): + def authenticate(self, + persist=True, + persisted_credentials_path='.rfcx_credentials'): """Authenticate an RFCx user to obtain a token If you want to persist/load the credentials to/from a custom path then set `persisted_credentials_path` @@ -27,16 +27,16 @@ def authentication(self, None. """ auth = Authentication(persist, persisted_credentials_path) - auth.authentication() + auth.authenticate() self.credentials = auth.credentials def download_audio_file(self, - stream, - dest_path, - start_time, - end_time, - gain=1, - file_ext='wav'): + stream, + dest_path, + start_time, + end_time, + gain=1, + file_ext='wav'): """ Download single audio. Duration can not be more than 15 minutes. Args: stream: (required) Identifies a stream/site. @@ -64,18 +64,17 @@ def download_audio_file(self, print("end_time is not type datetime") return - return audio.download_file(self.credentials.token, dest_path, - stream, start_time, end_time, gain, - file_ext) + return audio.download_file(self.credentials.token, dest_path, stream, + start_time, end_time, gain, file_ext) def download_audio_files(self, - stream, - dest_path='./audios', - min_date=None, - max_date=None, - gain=1, - file_ext='wav', - parallel=True): + stream, + dest_path='./audios', + min_date=None, + max_date=None, + gain=1, + file_ext='wav', + parallel=True): """Download multiple audio in giving time range. Args: @@ -114,47 +113,39 @@ def download_audio_files(self, if not os.path.exists(dest_path): os.makedirs(dest_path) - elif len(os.listdir(dest_path)) > 0: - print(f'{dest_path} directory is not empty. Please change or empty directory.') - return - return audio.download_file_segments(self.credentials.token, - dest_path, stream, min_date, - max_date, gain, file_ext, parallel) + return audio.download_file_segments(self.credentials.token, dest_path, + stream, min_date, max_date, gain, + file_ext, parallel) - def stream_segments(self, stream, start=None, end=None, limit=50, offset=0): - """Retrieve audio information about a specific stream + def projects(self, + keyword=None, + created_by=None, + only_public=None, + only_deleted=None, + limit=1000, + offset=0): + """Retrieve a list of projects Args: - stream: (required) Identifies a stream/site. - start: (optional, default= None) Minimum timestamp of the audio. If None then defaults to exactly 30 days ago. - end: (optional, default= None) Maximum timestamp of the audio. If None then defaults to now. - limit: (optional, default= 50) Maximum results to return. Defaults to 50. - offset: (optional, default= 0) Offset of the audio group. + keyword: (optional, default= None) Match project name with keyword + created_by: (optional, default= None) The project owner. Have 3 options: None, me, or collaborator id + only_public: (optional, default= None) Return only public projects + only_deleted: (optional, default= None) Return only deleted projects + limit: (optional, default= 1000) Maximum number of results to return + offset: (optional, default= 0) Number of results to skip Returns: - List of audio files (meta data showing audio id and recorded timestamp) + List of projects """ - if self.credentials is None: - print('Not authenticated') - return - - if stream is None: - print('Require stream id') - return - - if start is None: - start = util.date_before() - if end is None: - end = util.date_now() - - return api_rfcx.stream_segments(self.credentials.token, stream, - start, end, limit, offset) + return api_rfcx.projects(self.credentials.token, keyword, created_by, + only_public, only_deleted, limit, offset) def streams(self, organizations=None, projects=None, created_by=None, + name=None, keyword=None, is_public=True, is_deleted=False, @@ -166,11 +157,12 @@ def streams(self, organizations: (optional, default= None) List of organization ids projects: (optional, default= None) List of project ids created_by: (optional, default= None) The stream owner. Have 3 options: None, me, or collaborators - keyword: (optional, default= None) Match streams name with keyword + name: (optional, default= None) Match exact streams with name (support *) + keyword: (optional, default= None) Match stream name with keyword is_public: (optional, default=True) Match public or private streams is_deleted: (optional, default=False) Match deleted streams - limit: (optional, default=1000) Maximum number of results to return - offset: (optional, default=0) Number of results to skip + limit: (optional, default= 1000) Maximum number of results to return + offset: (optional, default= 0) Number of results to skip Returns: List of streams @@ -183,9 +175,43 @@ def streams(self, return return api_rfcx.streams(self.credentials.token, organizations, - projects, created_by, keyword, is_public, + projects, created_by, name, keyword, is_public, is_deleted, limit, offset) + def stream_segments(self, + stream, + start=None, + end=None, + limit=50, + offset=0): + """Retrieve audio information about a specific stream + + Args: + stream: (required) Identifies a stream/site. + start: (optional, default= None) Minimum timestamp of the audio. If None then defaults to exactly 30 days ago. + end: (optional, default= None) Maximum timestamp of the audio. If None then defaults to now. + limit: (optional, default= 50) Maximum results to return. Defaults to 50. + offset: (optional, default= 0) Offset of the audio group. + + Returns: + List of audio files (meta data showing audio id and recorded timestamp) + """ + if self.credentials is None: + print('Not authenticated') + return + + if stream is None: + print('Require stream id') + return + + if start is None: + start = util.date_before() + if end is None: + end = util.date_now() + + return api_rfcx.stream_segments(self.credentials.token, stream, start, + end, limit, offset) + def ingest_file(self, stream, filepath, timestamp): """ Ingest an audio to RFCx Args: diff --git a/package-rfcx/setup.py b/package-rfcx/setup.py index 2635004..fa07aaa 100644 --- a/package-rfcx/setup.py +++ b/package-rfcx/setup.py @@ -3,7 +3,7 @@ REQUIRED_PACKAGES = ['httplib2', 'six'] setup(name='rfcx', - version='0.2.0', + version='0.2.1', url='https://github.com/rfcx/rfcx-sdk-python', license='None', author='Rainforest Connection',