main   A
last analyzed

Complexity

Total Complexity 24

Size/Duplication

Total Lines 152
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 24
eloc 89
dl 0
loc 152
rs 10
c 0
b 0
f 0

7 Functions

Rating   Name   Duplication   Size   Complexity  
A upload() 0 24 3
A download() 0 22 5
A _create_imageb64_list() 0 8 3
A show_version() 0 3 1
A _convert_extension_to_content_type() 0 10 4
A convert() 0 23 3
A main() 0 11 5
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
    print(r["release_date"])
94
    upload_id = r["upload_id"]
95
    print("upload_id: {}".format(upload_id))
96
97
98
def download(args):
99
    """download result PDF"""
100
101
    if not args["--host"]:
102
        host = "http://localhost"
103
    else:
104
        host = args["<host>"]
105
    if not args["--port"]:
106
        port = 8080
107
    else:
108
        port = args["<port>"]
109
    upload_id = args["<id>"]
110
    file_name = args["<file>"]
111
    print("URL: {host}:{port}".format(host=host, port=port))
112
    url = "{host}:{port}/convert/pdf/download".format(host=host, port=port)
113
    data = json.dumps({"uploadId": upload_id})
114
    r = requests.post(url, data=data)
115
    if r.status_code == requests.codes.ok:
116
        with open(file_name, "wb") as result_file:
117
            result_file.write(r.content)
118
    else:
119
        print("No result file")
120
121
122
def show_version():
123
    """Show version"""
124
    print("ebook-homebrew Rest Client: {version}".format(version=__version__))
125
126
127
def _convert_extension_to_content_type(extension):
128
    if extension == "jpg":
129
        content_type = "image/jpeg"
130
    elif extension == "png":
131
        content_type = "image/png"
132
    elif extension == "gif":
133
        content_type = "image/gif"
134
    else:
135
        content_type = False
136
    return content_type
137
138
139
def _create_imageb64_list(directory, extension):
140
    images_b64 = []
141
    images = glob.glob(os.path.join(directory, "*." + extension))
142
    for image in images:
143
        with open(image, "rb") as image_binary:
144
            images_b64.append(base64.b64encode(image_binary.read()).decode("utf-8"))
145
146
    return images_b64
147
148
149
if __name__ == "__main__":
150
    args = docopt(__doc__)
151
    main(args)
152