Created
October 14, 2018 14:17
-
-
Save yukiyoshisato/8518239423f65f8b848a1d0a1a36b0db to your computer and use it in GitHub Desktop.
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
import datetime | |
import calendar | |
import traceback | |
from pathlib import Path | |
from selenium import webdriver | |
import sys | |
sys.path.append("../") | |
from utils.my_logger import MyLogger | |
from utils.my_twitter import MyTwitter | |
from utils.my_slacker import MySlacker | |
class TennisTokyo: | |
COURT_TYPES = {"ハード": "2-1000-1020", "人工芝": "2-1000-1030"} | |
def __init__(self, from_time, to_time, court): | |
self.logger = MyLogger(log_name="TENNIS TOKYO", log_path=str(Path.home()) + "\log\\tennis_tokyo.log").logger | |
self.driver = webdriver.Chrome() | |
self.from_time = from_time | |
self.to_time = to_time | |
self.court = court | |
self.court_string = self.COURT_TYPES[self.court] | |
self.slacker = MySlacker() | |
self.twitter = MyTwitter(self.logger) | |
# private method | |
def _handle_conditions(self, year, month, day): | |
result_msg = "" | |
for day in self._handle_dates(year, month, day): | |
result_msg += self._handle_elements(year, month, day) | |
return result_msg | |
def _handle_elements(self, year, month, day): | |
self.driver.get("https://yoyaku.sports.metro.tokyo.jp/user/view/user/homeIndex.html") | |
self.driver.find_element_by_id('dateSearch').click() | |
self.driver.find_element_by_name('layoutChildBody:childForm:year').send_keys(str(year)) | |
self.driver.find_element_by_name('layoutChildBody:childForm:month').send_keys(str(month) + "月") | |
self.driver.find_element_by_name('layoutChildBody:childForm:day').send_keys(str(day) + "日") | |
self.driver.find_element_by_name('layoutChildBody:childForm:sHour').send_keys(str(self.from_time) + "時") | |
self.driver.find_element_by_name('layoutChildBody:childForm:eHour').send_keys(str(self.to_time) + "時") | |
self.driver.find_element_by_xpath("//input[@value='{0}']".format(self.court_string)).click() | |
self.driver.find_element_by_id('srchBtn').click() | |
web_elements = self.driver.find_elements_by_id("bnamem") | |
week = self._handle_week(year, month, day) | |
result_msg = "" | |
if len(web_elements) > 0: | |
for e in web_elements: | |
msg = "{0}({1}) : {2}".format(day, week, e.text) | |
result_msg += "\n" + msg | |
self.logger.debug(msg) | |
else: | |
self.logger.debug("{0}({1}) : {2}コート空きがありません。".format(day, week, self.court)) | |
return result_msg | |
# public method | |
def run(self): | |
try: | |
now = datetime.datetime.now() | |
msg = "今月{0}-{1}時の空きテニスコート({2}) {3} 現在\n".format( | |
self.from_time, self.to_time, self.court, now.strftime("%Y-%m-%d %H:%M:%S")) | |
msg += "https://yoyaku.sports.metro.tokyo.jp/" | |
self.logger.debug(msg) | |
msg += self._handle_conditions(now.year, now.month, now.day) | |
self.slacker.post_message(channel="tennis", text=msg) | |
self.twitter.post(msg) | |
if now.day >= 22: | |
msg = "来月{0}-{1}時の空きテニスコート({2}) {3} 現在\n".format( | |
self.from_time, self.to_time, self.court, now.strftime("%Y-%m-%d %H:%M:%S")) | |
msg += "https://yoyaku.sports.metro.tokyo.jp/" | |
self.logger.debug(msg) | |
year = now.year + 1 if now.month == 12 else now.year | |
month = 1 if now.month == 12 else now.month + 1 | |
msg += self._handle_conditions(year, month, 1) | |
self.slacker.post_message(channel="tennis", text=msg) | |
self.twitter.post(msg) | |
except Exception: | |
self.logger.error(traceback.format_exc()) | |
finally: | |
self.driver.close() | |
self.driver.quit() | |
# static method | |
@staticmethod | |
def _handle_dates(year, month, day): | |
mr = calendar.monthrange(year, month) | |
return range(day, mr[1] + 1) | |
@staticmethod | |
def _handle_week(year, month, day): | |
d = datetime.datetime(year, month, day, 0, 0, 0) | |
week = ["月", "火", "水", "木", "金", "土", "日"] | |
return week[d.weekday()] | |
if __name__ == "__main__": | |
def main(args): | |
if len(args) == 4: | |
TennisTokyo(args[1], args[2], args[3]).run() | |
else: | |
TennisTokyo(19, 21, "人工芝").run() | |
main(sys.argv) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment