Skip to content

Commit

Permalink
Merge pull request #47 from Jim137/Develop
Browse files Browse the repository at this point in the history
v0.3.9.1
  • Loading branch information
Jim137 authored Apr 3, 2024
2 parents a9eda86 + 5ac0517 commit cfdc281
Show file tree
Hide file tree
Showing 16 changed files with 105 additions and 106 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@ jobs:
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics --per-file-ignores="paifulogger/log.py:C901,F401,F403,F405,E501 paifulogger/src/__init__.py:F401 test/test.py:E402" --exclude mjlog2mjai
- name: Test with test
run: python ./test/test.py
run: python -m unittest
2 changes: 1 addition & 1 deletion paifulogger/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.3.9"
__version__ = "0.3.9.1"
27 changes: 14 additions & 13 deletions paifulogger/log.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import argparse
import json
from typing import Callable
import urllib.request
import re
import os
import sys
import warnings
from typing import Callable

from pandas import HDFStore, DataFrame
from platformdirs import user_data_dir

from .src.get_paifu import get_paifu
from .src.i18n import localized_str, local_str
from .src.i18n import localized_str, LocalStr
from .src.log_into_csv import log_into_csv
from .src.log_into_xlsx import log_into_xlsx
from .src.log_into_html import log_into_html
Expand Down Expand Up @@ -53,7 +53,7 @@ def remove_old_paifu(paifu_str: str, formats: list[str], output: str) -> None:
return None


def _get_lang(lang: str | None = None) -> local_str:
def _get_lang(lang: str | None = None) -> LocalStr:
"""
Get the localized string.
Expand Down Expand Up @@ -85,7 +85,9 @@ def _get_output(output: str = "./") -> str:

def _get_urls(
url: list[str] | None = None,
local_lang: local_str = local_str("en", os.path.dirname(os.path.abspath(__file__))),
local_lang: LocalStr = localized_str(
"en", os.path.dirname(os.path.abspath(__file__))
),
output: str = os.path.abspath("./"),
remake: bool = False,
) -> list[str]:
Expand Down Expand Up @@ -146,7 +148,7 @@ def _get_formats(format: list[str] | None = None) -> list:


def _remake_log(
local_lang: local_str, output: str, formats: list[str], all_formats: bool = False
local_lang: LocalStr, output: str, formats: list[str], all_formats: bool = False
) -> None:
"""
Remake the log file from url_log.h5 (past logging log), and remove old paifu files.
Expand Down Expand Up @@ -195,7 +197,7 @@ def _get_log_func(formats: list[str], all_formats: bool = False) -> list[Callabl
def log_paifu(
*urls: list[str] | str,
log_formats: list[Callable] = [log_into_csv],
local_lang: local_str = local_str("en", os.path.dirname(os.path.abspath(__file__))),
local_lang: LocalStr = LocalStr("en", os.path.dirname(os.path.abspath(__file__))),
output: str = os.path.abspath("./"),
remake: bool = False,
ignore_duplicated: bool = False,
Expand All @@ -209,7 +211,7 @@ def log_paifu(
The urls of the paifu files.
log_formats: list
The list of log functions.
local_lang: local_str
local_lang: LocalStr
The localized string.
output: str
The output directory.
Expand All @@ -224,7 +226,7 @@ def log_paifu(
int
"""

retCode = [0]
retCode = 0
_urls: list[str] = []
for url in urls:
if isinstance(url, list):
Expand All @@ -248,17 +250,16 @@ def log_paifu(
pass
else:
url_log(url, local_lang, output)
retCode.append(0)
except urllib.error.URLError:
print(local_lang.hint_url, url)
retCode.append(1)
retCode = 1
except OSError:
print(local_lang.hint_url, url)
retCode.append(1)
retCode = 1
except ValueError:
print(local_lang.hint_tw, url)
retCode.append(1)
return max(retCode)
retCode = 1
return retCode


def log(args: argparse.Namespace) -> int:
Expand Down
13 changes: 8 additions & 5 deletions paifulogger/paifu_dl.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import os
import re

from .src.i18n import local_str
from .src.i18n import LocalStr
from .src.get_paifu import get_paifu


url_reg = r"https?://tenhou\.net/\d/\?log=\d{10}gm-\w{4}-\w{4}-\w{8}&tw=\d"


def _get_urls(urls, local_lang: local_str) -> list[str]:
def _get_urls(urls, local_lang: LocalStr) -> list[str]:
"""
Get urls from input or args.url.
Expand All @@ -32,17 +32,17 @@ def _get_urls(urls, local_lang: local_str) -> list[str]:

def paifu_dl(
urls: str | list[str] | None = None,
local_lang: local_str = local_str("en", os.path.dirname(os.path.abspath(__file__))),
local_lang: LocalStr = LocalStr("en", os.path.dirname(os.path.abspath(__file__))),
output: str = "./",
mjai: bool = False,
) -> None:
) -> int:
"""
Download paifu from tenhou.net.
Args:
urls: str | list[str]
The url of the game log.
local_lang: local_str
local_lang: LocalStr
The localized string.
output: str
The output directory.
Expand All @@ -55,9 +55,12 @@ def paifu_dl(

check_urls = _get_urls(urls, local_lang)

retCode = 0
for url in check_urls:
try:
get_paifu(url, local_lang, output, mjai)
print(f"paifu_dl: {url} has been downloaded.")
except Exception as e:
print(e)
retCode = 1
return retCode
22 changes: 22 additions & 0 deletions paifulogger/src/Paifu.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,25 @@ def go_type_distinguish(self):

if self.go_type & 64:
self.go_str += "速"

def get_place(self, ban):
"""
Return the placing and rate before match
"""
o0, s0, o1, s1, o2, s2, o3, s3 = self.owari

if self.player_num == 4:
sp = [float(s0), float(s1), float(s2), float(s3)]
placing = [1, 1, 1, 1]
for i in range(4):
for j in range(4):
if sp[i] < sp[j]:
placing[i] += 1
else:
sp = [float(s0), float(s1), float(s2)]
placing = [1, 1, 1]
for i in range(3):
for j in range(3):
if sp[i] < sp[j]:
placing[i] += 1
return placing[ban]
27 changes: 24 additions & 3 deletions paifulogger/src/get_paifu.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,33 @@
import gzip
import os
import urllib.request
import xml.etree.ElementTree as ET

from .i18n import local_str
from .i18n import LocalStr
from .Paifu import Paifu
from .url_request_handler import url_request_handler


def get_paifu(url: str, local_lang: local_str, output: str, mjai: bool = False):
HEADER = {
"Host": "e.mjv.jp",
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:65.0) Gecko/20100101 Firefox/65.0",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
"Accept-Language": "en-US,en;q=0.5",
"Accept-Encoding": "gzip, deflate",
"Connection": "keep-alive",
}


def url_request_handler(url: str):
url = url.split("=")[1]
url = "https://tenhou.net/0/log/?" + url[:-3]
req = urllib.request.Request(url=url, headers=HEADER)
opener = urllib.request.build_opener()
response = opener.open(req)
response = gzip.decompress(response.read()).decode("utf-8")
return response


def get_paifu(url: str, local_lang: LocalStr, output: str, mjai: bool = False):
response = url_request_handler(url)
root = ET.fromstring(response)
paifu = Paifu(url, root)
Expand Down
24 changes: 0 additions & 24 deletions paifulogger/src/get_place.py

This file was deleted.

6 changes: 3 additions & 3 deletions paifulogger/src/i18n.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import json


class local_str:
class LocalStr:
def __init__(self, lang: str = "en", main_path: str = "./"):
self.lang = lang
self.main_path = main_path
Expand All @@ -22,8 +22,8 @@ def __getattr__(self, name):
return data[name]


def localized_str(lang: str, main_path: str) -> local_str:
localized = local_str(lang, main_path)
def localized_str(lang: str, main_path: str) -> LocalStr:
localized = LocalStr(lang, main_path)
try:
with open(f"{main_path}/localizations/{lang}.json", encoding="utf-8") as f:
data = json.load(f)
Expand Down
9 changes: 4 additions & 5 deletions paifulogger/src/log_into_csv.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import csv
from datetime import datetime
import re
import os.path
from datetime import datetime

import pandas as pd

from .get_place import get_place
from .i18n import local_str
from .i18n import LocalStr
from .Paifu import Paifu


def log_into_csv(paifu: Paifu, local_lang: local_str, output: str):
def log_into_csv(paifu: Paifu, local_lang: LocalStr, output: str):
if paifu.player_num == 3:
paifu_str = local_lang.sanma + local_lang.paifu
else:
Expand Down Expand Up @@ -39,7 +38,7 @@ def log_into_csv(paifu: Paifu, local_lang: local_str, output: str):
[
df.shape[0],
datetime.strptime(re.findall(r"\d{10}", paifu.url)[0], "%Y%m%d%H"),
get_place(paifu, paifu.ban),
paifu.get_place(paifu.ban),
paifu.url,
float(paifu.r[paifu.ban]),
]
Expand Down
17 changes: 8 additions & 9 deletions paifulogger/src/log_into_html.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
from datetime import datetime
import io
import re
from datetime import datetime

import pandas as pd

from .get_place import get_place
from .i18n import local_str
from .i18n import LocalStr
from .Paifu import Paifu


def create_html(html_str, paifu_str, local_lang: local_str):
def create_html(html_str, paifu_str, local_lang: LocalStr):
html_str += f"""<!DOCTYPE html>
<html lang={local_lang.lang}>
<head>
Expand Down Expand Up @@ -46,12 +45,12 @@ def create_html(html_str, paifu_str, local_lang: local_str):
return html_str


def log_into_table(html_str, paifu: Paifu, local_lang: local_str):
def log_into_table(html_str, paifu: Paifu, local_lang: LocalStr):
time_str = datetime.strptime(re.findall(r"\d{10}", paifu.url)[0], "%Y%m%d%H")
html_str += f"""
<tr>
<td>{time_str}</td>
<td>{get_place(paifu, paifu.ban)}</td>
<td>{paifu.get_place(paifu.ban)}</td>
<td><a href="{paifu.url}">{paifu.url}</a></td>
<td><textarea id="persisted-text"></textarea></td>
<td>{float(paifu.r[paifu.ban])}</td>
Expand All @@ -60,7 +59,7 @@ def log_into_table(html_str, paifu: Paifu, local_lang: local_str):
return html_str


def average_plc(html_str, local_lang: local_str):
def average_plc(html_str, local_lang: LocalStr):
html_p = (
html_str
+ """
Expand All @@ -76,7 +75,7 @@ def average_plc(html_str, local_lang: local_str):
return avg_plc


def end_of_table(html_str, avg_plc, local_lang: local_str):
def end_of_table(html_str, avg_plc, local_lang: LocalStr):
html_str += (
f"""
</tbody>
Expand Down Expand Up @@ -107,7 +106,7 @@ def clear_end(html_str):
return html_str


def log_into_html(paifu: Paifu, local_lang: local_str, output: str):
def log_into_html(paifu: Paifu, local_lang: LocalStr, output: str):
if paifu.player_num == 3:
paifu_str = local_lang.sanma + local_lang.paifu
else:
Expand Down
9 changes: 4 additions & 5 deletions paifulogger/src/log_into_xlsx.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import re
from datetime import datetime
from typing import cast
import re

import openpyxl as xl
from openpyxl.worksheet.worksheet import Worksheet

from .get_place import get_place
from .i18n import local_str
from .i18n import LocalStr
from .Paifu import Paifu


def log_into_xlsx(paifu: Paifu, local_lang: local_str, output: str):
def log_into_xlsx(paifu: Paifu, local_lang: LocalStr, output: str):
try:
if paifu.player_num == 3:
paifu_str = local_lang.sanma + local_lang.paifu
Expand Down Expand Up @@ -38,7 +37,7 @@ def log_into_xlsx(paifu: Paifu, local_lang: local_str, output: str):
sheet.append(
[
datetime.strptime(re.findall(r"\d{10}", paifu.url)[0], "%Y%m%d%H"),
get_place(paifu, paifu.ban),
paifu.get_place(paifu.ban),
paifu.url,
"",
float(paifu.r[paifu.ban]),
Expand Down
Loading

0 comments on commit cfdc281

Please sign in to comment.