<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

from __future__ import absolute_import, unicode_literals

from argparse import ArgumentParser
from threading import Thread
from time import sleep
import sys

import requests

from . import __version__


def parse_html(html):
    return html.splitlines()


def upload(f):
    url = 'http://www.bild.me/index.php'
    data = {
        't': 1,
        'C1': 'ON',
        'upload': 1,
    }
    files = {
        'F1': f
    }
    try:
        html = requests.post(url, data=data, files=files).text
        return {'status': 0, 'result': parse_html(html)}
    except requests.exceptions:
        return {'status': 1, 'message': 'Upload failed!'}
    except Exception as e:
        return {'status': 1, 'message': e}


class ProgressBar(object):
    def __init__(self, list_all):
        self.progress = {}
        self.list_all = list_all

    def run(self):
        f = sys.stdout

        for (k, p) in self.progress.items():
            for x in range(1, 50):
                if p['finish']:
                    x = 50
                s = '{0}: [{1}]'.format(k, ('=' * x + '>').ljust(50, '-'))
                f.write(s)
                f.flush()
                f.write('\r')

                if x != 50:
                    sleep(0.3)
                else:
                    break
            f.write('\n')
            if p['finish']:
                result = p['result']
                if result['status'] == 1:
                    sys.stderr.write(result['message'] + '\n')
                if self.list_all:
                    print('\n\n'.join(result['result']))
                else:
                    print(result['result'][5])


class UploadThread(Thread):
    def __init__(self, img, bar, *args, **kwargs):
        self.img = img
        self.bar = bar
        super(UploadThread, self).__init__(*args, **kwargs)

    def run(self):
        with open(self.img, 'rb') as f:
            self.bar.progress[f.name] = {'finish': False}
            result = upload(f)
            self.bar.progress[f.name]['finish'] = True
            self.bar.progress[f.name]['result'] = result


def main():
    parser = ArgumentParser(description='CLI tool for bild.me.')
    parser.add_argument('-V', '--version', action='version',
                        version=__version__)
    parser.add_argument('-l', '--list', action='store_true',
                        help='list all result')
    parser.add_argument('-f', '-F', '--file', required=True,
                        nargs='+', help='picture file')
    args = parser.parse_args()
    files = set(args.file)
    list_all = args.list
    bar = ProgressBar(list_all)

    for n, img in enumerate(files):
        t = UploadThread(img, bar)
        t.start()

    sleep(0.2)
    bar.run()

if __name__ == '__main__':
    main()<commit_msg>add [-q|--quiet] argument for hide progress bar<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

from __future__ import absolute_import, unicode_literals

from argparse import ArgumentParser
from threading import Thread, active_count
from time import sleep
import sys

import requests

from . import __version__


def parse_html(html):
    return html.splitlines()


def upload(f):
    url = 'http://www.bild.me/index.php'
    data = {
        't': 1,
        'C1': 'ON',
        'upload': 1,
    }
    files = {
        'F1': f
    }
    try:
        html = requests.post(url, data=data, files=files).text
        return {'status': 0, 'result': parse_html(html)}
    except requests.exceptions:
        return {'status': 1, 'message': 'Upload failed!'}
    except Exception as e:
        return {'status': 1, 'message': e}


def output_result(result, list_all):
    if result['status'] == 1:
        sys.stderr.write(result['message'] + '\n')
    if list_all:
        print('\n\n'.join(result['result']))
    else:
        print(result['result'][5])


class ProgressBar(object):
    def __init__(self, list_all, show=True):
        self.progress = {}
        self.list_all = list_all
        self.show = show

    def output(self, name, n, size):
        s = '{0}: [{1}]\r'.format(name, ('=' * n + '>').ljust(size))
        sys.stdout.write(s)
        sys.stdout.flush()

    def run(self):
        size = 50
        for x in range(1, size + 1):
            for (k, p) in self.progress.items():
                _x = x
                if p['finish']:
                    _x = size
                if self.show:
                    self.output(k, _x, size)

                if p['finish']:
                    if self.show:
                        print('')
                    result = p['result']
                    output_result(result, self.list_all)
                    del self.progress[k]
                    continue

                sleep(0.3)

            if not self.progress:
                break


class UploadThread(Thread):
    def __init__(self, img, bar, *args, **kwargs):
        self.img = img
        self.bar = bar
        super(UploadThread, self).__init__(*args, **kwargs)

    def run(self):
        with open(self.img, 'rb') as f:
            self.bar.progress[f.name] = {'finish': False}
            result = upload(f)
            self.bar.progress[f.name]['finish'] = True
            self.bar.progress[f.name]['result'] = result


def main():
    parser = ArgumentParser(description='CLI tool for bild.me.')
    parser.add_argument('-V', '--version', action='version',
                        version=__version__)
    parser.add_argument('-l', '--list', action='store_true',
                        help='list all result')
    parser.add_argument('-q', '--quiet', action='store_true',
                        help='decrease verbosity')
    parser.add_argument('-f', '-F', '--file', required=True,
                        nargs='+', help='picture file')
    args = parser.parse_args()
    files = set(args.file)
    list_all = args.list
    show = not args.quiet
    bar = ProgressBar(list_all, show)

    for n, img in enumerate(files):
        t = UploadThread(img, bar)
        t.start()

    while True:
        n = active_count()
        if n == len(files):
            bar.run()
            break

if __name__ == '__main__':
    main()
{'fraction_non_alphanumeric': 0.14742251485466518, 'fraction_numerical': 0.006905411915850329, 'mean_word_length': 1.7928251121076233, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3040917', 'n_tokens_mistral': 1947, 'n_tokens_neox': 1859, 'n_words': 458}