Akane710 commited on
Commit
afc230a
·
verified ·
1 Parent(s): fbac4c6

Upload 5 files

Browse files
Files changed (5) hide show
  1. .gitignore +162 -0
  2. Dockerfile +33 -0
  3. Pipfile +12 -0
  4. main.py +128 -0
  5. pyenv_run.sh +8 -0
.gitignore ADDED
@@ -0,0 +1,162 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Byte-compiled / optimized / DLL files
2
+ __pycache__/
3
+ *.py[cod]
4
+ *$py.class
5
+
6
+ # C extensions
7
+ *.so
8
+ myenv
9
+
10
+ # Distribution / packaging
11
+ .Python
12
+ build/
13
+ develop-eggs/
14
+ dist/
15
+ downloads/
16
+ eggs/
17
+ .eggs/
18
+ lib/
19
+ lib64/
20
+ parts/
21
+ sdist/
22
+ var/
23
+ wheels/
24
+ share/python-wheels/
25
+ *.egg-info/
26
+ .installed.cfg
27
+ *.egg
28
+ MANIFEST
29
+
30
+ # PyInstaller
31
+ # Usually these files are written by a python script from a template
32
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
33
+ *.manifest
34
+ *.spec
35
+
36
+ # Installer logs
37
+ pip-log.txt
38
+ pip-delete-this-directory.txt
39
+
40
+ # Unit test / coverage reports
41
+ htmlcov/
42
+ .tox/
43
+ .nox/
44
+ .coverage
45
+ .coverage.*
46
+ .cache
47
+ nosetests.xml
48
+ coverage.xml
49
+ *.cover
50
+ *.py,cover
51
+ .hypothesis/
52
+ .pytest_cache/
53
+ cover/
54
+
55
+ # Translations
56
+ *.mo
57
+ *.pot
58
+
59
+ # Django stuff:
60
+ *.log
61
+ local_settings.py
62
+ db.sqlite3
63
+ db.sqlite3-journal
64
+
65
+ # Flask stuff:
66
+ instance/
67
+ .webassets-cache
68
+
69
+ # Scrapy stuff:
70
+ .scrapy
71
+
72
+ # Sphinx documentation
73
+ docs/_build/
74
+
75
+ # PyBuilder
76
+ .pybuilder/
77
+ target/
78
+
79
+ # Jupyter Notebook
80
+ .ipynb_checkpoints
81
+
82
+ # IPython
83
+ profile_default/
84
+ ipython_config.py
85
+
86
+ # pyenv
87
+ # For a library or package, you might want to ignore these files since the code is
88
+ # intended to run in multiple environments; otherwise, check them in:
89
+ # .python-version
90
+
91
+ # pipenv
92
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
93
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
94
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
95
+ # install all needed dependencies.
96
+ #Pipfile.lock
97
+
98
+ # poetry
99
+ # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
100
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
101
+ # commonly ignored for libraries.
102
+ # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
103
+ #poetry.lock
104
+
105
+ # pdm
106
+ # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
107
+ #pdm.lock
108
+ # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
109
+ # in version control.
110
+ # https://pdm.fming.dev/#use-with-ide
111
+ .pdm.toml
112
+
113
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
114
+ __pypackages__/
115
+
116
+ # Celery stuff
117
+ celerybeat-schedule
118
+ celerybeat.pid
119
+
120
+ # SageMath parsed files
121
+ *.sage.py
122
+
123
+ # Environments
124
+ .env
125
+ .venv
126
+ env/
127
+ venv/
128
+ ENV/
129
+ env.bak/
130
+ venv.bak/
131
+
132
+ # Spyder project settings
133
+ .spyderproject
134
+ .spyproject
135
+
136
+ # Rope project settings
137
+ .ropeproject
138
+
139
+ # mkdocs documentation
140
+ /site
141
+
142
+ # mypy
143
+ .mypy_cache/
144
+ .dmypy.json
145
+ dmypy.json
146
+
147
+ # Pyre type checker
148
+ .pyre/
149
+
150
+ # pytype static type analyzer
151
+ .pytype/
152
+
153
+ # Cython debug symbols
154
+ cython_debug/
155
+
156
+ # PyCharm
157
+ # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
158
+ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
159
+ # and can be added to the global gitignore or merged into this file. For a more nuclear
160
+ # option (not recommended) you can uncomment the following to ignore the entire idea folder.
161
+ #.idea/
162
+ .vercel
Dockerfile ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ARG PYTHON_VERSION=3.12.1
2
+ FROM python:${PYTHON_VERSION}-slim AS base
3
+
4
+ RUN apt-get update && \
5
+ apt-get install -y pipenv && \
6
+ apt-get install -y \
7
+ fontconfig \
8
+ fonts-dejavu \
9
+ fonts-dejavu-core \
10
+ fonts-dejavu-extra \
11
+ fonts-liberation \
12
+ fonts-noto \
13
+ git && \
14
+ rm -rf /var/lib/apt/lists/*
15
+
16
+ ENV PORT=7860
17
+
18
+ ENV PIPENV_VENV_IN_PROJECT=1
19
+
20
+ WORKDIR /app
21
+
22
+ # COPY requirements.txt .
23
+
24
+ # RUN pipenv install --dev --ignore-pipfile
25
+
26
+ COPY . .
27
+
28
+ EXPOSE 7860
29
+
30
+ RUN pipenv run pip install git+https://github.com/KuntilBogel/EnkaNetwork.py fastapi asyncio enkacard uvicorn requests starrailcard
31
+
32
+
33
+ CMD [ "pipenv", "run", "python", "-m", "uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860", "--workers", "8", "--timeout-keep-alive", "600" ]
Pipfile ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [[source]]
2
+ url = "https://pypi.org/simple"
3
+ verify_ssl = true
4
+ name = "pypi"
5
+
6
+ [packages]
7
+
8
+ [dev-packages]
9
+
10
+
11
+ [requires]
12
+ python_version = "3.12"
main.py ADDED
@@ -0,0 +1,128 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import asyncio
3
+ import concurrent.futures
4
+ import requests
5
+ from fastapi import FastAPI
6
+ from io import BytesIO
7
+ from enkacard import encbanner
8
+ from fastapi.responses import JSONResponse
9
+ import enkacard
10
+ import starrailcard # Import StarRailCard module
11
+ import enkanetwork
12
+ import uvicorn
13
+ from fastapi.staticfiles import StaticFiles
14
+
15
+ app = FastAPI()
16
+
17
+ # Directory where images will be saved
18
+ IMAGE_SAVE_PATH = "static/cards"
19
+
20
+ # Ensure the directory exists
21
+ os.makedirs(IMAGE_SAVE_PATH, exist_ok=True)
22
+
23
+ # Mount the static files directory to serve saved images
24
+ app.mount("/cards", StaticFiles(directory=IMAGE_SAVE_PATH), name="cards")
25
+
26
+ # Genshin Impact card creation
27
+ async def genshin_card(id, designtype):
28
+ async with encbanner.ENC(uid=str(id)) as encard:
29
+ return await encard.creat(akasha=True, template=(2 if str(designtype) == "2" else 1))
30
+
31
+ # Star Rail card creation
32
+ async def starrail_card(id, designtype):
33
+ async with starrailcard.Card(seeleland=True, remove_logo=True, enka=True) as card:
34
+ return await card.create(id, style=(2 if str(designtype) == "2" else 1))
35
+
36
+ # Star Rail profile creation
37
+ async def starrail_profile(id):
38
+ async with starrailcard.Card(remove_logo=True, seeleland=True, enka=True) as card:
39
+ return await card.create_profile(id, style=2)
40
+
41
+ # Helper function to save the image locally
42
+ def save_image(data, file_name):
43
+ file_path = os.path.join(IMAGE_SAVE_PATH, file_name)
44
+ with open(file_path, "wb") as file:
45
+ file.write(data)
46
+ return f"/cards/{file_name}"
47
+
48
+ # Process individual image card
49
+ def process_image(dt, user_id):
50
+ file_name = f"{dt.id}_{user_id}.png"
51
+ with BytesIO() as byte_io:
52
+ dt.card.save(byte_io, "PNG")
53
+ byte_io.seek(0)
54
+ # Save the image locally
55
+ image_url = save_image(byte_io.read(), file_name)
56
+ return {
57
+ "name": dt.name,
58
+ "url": image_url
59
+ }
60
+
61
+ # Process the profile image
62
+ def process_profile(profile_card, user_id):
63
+ file_name = f"profile_{user_id}.png"
64
+ with BytesIO() as byte_io:
65
+ profile_card.card.save(byte_io, "PNG")
66
+ byte_io.seek(0)
67
+ # Save the profile image locally
68
+ image_url = save_image(byte_io.read(), file_name)
69
+ return {
70
+ "name": profile_card.character_name,
71
+ "url": image_url,
72
+ "character_ids": profile_card.character_id
73
+ }
74
+
75
+ # Process all the images returned
76
+ def process_images(result, user_id):
77
+ characters = []
78
+ with concurrent.futures.ThreadPoolExecutor() as executor:
79
+ futures = [executor.submit(process_image, dt, user_id) for dt in result.card]
80
+ for future in concurrent.futures.as_completed(futures):
81
+ try:
82
+ characters.append(future.result())
83
+ except Exception as e:
84
+ print(f"Error processing image: {e}")
85
+ return characters
86
+
87
+ # Route for Genshin Impact
88
+ @app.get("/genshin/{id}")
89
+ async def genshin_characters(id: int, design: str = "1"):
90
+ try:
91
+ result = await genshin_card(id, design)
92
+ characters = process_images(result, id)
93
+ return JSONResponse(content={'response': characters})
94
+
95
+ except enkanetwork.exception.VaildateUIDError:
96
+ return JSONResponse(content={'error': 'Invalid UID. Please check your UID.'}, status_code=400)
97
+
98
+ except enkacard.enc_error.ENCardError:
99
+ return JSONResponse(content={'error': 'Enable display of the showcase in the game or add characters there.'}, status_code=400)
100
+
101
+ except Exception as e:
102
+ return JSONResponse(content={'error': 'UNKNOWN ERR: ' + str(e)}, status_code=500)
103
+
104
+ # Route for Star Rail
105
+ @app.get("/starrail/{id}")
106
+ async def starrail_characters(id: int, design: str = "1"):
107
+ try:
108
+ result = await starrail_card(id, design)
109
+ characters = process_images(result, id)
110
+ return JSONResponse(content={'response': characters})
111
+
112
+ except Exception as e:
113
+ return JSONResponse(content={'error': 'UNKNOWN ERR: ' + str(e)}, status_code=500)
114
+
115
+ # Route for Star Rail profile
116
+ @app.get("/starrail/profile/{id}")
117
+ async def starrail_profile_route(id: int):
118
+ try:
119
+ result = await starrail_profile(id)
120
+ profile_data = process_profile(result, id)
121
+ return JSONResponse(content={'response': profile_data})
122
+
123
+ except Exception as e:
124
+ return JSONResponse(content={'error': 'UNKNOWN ERR: ' + str(e)}, status_code=500)
125
+
126
+ # Start the FastAPI server
127
+ if __name__ == "__main__":
128
+ uvicorn.run("main:app", host="0.0.0.0", port=7860, workers=8, timeout_keep_alive=60000)
pyenv_run.sh ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ export PYENV_ROOT="$HOME/.pyenv"
2
+ [[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"
3
+ eval "$(pyenv init -)"
4
+ pyenv install 3.10.13
5
+ pyenv global 3.10.13
6
+ python -m pip install -r requirements.txt
7
+ chmod +x run.sh
8
+ ./run.sh