summaryrefslogtreecommitdiff
path: root/net/onlineusers.py
blob: 92b5b0fad1af4b7da7652a69a1a41f872a4e5182 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
"""
Copyright 2015-2016, Joseph Botosh <rumly111@gmail.com>

This file is part of tradey, a trading bot in The Mana World
see www.themanaworld.org

This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the Free
Software Foundation; either version 2 of the License, or (at your option)
any later version.

This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
more details.

You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>.

Additionally to the GPL, you are *strongly* encouraged to share any
modifications you do on these sources.
"""

import urllib2
import string
import threading
import time

from common import netlog


class OnlineUsers(threading.Thread):

    def __init__(self, online_url='https://server.themanaworld.org/online-old.txt',
                 update_interval=60, refresh_hook=None):
        self._active = True
        self._timer = 0
        self._url = online_url
        self._update_interval = update_interval
        self.__lock = threading.Lock()
        self.__online_users = []
        self.refresh_hook = refresh_hook
        threading.Thread.__init__(self)

    @property
    def online_users(self):
        self.__lock.acquire(True)
        users = self.__online_users[:]
        self.__lock.release()
        return users

    @staticmethod
    def dl_online_list(url):
        """
        Download online.txt, parse it, and return a list of online user nicks.
        If error occurs, return empty list
        """
        try:
            data = urllib2.urlopen(url).read()
        except urllib2.URLError, e:
            netlog.error("urllib error: %s", e.message)
            return []
        start = string.find(data, '------------------------------\n') + 31
        end = string.rfind(data, '\n\n')
        s = data[start:end]
        return map(lambda n: n[:-5].strip() if n.endswith('(GM) ')
                   else n.strip(), s.split('\n'))

    def run(self):
        while self._active:
            if (time.time() - self._timer) > self._update_interval:
                users = self.dl_online_list(self._url)

                if self.refresh_hook:
                    self.refresh_hook(set(users), set(self.__online_users))

                self.__lock.acquire(True)
                self.__online_users = users
                self.__lock.release()
                self._timer = time.time()
            else:
                time.sleep(1.0)

    def stop(self):
        self._active = False