-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
67 lines (51 loc) · 1.69 KB
/
app.py
File metadata and controls
67 lines (51 loc) · 1.69 KB
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
# -*- coding: utf-8 -*-
"""
Running this file will run the application.
Remember to configure settings in settings.py before running the app.
Usage:
$ python3 app.py
"""
import sys
import time
import _thread
from datetime import datetime
import irc.client
from jaraco.stream import buffer
from ircbot import IRCBot
import lichess
import settings
try:
from settings import SERVER, PORT, CHANNEL, NICKNAME
except ImportError as e:
print('Unable to import required settings: {}'.format(e), file=sys.stderr)
sys.exit()
def print_error(msg):
"""Prints current datetime and the message msg."""
print('{now} * {msg}'.format(now=str(datetime.now()), msg=msg), file=sys.stderr)
def run_lichess(instance, bot):
"""
:param instance: The instance to run.
:param bot:
:return:
"""
while True:
# This sets the wait time for checking lichess (in seconds).
time.sleep(settings.GET_PLAYING_DELAY)
instance.get_playing(bot)
def main():
server = settings.SERVER
port = settings.PORT
channel = settings.CHANNEL
nickname = settings.NICKNAME
lichessmate = lichess.LichessBot()
# LenientDecodingBuffer attempts UTF-8 but falls back to latin-1 to avoid UnicodeDecoreError in all cases.
irc.client.ServerConnection.buffer_class = buffer.LenientDecodingLineBuffer
ircbot = IRCBot(channel, nickname, server, port, lichessmate, settings)
try:
# Start a new thread for running Lichess, send the ircbot instance to it.
_thread.start_new_thread(run_lichess, (lichessmate, ircbot))
except _thread.error:
print_error('Error: Unable to start thread.')
ircbot.start()
if __name__ == '__main__':
main()