import json import os import sys import re from datetime import datetime, timezone, timedelta from collections import defaultdict def convert_timestamp_to_jst(timestamp): jst = timezone(timedelta(hours=9)) dt = datetime.fromtimestamp(timestamp / 1000, jst) return dt.strftime('%Y-%m-%d %H:%M:%S JST') def get_year_month(timestamp): jst = timezone(timedelta(hours=9)) dt = datetime.fromtimestamp(timestamp / 1000, jst) return dt.strftime('%Y-%m') def process_directory(directory): try: # 年月ごとの投稿を格納する辞書 monthly_posts = defaultdict(list) # ディレクトリ内の全JSONファイルを処理 for filename in os.listdir(directory): if not filename.endswith('.json') or filename.endswith('_s.json') or filename.startswith('log_short'): continue # ファイル名が数字のみで構成されているか確認 base_name = os.path.splitext(filename)[0] if not re.match(r'^\d+$', base_name): print(f"スキップ: {filename} はファイル名が数字のみではありません") continue input_file = os.path.join(directory, filename) thread_no = int(base_name) # JSONファイルを読み込む with open(input_file, 'r', encoding='utf-8') as f: data = json.load(f) if "thread_array" in data: for post in data["thread_array"]: timestamp = post.get("timestamp") if timestamp: year_month = get_year_month(timestamp) new_post = { "thread_no": thread_no, "num": post.get("num"), "timestamp": timestamp, "datetime": convert_timestamp_to_jst(timestamp), "body": post.get("body") } monthly_posts[year_month].append(new_post) # 年月ごとにJSONファイルを出力 for year_month, posts in monthly_posts.items(): output_file = os.path.join(directory, f'log_short_m_{year_month}.json') with open(output_file, 'w', encoding='utf-8') as f: json.dump({"posts": posts}, f, ensure_ascii=False, indent=2) print(f"変換完了: {output_file}") return True except json.JSONDecodeError as e: print(f"JSONパースエラー: {str(e)}") return False except Exception as e: print(f"エラー: {str(e)}") return False def main(): # 引数が指定されていない場合はカレントディレクトリを使用 directory = sys.argv[1] if len(sys.argv) > 1 else "." process_directory(directory) if __name__ == "__main__": main()