Example #1
0
def __upload_to_imgur(path, caption):
    if not isfile(path):
        return

    # TODO: Convert to GIF and upload.
    if path[-3:] == 'mp4':
        remove(path)
        return

    im = Imgur(environ.get('IMGUR_CLIENT_ID'), environ.get('IMGUR_CLIENT_KEY'),
               environ.get('IMGUR_ACCESS_TOKEN'),
               environ.get('IMGUR_REFRESH_TOKEN'))
    for _ in range(5):
        # noinspection PyBroadException
        try:
            im.upload_image(path=abspath(path),
                            title=caption,
                            album=environ.get('IMGUR_ALBUM'))
        except Exception:
            im.refresh_access_token()
            sleep(10)
            continue

        remove(path)
        return
Example #2
0
def __upload_to_imgur(path, caption):
    log_debug('__upload started')
    if not isfile(path):
        log_warn('File to be uploaded not found')
        return

    # TODO: Convert to GIF and upload.
    if path[-3:] == 'mp4':
        remove(path)
        log_warn('Skipping mp4 upload')
        return

    log_debug('Authorizing imgur client')
    im = Imgur(environ.get('IMGUR_CLIENT_ID'), environ.get('IMGUR_CLIENT_KEY'),
               environ.get('IMGUR_ACCESS_TOKEN'),
               environ.get('IMGUR_REFRESH_TOKEN'))

    for _ in range(5):
        try:
            im.upload_image(path=abspath(path),
                            title=caption,
                            album=environ.get('IMGUR_ALBUM'))
            log_debug('Image successfully uploaded')
            break
        except Exception:
            log_warn('Upload failed, refreshing token')
            im.refresh_access_token()
            sleep(10)
            continue
    else:
        log_error('Upload failed, proceeding')

    log_debug('Deleting file')
    remove(path)
    return
Example #3
0
def get_authed_client():
    refresh_token = config.get(config.IMGUR_SECTION, 'refresh_token')
    if refresh_token:
        client = Imgur(client_id=config.IMGUR_CLIENT_ID,
                       client_secret=config.IMGUR_CLIENT_SECRET,
                       refresh_token=refresh_token)
    else:
        client = Imgur(client_id=config.IMGUR_CLIENT_ID,
                       client_secret=config.IMGUR_CLIENT_SECRET)
        auth_with_pin(client)

    try:
        access_token = client.refresh_access_token()
        config.set(config.IMGUR_SECTION, 'access_token', access_token)
    except Exception as e:
        print(e)
        print('Access token failed to refresh')
        auth_with_pin(client)
        client = Imgur(client_id=config.IMGUR_CLIENT_ID,
                       client_secret=config.IMGUR_CLIENT_SECRET,
                       refresh_token=config.get(config.IMGUR_SECTION,
                                                'refresh_token'))

    return client