ambon commited on
Commit
cdfff16
·
verified ·
1 Parent(s): d885278

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +43 -147
main.py CHANGED
@@ -1,94 +1,3 @@
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,44 +7,43 @@ import requests
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,46 +51,34 @@ 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)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import enkacard
2
  import enkacard.encbanner
3
  import enkanetwork
 
7
  from io import BytesIO
8
  from enkanetwork import EnkaNetworkAPI
9
  import os
 
10
 
11
  enka_update = EnkaNetworkAPI()
12
 
 
 
 
 
13
  app = Flask(__name__)
14
 
15
  async def card(id, designtype):
16
+ async with enkacard.encbanner.ENC(uid = str(id)) as encard:
17
+ return await encard.creat(template = (2 if str(designtype) == "2" else 1))
18
+
19
  @app.route("/<int:id>")
20
  def characters(id):
21
  try:
22
  characters = []
23
  result = asyncio.run(card(id, request.args.get('design')))
24
  for dt in result.card:
25
+ with BytesIO() as byte_io:
26
+ dt.card.save(byte_io, "PNG")
27
+ byte_io.seek(0)
28
+ image_url = upload_image(byte_io)
29
+
30
+ characters.append({
31
+ "name": dt.name,
32
+ "url": image_url
33
+ })
34
+ return jsonify({'response': characters})
 
35
  except enkanetwork.exception.VaildateUIDError:
36
+ # return jsonify({'error': 'Invalid UID. Please check your UID.'}), 400
37
+
38
+ return jsonify({'error': 'Invalid UID. Please check your UID.'})
39
+
40
  except enkacard.enc_error.ENCardError:
41
+ # return jsonify({'error': 'Enable display of the showcase in the game or add characters there.'}), 400
42
+ return jsonify({'error': 'Enable display of the showcase in the game or add characters there.'})
43
  except Exception as e:
44
+
45
+ return jsonify({'error': 'UNKNOWN ERR' + e.__str__()})
46
+
47
 
48
  @app.route("/")
49
  def hello_world():
 
51
 
52
  @app.route("/update_char")
53
  def upload():
54
+ data_dir = "/tmp/data"
55
+
56
+ if not os.path.exists(data_dir):
57
+ os.makedirs(data_dir)
58
 
59
+ data_dir = "/tmp/langs"
 
 
60
 
61
+ if not os.path.exists(data_dir):
62
+ os.makedirs(data_dir)
63
  asyncio.run(update_genshin())
64
+ return 'Update smth ig!!'
65
 
66
  def upload_image(data):
67
+ url = "https://telegra.ph/upload"
68
+ files = {'file': ('file', data, "image/png")}
69
+ response = requests.post(url, files=files)
70
 
71
+ if response.status_code != 200:
72
+ raise Exception(f"HTTP Error: {response.status_code}")
 
 
 
 
 
 
 
 
 
 
 
 
 
73
 
74
+ try:
75
+ body = response.json()
76
+ if isinstance(body, list) and 'src' in body[0]:
77
+ return "https://telegra.ph" + body[0]['src']
78
+ else:
79
+ raise Exception(f"Telegraph error: {body.get('error', 'Unknown error')}")
80
+ except (ValueError, KeyError, IndexError) as e:
81
+ raise Exception(f"Failed to parse response: {str(e)}")
82
 
 
 
 
 
 
 
 
 
 
83
  if __name__ == "__main__":
84
  app.run(debug=False, use_reloader=False)