From c0c7342091cb77bdfb3f27cd67394a3718cf9f4f Mon Sep 17 00:00:00 2001 From: Meghdad Date: Sat, 16 Nov 2024 23:37:47 +0330 Subject: [PATCH] setup first try --- .github/workflows/run-script.yml | 2 +- blockout.py | 20 ++++++++++++++++++++ main.py | 29 +++++++++++++++++++++++------ requirements.txt | 3 ++- 4 files changed, 46 insertions(+), 8 deletions(-) create mode 100644 blockout.py diff --git a/.github/workflows/run-script.yml b/.github/workflows/run-script.yml index 6e629c9..12b73ed 100644 --- a/.github/workflows/run-script.yml +++ b/.github/workflows/run-script.yml @@ -5,7 +5,7 @@ on: branches: - main schedule: - - cron: "0 * * * *" + - cron: "30 3-21/6 * * *" jobs: run-script: diff --git a/blockout.py b/blockout.py new file mode 100644 index 0000000..10c844a --- /dev/null +++ b/blockout.py @@ -0,0 +1,20 @@ +import requests +from bs4 import BeautifulSoup + + +def get_main_page() -> str: + return requests.get('https://www.qepd.co.ir/').text + + +def find_blockout_link(html: str) -> None | str: + soup = BeautifulSoup(html, 'html.parser') + a_tags = soup.find_all('a', href=True) + for a_tag in filter(lambda tag: 'اطلاعات برنامه خاموشی ها' in tag.text, a_tags): + return a_tag.get('href') + return None + + +page = get_main_page() +link = find_blockout_link(page) +if not link: + raise Exception('unable to find blockout link!') diff --git a/main.py b/main.py index 0d3e07c..fdb7118 100644 --- a/main.py +++ b/main.py @@ -1,6 +1,7 @@ import os import time import requests +from bs4 import BeautifulSoup token = os.getenv("EITAAYAR_TOKEN") chat_id = os.getenv("CHAT_ID") @@ -11,14 +12,30 @@ if not token: if not chat_id: raise EnvironmentError("CHAT_ID is not set!") + def send_message(text: str) -> dict: sent_at: int = int(time.time()) url = f'https://eitaayar.ir/api/{token}/sendMessage?chat_id={chat_id}&text={text}&title={sent_at=}' return requests.get(url).json() -response = send_message('test') -if not response.get('ok'): - if response.get('description'): - raise Exception(response.get('description')) - else: - raise Exception('something sent wrong!') \ No newline at end of file + +def get_blockout_page() -> str: + blockout_link = 'https://qepd.co.ir/fa-IR/DouranPortal/6423' + return requests.get(blockout_link).text + + +def main() -> None: + page = get_blockout_page() + soup = BeautifulSoup(page, 'html.parser') + table = soup.find('table', id='ctl01_ctl00_myDataList').get_text(separator='\n', strip=True) + + response = send_message(table) + if not response.get('ok'): + if response.get('description'): + raise Exception(response.get('description')) + else: + raise Exception('something sent wrong!') + + +if __name__ == '__main__': + main() diff --git a/requirements.txt b/requirements.txt index 663bd1f..a98ae43 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,2 @@ -requests \ No newline at end of file +requests +beautifulsoup4 \ No newline at end of file