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 (#51)
by Matic
45s
created

PsywerxKarma.fact()   F

Complexity

Conditions 9

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 9
dl 0
loc 29
rs 3

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
    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
            from traceback import format_exc
37
            self.bot.log_error('Could not get upboats: ' + str(format_exc()))
38
39
    def _karma(self, tokens, channel):
40
        if len(tokens) != 1:
41
            r = json.loads(self.request(channel, 'irc/karma_nick', {}))
42
            s = ""
43
            for p in r:
44
                s += str(p['nick']) + " (" + str(p['karma']) + "), "
45
            self.bot.say(s[:-2], channel)
46
        else:
47
            users = self.bot.known_users[channel]
48
            nick = tokens[0] if tokens[0] not in users else users[tokens[0]]
49
            params = {'nick': nick}
50
            response = self.request(channel, 'irc/karma_nick', params)
51
            if not response:
52
                return
53
            nick = tokens[0] if tokens[0] not in users else users[tokens[0]]
54
            self.bot.say(nick + " has " + response + " karma.", channel)
55
56
    def handle_message(self, channel, nick, msg, line=None):
57
        msg_lower = msg.lower()
58
        self.handle_tokens(channel, msg,
59
                           ('karma', 'karmas', 'leaderboard',
60
                            'upboats', 'upvotes', 'stats',), self._karma)
61
62
        # count karma upvote
63
        if '++' not in msg_lower:
64
            return
65
66
        for user in re.split('[.,!?]* ', msg):
67
            users = self.bot.known_users[channel]
68
            name = user.replace('+', '')
69
            name_lower = name.lower()
70
            if (user.endswith('++') or user.startswith('++')) \
71
                    and name_lower in users:
72
                if name_lower == nick.lower():
73
                    self.bot.say("Nice try " + nick +
74
                                 ", but you can't give karma to yourself!",
75
                                 channel)
76
                    return
77
                else:
78
                    self.request(channel, 'irc/karma',
79
                                 {'nick': users[name_lower]})
80