forked from doubaniux/boofilsic
-
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Her Email
committed
Nov 10, 2023
1 parent
ddd196d
commit b8c3ed9
Showing
1 changed file
with
42 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import pprint | ||
from datetime import timedelta | ||
|
||
from django.core.management.base import BaseCommand | ||
from django.utils import timezone | ||
from loguru import logger | ||
|
||
from mastodon.api import detect_server_info | ||
from mastodon.models import MastodonApplication | ||
|
||
|
||
class Command(BaseCommand): | ||
help = "Check mastodon sites' status and disable unreachable sites." | ||
|
||
max_unreachable_days = 31 | ||
|
||
def handle(self, *args, **options): | ||
logger.info("Mastodon Site Check start.") | ||
count_checked = 0 | ||
count_unreachable = 0 | ||
count_disabled = 0 | ||
for site in MastodonApplication.objects.exclude(disabled=True): | ||
domain = None | ||
count_checked += 1 | ||
try: | ||
domain, api_domain, v = detect_server_info(site.domain_name) | ||
site.last_reachable_date = timezone.now() | ||
except: | ||
logger.warning(f"Failed to detect server info for {site.domain_name}") | ||
count_unreachable += 1 | ||
if site.last_reachable_date is None: | ||
site.last_reachable_date = timezone.now() - timedelta(days=1) | ||
if timezone.now() > site.last_reachable_date + timedelta( | ||
days=self.max_unreachable_days | ||
): | ||
site.disabled = True | ||
count_disabled += 1 | ||
finally: | ||
site.save(update_fields=["last_reachable_date", "disabled"]) | ||
logger.info( | ||
f"Mastodon Site Check finished, {count_checked} checked, {count_unreachable} unreachable, {count_disabled} disabled." | ||
) |