-
Notifications
You must be signed in to change notification settings - Fork 0
/
notify-slack-news.py
103 lines (76 loc) · 2.89 KB
/
notify-slack-news.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
import logging
import os
import sys
import psycopg2
import requests
from slack_sdk import WebClient
from slack_sdk.errors import SlackApiError
logging.basicConfig(stream=sys.stdout, format='%(levelname)s:%(message)s', level=logging.INFO)
database_url = 'postgresql://' \
+ os.environ.get("DB_USER") + ':' \
+ os.environ.get("DB_PASSWORD") + '@' \
+ os.environ.get("DB_HOST") + '/' \
+ os.environ.get("DB_NAME")
def create_table_if_not_exists():
db_connection.cursor().execute(
'CREATE TABLE IF NOT EXISTS listings('
'id SERIAL PRIMARY KEY, '
'url VARCHAR(125) NOT NULL, '
'title VARCHAR(255) NOT NULL);')
logging.info('Table created successfully')
def notify_slack(notification_text):
client = WebClient(token=os.environ.get("SLACK_TOKEN"))
channel_id = os.environ.get("SLACK_CHANNEL_ID")
try:
result = client.chat_postMessage(
channel=channel_id,
text=notification_text,
unfurl_links=False,
unfurl_media=False
)
logging.info(result)
except SlackApiError as e:
logging.error(e)
def is_allowed(text):
return not ('Trading Pairs' in text
or 'Isolated Margin' in text
or 'Futures' in text
or 'Binance Margin' in text)
def get_prefix(title):
if 'Launchpad' in title or 'Launchpool' in title:
return ':vibe: :rocket: '
else:
return ''
def notify_listing(code, title):
absolute_url = 'https://www.binance.com/en/support/announcement/' + code
logging.info('Page url ' + absolute_url)
logging.info('Page title ' + title)
cursor = db_connection.cursor()
cursor.execute(
'SELECT url FROM listings WHERE url like %s',
(absolute_url.strip(),))
data = cursor.fetchall()
if len(data) == 0:
if is_allowed(title):
logging.info('Action: NOTIFY')
message = get_prefix(title) + '*' + title + '* \n' + absolute_url
notify_slack(message)
else:
logging.info('Action: SKIP - content not allowed')
cursor.execute(
'INSERT INTO listings(url, title) VALUES (%s, %s)',
(absolute_url.strip(), title.strip()))
else:
logging.info('Action: SKIP - already notified')
logging.info(' --- ')
def notify_new_listings():
query = {'type': '1', 'pageNo': '1', 'pageSize': '5', 'catalogId': '48'}
response = requests.get('https://www.binance.com/bapi/composite/v1/public/cms/article/list/query', params=query)
articles = response.json().get('data').get('catalogs')[0].get('articles')
for article in articles:
notify_listing(article.get('code'), article.get('title'))
db_connection = psycopg2.connect(database_url)
db_connection.autocommit = True
create_table_if_not_exists()
notify_new_listings()
db_connection.close()