Passed
Push — master ( 5e64b7...572a73 )
by Yu
01:45 queued 11s
created

main.show_version()   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
"""
2
Overview:
3
  Client App with ebook-homebrew's rest API
4
5
Usage:
6
  main.py [-h|--help] [-v|--version]
7
  main.py upload <directory> <extension> [--host <host>] [--port <port>]
8
  main.py convert <id> <extension> [--host <host>] [--port <port>]
9
  main.py download <id> <file> [--host <host>] [--port <port>]
10
11
Options:
12
  upload         : upload
13
  convert        : convert
14
  <directory>    : directory
15
  <extension>    : extension
16
  <id>           : upload_id
17
  <file>         : filename
18
  -h, --help     : show this help message and exit
19
  -v, --version  : show version
20
  --host         : API server host
21
  --port         : API server port
22
"""
23
24
import json
25
import os
26
import glob
27
import base64
28
import requests
29
from docopt import docopt
30
31
__version__ = "2.0.0"
32
33
34
def main(args):
35
    """Call submodules"""
36
37
    if args["upload"]:
38
        upload(args)
39
    elif args["convert"]:
40
        convert(args)
41
    elif args["download"]:
42
        download(args)
43
    elif args["--version"]:
44
        show_version()
45
46
47
def upload(args):
48
    """upload image file"""
49
    if not args["--host"]:
50
        host = "http://localhost"
51
    else:
52
        host = args["<host>"]
53
    if not args["--port"]:
54
        port = 8080
55
    else:
56
        port = args["<port>"]
57
    print("URL: {host}:{port}".format(host=host, port=port))
58
    url = "{host}:{port}/data/upload".format(host=host, port=port)
59
60
    directory = args["<directory>"]
61
    extension = args["<extension>"]
62
63
    content_type = _convert_extension_to_content_type(extension)
64
65
    images_b64 = _create_imageb64_list(directory, extension)
66
67
    data = json.dumps({"contentType": content_type, "images": images_b64})
68
    r = requests.post(url, data=data).json()
69
    upload_id = r["upload_id"]
70
    print("upload_id: {}".format(upload_id))
71
72
73
def convert(args):
74
    """convert requests"""
75
76
    if not args["--host"]:
77
        host = "http://localhost"
78
    else:
79
        host = args["<host>"]
80
    if not args["--port"]:
81
        port = 8080
82
    else:
83
        port = args["<port>"]
84
    upload_id = args["<id>"]
85
    extension = args["<extension>"]
86
    print("URL: {host}:{port}".format(host=host, port=port))
87
    url = "{host}:{port}/convert/pdf".format(host=host, port=port)
88
89
    content_type = _convert_extension_to_content_type(extension)
90
91
    data = json.dumps({"uploadId": upload_id, "contentType": content_type})
92
    r = requests.post(url, data=data).json()
93
    upload_id = r["upload_id"]
94
    print("upload_id: {}".format(upload_id))
95
96
97
def download(args):
98
    """download result PDF"""
99
100
    if not args["--host"]:
101
        host = "http://localhost"
102
    else:
103
        host = args["<host>"]
104
    if not args["--port"]:
105
        port = 8080
106
    else:
107
        port = args["<port>"]
108
    upload_id = args["<id>"]
109
    file_name = args["<file>"]
110
    print("URL: {host}:{port}".format(host=host, port=port))
111
    url = "{host}:{port}/convert/pdf/download".format(host=host, port=port)
112
    data = json.dumps({"uploadId": upload_id})
113
    r = requests.post(url, data=data)
114
    if r.status_code == requests.codes.ok:
115
        with open(file_name, "wb") as result_file:
116
            result_file.write(r.content)
117
    else:
118
        print("No result file")
119
120
121
def show_version():
122
    """Show version"""
123
    print("ebook-homebrew Rest Client: {version}".format(version=__version__))
124
125
126
def _convert_extension_to_content_type(extension):
127
    if extension == "jpg":
128
        content_type = "image/jpeg"
129
    elif extension == "png":
130
        content_type = "image/png"
131
    elif extension == "gif":
132
        content_type = "image/gif"
133
    else:
134
        content_type = False
135
    return content_type
136
137
138
def _create_imageb64_list(directory, extension):
139
    images_b64 = []
140
    images = glob.glob(os.path.join(directory, "*." + extension))
141
    for image in images:
142
        with open(image, "rb") as image_binary:
143
            images_b64.append(base64.b64encode(image_binary.read()).decode("utf-8"))
144
145
    return images_b64
146
147
148
if __name__ == "__main__":
149
    args = docopt(__doc__)
150
    main(args)
151