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.
Completed
Pull Request — master (#53)
by Matic
45s
created

PsywerxKarma._karma()   C

Complexity

Conditions 7

Size

Total Lines 22

Duplication

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