Iwak commited on
Commit
75eba7e
·
1 Parent(s): a23de7e
Files changed (1) hide show
  1. main.py +145 -45
main.py CHANGED
@@ -1,3 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import enkacard
2
  import enkacard.encbanner
3
  import enkanetwork
@@ -7,47 +98,44 @@ import requests
7
  from io import BytesIO
8
  from enkanetwork import EnkaNetworkAPI
9
  import os
 
10
 
11
  enka_update = EnkaNetworkAPI()
12
 
13
  async def update_genshin():
14
  async with enka_update:
15
- await enka_update.update_assets(lang = ["EN"], path="/tmp")
16
 
17
  app = Flask(__name__)
18
 
19
  async def card(id, designtype):
20
- async with enkacard.encbanner.ENC(uid = str(id)) as encard:
21
- return await encard.creat(template = (2 if str(designtype) == "2" else 1))
22
-
23
  @app.route("/<int:id>")
24
  def characters(id):
25
  try:
26
  characters = []
27
  result = asyncio.run(card(id, request.args.get('design')))
28
  for dt in result.card:
29
- with BytesIO() as byte_io:
30
- dt.card.save(byte_io, "PNG")
31
- byte_io.seek(0)
32
- image_url = upload_image(byte_io)
33
-
34
- characters.append({
35
- "name": dt.name,
36
- "url": image_url
37
- })
38
- return jsonify({'response': characters})
 
39
  except enkanetwork.exception.VaildateUIDError:
40
- # return jsonify({'error': 'Invalid UID. Please check your UID.'}), 400
41
-
42
- return jsonify({'error': 'Invalid UID. Please check your UID.'})
43
-
44
  except enkacard.enc_error.ENCardError:
45
- # return jsonify({'error': 'Enable display of the showcase in the game or add characters there.'}), 400
46
- return jsonify({'error': 'Enable display of the showcase in the game or add characters there.'})
47
  except Exception as e:
48
-
49
- return jsonify({'error': 'UNKNOWN ERR' + e.__str__()})
50
-
51
 
52
  @app.route("/")
53
  def hello_world():
@@ -55,34 +143,46 @@ def hello_world():
55
 
56
  @app.route("/update_char")
57
  def upload():
58
- data_dir = "/tmp/data"
59
-
60
- if not os.path.exists(data_dir):
61
- os.makedirs(data_dir)
62
 
63
- data_dir = "/tmp/langs"
 
 
64
 
65
- if not os.path.exists(data_dir):
66
- os.makedirs(data_dir)
67
  asyncio.run(update_genshin())
68
- return 'Update smth ig!!'
69
 
70
  def upload_image(data):
71
- url = "https://telegra.ph/upload"
72
- files = {'file': ('file', data, "image/png")}
73
- response = requests.post(url, files=files)
74
 
75
- if response.status_code != 200:
76
- raise Exception(f"HTTP Error: {response.status_code}")
 
 
 
 
 
 
77
 
78
- try:
79
- body = response.json()
80
- if isinstance(body, list) and 'src' in body[0]:
81
- return "https://telegra.ph" + body[0]['src']
82
- else:
83
- raise Exception(f"Telegraph error: {body.get('error', 'Unknown error')}")
84
- except (ValueError, KeyError, IndexError) as e:
85
- raise Exception(f"Failed to parse response: {str(e)}")
86
 
 
 
 
 
 
 
 
 
 
 
 
 
87
  if __name__ == "__main__":
88
- app.run(debug=False, use_reloader=False)
 
1
+ # import enkacard
2
+ # import enkacard.encbanner
3
+ # import enkanetwork
4
+ # import asyncio
5
+ # from flask import Flask, jsonify, request
6
+ # import requests
7
+ # from io import BytesIO
8
+ # from enkanetwork import EnkaNetworkAPI
9
+ # import os
10
+
11
+ # enka_update = EnkaNetworkAPI()
12
+
13
+ # async def update_genshin():
14
+ # async with enka_update:
15
+ # await enka_update.update_assets(lang = ["EN"], path="/tmp")
16
+
17
+ # app = Flask(__name__)
18
+
19
+ # async def card(id, designtype):
20
+ # async with enkacard.encbanner.ENC(uid = str(id)) as encard:
21
+ # return await encard.creat(template = (2 if str(designtype) == "2" else 1))
22
+
23
+ # @app.route("/<int:id>")
24
+ # def characters(id):
25
+ # try:
26
+ # characters = []
27
+ # result = asyncio.run(card(id, request.args.get('design')))
28
+ # for dt in result.card:
29
+ # with BytesIO() as byte_io:
30
+ # print("prcess" + dt.name)
31
+ # dt.card.save(byte_io, "PNG")
32
+ # byte_io.seek(0)
33
+ # image_url = upload_image(byte_io)
34
+
35
+ # characters.append({
36
+ # "name": dt.name,
37
+ # "url": image_url
38
+ # })
39
+ # print("done" + dt.name)
40
+ # return jsonify({'response': characters})
41
+ # except enkanetwork.exception.VaildateUIDError:
42
+ # # return jsonify({'error': 'Invalid UID. Please check your UID.'}), 400
43
+
44
+ # return jsonify({'error': 'Invalid UID. Please check your UID.'})
45
+
46
+ # except enkacard.enc_error.ENCardError:
47
+ # # return jsonify({'error': 'Enable display of the showcase in the game or add characters there.'}), 400
48
+ # return jsonify({'error': 'Enable display of the showcase in the game or add characters there.'})
49
+ # except Exception as e:
50
+
51
+ # return jsonify({'error': 'UNKNOWN ERR' + e.__str__()})
52
+
53
+
54
+ # @app.route("/")
55
+ # def hello_world():
56
+ # return 'AMERICA YA HALLO!!'
57
+
58
+ # @app.route("/update_char")
59
+ # def upload():
60
+ # data_dir = "/tmp/data"
61
+
62
+ # if not os.path.exists(data_dir):
63
+ # os.makedirs(data_dir)
64
+
65
+ # data_dir = "/tmp/langs"
66
+
67
+ # if not os.path.exists(data_dir):
68
+ # os.makedirs(data_dir)
69
+ # asyncio.run(update_genshin())
70
+ # return 'Update smth ig!!'
71
+
72
+ # def upload_image(data):
73
+ # url = "https://telegra.ph/upload"
74
+ # files = {'file': ('file', data, "image/png")}
75
+ # response = requests.post(url, files=files)
76
+
77
+ # if response.status_code != 200:
78
+ # raise Exception(f"HTTP Error: {response.status_code}")
79
+
80
+ # try:
81
+ # body = response.json()
82
+ # if isinstance(body, list) and 'src' in body[0]:
83
+ # return "https://telegra.ph" + body[0]['src']
84
+ # else:
85
+ # raise Exception(f"Telegraph error: {body.get('error', 'Unknown error')}")
86
+ # except (ValueError, KeyError, IndexError) as e:
87
+ # raise Exception(f"Failed to parse response: {str(e)}")
88
+
89
+ # if __name__ == "__main__":
90
+ # app.run(debug=False, use_reloader=False)
91
+
92
  import enkacard
93
  import enkacard.encbanner
94
  import enkanetwork
 
98
  from io import BytesIO
99
  from enkanetwork import EnkaNetworkAPI
100
  import os
101
+ import base64
102
 
103
  enka_update = EnkaNetworkAPI()
104
 
105
  async def update_genshin():
106
  async with enka_update:
107
+ await enka_update.update_assets(lang=["EN"], path="/tmp")
108
 
109
  app = Flask(__name__)
110
 
111
  async def card(id, designtype):
112
+ async with enkacard.encbanner.ENC(uid=str(id)) as encard:
113
+ return await encard.creat(template=(2 if str(designtype) == "2" else 1))
114
+
115
  @app.route("/<int:id>")
116
  def characters(id):
117
  try:
118
  characters = []
119
  result = asyncio.run(card(id, request.args.get('design')))
120
  for dt in result.card:
121
+ with BytesIO() as byte_io:
122
+ print("processing " + dt.name)
123
+ dt.card.save(byte_io, "PNG")
124
+ byte_io.seek(0)
125
+ image_url = upload_image(byte_io)
126
+
127
+ characters.append({
128
+ "name": dt.name,
129
+ "url": image_url
130
+ })
131
+ return jsonify({'response': characters})
132
  except enkanetwork.exception.VaildateUIDError:
133
+ return jsonify({'error': 'Invalid UID. Please check your UID.'}), 400
 
 
 
134
  except enkacard.enc_error.ENCardError:
135
+ return jsonify({'error': 'Enable display of the showcase in the game or add characters there.'}), 400
 
136
  except Exception as e:
137
+ print(e)
138
+ return jsonify({'error': 'UNKNOWN ERR: ' + str(e)}), 500
 
139
 
140
  @app.route("/")
141
  def hello_world():
 
143
 
144
  @app.route("/update_char")
145
  def upload():
146
+ data_dirs = ["/tmp/data", "/tmp/langs"]
 
 
 
147
 
148
+ for directory in data_dirs:
149
+ if not os.path.exists(directory):
150
+ os.makedirs(directory)
151
 
 
 
152
  asyncio.run(update_genshin())
153
+ return 'Update completed!'
154
 
155
  def upload_image(data):
156
+ """
157
+ Convert image data to a base64-encoded string.
 
158
 
159
+ Parameters:
160
+ - data: A BytesIO object containing the image data.
161
+
162
+ Returns:
163
+ - A base64-encoded string of the image.
164
+ """
165
+ # Convert the image data to a base64-encoded string
166
+ image_base64 = base64.b64encode(data.getvalue()).decode('utf-8')
167
 
168
+ # Return the base64-encoded string
169
+ return image_base64
170
+ # def upload_image(data):
171
+ # url = "https://telegra.ph/upload"
172
+ # files = {'file': ('file', data, "image/png")}
173
+ # response = requests.post(url, files=files)
 
 
174
 
175
+ # if response.status_code != 200:
176
+ # raise Exception(f"HTTP Error: {response.status_code}")
177
+
178
+ # try:
179
+ # body = response.json()
180
+ # if isinstance(body, list) and 'src' in body[0]:
181
+ # return "https://telegra.ph" + body[0]['src']
182
+ # else:
183
+ # raise Exception(f"Telegraph error: {body.get('error', 'Unknown error')}")
184
+ # except (ValueError, KeyError, IndexError) as e:
185
+ # raise Exception(f"Failed to parse response: {str(e)}")
186
+
187
  if __name__ == "__main__":
188
+ app.run(debug=False, use_reloader=False)