-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.py
71 lines (62 loc) · 2.04 KB
/
functions.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
import yaml
import requests
import sqlite3
# Load Properties
try:
with open("configuration.yml", "r") as stream:
try:
properties = yaml.safe_load(stream)
apiToken = properties["telegram"]["apiToken"]
telegramChats = properties["telegram"]["chats"]
databaseName = properties["database"]["name"]
pages = properties["pages"]
except yaml.YAMLError as exc:
print(exc)
except FileNotFoundError:
print("Missing configuration.yml file")
exit()
# Recover sites from properties file
def loadSites():
return pages
# Send Telegram Message to every Chat ID
def sendToTelegram(message):
apiURL = f'https://api.telegram.org/bot{apiToken}/sendMessage'
for chat in telegramChats:
requests.post(apiURL, json={'chat_id': chat, 'text': message})
# Create Database
def CreateLinksTable():
connection=sqlite3.connect(databaseName)
try:
connection.execute("""create table links (
codigo integer primary key autoincrement,
url text
)""")
print("Table LINKS created")
except Exception as e:
print("Using pre-existent database table")
finally:
connection.close()
# Insert url into database
def InsertLink(url):
connection=sqlite3.connect(databaseName)
try:
connection.execute("insert into links(url) values (?)", (url,))
connection.commit()
except Exception as e:
print(e)
finally:
connection.close()
# Boolean to check if url exists in database (return True = it exists)
def LinkExists(url):
connection=sqlite3.connect(databaseName)
try:
cursor=connection.execute("select url from links where url=?", (url,))
row=cursor.fetchall()
if len(row)>0:
return True
else:
return False
except Exception as e:
print(e)
finally:
connection.close()