-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrss2irc.py
More file actions
executable file
·95 lines (82 loc) · 2.63 KB
/
rss2irc.py
File metadata and controls
executable file
·95 lines (82 loc) · 2.63 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/env python
import argparse
import time
import datetime
import ircc
import storage
import grabber
import publisher
import threading
import Queue
import logging
from pprint import pprint
if storage.DEBUG:
loglevel = logging.DEBUG
else:
loglevel = logging.INFO
logging.basicConfig(
level=loglevel,
format='[%(asctime)s %(levelname)s] (%(threadName)-10s) %(message)s',
)
def main():
parser = argparse.ArgumentParser(description='Irc Bot.')
parser.add_argument('--server', dest='server', help='IRC server')
parser.add_argument('--channel', dest='channel', help='IRC channel')
args = parser.parse_args()
if args.server:
host = args.server
else:
host = 'irc.freenode.net'
if args.channel:
channel = args.channel
if not channel.startswith('#'):
channel = '#' + channel
else:
channel = '#999net'
channels = [channel]
port = 6667
feed_queue = Queue.Queue(100)
# Satisfying IRCConnector interface
main_thread = threading.current_thread()
main_thread.kill_received = threading.Event()
grabber_thread = grabber.Grabber(feed_queue)
grabber_thread.daemon = True
grabber_thread.start()
irc_thread = ircc.IRCConnector(main_thread, host, port, channels)
irc_thread.daemon = True
irc_thread.start()
irc_queue = None
attempts = 3
reconnect_time = 5
while attempts:
try:
irc_queue = irc_thread.channel_queues[channel]
except KeyError:
logging.warning("No channel_queues['{0}'] instantiated. Get some sleep.".format(channel))
attempts -= 1
time.sleep(reconnect_time)
else:
logging.info("channel_queues['{0}'] instantiated. Go ahead.".format(channel))
break
store = storage.Storage()
store.daemon = True
store.start()
publisher_thread = publisher.Publisher(feed_queue, store.queue, irc_queue, irc_thread.botname)
publisher_thread.start()
threads = []
threads.append(grabber_thread)
threads.append(irc_thread)
threads.append(store)
threads.append(publisher_thread)
while not irc_thread.kill_received.is_set():
logging.debug("irc_thread.kill_received IS NOT SET.")
logging.debug(str([t.name for t in threading.enumerate()]))
time.sleep(1)
logging.info("irc_thread.kill_received IS SET. Killing all threads and exiting.")
store.kill_received.set()
grabber_thread.kill_received.set()
publisher_thread.kill_received.set()
time.sleep(1)
logging.debug(str([t.name for t in threading.enumerate()]))
if __name__ == '__main__':
main()