From d48fc7e9b3c476d29e313b0d2eb2c1a6b4872656 Mon Sep 17 00:00:00 2001 From: Habu Date: Mon, 2 Apr 2018 12:38:30 +0900 Subject: [PATCH] =?utf8?q?[modify]=E8=A8=AD=E5=AE=9A=E3=82=92=E5=BC=95?= =?utf8?q?=E6=95=B0=E3=81=A7=E6=B8=A1=E3=81=95=E3=81=9A=E3=80=81config?= =?utf8?q?=E3=83=A2=E3=82=B8=E3=83=A5=E3=83=BC=E3=83=AB=E3=81=AE=E5=80=A4?= =?utf8?q?=E3=82=92=E7=9B=B4=E6=8E=A5=E5=8F=82=E7=85=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- score/tools/tweet_score.py | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/score/tools/tweet_score.py b/score/tools/tweet_score.py index ccc51ed..bd1cf79 100755 --- a/score/tools/tweet_score.py +++ b/score/tools/tweet_score.py @@ -34,11 +34,10 @@ import re import config -def get_score_data(score_db_path, score_id): +def get_score_data(score_id): '''DBからスコアデータを取得する。 Args: - score_db_path: スコアデータが格納されているDBへのパスを表す文字列。 score_id: 取得するスコアのスコアID。 Noneの場合最新のスコアを取得する。 @@ -51,7 +50,7 @@ def get_score_data(score_db_path, score_id): else: cond = 'WHERE score_id = :score_id' - with sqlite3.connect(score_db_path) as con: + with sqlite3.connect(config.config['ScoreDB']['path']) as con: con.row_factory = sqlite3.Row sql = ''' SELECT @@ -84,11 +83,10 @@ GROUP BY return score[0] if len(score) == 1 else None -def get_daily_score_stats(score_db_path, year, month, day): +def get_daily_score_stats(year, month, day): '''DBから指定した日付のスコア統計データを得る Args: - score_db_path: スコアデータが格納されているDBへのパスを表す文字列。 year: 指定する年。 month: 指定する月。 day: 指定する日。 @@ -97,7 +95,7 @@ def get_daily_score_stats(score_db_path, year, month, day): 取得したスコア統計データを格納した辞書。 'total_count': 総スコア件数, 'winner_count': 勝利スコア件数 ''' - with sqlite3.connect(score_db_path) as con: + with sqlite3.connect(config.config['ScoreDB']['path']) as con: con.row_factory = sqlite3.Row sql = ''' SELECT @@ -139,18 +137,17 @@ def get_death_reason_detail(score_id): return match.group(1) if match else None -def create_tweet(score_db_path, score_id): +def create_tweet(score_id): '''ツイートするメッセージを生成する。 Args: - score_db: スコアデータが格納されているDBへのパスを表す文字列。 score_id: 指定するスコアID。Noneの場合最新のスコアを取得する。 Returns: 生成したツイートメッセージ文字列。 なんらかの理由により生成できなかった場合None。 ''' - score_data = get_score_data(score_db_path, options.score_id) + score_data = get_score_data(score_id) if score_data is None: return None @@ -180,11 +177,10 @@ def create_tweet(score_db_path, score_id): return tweet -def create_daily_stats_tweet(score_db_path, year, month, day): +def create_daily_stats_tweet(year, month, day): '''デイリースコア統計データのツイートを生成する Args: - score_db_path: スコアデータが格納されているDBへのパスを表す文字列。 year: 指定する年。 month: 指定する月。 day: 指定する日。 @@ -193,7 +189,7 @@ def create_daily_stats_tweet(score_db_path, year, month, day): 生成したツイートメッセージ文字列。 なんらかの理由により生成できなかった場合None。 ''' - daily_stats = get_daily_score_stats(score_db_path, year, month, day) + daily_stats = get_daily_score_stats(year, month, day) tweet = (u"{year}年{month}月{day}日のスコア\n" u"全 {total_count} 件, 勝利 {winner_count} 件\n" @@ -204,7 +200,7 @@ def create_daily_stats_tweet(score_db_path, year, month, day): return tweet -def tweet(oauth, tweet_contents): +def tweet(tweet_contents): '''ツイートする。 Args: @@ -217,7 +213,7 @@ def tweet(oauth, tweet_contents): from logging import getLogger logger = getLogger(__name__) - twitter = OAuth1Session(**oauth) + twitter = OAuth1Session(**config.config['TwitterOAuth']) url = "https://api.twitter.com/1.1/statuses/update.json" @@ -300,14 +296,12 @@ if __name__ == '__main__': target_datetime = datetime.datetime.strptime( options.stats_date, "%Y-%m-%d") tweet_contents = create_daily_stats_tweet( - config.config['ScoreDB']['path'], target_datetime.year, target_datetime.month, target_datetime.day ) else: - tweet_contents = create_tweet(config.config['ScoreDB']['path'], - options.score_id) + tweet_contents = create_tweet(options.score_id) if tweet_contents is None: logger.warning('No score data found.') @@ -316,7 +310,7 @@ if __name__ == '__main__': if (options.dry_run): print(tweet_contents.encode("UTF-8")) else: - tweet(config.config['TwitterOAuth'], tweet_contents) + tweet(tweet_contents) except Exception: from traceback import format_exc logger.critical(format_exc()) -- 2.11.0