diff --git a/kaktus_bot.py b/kaktus_bot.py new file mode 100644 index 0000000..672f9a5 --- /dev/null +++ b/kaktus_bot.py @@ -0,0 +1,72 @@ +from bs4 import BeautifulSoup +import requests +import time +import sys + +api_token = '1437509413:AAHyyA5tnDW4cJm2SAdj1655YAwbuGQVuNU' +channel_name = '@kaktusdobijecka' +devlog_channel_name = -1001399814968 +path = 'https://api.telegram.org/bot%s/sendMessage' % api_token +kaktus = 'https://www.mujkaktus.cz/novinky' + +def last_text(text): + with open('/home/saitama/kaktusbot/last.txt', 'r+', encoding='utf-8') as f: + if f.read() == text: + return True + else: + f.seek(0) + f.truncate(0) + f.write(text) + return False + +def report_bug(text): + params = { + 'chat_id': devlog_channel_name, + 'text': text, + } + + r = requests.get(path, params=params) + print(r) + +def send_to_telegram(text): + params = { + 'chat_id': channel_name, + 'text': text, + } + + r = requests.get(path, params=params) + if r.status_code != 200: + print('Failed to send, response:') + print(r.content) + else: + print('Sent successfully!') + +try: + print('Checking for new messages... ', end='') + text = requests.get(kaktus).text + bs = BeautifulSoup(text, 'html.parser') + entries = bs.find_all("div", {"class": "journal-content-article"}) + + entry = entries[0] + split = entry.text.split('\n') + split = [_ for _ in split if _ and _ != 'Sdílet na Facebooku'] + text = '\n\n'.join(split) + + if not last_text(text): + print('found!') + send_to_telegram(text) + else: + print('not found.') + +except Exception as e: + error_text = repr(e) + print(error_text, 'RESTARTING SERVICE') + while True: + try: + report_bug(error_text) + except Exception as ex: + print(repr(ex)) + print('failed to send error report, trying again...') + time.sleep(5) + else: + break diff --git a/last.txt b/last.txt new file mode 100644 index 0000000..cfaf5ce --- /dev/null +++ b/last.txt @@ -0,0 +1,3 @@ +Čas posbírat dvojitý kreditový koření na Kaktusím písečku ;) + +Stačí dobít za 200 - 500 Kč dneska 12. 3. mezi 16 a 19 hodinou a užívat si 2x takový moci. 🏜️ \ No newline at end of file diff --git a/log b/log new file mode 100644 index 0000000..e69de29 diff --git a/update_last.py b/update_last.py new file mode 100644 index 0000000..7b914fd --- /dev/null +++ b/update_last.py @@ -0,0 +1,32 @@ +from bs4 import BeautifulSoup +import requests + +api_token = '1437509413:AAHyyA5tnDW4cJm2SAdj1655YAwbuGQVuNU' +#channel_name = '@kaktusdobijecka' +#devlog_channel_name = -1001399814968 +#path = 'https://api.telegram.org/bot%s/sendMessage' % api_token +kaktus = 'https://www.mujkaktus.cz/novinky' + +def last_text(text): + with open('last.txt', 'r+', encoding='utf-8') as f: + if f.read() == text: + return True + else: + f.seek(0) + f.truncate(0) + f.write(text) + return False + +print('Updating last text...') +text = requests.get(kaktus).text +bs = BeautifulSoup(text, 'html.parser') +entries = bs.find_all("div", {"class": "journal-content-article"}) + +entry = entries[0] +split = entry.text.split('\n') +split = [_ for _ in split if _ and _ != 'Sdílet na Facebooku'] +text = '\n\n'.join(split) + +updated = not last_text(text) +print('Updated:', updated) +print('Last text:', text)