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 (#48)
by
unknown
55s
created

PsywerxKarma   A

Complexity

Total Complexity 21

Size/Duplication

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