File size: 5,596 Bytes
75eba7e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fd1fd71
 
 
 
 
 
 
 
84e7abf
75eba7e
fd1fd71
 
 
bdb3458
 
fd1fd71
 
75eba7e
fd1fd71
9ea7cfa
 
bdb3458
 
 
1dce11f
 
 
 
 
 
bdb3458
fd1fd71
 
75eba7e
 
 
fd1fd71
 
 
 
 
 
75eba7e
 
 
 
 
bdb3458
75eba7e
 
 
 
 
fd1fd71
75eba7e
fd1fd71
75eba7e
fd1fd71
75eba7e
 
fd1fd71
 
 
 
 
1dce11f
 
 
7f45073
1dce11f
 
 
7f45073
1dce11f
 
fd1fd71
 
75eba7e
 
fd1fd71
75eba7e
 
 
 
 
 
 
bdb3458
75eba7e
 
 
fd1fd71
75eba7e
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
# import enkacard
# import enkacard.encbanner
# import enkanetwork
# import asyncio
# from flask import Flask, jsonify, request
# import requests
# from io import BytesIO
# from enkanetwork import EnkaNetworkAPI
# import os

# enka_update = EnkaNetworkAPI()

# async def update_genshin():
#     async with enka_update:
#         await enka_update.update_assets(lang = ["EN"], path="/tmp")

# app = Flask(__name__)

# async def card(id, designtype):
#     async with enkacard.encbanner.ENC(uid = str(id)) as encard:
#         return await encard.creat(template = (2 if str(designtype) == "2" else 1))
    
# @app.route("/<int:id>")
# def characters(id):
#     try:
#         characters = []
#         result = asyncio.run(card(id, request.args.get('design')))
#         for dt in result.card:
#          with BytesIO() as byte_io: 
#             print("prcess" + dt.name)
#             dt.card.save(byte_io, "PNG")
#             byte_io.seek(0)
#             image_url = upload_image(byte_io)
            
#             characters.append({
#                 "name": dt.name,
#                 "url": image_url
#             })
#             print("done" + dt.name)
#         return jsonify({'response': characters}) 
#     except enkanetwork.exception.VaildateUIDError:
#         # return jsonify({'error': 'Invalid UID. Please check your UID.'}), 400 

#         return jsonify({'error': 'Invalid UID. Please check your UID.'})

#     except enkacard.enc_error.ENCardError:
#         # return jsonify({'error': 'Enable display of the showcase in the game or add characters there.'}), 400 
#         return jsonify({'error': 'Enable display of the showcase in the game or add characters there.'})
#     except Exception as e:

#         return jsonify({'error': 'UNKNOWN ERR' + e.__str__()})


# @app.route("/")
# def hello_world():
#     return 'AMERICA YA HALLO!!'

# @app.route("/update_char")
# def upload():
#     data_dir = "/tmp/data"

#     if not os.path.exists(data_dir):
#         os.makedirs(data_dir)

#     data_dir = "/tmp/langs"

#     if not os.path.exists(data_dir):
#         os.makedirs(data_dir)
#     asyncio.run(update_genshin())
#     return 'Update smth ig!!'

# def upload_image(data):
#     url = "https://telegra.ph/upload"
#     files = {'file': ('file', data, "image/png")}  
#     response = requests.post(url, files=files)
    
#     if response.status_code != 200:
#         raise Exception(f"HTTP Error: {response.status_code}")
    
#     try:
#         body = response.json()
#         if isinstance(body, list) and 'src' in body[0]:
#             return "https://telegra.ph" + body[0]['src']
#         else:
#             raise Exception(f"Telegraph error: {body.get('error', 'Unknown error')}")
#     except (ValueError, KeyError, IndexError) as e:
#         raise Exception(f"Failed to parse response: {str(e)}")
    
# if __name__ == "__main__":
#     app.run(debug=False, use_reloader=False)
import enkacard
import enkacard.encbanner
import enkanetwork
import asyncio
from flask import Flask, jsonify, request
import requests
from io import BytesIO
from enkanetwork import EnkaNetworkAPI
import os
import base64

enka_update = EnkaNetworkAPI()

app = Flask(__name__)

async def update_genshin():
    async with enka_update:
        await enka_update.update_assets(lang=["EN"], path="/tmp")

@app.before_serving
def updet():
    """
    Run update_genshin before handling the first request.
    """
    data_dirs = ["/tmp/data", "/tmp/langs"]

    for directory in data_dirs:
        if not os.path.exists(directory):
            os.makedirs(directory)

    asyncio.run(update_genshin())

async def card(id, designtype):
    async with enkacard.encbanner.ENC(uid=str(id)) as encard:
        return await encard.creat(template=(2 if str(designtype) == "2" else 1))

@app.route("/<int:id>")
def characters(id):
    try:
        characters = []
        result = asyncio.run(card(id, request.args.get('design')))
        for dt in result.card:
            with BytesIO() as byte_io:
                print("processing " + dt.name)
                dt.card.save(byte_io, "PNG")
                byte_io.seek(0)
                image_url = upload_image(byte_io)

                characters.append({
                    "name": dt.name,
                    "url": image_url
                })
        return jsonify({'response': characters})
    except enkanetwork.exception.VaildateUIDError:
        return jsonify({'error': 'Invalid UID. Please check your UID.'}), 400
    except enkacard.enc_error.ENCardError:
        return jsonify({'error': 'Enable display of the showcase in the game or add characters there.'}), 400
    except Exception as e:
        print(e)
        return jsonify({'error': 'UNKNOWN ERR: ' + str(e)}), 500

@app.route("/")
def hello_world():
    return 'AMERICA YA HALLO!!'

# @app.route("/update_char")
# def upload():
#     data_dirs = ["/tmp/data", "/tmp/langs"]

#     for directory in data_dirs:
#         if not os.path.exists(directory):
#             os.makedirs(directory)

#     asyncio.run(update_genshin())
    # return 'Update completed!'

def upload_image(data):
    """
    Convert image data to a base64-encoded string.
    
    Parameters:
    - data: A BytesIO object containing the image data.
    Returns:
    - A base64-encoded string of the image.
    """
    # Convert the image data to a base64-encoded string
    image_base64 = base64.b64encode(data.getvalue()).decode('utf-8')

    # Return the base64-encoded string
    return image_base64

if __name__ == "__main__":
    app.run(debug=False, use_reloader=False)