Skip to content

Commit

Permalink
Merge pull request #18 from LTurret/Developing
Browse files Browse the repository at this point in the history
General bug fixes & rewrite time info. functions
  • Loading branch information
LTurret authored Aug 19, 2022
2 parents b9b3001 + d13549c commit 48b7d21
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 98 deletions.
16 changes: 8 additions & 8 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@

AnnaBorder-x86-64-win
AnnaBorder-x86-64-win/components
AnnaBorder-x86-64-win/components/AnnaFrame.png
AnnaBorder-x86-64-win/components/AnnaFrame.psd
AnnaBorder-x86-64-win/components/ChiuKongGothic-CL-Regular.otf
AnnaBorder-x86-64-win/components/jf-openhuninn-1.1.ttf
AnnaBorder-x86-64-win/AnnaBorder.exe
AnnaBorder-x86-64-win/config.json
AnnaBorder_v1.1.0-x86-64-win
AnnaBorder_v1.1.0-x86-64-win/components
AnnaBorder_v1.1.0-x86-64-win/components/AnnaFrame.png
AnnaBorder_v1.1.0-x86-64-win/components/AnnaFrame.psd
AnnaBorder_v1.1.0-x86-64-win/components/ChiuKongGothic-CL-Regular.otf
AnnaBorder_v1.1.0-x86-64-win/components/jf-openhuninn-1.1.ttf
AnnaBorder_v1.1.0-x86-64-win/AnnaBorder.exe
AnnaBorder_v1.1.0-x86-64-win/config.json

**/__pycache__
image
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,5 @@ Licensed under [MIT](LICENSE).
The copyright of any characters in the image(includes .psd, .png) belongs to [Bandai Namco Entertainment](https://www.bandainamcoent.co.jp/).
All the event information is provided by [api.matsurihi.me](https://api.matsurihi.me/docs/)
Font used in the image title is [ChiuKong Gothic](https://github.com/ChiuMing-Neko/ChiuKongGothic)
Font used in the image body is [jf open 粉圓](https://github.com/justfont/open-huninn-font)
Font used in the image subtitles are [jf open 粉圓](https://github.com/justfont/open-huninn-font)
Font used in the image body is [FiraCode](https://github.com/tonsky/FiraCode)
3 changes: 2 additions & 1 deletion README.zh-TW.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,5 @@ pyinstaller -F "main.py" -i "icon.ico"
所有的活動資訊皆由[api.matsurihi.me](https://api.matsurihi.me/docs/)提供
出現在影像中的任何角色(包括.psd、.png)版權皆屬於萬代南夢宮娛樂所有
字體使用於圖片標題為[秋空黑體](https://github.com/ChiuMing-Neko/ChiuKongGothic)
字體使用於圖片內容為[jf open 粉圓](https://github.com/justfont/open-huninn-font)
字體使用於圖片子標題為[jf open 粉圓](https://github.com/justfont/open-huninn-font)
字體使用於圖片內容為[FiraCode](https://github.com/tonsky/FiraCode)
Binary file added components/FiraCode-Regular.ttf
Binary file not shown.
8 changes: 5 additions & 3 deletions config.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
{
"data_broadcasting": "./dataset/border.json",
"border_data": "./dataset/border.json",
"data_eventinfo": "./dataset/information.json",
"background": "./components/AnnaFrame.png",
"font_filepath": "./components/jf-openhuninn-1.1.ttf",
"title_font_filepath": "./components/ChiuKongGothic-CL-Regular.otf"
"font_event_title":"./components/ChiuKongGothic-CL-Regular.otf" ,
"font_data_title": "./components/jf-openhuninn-1.1.ttf",
"font_subtitle": "./components/jf-openhuninn-1.1.ttf",
"font_body": "./components/FiraCode-Regular.ttf"
}
11 changes: 7 additions & 4 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ async def main(output_type, output_path, checksum, dryrun, static, identify):
# identify_maximun = lambda identify, idmax: True if (identify is not None and identify > idmax) else 2
matchtype = lambda typecode: ([3, 4, 5, 11, 13, 16].count(typecode)) == 1
border_exists = lambda file: True if (len(file) > 0) else False
early_announcement = lambda message: True if (len(message) > 0) else False

announcement = ""
border_data = None
Expand All @@ -56,10 +57,10 @@ async def main(output_type, output_path, checksum, dryrun, static, identify):
async with aiohttp.ClientSession() as session:
event_data = await GetNewest(session)

if check_identified(identify):
event_data = await Search(identify, session)
else:
identify = event_data["id"]
if check_identified(identify[0]):
event_data = await Search(identify[0], session)

identify = event_data["id"]

if matchtype(event_data["type"]):
border_data = await FetchBorder(identify, session)
Expand All @@ -68,6 +69,8 @@ async def main(output_type, output_path, checksum, dryrun, static, identify):

if checksum:
announcement = "checksum complete."
elif early_announcement(announcement):
pass
else:

if not os.path.isdir("./dataset"):
Expand Down
179 changes: 98 additions & 81 deletions src/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

async def makeimg(category, path:str="image"):

# 轉為中文
# Abbreviation to Chinese
def categories(category):
manifest = {
"pt": "PT榜",
Expand All @@ -14,7 +14,7 @@ def categories(category):
}
return manifest[category]

# 轉為全名
# Abbreviation to fullform
def fullform(abbreviation):
manifest = {
"pt": "eventPoint",
Expand All @@ -23,7 +23,7 @@ def fullform(abbreviation):
}
return manifest[abbreviation]

# 活動類型轉譯
# Event type convert
def idtostring(typeid):
manifest = {
3: "Theater",
Expand All @@ -35,59 +35,68 @@ def idtostring(typeid):
}
return manifest[typeid]

# 檔案設定
# Configurations
with open("./config.json") as config:
config = json.load(config)
background = "./components/AnnaFrame.png"
data_border = "./dataset/border.json"
data_eventinfo = "./dataset/information.json"
font_filepath = "./components/jf-openhuninn-1.1.ttf"
title_font_filepath = "./components/ChiuKongGothic-CL-Regular.otf"

with open(data_border, mode="r", encoding='utf8') as file:
border_data = config["border_data"]
data_eventinfo = config["data_eventinfo"]
background = config["background"]

font_event_title = config["font_event_title"]
font_data_title = config["font_data_title"]
font_subtitle = config["font_subtitle"]
font_body = config["font_body"]

with open(border_data, mode="r", encoding='utf8') as file:
borderData = json.load(file)
with open(data_eventinfo, mode="r", encoding='utf8') as file:
eventData = json.load(file)

# 指定資料
# Assign data
eventName = eventData["name"]
eventType = eventData["type"]
beginDate = eventData["schedule"]["beginDate"]
endDate = eventData["schedule"]["endDate"]
boostDate = eventData["schedule"]["boostBeginDate"]
timeSummaries = borderData[fullform(category)]["summaryTime"]

# 格式化日期
# Formatting datetime
beginDate = beginDate.replace("-", "/")[0:10]
endDate = endDate.replace("-", "/")[0:10]
boostDate = boostDate.replace("-", "/")[0:10]
time_date = timeSummaries.replace("-","/")[0:10]
time_date += f" {timeSummaries[11:16]}"

# 活動天數
# Remaining days ("BD" = "Begin Date", "ED" = "End Date")
formatedBD = datetime.date(int(beginDate[0:4]), int(beginDate[5:7]), int(beginDate[8:10]))
formatedED = datetime.date(int(endDate[0:4]), int(endDate[5:7]), int(endDate[8:10]))

if eventType == 5:
dayLength = (formatedED - formatedBD).days + 1
else:
dayLength = (formatedED - formatedBD).days

# 新增台灣時間,方便觀察
current_time = datetime.datetime.now()
# Configure current time with Japan Standard Time(GMT+9)
current_time = datetime.datetime.utcnow().replace(tzinfo=datetime.timezone.utc)
current_time = current_time.astimezone(datetime.timezone(datetime.timedelta(hours=9)))

# Special case for Annivaersary
if eventType == 5:
end_time = datetime.datetime(int(endDate[0:4]), int(endDate[5:7]), int(endDate[8:10]), 22, 59, 59, 0)
end_time = datetime.datetime(int(endDate[0:4]), int(endDate[5:7]), int(endDate[8:10]), 22, 59, 59, 0, tzinfo=datetime.timezone.utc)
else:
end_time = datetime.datetime(int(endDate[0:4]), int(endDate[5:7]), int(endDate[8:10]), 19, 59, 59, 0)
end_time = datetime.datetime(int(endDate[0:4]), int(endDate[5:7]), int(endDate[8:10]), 19, 59, 59, 0, tzinfo=datetime.timezone.utc)

different_time = end_time - current_time
different_hours = different_time.total_seconds() / 3600
different_days = different_hours / 24
total_hours = dayLength*24
total_hours = dayLength * 24
progress = (total_hours-different_hours) / total_hours

# Pillow 畫布設定
AnnaFrame = Image.open(background)
AnnaFrame = AnnaFrame.convert('RGBA')
draw = ImageDraw.Draw(AnnaFrame)
frame = Image.open(background)
frame = frame.convert('RGBA')
draw = ImageDraw.Draw(frame)

# Pillow 設定
y_globe = 290
Expand All @@ -97,67 +106,75 @@ def idtostring(typeid):
adjust_x = 150

# 字型設定
broadcasting = ImageFont.truetype(font_filepath, 30)
title = ImageFont.truetype(title_font_filepath, 27)
fetchtime = ImageFont.truetype(font_filepath, 25)
body = ImageFont.truetype(font_filepath, 20)
event_title = ImageFont.truetype(font_event_title, 27)
data_title = ImageFont.truetype(font_data_title, 25)
subtitle = ImageFont.truetype(font_subtitle, 20)
body = ImageFont.truetype(font_body, 30)
interval = ImageFont.truetype(font_subtitle, 30)

# 圖片資訊產生
draw.text((x_globe,28), f"{eventName}", (2, 62, 125), font=title)
draw.text((x_globe,70), f"資料時間:{time_date} ({progress:.1%})\n", (0, 40, 85), font=fetchtime)
draw.text((x_globe,105), f"活動期間:{beginDate} ~ {endDate} ({dayLength*24}小時)\n", (92, 103, 125), font=body)
draw.text((x_globe,130), f"後半期間:{boostDate} ~ {endDate}\n", (92, 103, 125), font=body)
draw.text((x_globe,155), f"剩下時間:{different_days:.2f}天 ({int(different_hours)}小時)\n", (92, 103, 125), font=body)
draw.text((x_globe,180), f"榜線類型:{idtostring(eventType)} ({categories(category)})\n", (92, 103, 125), font=body)
draw.text((x_globe,28), f"{eventName}", (2, 62, 125), font=event_title)

if progress >= 100:
draw.text((x_globe,70), f"資料時間:{time_date} (100%)\n", (0, 40, 85), font=data_title)
else:
draw.text((x_globe,70), f"資料時間:{time_date} ({progress:.1%})\n", (0, 40, 85), font=data_title)

draw.text((x_globe,105), f"活動期間:{beginDate} ~ {endDate} ({dayLength*24}小時)\n", (92, 103, 125), font=subtitle)
draw.text((x_globe,130), f"後半期間:{boostDate} ~ {endDate}\n", (92, 103, 125), font=subtitle)

if different_days <= 0:
draw.text((x_globe,155), f"剩下時間:已經結束囉~\n", (92, 103, 125), font=subtitle)
else:
draw.text((x_globe,155), f"剩下時間:{different_days:.2f}天 ({int(different_hours)}小時)\n", (92, 103, 125), font=subtitle)

draw.text((x_globe,180), f"榜線類型:{idtostring(eventType)} ({categories(category)})\n", (92, 103, 125), font=subtitle)

# 圖片排名產生
try:
for data in borderData[fullform(category)]["scores"]:
rank = data["rank"]
score = data["score"]
if score is not None:
if (length_adjust > 9):
break
elif (len(str(rank)) == 1):
argx = 73
elif (len(str(rank)) == 2):
argx = 54
elif (len(str(rank)) == 3):
argx = 36
elif (len(str(rank)) == 4):
argx = 18
else:
argx = 0

draw.text(
xy=(x_globe + argx, y_globe),
text=f"{rank}",
fill=(3, 83, 164),
font=broadcasting
)
draw.text(
xy=(x_globe + 95, y_globe),
text="位",
fill=(3, 83, 164),
font=broadcasting
)

if len(str(score)) == 8:
adjust_x = 176.8
elif len(str(score)) == 7:
adjust_x = 194
elif len(str(score)) == 6:
adjust_x = 211.2

draw.text(
xy=(x_globe + adjust_x + 30, y_globe),
text=f"{score:,.0f}",
fill=(4, 102, 200),
font=broadcasting
)
y_globe += y_accumulate
length_adjust += 1
print(f"makeimg: {categories(category)}.png")
AnnaFrame.save(f"./{path}/{categories(category)}.png")
except Exception as e:
print(f"makeimg {e}")
for data in borderData[fullform(category)]["scores"]:
rank = data["rank"]
score = data["score"]
if score is not None:
if (length_adjust > 9):
break
elif (len(str(rank)) == 1):
argx = 73
elif (len(str(rank)) == 2):
argx = 54
elif (len(str(rank)) == 3):
argx = 36
elif (len(str(rank)) == 4):
argx = 18
else:
argx = 0

draw.text(
xy=(x_globe + argx, y_globe),
text=f"{rank}",
fill=(3, 83, 164),
font=interval
)
draw.text(
xy=(x_globe + 95, y_globe),
text="位",
fill=(3, 83, 164),
font=interval
)

if len(str(score)) == 8:
adjust_x = 186.8
elif len(str(score)) == 7:
adjust_x = 204
elif len(str(score)) == 6:
adjust_x = 211.2

draw.text(
xy=(x_globe + adjust_x + 30, y_globe),
text=f"{score:,.0f}",
fill=(4, 102, 200),
font=body
)
y_globe += y_accumulate
length_adjust += 1
print(f"Generating border image: ./{path}/{categories(category)}.png")
frame.save(f"./{path}/{categories(category)}.png")

0 comments on commit 48b7d21

Please sign in to comment.