diff --git a/libs/filonov/filonov/entrypoints/server.py b/libs/filonov/filonov/entrypoints/server.py index b72096f..94b6f74 100644 --- a/libs/filonov/filonov/entrypoints/server.py +++ b/libs/filonov/filonov/entrypoints/server.py @@ -11,7 +11,7 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -"""Provides HTTP endpoint for media similarity requests.""" +"""Provides HTTP endpoint for filonov requests.""" # pylint: disable=C0330, g-bad-import-order, g-multiple-import import os @@ -41,10 +41,13 @@ class CreativeMapPostRequest(pydantic.BaseModel): - """Specifies structure of request for tagging media. + """Specifies structure of request for returning creative map. Attributes: - media_paths: Identifiers or media to cluster (file names or links). + source: Source of getting data for creative map. + media_type: Type of media to get. + input_parameters: Parameters to get data from the source. + tagger: Type of tagger to use. normalize: Whether to apply normalization threshold. """ @@ -59,7 +62,7 @@ class CreativeMapPostRequest(pydantic.BaseModel): async def generate_creative_map( request: CreativeMapPostRequest, ) -> filonov.creative_map.CreativeMapJson: - """Performs media clustering.""" + """Generates Json with creative map data.""" input_service = filonov.MediaInputService(request.source) media_info, context = input_service.generate_media_info( request.media_type, request.input_parameters diff --git a/libs/filonov/filonov/inputs/input_service.py b/libs/filonov/filonov/inputs/input_service.py index f017276..ea039e8 100644 --- a/libs/filonov/filonov/inputs/input_service.py +++ b/libs/filonov/filonov/inputs/input_service.py @@ -16,7 +16,7 @@ """Module responsible for extracting media dimensions and metrics.""" -from typing import Literal +from typing import Literal, get_args from filonov.inputs import google_ads, interfaces, youtube @@ -24,11 +24,20 @@ Context = dict[str, str] +class MediaInputServiceError(Exception): + """Base exception for MediaInputService.""" + + class MediaInputService: """Extracts media information from a specified source.""" - def __init__(self, source: InputSource) -> None: + def __init__(self, source: InputSource = 'googleads') -> None: """Initializes InputService.""" + if source not in get_args(InputSource): + raise MediaInputServiceError( + f'Incorrect source: {source}. Only {get_args(InputSource)} ' + 'are supported.' + ) self.source = source def generate_media_info( @@ -46,10 +55,15 @@ def generate_media_info( Returns: Tuple with mapping between media identifiers and media info and a context. + + Raises: + MediaInputServiceError: When incomplete input parameters are supplied. """ if self.source == 'youtube': if not (channel := input_parameters.get('channel')): - raise ValueError + raise MediaInputServiceError( + 'Missing required argument `channel` for source `youtube`' + ) context = {'channel': channel} return ( youtube.ExtraInfoFetcher(channel).generate_extra_info(), @@ -57,7 +71,10 @@ def generate_media_info( ) if self.source == 'file': if 'performance_results_path' not in input_parameters: - raise ValueError + raise MediaInputServiceError( + 'Missing required argument `performance_results_path` ' + 'for source `file`' + ) return ( google_ads.from_file( media_type=media_type, @@ -66,21 +83,19 @@ def generate_media_info( ), {}, ) - if self.source == 'googleads': - if 'ads_config_path' in input_parameters: - ads_config = input_parameters.pop('ads_config_path') - if 'account' in input_parameters: - accounts = input_parameters.pop('account') - fetcher = google_ads.ExtraInfoFetcher( - accounts=accounts, ads_config=ads_config - ) - fetching_request = google_ads.FetchingRequest( - media_type=media_type, - **input_parameters, - ) - context = {**fetching_request.to_dict(), 'account': accounts} - return ( - fetcher.generate_extra_info(fetching_request, with_size_base), - context, - ) - raise ValueError + if 'ads_config_path' in input_parameters: + ads_config = input_parameters.pop('ads_config_path') + if 'account' in input_parameters: + accounts = input_parameters.pop('account') + fetcher = google_ads.ExtraInfoFetcher( + accounts=accounts, ads_config=ads_config + ) + fetching_request = google_ads.FetchingRequest( + media_type=media_type, + **input_parameters, + ) + context = {**fetching_request.to_dict(), 'account': accounts} + return ( + fetcher.generate_extra_info(fetching_request, with_size_base), + context, + ) diff --git a/ui/creative-maps/package-lock.json b/ui/creative-maps/package-lock.json index c8727a5..87bedb7 100644 --- a/ui/creative-maps/package-lock.json +++ b/ui/creative-maps/package-lock.json @@ -7679,15 +7679,16 @@ } }, "node_modules/nanoid": { - "version": "3.3.7", - "resolved": "https://registry.npmjs.org/nanoid/-/nanoid-3.3.7.tgz", - "integrity": "sha512-eSRppjcPIatRIMC1U6UngP8XFcz8MQWGQdt1MTBQ7NaAmvXDfvNxbvWV3x2y6CdEUciCSsDHDQZbhYaB8QEo2g==", + "version": "3.3.8", + "resolved": "https://registry.npmjs.org/nanoid/-/nanoid-3.3.8.tgz", + "integrity": "sha512-WNLf5Sd8oZxOm+TzppcYk8gVOgP+l58xNy58D0nbUnOxOWRWvlcCV4kUF7ltmI6PsrLl/BgKEyS4mqsGChFN0w==", "funding": [ { "type": "github", "url": "https://github.com/sponsors/ai" } ], + "license": "MIT", "bin": { "nanoid": "bin/nanoid.cjs" }, @@ -10602,10 +10603,11 @@ } }, "node_modules/vite": { - "version": "5.4.8", - "resolved": "https://registry.npmjs.org/vite/-/vite-5.4.8.tgz", - "integrity": "sha512-FqrItQ4DT1NC4zCUqMB4c4AZORMKIa0m8/URVCZ77OZ/QSNeJ54bU1vrFADbDsuwfIPcgknRkmqakQcgnL4GiQ==", + "version": "5.4.14", + "resolved": "https://registry.npmjs.org/vite/-/vite-5.4.14.tgz", + "integrity": "sha512-EK5cY7Q1D8JNhSaPKVK4pwBFvaTmZxEnoKXLG/U9gmdDcihQGNzFlgIvaxezFR4glP1LsuiedwMBqCXH3wZccA==", "dev": true, + "license": "MIT", "dependencies": { "esbuild": "^0.21.3", "postcss": "^8.4.43",