GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

PsywerxKarma.fact()   F
last analyzed

Complexity

Conditions 9

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
dl 0
loc 28
rs 3
c 1
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A PsywerxKarma.pf() 0 9 3
1
from base import PsywerxPlugin
2
import re
3
import json
4
5
6
class PsywerxKarma(PsywerxPlugin):
7
8
    # factorize users current karma
9
10
    def fact(self, tokens, channel):
11
        try:
12
            def pf(number):
13
                factors = []
14
                d = 2
15
                while number > 1:
16
                    while number % d == 0:
17
                        factors.append(d)
18
                        number = number / d
19
                    d += 1
20
                return factors
21
22
            if len(tokens) != 1:
23
                response = self.request(channel, 'irc/karma_nick_all', {})
24
                r = json.loads(response)
25
                r = sorted(r, key=lambda x: pf(x['karma']))
26
                for p in r:
27
                    karmas = pf(p['karma'])
28
                    if int(p['karma']) < 2:
29
                        continue
30
                    # insert sleep to prevent floods
31
                    self.bot.say(str(p['nick']) + " " + str(max(karmas)) +
32
                                 " (" + str(p['karma']) + "=" +
33
                                 "*".join(map(str, karmas)) + ")", channel)
34
                self.bot.say(str("** CONGRATS " + p['nick'] + " **"), channel)
35
36
        except Exception:
37
            self.bot.log_error('Could not get upboats.')
38
39
    def _karma(self, tokens, channel):
40
        if len(tokens) != 1:
41
            response = self.request(channel, 'irc/karma_nick', {})
42
            if not response:
43
                msg = "Sorry, could not get karmas."
44
                self.bot.say(msg, channel)
45
                return
46
            s = ""
47
            for p in json.loads(response):
48
                s += str(p['nick']) + " (" + str(p['karma']) + "), "
49
            self.bot.say(s[:-2], channel)
50
        else:
51
            users = self.bot.known_users[channel]
52
            nick = tokens[0] if tokens[0] not in users else users[tokens[0]]
53
            params = {'nick': nick}
54
            response = self.request(channel, 'irc/karma_nick', params)
55
            if not response:
56
                msg = "Sorry, could not get karma for " + nick + "."
57
                self.bot.say(msg, channel)
58
                return
59
            nick = tokens[0] if tokens[0] not in users else users[tokens[0]]
60
            self.bot.say(nick + " has " + response + " karma.", channel)
61
62
    def handle_message(self, channel, nick, msg, line=None):
63
        msg_lower = msg.lower()
64
        self.handle_tokens(channel, msg,
65
                           ('karma', 'karmas', 'leaderboard',
66
                            'upboats', 'upvotes', 'stats',), self._karma)
67
68
        # count karma upvote
69
        if '++' not in msg_lower:
70
            return
71
72
        for user in re.split('[.,!?]* ', msg):
73
            users = self.bot.known_users[channel]
74
            name = user.replace('+', '')
75
            name_lower = name.lower()
76
            if (user.endswith('++') or user.startswith('++')) \
77
                    and name_lower in users:
78
                if name_lower == nick.lower():
79
                    self.bot.say("Nice try " + nick +
80
                                 ", but you can't give karma to yourself!",
81
                                 channel)
82
                    return
83
                else:
84
                    self.request(channel, 'irc/karma',
85
                                 {'nick': users[name_lower]})
86