Last active
January 30, 2021 06:23
-
-
Save rinjugatla/8fca825807cb8c7c3db937e6dc796802 to your computer and use it in GitHub Desktop.
FF14のワールド稼働状況を確認し、任意のワールドのキャラクタ作成が可能になった場合にデスクトップ通知を行います。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pprint import pprint | |
import requests | |
from bs4 import BeautifulSoup | |
from plyer import notification | |
import time | |
class FF14WorldStatusCrawler(): | |
__world_names = [] # チェック対象のワールド名 | |
__interval_min = 10 # チェック間隔(分) 最低10分 | |
__loop_limit = -1 # チェック回数(-1)で無限実行 | |
def __init__(self, world_names, interval_min, loop_limit = -1): | |
self.__world_names = world_names | |
if(interval_min > 10): | |
self.__interval_min = interval_min | |
if(loop_limit > 0): | |
self.__loop_limit = loop_limit | |
def __enter__(self): | |
print('ワールド稼働状況の監視を開始します。') | |
return self | |
def __exit__(self, exc_type, exc_value, traceback): | |
print('ワールド稼働状況の監視を終了します。') | |
def Crawl(self): | |
is_next_loop = True | |
while is_next_loop: | |
# ワールド稼働状況取得・確認 | |
worlds = self.__GetSource() | |
worlds_status = self.__GetWorldStatus(worlds) | |
self.__NotificatStatus(worlds_status, self.__world_names) | |
# 実行回数更新 | |
self.__loop_limit -= 1 | |
if(self.__loop_limit == 0): | |
return | |
# 次回実行まで待機 | |
time.sleep(self.__interval_min * 60) | |
def __GetSource(self): | |
source = requests.get('https://jp.finalfantasyxiv.com/lodestone/worldstatus/') | |
soup = BeautifulSoup(source.text, 'html.parser') | |
worlds_selector = 'div[class="world-list__item"]' | |
worlds = soup.select(worlds_selector) | |
return worlds | |
def __GetWorldStatus(self, worlds, is_debug = False): | |
# サーバ名、サーバ状況、キャラクタ作成状態 | |
name_selector = 'div[class="world-list__world_name"]>p' | |
category_selector = 'div[class="world-list__world_category"]>p' | |
status_selector = 'div[class="world-list__create_character"]>i' | |
worlds_status = {} | |
for world in worlds: | |
name = world.select_one(name_selector).text | |
category = world.select_one(category_selector).text | |
# キャラクタ作成可能か | |
status = 'world-ic__available' in world.select_one(status_selector)['class'] | |
# デバッグ出力 | |
if(is_debug): | |
print('{name} : {category} : {status}'.format( | |
name = name, category = category, | |
status = '〇' if status else '×')) | |
# Worlds状態更新 | |
worlds_status[name] = { | |
'category': category, | |
'is_can_create': status | |
} | |
return worlds_status | |
def __NotificatStatus(self, worlds_status, world_names): | |
can_create_world_names = [] | |
for name in world_names: | |
if(not(name in worlds_status)): | |
print('存在しないワールド名({0})が指定されました。'.format(name)) | |
continue | |
if(worlds_status[name]['is_can_create']): | |
can_create_world_names.append(name) | |
if(len(can_create_world_names) != 0): | |
# 複数のサーバ名を結合 | |
names = ','.join(can_create_world_names) | |
notification.notify( | |
title = '生まれたい', | |
message = '{0}サーバ\nキャラクタ作成できます!!!!!はやく作って!!!!'.format(names), | |
app_name = "FF14WorldsStatusCrawler" | |
) | |
else: | |
print('キャラクタ作成の可能なサーバが見つかりませんでした。次の実行を待機します。') | |
with FF14WorldStatusCrawler(['Hades'], 15, 1) as ff14: | |
ff14.Crawl() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment