-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotion_auto.py
337 lines (255 loc) · 11.1 KB
/
notion_auto.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
from notion.client import NotionClient
import telebot
from github import Github
import dotenv
import os
import requests
from bs4 import BeautifulSoup
from flask import Flask
import time
from threading import Thread
dotenv.load_dotenv()
# Idea
# The idea of this project is to implement the Notion api to my personal notes and automate the creation
# of links and table items through a Telegram Bot.
# How it Works
# The bot will be added to a group and once a link is send the bot will use regular expressions to read the link
# based on the items
# information contained within the links, the bot will add it to the appropriate table.
#
# Step by step process
# get link from groupchat
# get tags, language
# use requests to get title (only for yt, medium, tds, else title = " ")
# use regex to check the title and the link for a programming language/ possible tags
# if none, tag = " "
# add to table
# message chat when complete
#
#
app = Flask(__name__)
list_urls = []
class Bots:
def __init__(self, _type, urls_list):
"""On start, the bot will automatically try to add links to Notion. If there are no links,
it will wait several seconds and retry recursively.
"""
# Store the URLs
self.urls = urls_list
# Create the _type variable
self._type = _type
# Connect to Notion
token = os.getenv(f"TOKEN")
self.client = NotionClient(token_v2=token)
self.link = os.getenv("LINK")
# Connect to GitHub
# Loading the github token & connecting to GH bot
gh_token = os.getenv("GITHUB_TOKEN")
self.GH = Github(gh_token)
# Connecting bots
tg_token = os.getenv(self._type)
self.bot = telebot.TeleBot(token=tg_token)
# Recipient
self.send_to = os.getenv('SEND_TO')
@staticmethod
def back_up(url, new_type=None):
# This will run if the below mentioned functions fail.
r = requests.get(url).text
soup = BeautifulSoup(r, 'lxml')
title = soup.find('title').text
language = "None"
# Preparing for the Notion Class
return title, language, new_type, url
def gh(self, url):
# Get the items
# information for the named repository.
title = url.split("/")[-1]
repository = url.split('/')[-2] + "/" + title
language = self.GH.get_repo(repository).language
new_type = "Github"
items = (title, language, new_type, url)
# Preparing for the Notion Class
return tuple(items)
@staticmethod
def yt(url):
# Get the items
# information of the youtube video.
r = requests.get(url).text
soup = BeautifulSoup(r, 'lxml')
title = soup.find('title').text.replace(' - YouTube', '')
language = 'None'
new_type = 'Video'
items = (title, language, new_type, url)
# Preparing for the Notion Class
return tuple(items)
@staticmethod
def tds(url):
# Finding the items
# information of the Medium/Towards Data Science articles.
r = requests.get(url).text
soup = BeautifulSoup(r, 'lxml')
# Getting the title
title = soup.find('title').text
# Getting all of the <a> tags from the HTML
list_a = soup.findAll('a', {'class': ['cd', 'b', 'ce', 'qm', 'cg', 'qn', 'qo', 'gr', 's', 'ln']})
prog = ['python', 'java', 'javascript', 'cpp', 'c++']
languages = [lang for lang in prog if lang in [_.text.lower() for _ in list_a]]
new_type = 'Article'
items = (title, languages[0], new_type, url)
# Preparing for the Notion Class
return tuple(items)
class VideoBot(Bots):
def run(self):
print('|Starting Videos Bot|')
@self.bot.message_handler(func=(lambda message: True))
def _reply(message):
time.sleep(5)
# Check every message to see if any of the values in white list are in it, and then
# get sorted out based on the "if" statements.
if 'http' in message.text:
# Allowed links
white_list = ['github.com', 'youtu.be', 'youtube.com']
# Returns True if any of the values in white list are in the text
if any([True for value in white_list if value in message.text]):
value = [value for value in white_list if value in message.text]
if value[0] == 'youtu.be' or value[0] == 'youtube.com':
try:
# Main function for Videos
link = self.yt(message.text)
self.urls.append(link)
except Exception as e:
print(e)
# Calling the back up in case of failure
link = self.back_up(message.text, new_type='Article')
self.urls.append(link)
finally:
self.bot.send_message(self.send_to,
f'Adding a link to Notion ASAP...',
disable_web_page_preview=True)
if value[0] == 'github.com':
try:
# Main function for Repositories
link = self.gh(message.text)
self.urls.append(link)
except Exception as e:
print(e)
# Calling the back up in case of failure
link = self.back_up(message.text, new_type='Article')
self.urls.append(link)
finally:
self.bot.send_message(self.send_to,
f'Adding "{self.link[0]}" to Notion ASAP...',
disable_web_page_preview=True)
else:
time.sleep(1)
self.bot.send_message(self.send_to, 'That seems like an article!')
else:
time.sleep(1)
self.bot.send_message(self.send_to, "Hey, Are you there? I think this is for you!")
self.bot.infinity_polling()
class ArticleBot(Bots):
def run(self):
print('|Starting Articles Bot|')
@self.bot.message_handler(func=lambda message: True)
def _reply(message):
time.sleep(5)
if "http" in message.text:
# Black list the values below as the bots will be in the
black_list = ['github.com', 'youtu.be', 'youtube.com']
if any([True for value in black_list if value in message.text]):
# After filtering out values:
self.bot.send_message(self.send_to, f'Hey! I will leave that to someone else!')
return
if 'medium.com' in message.text or 'towardsdatascience.com' in message.text:
try:
# Main function for Articles
link = self.tds(message.text)
self.urls.append(link)
except Exception as e:
print(e)
# Calling the back up in case of failure
link = self.back_up(message.text, new_type='Article')
self.urls.append(link)
finally:
self.bot.send_message(self.send_to,
f'Adding "{self.link[0]}" to Notion ASAP...',
disable_web_page_preview=True)
else:
try:
# Calling the back up since this will be for a general case
link = self.back_up(message.text, new_type='Article')
self.urls.append(link)
except Exception as e:
print(e)
link = ('None', 'None', 'Article', message.text)
self.urls.append(link)
finally:
self.bot.send_message(self.send_to,
f'Adding "{self.link[0]}" to Notion ASAP...',
disable_web_page_preview=True)
else:
time.sleep(1)
# Replying to any other message that isn't http
self.bot.send_message(self.send_to, 'I think that he isn\'t talking to me...')
self.bot.infinity_polling()
class WorkingBot(Bots):
def run(self):
print('|Starting Working Bot|')
self.bot.send_message(self.send_to, "Starting to work, don't bother me!")
@self.bot.message_handler(func=lambda message: True)
def _reply(message):
time.sleep(5)
if 'http' in message.text:
self.bot.send_dice(self.send_to)
white_list = ['github.com', 'youtu.be', 'youtube.com']
if any([True for value in white_list if value in message.text]):
self.bot.send_message(self.send_to, 'I will add it to Notion right now ')
else:
self.bot.send_message(self.send_to, 'I will add it to Notion right now!')
while len(self.urls) != 0:
try:
for items in self.urls:
title, language, new_type, url = items
# Add to notion table.
page = self.client.get_collection_view(self.link)
new_row = page.collection.add_row()
new_row.name = title
new_row.link = url
new_row.type = new_type
try:
new_row.language = language
except Exception as e:
print(e)
new_row.language = "None"
print('|Added to Notion!|')
self.urls.remove(items)
except Exception as e:
return e
finally:
print('Done')
else:
time.sleep(2)
self.bot.send_message(self.send_to, f"Don't interrupt me {os.getenv('DEV_USERNAME')}!")
self.bot.infinity_polling()
@app.route('/')
def home():
return "Hello. I am alive!"
def run():
app.run(host='0.0.0.0', port=8080)
def keep_alive():
t = Thread(target=run)
t.start()
def run_bots():
# Adding server
keep_alive()
# Initializing bots
b1 = VideoBot('1', list_urls)
b2 = ArticleBot('2', list_urls)
b3 = WorkingBot('3', list_urls)
# Threading
Thread(target=b1.run).start()
Thread(target=b2.run).start()
Thread(target=b3.run).start()
if __name__ == '__main__':
# app.run(debug=True)
run_bots()