OSDN Git Service

[feature]デイリースコア統計ツイートオプション
authorHabu <habu@users.sourceforge.jp>
Mon, 2 Apr 2018 00:30:44 +0000 (09:30 +0900)
committerHabu <habu@users.sourceforge.jp>
Mon, 2 Apr 2018 03:16:05 +0000 (12:16 +0900)
score/tools/tweet_score.py

index 384778d..ccc51ed 100755 (executable)
@@ -27,6 +27,7 @@ OSDNのサーバでrequests_oauthlibを使う方法のメモ
 '''
 
 import sys
+import datetime
 import sqlite3
 import gzip
 import re
@@ -83,6 +84,36 @@ GROUP BY
     return score[0] if len(score) == 1 else None
 
 
+def get_daily_score_stats(score_db_path, year, month, day):
+    '''DBから指定した日付のスコア統計データを得る
+
+    Args:
+        score_db_path: スコアデータが格納されているDBへのパスを表す文字列。
+        year: 指定する年。
+        month: 指定する月。
+        day: 指定する日。
+
+    Returns:
+        取得したスコア統計データを格納した辞書。
+        'total_count': 総スコア件数, 'winner_count': 勝利スコア件数
+    '''
+    with sqlite3.connect(score_db_path) as con:
+        con.row_factory = sqlite3.Row
+        sql = '''
+SELECT
+  count(*) AS total_count,
+  count(winner = 1 OR NULL) AS winner_count
+FROM
+  scores
+WHERE
+  date >= date('{target_date}') AND date < date('{target_date}', '+1 day')
+'''.format(target_date=datetime.date(year, month, day).isoformat())
+        c = con.execute(sql, {})
+        score = c.fetchall()
+
+    return score[0]
+
+
 def get_death_reason_detail(score_id):
     '''ダンプファイル内から詳細な死因を取得する。
 
@@ -113,7 +144,7 @@ def create_tweet(score_db_path, score_id):
 
     Args:
         score_db: スコアデータが格納されているDBへのパスを表す文字列。
-        score_id: 指定するスコアID。
+        score_id: 指定するスコアID。Noneの場合最新のスコアを取得する。
 
     Returns:
         生成したツイートメッセージ文字列。
@@ -123,7 +154,7 @@ def create_tweet(score_db_path, score_id):
     if score_data is None:
         return None
 
-    death_reason_detail = get_death_reason_detail(score_id)
+    death_reason_detail = get_death_reason_detail(score_data['score_id'])
     if death_reason_detail is None:
         death_reason_detail = (u"{0} {1}階"
                                .format(score_data['death_reason'],
@@ -149,6 +180,30 @@ def create_tweet(score_db_path, score_id):
     return tweet
 
 
+def create_daily_stats_tweet(score_db_path, year, month, day):
+    '''デイリースコア統計データのツイートを生成する
+
+    Args:
+        score_db_path: スコアデータが格納されているDBへのパスを表す文字列。
+        year: 指定する年。
+        month: 指定する月。
+        day: 指定する日。
+
+    Returns:
+        生成したツイートメッセージ文字列。
+        なんらかの理由により生成できなかった場合None。
+    '''
+    daily_stats = get_daily_score_stats(score_db_path, year, month, day)
+
+    tweet = (u"{year}年{month}月{day}日のスコア\n"
+             u"全 {total_count} 件, 勝利 {winner_count} 件\n"
+             u"#hengband"
+             ).format(year=year, month=month, day=day,
+                      **daily_stats)
+
+    return tweet
+
+
 def tweet(oauth, tweet_contents):
     '''ツイートする。
 
@@ -188,10 +243,15 @@ def parse_option():
     '''
     from optparse import OptionParser
     parser = OptionParser()
-    parser.add_option("-s", "--score-id",
-                      type="int", dest="score_id",
-                      help="Target score id.\n"
-                           "If this option is not set, latest score is used.")
+    parser.add_option(
+        "-s", "--score-id",
+        type="int", dest="score_id",
+        help="Tweet score with specified id.\n"
+             "When this option and -d are not set, latest score is specified.")
+    parser.add_option(
+        "-d", "--daily-stats",
+        type="string", dest="stats_date",
+        help="Tweet statistics of the score of the specified day.")
     parser.add_option("-c", "--config",
                       type="string", dest="config_file",
                       default="tweet_score.cfg",
@@ -236,8 +296,19 @@ if __name__ == '__main__':
         if 'Python' in config.config:
             sys.path.append(config.config['Python']['local_lib_path'])
 
-        tweet_contents = create_tweet(config.config['ScoreDB']['path'],
-                                      options.score_id)
+        if options.stats_date:
+            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)
+
         if tweet_contents is None:
             logger.warning('No score data found.')
             sys.exit(1)