Пример #1
0
    def update_bot_details(self, to_check: BotModel, peer):
        """
        Set basic properties of the bot
        """
        if isinstance(peer, ResolvedPeer):
            peer = self.resolve_peer(peer.peer.user_id)
        elif isinstance(peer, InputPeerUser):
            pass
        else:
            peer = self.resolve_peer(peer.id)

        try:
            user = self.send(GetUsers([peer]))[0]
        except:
            traceback.print_exc()
            print("this peer does not work for GetUsers:")
            print(type(peer))
            print(peer)
            return None

        if hasattr(user, 'bot') and user.bot is True:
            # Regular bot
            to_check.official = bool(user.verified)
            to_check.inlinequeries = bool(user.bot_inline_placeholder)
            to_check.name = user.first_name
            to_check.bot_info_version = user.bot_info_version
        else:
            # Userbot
            to_check.userbot = True
            to_check.name = helpers.format_name(user)

        # In any case
        to_check.chat_id = int(user.id)
        to_check.username = '******' + str(user.username)
Пример #2
0
async def get_ranks(after='1y', sort='top', limit=None):
    if sort == 'hot':
        after = '1y'
    epoch = get_epoch(after)
    ranks = []
    db = DB(DB_FILE)
    await db.connect()
    data = await db.get_ranks(epoch, sort, limit)
    num = 1

    async for row in data:
        bot, link_karma, comment_karma, good_bots, bad_bots, top_score, hot_score, controversial_score = row
        rank = Bot()
        rank.name = bot
        rank.score = top_score
        votes = Votes()
        votes.good = good_bots
        votes.bad = bad_bots
        rank.votes = votes
        karma = Karma()
        karma.link = link_karma
        karma.comment = comment_karma
        rank.karma = karma
        ranks.append(rank)
    await db.close()
    for bot in sorted(ranks, key=lambda x: x.score, reverse=True):
        bot.rank = num
        num += 1
    return ranks
def submit():
    if not request.is_json:
        res = _error('MimeType must be application/json.')
        res.status_code = 400
        return res

    content = request.get_json()
    try:
        access = APIAccess.get(APIAccess.token == content.get('token'))
    except APIAccess.DoesNotExist:
        res = _error('The access token is invalid.')
        res.status_code = 401
        return res

    username = content.get('username')
    if username is None or not isinstance(username, str):
        res = _error('You must supply a username.')
        res.status_code = 400
        return res

    # insert `@` if omitted
    username = '******' + username if username[0] != '@' else username

    try:
        Bot.get(Bot.username == username)
        res = _error('The bot {} is already in the BotList.'.format(username))
        res.status_code = 409
        return res
    except Bot.DoesNotExist:
        b = Bot(username=username)

    name = content.get('name')
    description = content.get('description')
    inlinequeries = content.get('inlinequeries')

    try:
        if name:
            if isinstance(name, str):
                b.name = name
            else:
                raise AttributeError('The name field must be a string.')
        if description:
            if isinstance(description, str):
                b.description = description
            else:
                raise AttributeError('The description field must be a string.')
        if inlinequeries:
            if isinstance(inlinequeries, bool):
                b.inlinequeries = inlinequeries
            else:
                raise AttributeError(
                    'The inlinequeries field must be a boolean.')
    except Exception as e:
        res = _error(str(e))
        res.status_code = 400
        return res

    b.date_added = datetime.date.today()
    b.submitted_by = access.user
    b.approved = False

    b.save()
    res = jsonify({'success': '{} was submitted for approval.'.format(b)})
    res.status_code = 201
    return res