diff --git a/README.md b/README.md index f9665c3..86d6454 100755 --- a/README.md +++ b/README.md @@ -55,12 +55,12 @@ $ python3 -m pip install -r requirements.txt ``` -4. Copy `config.yaml.example` and save as `config.yaml`. Change the content of `config.yaml`, fill in your host, access_key and access_secret. +4. Copy `config.yaml.example` and save as `config.yaml`. Change the content of `config.yaml`, fill in your host, access_key and access_secret. You can save on ̣̣`~/.acrcloudscan/config.yaml` or `/etc/acrcloud/scan/config.yaml` 5. Run the script to scan your audio/video files: ```bash -$ python main.py -t ~/test/test.mp4 +$ acrscancloud -t ~/test/test.mp4 ``` ## Usage @@ -97,12 +97,12 @@ Options: - with played duration: ```bash -$ python main.py -t ~/test/test.mp4 -w +$ acrcloudscan -t ~/test/test.mp4 -w ``` - with played duration and filter results ```bash -$ python main.py -t ~/test/test.mp4 -w --filter-results +$ acrcloudscan -t ~/test/test.mp4 -w --filter-results ``` ## Using Docker diff --git a/acrscan/__init__.py b/acrcloudscan/acrscan/__init__.py similarity index 100% rename from acrscan/__init__.py rename to acrcloudscan/acrscan/__init__.py diff --git a/acrscan/__main__.py b/acrcloudscan/acrscan/__main__.py similarity index 100% rename from acrscan/__main__.py rename to acrcloudscan/acrscan/__main__.py diff --git a/acrscan/acrcloud/__init__.py b/acrcloudscan/acrscan/acrcloud/__init__.py similarity index 100% rename from acrscan/acrcloud/__init__.py rename to acrcloudscan/acrscan/acrcloud/__init__.py diff --git a/acrscan/acrcloud/recognizer.py b/acrcloudscan/acrscan/acrcloud/recognizer.py similarity index 100% rename from acrscan/acrcloud/recognizer.py rename to acrcloudscan/acrscan/acrcloud/recognizer.py diff --git a/acrscan/acrscan.py b/acrcloudscan/acrscan/acrscan.py similarity index 100% rename from acrscan/acrscan.py rename to acrcloudscan/acrscan/acrscan.py diff --git a/acrscan/lib_downloader.py b/acrcloudscan/acrscan/lib_downloader.py similarity index 100% rename from acrscan/lib_downloader.py rename to acrcloudscan/acrscan/lib_downloader.py diff --git a/acrscan/models/__init__.py b/acrcloudscan/acrscan/models/__init__.py similarity index 100% rename from acrscan/models/__init__.py rename to acrcloudscan/acrscan/models/__init__.py diff --git a/acrscan/models/base_model.py b/acrcloudscan/acrscan/models/base_model.py similarity index 100% rename from acrscan/models/base_model.py rename to acrcloudscan/acrscan/models/base_model.py diff --git a/acrscan/models/config.py b/acrcloudscan/acrscan/models/config.py similarity index 100% rename from acrscan/models/config.py rename to acrcloudscan/acrscan/models/config.py diff --git a/acrscan/utils.py b/acrcloudscan/acrscan/utils.py similarity index 100% rename from acrscan/utils.py rename to acrcloudscan/acrscan/utils.py diff --git a/acrcloudscan/main.py b/acrcloudscan/main.py new file mode 100755 index 0000000..835524a --- /dev/null +++ b/acrcloudscan/main.py @@ -0,0 +1,102 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +from .acrscan.lib_downloader import download_lib, current_platform + +try: + from .acrscan.acrscan import ACRCloudScan +except ImportError: + download_lib() + from .acrscan.acrscan import ACRCloudScan +import logging +import yaml +import click +import sys +import os + +CONFIGS_FILES = ['config.yaml', "~/.acrcloudscan/config.yaml", "/etc/acrcloud/scan/config.yaml"] + +config_file = None +for conf in CONFIGS_FILES: + conf_path = os.path.expanduser(conf) + if os.path.exists(conf_path): + config_file = conf_path + break + +if config_file is None: + logging.error("Please make sure you fill the config.yaml in some of the following paths: {}".format(CONFIGS_FILES)) + raise ValueError() + +with open(config_file, 'r') as f: + try: + config_dict = yaml.safe_load(f) + except yaml.YAMLError as exc: + logging.error(exc) + +acrcloud_config = config_dict.get('acrcloud') + +if acrcloud_config.get('debug'): + logging.basicConfig(level=logging.DEBUG) +else: + logging.basicConfig(level=logging.INFO) + + +class OptionRequiredIf(click.Option): + + def full_process_value(self, ctx, value): + value = super(OptionRequiredIf, self).full_process_value(ctx, value) + if not ctx.params.get('with_duration') and value: + msg = 'Required --with-duration, if you want filter pls add --with-duration' + raise click.MissingParameter(ctx=ctx, param=self, message=msg) + return value + + +@click.command() +@click.option('--target', '-t', + help='The target need to scan (a folder or a file).', required=True) +@click.option('--output', '-o', default='', + help='Output result to this folder. (Must be a folder path)') +@click.option('--format', 'output_format', type=click.Choice(['csv', 'json']), + help='output format.(csv or json)') +@click.option('--with-duration/--no-duration', '-w', default=False, + help='Add played duration to the result') +@click.option('--filter-results/--no-filter', default=False, + help='Enable filter.(It must be used when the with-duration option is on)', cls=OptionRequiredIf) +@click.option('--split-results/--no-split', '-p', default=False, + help='Each audio/video file generate a report') +@click.option('--scan-type', '-c', type=click.Choice(['music', 'custom', 'both']), default='both', + help='scan type') +@click.option('--start-time-ms', '-s', default=0, + help='scan start time') +@click.option('--end-time-ms', '-e', default=0, + help='scan end time') +@click.option('--is-fp/--not-fp', '-f', default=False, + help='scan fingerprint') +@click.option('--interval', '-i', default=10, + help='interval') +def main(target, output, output_format, with_duration, filter_results, split_results, scan_type, start_time_ms, + end_time_ms, is_fp, interval): + ctx = click.get_current_context() + if not any(v for v in ctx.params.values()): + click.echo(ctx.get_help()) + ctx.exit() + + platform = current_platform() + fp_support_platforms = ['linux_64', 'mac'] + if is_fp and platform not in fp_support_platforms: + print('Your system not support scan fingerprint') + sys.exit() + acr = ACRCloudScan(acrcloud_config) + acr.with_duration = with_duration + acr.filter_results = filter_results + acr.split_results = split_results + acr.scan_type = scan_type + acr.start_time_ms = start_time_ms * 1000 + acr.end_time_ms = end_time_ms * 1000 + acr.is_fingerprint = is_fp + acr.interval_length_ms = interval * 1000 + acr.scan_main(target, output, output_format) + + +if __name__ == '__main__': + main() diff --git a/main.py b/main.py index 949e483..f7841d5 100755 --- a/main.py +++ b/main.py @@ -12,8 +12,16 @@ import yaml import click import sys +import os -with open('config.yaml', 'r') as f: +CONFIGS_FILES = ["~/.acrcloud/config.yaml" , 'config.yaml'] + +for conf in CONFIGS_FILES: + if os.path.exists(conf): + config_file = conf + break + +with open(config_file, 'r') as f: try: config_dict = yaml.safe_load(f) except yaml.YAMLError as exc: diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..c600bbd --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,20 @@ +[build-system] +requires = ["setuptools"] +build-backend = "setuptools.build_meta" + +[project] +name = "acrcloudscan" +version = "0.0.1" +dependencies = [ + "retrying", + "click", + "python-dateutil", + "tqdm", + "fuzzywuzzy", + "ffmpeg-python", + "PyYAML", + "urllib3" +] + +[project.scripts] +acrcloudscan = "acrcloudscan.main:main" \ No newline at end of file