-
Notifications
You must be signed in to change notification settings - Fork 0
/
servers.py
47 lines (35 loc) · 1.18 KB
/
servers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import requests
import json
import os
from datetime import datetime
# Fediverseサーバーの情報を取得する関数
def fetch_servers(limit=40, total=40):
servers = []
next_cursor = None
while len(servers) < total:
params = {'limit': limit}
if next_cursor:
params['cursor'] = next_cursor
response = requests.get('https://api.fedidb.org/v1/servers', params=params)
if response.status_code != 200:
print(f"データ取得エラー: {response.status_code}")
break
data = response.json()
servers.extend(data['data'])
next_cursor = data['meta'].get('next_cursor')
if not next_cursor:
break
return servers[:total]
# データをJSONファイルに保存する関数
def save_json(data):
os.makedirs('servers', exist_ok=True)
date_str = datetime.now().strftime('%Y%m%d%H%M%S')
filename = f'servers/{date_str}.json'
with open(filename, 'w', encoding='utf-8') as f:
json.dump(data, f, ensure_ascii=False, indent=2)
def main():
server_data = fetch_servers(limit=40, total=2000)
save_json(server_data)
print(f"{len(server_data)}件のサーバーを取得して保存しました。")
if __name__ == "__main__":
main()