-
Notifications
You must be signed in to change notification settings - Fork 37
/
rss2telegram.py
196 lines (173 loc) ยท 6.17 KB
/
rss2telegram.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
from bs4 import BeautifulSoup
from telebot import types
from time import gmtime
import feedparser
import os
import re
import telebot
import telegraph
import time
import random
import requests
import sqlite3
def get_variable(variable):
if not os.environ.get(f'{variable}'):
var_file = open(f'{variable}.txt', 'r')
return var_file.read()
return os.environ.get(f'{variable}')
URL = get_variable('URL')
DESTINATION = get_variable('DESTINATION')
BOT_TOKEN = os.environ.get('BOT_TOKEN')
EMOJIS = os.environ.get('EMOJIS', '๐,๐ฐ,๐,๐,๐,๐,๐,๐')
PARAMETERS = os.environ.get('PARAMETERS', False)
HIDE_BUTTON = os.environ.get('HIDE_BUTTON', False)
DRYRUN = os.environ.get('DRYRUN')
TOPIC = os.environ.get('TOPIC', False)
TELEGRAPH_TOKEN = os.environ.get('TELEGRAPH_TOKEN', False)
bot = telebot.TeleBot(BOT_TOKEN)
def add_to_history(link):
conn = sqlite3.connect('rss2telegram.db')
cursor = conn.cursor()
aux = f'INSERT INTO history (link) VALUES ("{link}")'
cursor.execute(aux)
conn.commit()
conn.close()
def check_history(link):
conn = sqlite3.connect('rss2telegram.db')
cursor = conn.cursor()
aux = f'SELECT * from history WHERE link="{link}"'
cursor.execute(aux)
data = cursor.fetchone()
conn.close()
return data
def firewall(text):
try:
rules = open(f'RULES.txt', 'r')
except FileNotFoundError:
return True
result = None
for rule in rules.readlines():
opt, arg = rule.split(':')
arg = arg.strip()
if arg == 'ALL' and opt == 'DROP':
result = False
elif arg == 'ALL' and opt == 'ACCEPT':
result = True
elif arg.lower() in text.lower() and opt == 'DROP':
result = False
elif arg.lower() in text.lower() and opt == 'ACCEPT':
result = True
return result
def create_telegraph_post(topic):
telegraph_auth = telegraph.Telegraph(
access_token=f'{get_variable("TELEGRAPH_TOKEN")}'
)
response = telegraph_auth.create_page(
f'{topic["title"]}',
html_content=(
f'{topic["summary"]}<br><br>'
+ f'<a href="{topic["link"]}">Ver original ({topic["site_name"]})</a>'
),
author_name=f'{topic["site_name"]}'
)
return response["url"]
def send_message(topic, button):
if DRYRUN == 'failure':
return
MESSAGE_TEMPLATE = os.environ.get(f'MESSAGE_TEMPLATE', False)
if MESSAGE_TEMPLATE:
MESSAGE_TEMPLATE = set_text_vars(MESSAGE_TEMPLATE, topic)
else:
MESSAGE_TEMPLATE = f'<b>{topic["title"]}</b>'
if TELEGRAPH_TOKEN:
iv_link = create_telegraph_post(topic)
MESSAGE_TEMPLATE = f'<a href="{iv_link}">๓ </a>{MESSAGE_TEMPLATE}'
if not firewall(str(topic)):
print(f'xxx {topic["title"]}')
return
btn_link = button
if button:
btn_link = types.InlineKeyboardMarkup()
btn = types.InlineKeyboardButton(f'{button}', url=topic['link'])
btn_link.row(btn)
if HIDE_BUTTON or TELEGRAPH_TOKEN:
for dest in DESTINATION.split(','):
bot.send_message(dest, MESSAGE_TEMPLATE, parse_mode='HTML', reply_to_message_id=TOPIC)
else:
if topic['photo'] and not TELEGRAPH_TOKEN:
response = requests.get(topic['photo'], headers = {'User-agent': 'Mozilla/5.1'})
open('img', 'wb').write(response.content)
for dest in DESTINATION.split(','):
photo = open('img', 'rb')
try:
bot.send_photo(dest, photo, caption=MESSAGE_TEMPLATE, parse_mode='HTML', reply_markup=btn_link, reply_to_message_id=TOPIC)
except telebot.apihelper.ApiTelegramException:
topic['photo'] = False
send_message(topic, button)
else:
for dest in DESTINATION.split(','):
bot.send_message(dest, MESSAGE_TEMPLATE, parse_mode='HTML', reply_markup=btn_link, disable_web_page_preview=True, reply_to_message_id=TOPIC)
print(f'... {topic["title"]}')
time.sleep(0.2)
def get_img(url):
try:
response = requests.get(url, headers = {'User-agent': 'Mozilla/5.1'}, timeout=3)
html = BeautifulSoup(response.content, 'html.parser')
photo = html.find('meta', {'property': 'og:image'})['content']
except TypeError:
photo = False
except requests.exceptions.ReadTimeout:
photo = False
except requests.exceptions.TooManyRedirects:
photo = False
return photo
def define_link(link, PARAMETERS):
if PARAMETERS:
if '?' in link:
return f'{link}&{PARAMETERS}'
return f'{link}?{PARAMETERS}'
return f'{link}'
def set_text_vars(text, topic):
cases = {
'SITE_NAME': topic['site_name'],
'TITLE': topic['title'],
'SUMMARY': re.sub('<[^<]+?>', '', topic['summary']),
'LINK': define_link(topic['link'], PARAMETERS),
'EMOJI': random.choice(EMOJIS.split(","))
}
for word in re.split('{|}', text):
try:
text = text.replace(word, cases.get(word))
except TypeError:
continue
return text.replace('\\n', '\n').replace('{', '').replace('}', '')
def check_topics(url):
now = gmtime()
feed = feedparser.parse(url)
try:
source = feed['feed']['title']
except KeyError:
print(f'\nERRO: {url} nรฃo parece um feed RSS vรกlido.')
return
print(f'\nChecando {source}:{url}')
for tpc in reversed(feed['items'][:10]):
if check_history(tpc.links[0].href):
continue
add_to_history(tpc.links[0].href)
topic = {}
topic['site_name'] = feed['feed']['title']
topic['title'] = tpc.title.strip()
topic['summary'] = tpc.summary
topic['link'] = tpc.links[0].href
topic['photo'] = get_img(tpc.links[0].href)
BUTTON_TEXT = os.environ.get('BUTTON_TEXT', False)
if BUTTON_TEXT:
BUTTON_TEXT = set_text_vars(BUTTON_TEXT, topic)
try:
send_message(topic, BUTTON_TEXT)
except telebot.apihelper.ApiTelegramException as e:
print(e)
pass
if __name__ == "__main__":
for url in URL.split():
check_topics(url)