Skip to content

Commit

Permalink
feature: change data storage to SQLite (#19)
Browse files Browse the repository at this point in the history
### What this PR does / why we need it:

- change data storage to SQLite
- refactor code
- remove setting item: FLAG_GENERATE_SRGF, FLAG_GENERATE_XLSX
- bump version from 1.1.2 to 2.0.0
  • Loading branch information
cntvc authored Aug 16, 2023
1 parent 2965416 commit c18d1b1
Showing 42 changed files with 1,551 additions and 1,410 deletions.
86 changes: 49 additions & 37 deletions README.md
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@
[![license](https://img.shields.io/github/license/cntvc/star-rail-tools)](https://github.com/cntvc/star-rail-tools/blob/main/LICENSE)


崩坏:星穹铁道小工具,可导出跃迁记录,目前仅支持 Windows 系统。
崩坏:星穹铁道小工具,可导出跃迁记录和开拓月历

<p>
<img src="docs/image/analyze_result.png" alt="analyze_result" height = 80% width = 80% align="middle">
@@ -21,6 +21,33 @@
- [Github][latest_release]
- [制品库][coding_latest](国内下载更快


### 0. 添加账号

1. 添加账户 Cookie (也可以通过该途径手动更新账号 Cookie)
首先登陆[米哈游通行证]()页面,打开F12,选择控制台,粘贴以下代码,弹出的对话框复制 cookie
```javascript
javascript:(function(){prompt(document.domain,document.cookie)})();
```

然后在 "账号设置" 菜单中选择 "通过 Cookie 添加账号",这将自动读取剪切板数据并获得相关账号

> 其他方式获取 Cookie : https://hut.ao/zh/advanced/get-stoken-cookie-from-the-third-party.html


<details>
<summary>Cookie 获取示例</summary>
<p>

<p>
<img src="docs/image/web_cookie.png" alt="web cookie" height = 80% width = 80% align="middle">
</p>

</p>
</details>


### 1. 跃迁记录

#### (1) 跃迁记录导出
@@ -30,35 +57,21 @@
3. 切换到软件,依次选择菜单 "导出抽卡数据" -> "使用游戏缓存导出"
4. 完成导出后,根据提示查看抽卡报告

#### (2) 导入或合并抽卡数据
#### (2) 导入抽卡数据

1. 设置账户:输入或选择星穹铁道账号 UID
2. 将需要导入或合并的数据放入 `merge` 文件夹内,可一次放入多个文件。支持 [SRGF][SRGF] 格式以及软件自带格式的 json 文件
3. 切换到软件,选择菜单 **导入或合并数据**

### 抽卡月历
2. 将需要导入的数据放入 "import" 文件夹内,可一次放入多个文件。支持 [SRGF][SRGF] 格式的 json 文件
3. 切换到软件,选择菜单 **导入跃迁记录**

1. 设置账户Cookie:
首先登陆[米哈游通行证](https://user.mihoyo.com/)页面,打开F12,选择控制台,粘贴以下代码,弹出的对话框复制 cookie。
```javascript
javascript:(function(){prompt(document.domain,document.cookie)})();
```
<details>
<summary>Cookie 获取示例</summary>
<p>
### 2. 开拓月历

<p>
<img src="docs/image/web_cookie.png" alt="web cookie" height = 80% width = 80% align="middle">
</p>
#### 开拓月历导出

</p>
</details>
1. 添加或更新账户 Cookie
2. 选择需要操作的 UID
3. 菜单中选择 "开拓月历" -> "获取开拓月历" 功能,等待结果显示

> 其他方式获取 Cookie : https://hut.ao/zh/advanced/get-stoken-cookie-from-the-third-party.html

2.`账号设置`菜单中选择 `通过 Cookie 添加账号`,这将自动读取剪切板数据并获得相关账号,然后选择需要操作的 UID

3. 选择 `开拓月历` -> `获取开拓月历` 功能,等待结果显示


<details>
@@ -67,23 +80,22 @@




```cmd
StarRailTools_1.0.0.exe # 主程序文件
StarRailTools # 软件数据目录
+---101793414 # 账号 101793414 的抽卡数据
| GachaAnalyze_101793414.json # 抽卡分析结果
| GachaLog_101793414.json # 抽卡原始数据
| GachaLog_101793414.xlsx # 导出的XLSX文件
| UserProfile_101793414.json # 账号信息
|
+---merge # 用于读取需要导入或合并的数据文件
|
+---config
| settings.json # 软件设置(如果没有进行过设置则不存在
|
\---log
log_2023_05.log # 日志文件
├── 101793414 # 账号 101793414 导出数据的目录
│   ├── GachaLog_101793414.xlsx
│   └── GachaLog_SRGF_101793414.json
├── AppData # 软件运行数据
│   ├── config
│   │   └── settings.json
│   ├── data
│   │   └── star_rail.db
│   ├── log
│   │   └── log_2023_08.log
│   └── temp
│   └── GachaAnalyze_101793414.json
└── import # 读取导入数据的目录
```

</p>
172 changes: 171 additions & 1 deletion poetry.lock

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "star_rail"
version = "1.1.2"
version = "2.0.0"
description = "Honkai: Star Rail GachaLog Export Tools"
authors = ["Charlie Brown <cntvc@outlook.com>"]
license = "GPLv3"
@@ -29,6 +29,7 @@ tqdm = "^4.65.0"
xlsxwriter = "^3.1.0"
pydantic = "^2.1.1"
pyperclip = "^1.8.2"
yarl = "^1.9.2"


[tool.poetry.group.dev.dependencies]
2 changes: 1 addition & 1 deletion star_rail/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from star_rail.utils.version import get_version

VERSION = (1, 1, 2)
VERSION = (2, 0, 0)

__version__ = get_version(VERSION)
107 changes: 77 additions & 30 deletions star_rail/client.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,81 @@
import functools

from star_rail import exceptions as error
from star_rail.config import ConfigClient, settings
from star_rail.i18n import i18n
from star_rail.module.mihoyo.account import UserManager
from star_rail.module.month.client import MonthClient
from star_rail.module.month.mapper import MonthInfoMapper
from star_rail.module import AccountManager, GachaClient, MonthClient
from star_rail.utils import functional

__all__ = ["refresh_month_info", "show_month_info"]

_lang = i18n.client
##############################################################
# 跃迁记录
##############################################################


def refresh_month_info():
user = UserManager().user
if None is user:
print(functional.color_str(_lang.no_account, "yellow"))
return
if not user.cookie.verify_cookie_token():
print(functional.color_str(_lang.empty_cookie, "yellow"))
return
month_client = MonthClient(user)
month_client.refresh_month_info()
month_client.visualization(MonthInfoMapper.query(user.uid, None, 6))


def show_month_info():
user = UserManager().user
if None is user:
print(functional.color_str(_lang.no_account, "yellow"))
return
month_client = MonthClient(user)
month_client.visualization(MonthInfoMapper.query(user.uid, None, 6))

__all__ = ["HSRClient"]


class HSRClient(GachaClient, MonthClient, ConfigClient):
def __init__(self) -> None:
self.setting = settings
self.user = AccountManager().user

def check_user(func):
@functools.wraps(func)
def wrapper(self, *args, **kwargs):
self.user = AccountManager().user
if self.user is None:
print(functional.color_str(_lang.no_account, "yellow"))
return
return func(self, *args, **kwargs)

return wrapper

@error.exec_catch()
@check_user
def refresh_month_info(self):
# TODO 支持国际服
from star_rail.module.mihoyo import GameBiz

if GameBiz.get_by_uid(self.user.uid) == GameBiz.GLOBAL:
raise error.HsrException("该功能尚未支持国际服账号")
if not self.user.cookie.verify_cookie_token():
print(functional.color_str(_lang.empty_cookie, "yellow"))
return
super().refresh_month_info()
super().show_month_info()

@error.exec_catch()
@check_user
def show_month_info(self):
super().show_month_info()

@error.exec_catch()
@check_user
def refresh_record_by_user_cache(self):
super().refresh_record_by_user_cache()

@error.exec_catch()
@check_user
def refresh_record_by_game_cache(self):
super().refresh_record_by_game_cache()

@error.exec_catch()
def refresh_record_by_clipboard(self):
super().refresh_record_by_clipboard()

@error.exec_catch()
@check_user
def show_analyze_result(self):
super().show_analyze_result()

@error.exec_catch()
@check_user
def import_gacha_record(self):
super().import_gacha_record()

@error.exec_catch()
@check_user
def export_record_to_xlsx(self):
super().export_record_to_xlsx()

@error.exec_catch()
@check_user
def export_record_to_srgf(self):
super().export_record_to_srgf()
5 changes: 4 additions & 1 deletion star_rail/config/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
from .settings import * # noqa
from .client import *
from .settings import *

__all__ = ["settings", "ConfigClient"]
39 changes: 39 additions & 0 deletions star_rail/config/client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from star_rail.utils import functional
from star_rail.utils.log import logger

from .settings import Settings, settings

__all__ = ["ConfigClient"]


class ConfigClient:
def __init__(self, setting: Settings = settings) -> None:
self.setting = setting

def get_config_status(self, key):
if not hasattr(self.setting, key):
return
from star_rail.i18n import i18n

return "{}: {}".format(
i18n.config.settings.current_status,
functional.color_str(i18n.common.open, "green")
if settings.get(key)
else functional.color_str(i18n.common.close, "red"),
)

def open_setting(self, key: str):
if not hasattr(self.setting, key):
return
from star_rail.i18n import i18n

self.setting.set_and_save(key, True)
logger.success(i18n.config.settings.open_success)

def close_setting(self, key: str):
if not hasattr(self.setting, key):
return
from star_rail.i18n import i18n

self.setting.set_and_save(key, False)
logger.success(i18n.config.settings.close_success)
28 changes: 5 additions & 23 deletions star_rail/config/settings.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import os
from pathlib import Path

from pydantic import BaseModel
from pydantic import BaseModel, ConfigDict

from star_rail import constants
from star_rail.utils.functional import color_str, load_json, save_json
from star_rail.utils.functional import load_json, save_json
from star_rail.utils.log import logger

__all__ = ["settings", "get_config_status_desc"]
__all__ = ["settings"]

_config_path = Path(constants.CONFIG_PATH, "settings.json")

@@ -18,11 +18,6 @@ class Settings(BaseModel):
FLAG_UPDATE_SOURCE: str = "Github"
"""更新源 : ["Github", "Coding"] """

FLAG_GENERATE_XLSX: bool = False

FLAG_GENERATE_SRGF: bool = False
"""自动生成 SRGF 格式文件"""

FLAG_UPATED_COMPLETE: bool = False

OLD_EXE_NAME: str = ""
@@ -32,13 +27,12 @@ class Settings(BaseModel):

LANGUAGE: str = ""

class Config:
extra = "forbid"
model_config = ConfigDict(extra="ignore")

def __setattr__(self, k, v):
if k not in self.__fields__:
return
logger.debug("更新设置: {} -> {}", k, v)
logger.debug("update config: {} -> {}", k, v)
return super().__setattr__(k, v)

def __init__(self, **data):
@@ -75,15 +69,3 @@ def set(self, k, v):


settings = Settings()


def get_config_status_desc(key):
assert hasattr(settings, key)
from star_rail.i18n import i18n

return "{}: {}".format(
i18n.config.settings.current_status,
color_str(i18n.common.open, "green")
if settings.get(key)
else color_str(i18n.common.close, "red"),
)
Loading
Oops, something went wrong.

0 comments on commit c18d1b1

Please sign in to comment.