-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
195 lines (168 loc) · 6.07 KB
/
main.py
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import os
import pickle
import irc.client
import sys
import ssl
import time
import random
import re
import os
from functools import reduce
from telethon.sync import TelegramClient, events
from telethon.tl.functions.messages import (GetHistoryRequest)
##irc
server = os.environ.get('SERVER_URL', "irc.dead.guru")
port = int(os.environ.get('PORT', 6697))
channel = os.environ.get('CHANNEL_NAME', "#news")
botnick = os.environ.get('BOT_NICK', "news_bot")
delim = "________________"
##telegram
api_id = os.environ.get('API_ID', 10000001)
api_hash = os.environ.get('API_HASH', 'api_hash')
channels = {
't.me/operativnoZSU': {'url': 't.me/operativnoZSU', 'last': 0, 'peer': None},
't.me/OP_UA': {'url': 't.me/OP_UA', 'last': 0, 'peer': None},
't.me/SBUkr': {'url': 't.me/SBUkr', 'last': 0, 'peer': None},
't.me/mvs_ukraine': {'url': 't.me/mvs_ukraine', 'last': 0, 'peer': None},
't.me/Ukraine_MFA': {'url': 't.me/Ukraine_MFA', 'last': 0, 'peer': None},
't.me/spravdi': {'url': 't.me/spravdi', 'last': 0, 'peer': None},
't.me/dsszzi_official': {'url': 't.me/dsszzi_official', 'last': 0, 'peer': None},
't.me/air_alert_ua': {'url': 't.me/air_alert_ua', 'last': 0, 'peer': None},
't.me/dsns_telegram': {'url': 't.me/dsns_telegram', 'last': 0, 'peer': None},
't.me/DIUkraine': {'url': 't.me/DIUkraine', 'last': 0, 'peer': None}
}
client = TelegramClient('dev-session', api_id, api_hash)
client.start()
def resolve_peer(c):
global channels #
chan = channels[c]
if chan['peer'] is not None:
return pickle.loads(chan['peer'])
peer = client.get_entity(chan['url'])
channels[c]['peer'] = pickle.dumps(peer)
return peer
## ----- SANITIZER -----
# TODO: convert it in a single class/function/decorator/whatever
def remove_emojis(data):
emoj = re.compile("["
u"\U0001F600-\U0001F64F" # emoticons
u"\U0001F300-\U0001F5FF" # symbols & pictographs
u"\U0001F680-\U0001F6FF" # transport & map symbols
u"\U0001F1E0-\U0001F1FF" # flags (iOS)
u"\U00002500-\U00002BEF" # chinese char
u"\U00002702-\U000027B0"
u"\U00002702-\U000027B0"
u"\U000024C2-\U0001F251"
u"\U0001f926-\U0001f937"
u"\U00010000-\U0010ffff"
u"\u2640-\u2642"
u"\u2600-\u2B55"
u"\u200d"
u"\u23cf"
u"\u23e9"
u"\u231a"
u"\ufe0f" # dingbats
u"\u3030"
"]+", re.UNICODE)
return re.sub(emoj, '', data)
def remove_urls(data):
return re.sub(r'http\S+', '', data)
def remove_linebreaks(data):
return data.replace("\n", " ")
def disarm_hashtags(data):
'''TG Hashtags are being mistaken as an IRC channels'''
# with r'(?:^|\W)#(\S+)' we loose the whitespace, that's odd
return re.sub(r'(^|\W)#(\S+)', r'\g<1>tag:\g<2>', data)
def sanitize_tg_message(data):
# sequentially apply all the sanitizer functions
return reduce(lambda res, f: f(res), [remove_emojis, disarm_hashtags, remove_linebreaks, remove_urls], data)
## ----- END SANITIZER -----
#NO OP
def nop():
pass
def spUtf8(string, byte_limit):
symbol_size = lambda s : sys.getsizeof(str(s).encode("utf-8"))
check_the_limit = lambda a, l : symbol_size(a) < l
w = ' '
limit = byte_limit - symbol_size('[xx]: ') #spare byte limit minus the preambple of each message
calibrate = lambda a : check_the_limit(a, limit)
# message fits in one IRC packet
if calibrate(string):
return [string]
tmpStr = ""
strings = []
words = str.split(string)
# split incomming text into an aray of IRC-fitted messages
for word in words:
if calibrate(tmpStr + w + word):
tmpStr += (w + word)
else:
strings.append(f'[{len(strings)}]: {tmpStr}')
tmpStr = word
strings.append(f'[{len(strings)}]: {tmpStr}') if len(tmpStr) else nop()
return strings
def handle(connection):
connection.privmsg(channel, delim)
for key in channels:
chan = channels[key]
time.sleep(random.randint(2, 10))
posts = client(GetHistoryRequest(
peer=resolve_peer(key),
limit=1,
offset_date=None,
offset_id=0,
max_id=0,
min_id=0,
add_offset=0,
hash=0))
if chan['last'] != posts.messages[0].id:
chan['last'] = posts.messages[0].id
# remove links, remove linebreaks, remove emojis
message = sanitize_tg_message(posts.messages[0].message)
if len(message) != 0:
connection.privmsg(channel, chan['url'] + ":")
for line in spUtf8(message, 470):
if line is not None:
connection.privmsg(channel, line)
connection.privmsg(channel, delim)
connection.quit("done")
def on_connect(connection, event):
if irc.client.is_channel(channel):
connection.join(channel)
return
def on_join(connection, event):
handle(connection)
def on_disconnect(connection, event):
storeDB('db.bin')
raise SystemExit()
def storeDB(filepath):
f = open(filepath, 'wb')
f.write(pickle.dumps(channels))
def prepareDB(filepath):
global channels
try:
f = open(filepath,'rb')
try:
channels = pickle.loads(f.read())
except pickle.UnpicklingError:
os.unlink(filepath)
except IOError:
f = open(filepath, 'wb+')
f.write(pickle.dumps(channels))
print("DB Created")
def main():
prepareDB('db.bin')
ssl_factory = irc.connection.Factory(wrapper=ssl.wrap_socket)
reactor = irc.client.Reactor()
try:
c = reactor.server().connect(server, port, botnick, connect_factory=ssl_factory)
except:
print("ERROR")
print(sys.exc_info()[1])
raise SystemExit(1)
c.add_global_handler("welcome", on_connect)
c.add_global_handler("join", on_join)
c.add_global_handler("disconnect", on_disconnect)
reactor.process_forever()
if __name__ == "__main__":
main()