<commit_before>from django.db import models

from thefederation.models.base import ModelBase

__all__ = ('Platform',)


class Platform(ModelBase):
    VERSION_CLEAN_NONE = 'none'
    VERSION_CLEAN_REMOVE_AFTER_DASH = 'remove_after_dash'
    VERSION_CLEAN_STYLES = (
        (VERSION_CLEAN_NONE, VERSION_CLEAN_NONE),
        (VERSION_CLEAN_REMOVE_AFTER_DASH, VERSION_CLEAN_REMOVE_AFTER_DASH),
    )

    code = models.URLField(max_length=128, blank=True)
    description = models.TextField(blank=True)
    display_name = models.CharField(max_length=128, blank=True)
    latest_version = models.CharField(max_length=128, blank=True)
    license = models.CharField(max_length=128, blank=True)
    icon = models.CharField(max_length=80, default='unknown')
    install_guide = models.URLField(max_length=256, blank=True)
    name = models.CharField(max_length=80, unique=True)
    tagline = models.CharField(max_length=300, blank=True)
    version_clean_style = models.CharField(choices=VERSION_CLEAN_STYLES, default=VERSION_CLEAN_NONE, max_length=30)
    website = models.URLField(max_length=128, blank=True)

    def __str__(self):
        return f"{self.name}"

    def save(self, *args, **kwargs):
        if not self.display_name:
            self.display_name = self.name
        super().save(*args, **kwargs)

    def clean_version(self, version):
        if self.version_clean_style == Platform.VERSION_CLEAN_NONE:
            return version
        elif self.version_clean_style == Platform.VERSION_CLEAN_REMOVE_AFTER_DASH:
            return version.split('-')[0]
        return version

    def get_method(self, version):
        """
        Calls a function to get the preferred method.

        Tries to compare with a version.

        :param version: tuple of version, numeric only
        :return:
        """
        return {
            'diaspora': 'nodeinfo' if version >= (0, 5, 3, 0) else "statisticsjson",
            'friendica': 'nodeinfo' if version >= (3, 4, 2) else "statisticsjson",
            'ganggo': 'nodeinfo',
            'hubzilla': 'nodeinfo' if version >= (1, 6) else "statisticsjson",
            'socialhome': 'nodeinfo2' if version > (0, 8) else "nodeinfo",
        }.get(self.name)<commit_msg>Add preferred fetch method for mastodon<commit_after>from django.db import models

from thefederation.models.base import ModelBase

__all__ = ('Platform',)


class Platform(ModelBase):
    VERSION_CLEAN_NONE = 'none'
    VERSION_CLEAN_REMOVE_AFTER_DASH = 'remove_after_dash'
    VERSION_CLEAN_STYLES = (
        (VERSION_CLEAN_NONE, VERSION_CLEAN_NONE),
        (VERSION_CLEAN_REMOVE_AFTER_DASH, VERSION_CLEAN_REMOVE_AFTER_DASH),
    )

    code = models.URLField(max_length=128, blank=True)
    description = models.TextField(blank=True)
    display_name = models.CharField(max_length=128, blank=True)
    latest_version = models.CharField(max_length=128, blank=True)
    license = models.CharField(max_length=128, blank=True)
    icon = models.CharField(max_length=80, default='unknown')
    install_guide = models.URLField(max_length=256, blank=True)
    name = models.CharField(max_length=80, unique=True)
    tagline = models.CharField(max_length=300, blank=True)
    version_clean_style = models.CharField(choices=VERSION_CLEAN_STYLES, default=VERSION_CLEAN_NONE, max_length=30)
    website = models.URLField(max_length=128, blank=True)

    def __str__(self):
        return f"{self.name}"

    def save(self, *args, **kwargs):
        if not self.display_name:
            self.display_name = self.name
        super().save(*args, **kwargs)

    def clean_version(self, version):
        if self.version_clean_style == Platform.VERSION_CLEAN_NONE:
            return version
        elif self.version_clean_style == Platform.VERSION_CLEAN_REMOVE_AFTER_DASH:
            return version.split('-')[0]
        return version

    def get_method(self, version):
        """
        Calls a function to get the preferred method.

        Tries to compare with a version.

        :param version: tuple of version, numeric only
        :return:
        """
        return {
            'diaspora': 'nodeinfo' if version >= (0, 5, 3, 0) else "statisticsjson",
            'friendica': 'nodeinfo' if version >= (3, 4, 2) else "statisticsjson",
            'ganggo': 'nodeinfo',
            'hubzilla': 'nodeinfo' if version >= (1, 6) else "statisticsjson",
            'mastodon': 'mastodon',
            'socialhome': 'nodeinfo2' if version > (0, 8) else "nodeinfo",
        }.get(self.name)
{'fraction_non_alphanumeric': 0.11012826183104821, 'fraction_numerical': 0.017691287041132243, 'mean_word_length': 3.3158396946564888, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5087210', 'n_tokens_mistral': 1500, 'n_tokens_neox': 1414, 'n_words': 302}