File size: 7,296 Bytes
1e3b872
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# seealso: https://api.eagle.cool/item/add-from-path
# seealso: https://api.eagle.cool/item/add-from-paths
#
import requests
import os

import base64

DEBUG = False


def dprint(str):
    if DEBUG:
        print(str)


class EAGLE_ITEM_PATH:
    def __init__(
        self, filefullpath, filename="", website="", tags: list = [], annotation=""
    ):
        """Data container for addFromPath, addFromPaths

        Args:
            filefullpath : Required, full path of the local files.
            filename     : (option), name of image to be added.
            website      : (option), address of the source of the image.
            tags (list)  : (option), tags for the image.
            annotation   : (option), annotation for the image.
        """
        self.filefullpath = filefullpath
        self.filename = filename
        self.website = website
        self.tags = tags
        self.annotation = annotation

    def output_data(self):
        """
        output data in json format for POST
        """
        _data = {
            "path": self.filefullpath,
            "name": os.path.splitext(os.path.basename(self.filefullpath))[0]
            if (self.filename == None or self.filename == "")
            else self.filename,
        }
        if self.website and self.website != "":
            _data.update({"website": self.website})
        if self.tags and len(self.tags) > 0:
            _data.update({"tags": self.tags})
        if self.annotation and self.annotation != "":
            _data.update({"annotation": self.annotation})
        return _data


class EAGLE_ITEM_URL:
    def __init__(
        self,
        url,
        name,
        website="",
        tags=[],
        annotation="",
        modificationTime="",
        folderId="",
        headers={},
    ):
        """Data container for addFromURL, addFromURLs
        url  : Required, the URL of the image to be added. Supports http, https, base64
        name : Required, The name of the image to be added.
        website : The Address of the source of the image
        tags: Tags for the image.
        annotation: The annotation for the image.
        modificationTime: The creation date of the image. The parameter can be used to alter the image's sorting order in Eagle.
        headers: Optional, customize the HTTP headers properties, this could be used to circumvent the security of certain websites.

        folderId: If this parameter is defined, the image will be added to the corresponding folder.
        """
        self.url = url
        self.name = name
        self.website = website
        self.tags = tags
        self.annotation = annotation
        self.modificationTime = modificationTime
        self.folderId = folderId
        self.headers = headers

    def convert_file_to_base64url(self, filepath=None):
        if not filepath or filepath == "":
            if self.url and self.url != "":
                filepath = self.url
            else:
                print("Error convert_file_to_base64url: invalid filepath")
                return filepath
        else:
            self.url = filepath
        if not os.path.exists(filepath):
            print("Error convert_file_to_base64url: file not found.")
            return filepath
        try:
            with open(filepath, "rb") as file:
                enc_file = base64.urlsafe_b64encode(file.read())
                self.url = f"data:image/png;base64, {enc_file.decode('utf-8')}"
        except Exception as e:
            print("Error convert_file_to_base64url: eocode failed")
            print(e)
            return filepath

        return self.url

    def output_data(self):
        """
        output data in json format for POST
        """
        _data = {"url": self.url, "name": self.name}
        # add optional data
        if self.website and self.website != "":
            _data.update({"website": self.website})
        if self.tags and len(self.tags) > 0:
            _data.update({"tags": self.tags})
        if self.annotation and self.annotation != "":
            _data.update({"annotation": self.annotation})
        if self.modificationTime and self.modificationTime != "":
            _data.update({"modificationTime": self.modificationTime})
        if self.folderId and self.folderId != "":
            _data.update({"folderId": self.folderId})
        if self.headers and len(self.headers) > 0:
            _data.update({"headers": self.headers})
        return _data


def add_from_URL(
    item: EAGLE_ITEM_URL, folderId=None, server_url="http://localhost", port=41595
):
    API_URL = f"{server_url}:{port}/api/item/addFromURL"
    _data = item.output_data()
    if folderId and folderId != "":
        _data.update({"folderId": folderId})
    r_post = requests.post(API_URL, json=_data)
    return r_post


def add_from_URL_base64(
    item: EAGLE_ITEM_URL, folderId=None, server_url="http://localhost", port=41595
):
    API_URL = f"{server_url}:{port}/api/item/addFromURL"
    item.url = item.convert_file_to_base64url()
    _data = item.output_data()
    if folderId and folderId != "":
        _data.update({"folderId": folderId})
    r_post = requests.post(API_URL, json=_data)
    return r_post


def add_from_path(
    item: EAGLE_ITEM_PATH, folderId=None, server_url="http://localhost", port=41595
):
    API_URL = f"{server_url}:{port}/api/item/addFromPath"
    _data = item.output_data()
    if folderId and folderId != "":
        _data.update({"folderId": folderId})
    r_post = requests.post(API_URL, json=_data)
    return r_post


def add_from_paths(
    files, folderId=None, server_url="http://localhost", port=41595, step=None
):
    """EAGLE API:/api/item/addFromPaths

    Method: POST

    Args:
        path: Required, the path of the local files.
        name: Required, the name of images to be added.
        website: The Address of the source of the images.
        annotation: The annotation for the images.
        tags: Tags for the images.
        folderId: If this parameter is defined, the image will be added to the corresponding folder.
        step: interval image num of doing POST. Defaults is None (disabled)

    Returns:
        Response: return of requests.posts
    """
    API_URL = f"{server_url}:{port}/api/item/addFromPaths"

    if step:
        step = int(step)

    def _init_data():
        _data = {"items": []}
        if folderId and folderId != "":
            _data.update({"folderId": folderId})
        return _data

    r_posts = []
    data = _init_data()
    for _index, _item in enumerate(files):
        _item: EAGLE_ITEM_PATH = _item
        _data = _item.output_data()
        if _data:
            data["items"].append(_data)
        if step and step > 0:
            if ((_index + 1) - ((_index + 1) // step) * step) == 0:
                _ret = requests.post(API_URL, json=data)
                try:
                    r_posts.append(_ret.json())
                except:
                    r_posts.append(_ret)
                data = _init_data()
    if (len(data["items"]) > 0) or (not step or step <= 0):
        _ret = requests.post(API_URL, json=data)
        try:
            r_posts.append(_ret.json())
        except:
            r_posts.append(_ret)

    return [x for x in r_posts if x != ""]