Пример #1
0
def upload_from_url(arg_namespace):
    if not _check_upload_args(arg_namespace):
        return
    file_from_url = File.upload_from_url(arg_namespace.url)
    pprint(file_from_url)

    if arg_namespace.wait or arg_namespace.store:
        timeout = arg_namespace.timeout
        time_started = time.time()
        while time.time() - time_started < timeout:
            status = file_from_url.update_info()['status']
            if status == 'success':
                break
            if status in ('failed', 'error'):
                raise UploadError(
                    'could not upload file from url: {0}'.format(
                        file_from_url.info())
                )
            time.sleep(1)
        else:
            raise TimeoutError('timed out during upload')

    if arg_namespace.store or arg_namespace.info:
        file_ = file_from_url.get_file()
        if file_ is None:
            pprint('Cannot store or get info.')
            return

        _handle_uploaded_file(file_, arg_namespace)
Пример #2
0
def upload_from_url(arg_namespace):
    if not _check_upload_args(arg_namespace):
        return
    file_from_url = File.upload_from_url(arg_namespace.url)
    pprint(file_from_url)

    if arg_namespace.wait or arg_namespace.store:
        timeout = arg_namespace.timeout
        time_started = time.time()
        while time.time() - time_started < timeout:
            status = file_from_url.update_info()['status']
            if status == 'success':
                break
            if status in ('failed', 'error'):
                raise UploadError('could not upload file from url: {0}'.format(
                    file_from_url.info()))
            time.sleep(1)
        else:
            raise TimeoutError('timed out during upload')

    if arg_namespace.store or arg_namespace.info:
        file_ = file_from_url.get_file()
        if file_ is None:
            pprint('Cannot store or get info.')
            return

        _handle_uploaded_file(file_, arg_namespace)
Пример #3
0
    def _get(url, max_retry=3):
        response = None

        for i in range(max_retry):
            try:
                response = session.get(url, stream=True,
                                       verify=conf.verify_api_ssl)
            except requests.exceptions.ConnectionError as e:
                pprint('Connection Error: {0}'.format(e))
                pprint('Retry..')
                time.sleep(i ** 2)
                continue
            else:
                break

        if response is None:
            pprint('Can\'t fetch URL: {0}'.format(url))
            pprint('Skip it.')
            return

        try:
            response.raise_for_status()
        except requests.HTTPError as e:
            pprint(('Can\'t download file: `{0}`. '
                    'Origin error: {1}').format(url, e))
            return

        return response
Пример #4
0
def main(arg_namespace=None,
         config_file_names=('~/.uploadcare', 'uploadcare.ini')):
    if arg_namespace is None:
        arg_namespace = ucare_argparser().parse_args()

    if config_file_names:
        for file_name in config_file_names:
            load_config_from_file(file_name)
    load_config_from_args(arg_namespace)

    if hasattr(arg_namespace, 'func'):
        try:
            arg_namespace.func(arg_namespace)
        except UploadcareException as exc:
            pprint('ERROR: {0}'.format(exc))
Пример #5
0
def main(arg_namespace=None,
         config_file_names=('~/.uploadcare', 'uploadcare.ini')):
    if arg_namespace is None:
        arg_namespace = ucare_argparser().parse_args()

    if config_file_names:
        for file_name in config_file_names:
            load_config_from_file(file_name)
    load_config_from_args(arg_namespace)

    if hasattr(arg_namespace, 'func'):
        try:
            arg_namespace.func(arg_namespace)
        except UploadcareException as exc:
            pprint('ERROR: {0}'.format(exc))
Пример #6
0
def _list(api_list_class, arg_namespace, **extra):
    """ A common function for building methods of the "list showing".
    """
    if arg_namespace.starting_point:
        ordering_field = (arg_namespace.ordering or '').lstrip('-')
        if ordering_field in ('', 'datetime_uploaded', 'datetime_created'):
            arg_namespace.starting_point = parser.parse(
                arg_namespace.starting_point)

    items = api_list_class(starting_point=arg_namespace.starting_point,
                           ordering=arg_namespace.ordering,
                           limit=arg_namespace.limit,
                           request_limit=arg_namespace.request_limit,
                           **extra)
    items.constructor = lambda x: x

    try:
        pprint(list(items))
    except ValueError as e:
        print(e)
Пример #7
0
def _list(api_list_class, arg_namespace, **extra):
    """ A common function for building methods of the "list showing".
    """
    if arg_namespace.starting_point:
        ordering_field = (arg_namespace.ordering or '').lstrip('-')
        if ordering_field in ('', 'datetime_uploaded', 'datetime_created'):
            arg_namespace.starting_point = parser.parse(
                arg_namespace.starting_point)

    items = api_list_class(
        starting_point=arg_namespace.starting_point,
        ordering=arg_namespace.ordering,
        limit=arg_namespace.limit,
        request_limit=arg_namespace.request_limit,
        **extra
    )
    items.constructor = lambda x: x

    try:
        pprint(list(items))
    except ValueError as e:
        print(e)
Пример #8
0
def _handle_uploaded_file(file_, arg_namespace):
    if arg_namespace.store:
        file_.store()
        pprint('File stored successfully.')

    if arg_namespace.info:
        pprint(file_.info())

    if arg_namespace.cdnurl:
        pprint('CDN url: {0}'.format(file_.cdn_url))
Пример #9
0
def _handle_uploaded_file(file_, arg_namespace):
    if arg_namespace.store:
        file_.store()
        pprint('File stored successfully.')

    if arg_namespace.info:
        pprint(file_.info())

    if arg_namespace.cdnurl:
        pprint('CDN url: {0}'.format(file_.cdn_url))
Пример #10
0
def _check_upload_args(arg_namespace):
    if not conf.secret and (arg_namespace.store or arg_namespace.info):
        pprint('Cannot store or get info without "--secret" key')
        return False
    return True
Пример #11
0
def get_file(arg_namespace):
    pprint(File(arg_namespace.path).info())
Пример #12
0
def create_group(arg_namespace):
    files = [File(uuid) for uuid in arg_namespace.paths]
    group = FileGroup.create(files)
    pprint(group.info())
Пример #13
0
def _check_upload_args(arg_namespace):
    if not conf.secret and (arg_namespace.store or arg_namespace.info):
        pprint('Cannot store or get info without "--secret" key')
        return False
    return True
Пример #14
0
def get_file(arg_namespace):
    pprint(File(arg_namespace.path).info())
Пример #15
0
def create_group(arg_namespace):
    files = [File(uuid) for uuid in arg_namespace.paths]
    group = FileGroup.create(files)
    pprint(group.info())
Пример #16
0
def sync_files(arg_namespace):
    session = requests.Session()
    session.headers.update({'User-Agent': _build_user_agent()})

    def _get(url, max_retry=3):
        response = None

        for i in range(max_retry):
            try:
                response = session.get(url, stream=True,
                                       verify=conf.verify_api_ssl)
            except requests.exceptions.ConnectionError as e:
                pprint('Connection Error: {0}'.format(e))
                pprint('Retry..')
                time.sleep(i ** 2)
                continue
            else:
                break

        if response is None:
            pprint('Can\'t fetch URL: {0}'.format(url))
            pprint('Skip it.')
            return

        try:
            response.raise_for_status()
        except requests.HTTPError as e:
            pprint(('Can\'t download file: `{0}`. '
                    'Origin error: {1}').format(url, e))
            return

        return response

    if arg_namespace.starting_point:
        ordering_field = (arg_namespace.ordering or '').lstrip('-')
        if ordering_field in ('', 'datetime_uploaded'):
            arg_namespace.starting_point = parser.parse(
                arg_namespace.starting_point)

    kwargs = dict(uuids=arg_namespace.uuids or None)
    if kwargs['uuids'] is None:
        kwargs.update(dict(
            starting_point=arg_namespace.starting_point,
            ordering=arg_namespace.ordering,
            limit=arg_namespace.limit,
            stored=arg_namespace.stored,
            removed=arg_namespace.removed,
            request_limit=arg_namespace.request_limit,
        ))

    with SyncSession(TrackedFileList(**kwargs)) as files:
        for f in files:
            if f.is_image() and arg_namespace.effects:
                f.default_effects = arg_namespace.effects.lstrip('-/')

            local_filepath = build_filepath(arg_namespace.path, f)
            dirname = os.path.dirname(local_filepath)

            if dirname and not os.path.exists(dirname):
                os.makedirs(dirname)

            if os.path.exists(local_filepath) and not arg_namespace.replace:
                pprint('File `{0}` already exists. '
                       'To override it use `--replace` option'
                       .format(local_filepath))
                continue

            url = f.cdn_url
            response = _get(url)

            if response:
                save_file_locally(local_filepath, response, f.size())