TelegramNewsBot/main.py
2022-06-02 12:56:03 +04:00

42 lines
1.4 KiB
Python

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(
'DRIVER={ODBC Driver 17 for SQL Server};SERVER=' + server + ';' +
'DATABASE=' + database + ';UID=' + username + ';PWD=' + password)
def main():
with open('datetime.json') as json_file:
date = json.load(json_file)
cursor = cnxn.cursor()
cursor.execute("SELECT Id, Title, Body FROM Newses "
"WHERE Newses.IsDeleted = 0 AND Newses.DateCreate >= ? "
"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")
params = {'chat_id': '-1001637207513',
'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)
row = cursor.fetchone()
with open('datetime.json', 'w') as outfile:
json.dump(str(datetime.now()), outfile)
if __name__ == '__main__':
main()