TelegramNewsBot/main.py

45 lines
1.5 KiB
Python
Raw Normal View History

2022-06-02 12:56:03 +04:00
import json
from datetime import datetime
import pyodbc
import requests
from bs4 import BeautifulSoup
server = '10.3.1.13\SQLEXPRESS'
database = 'DepartmentDatabaseContext'
username = 'sa'
password = 'isadmin'
cnxn = pyodbc.connect(
2022-06-03 08:08:40 +00:00
'DRIVER={/opt/microsoft/msodbcsql18/lib64/libmsodbcsql-18.0.so.1.1};SERVER=' + server + ';' +
'DATABASE=' + database + ';UID=' + username + ';PWD=' + password + ';Encrypt=no')
2022-06-02 12:56:03 +04:00
2022-06-03 08:08:40 +00:00
messageTo = {'sam': '331491581', 'is': '-1001637207513'}
2022-06-02 12:56:03 +04:00
def main():
with open('datetime.json') as json_file:
date = json.load(json_file)
cursor = cnxn.cursor()
2022-06-06 19:52:58 +04:00
cursor.execute("SELECT Id, Title, Body, DateCreate FROM Newses "
"WHERE Newses.IsDeleted = 0 AND Newses.DateCreate > ? "
2022-06-02 12:56:03 +04:00
"ORDER BY Newses.DateCreate", date) # например - 2022-04-02 11:19:29.778400
row = cursor.fetchone()
while row:
soup = BeautifulSoup(row[2], features="html.parser")
textNews = soup.get_text('\n').replace("\n\n\n\n", "\n").replace("\n\n\n", "\n").replace("\n\n", "\n")
2022-06-03 08:08:40 +00:00
params = {'chat_id': messageTo.get('is'),
2022-06-02 12:56:03 +04:00
'text': f'{row[1]}\n{textNews}\nhttp://is.ulstu.ru/News/ShowNews/{row[0]}'}
requests.get('https://api.telegram.org/bot5567223643:AAG6DYNUNq7BNqm7-pI2p-SdvEmAKielViE/sendMessage',
params=params)
2022-06-06 19:52:58 +04:00
date = row[3]
2022-06-02 12:56:03 +04:00
row = cursor.fetchone()
2022-06-06 19:52:58 +04:00
2022-06-02 12:56:03 +04:00
with open('datetime.json', 'w') as outfile:
2022-06-06 19:52:58 +04:00
json.dump(str(date), outfile)
2022-06-02 12:56:03 +04:00
if __name__ == '__main__':
main()