Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from bs4 import BeautifulSoup
- import requests
- import json
- import time
- from typing import Set
- import traceback
- PAGE_URL = "http://www.unina.it/ateneo/concorsi/concorsi-personale/concorsi-pubblici"
- BOT_TOKEN = "<TELEGRAM-TOKEN>"
- CHAT_ID = "<TELEGRAM-CHAT-ID>"
- def get_page():
- return requests.get(PAGE_URL).content
- def get_hirings(page: bytes) -> {str: str}:
- soup = BeautifulSoup(page, "html.parser")
- return {i.find("h2").get_text().strip() : i.find_all("p")[1].get_text().strip() for i in soup.find_all("div", {"class": "asset-full-content"})}
- def load_done() -> Set[str]:
- try:
- return set(json.load(open("done.json")))
- except:
- return set()
- def save_done(codes: Set[str]) -> bool:
- try:
- json.dump(list(codes), open("done.json", "w"))
- return True
- except Exception as e:
- return False
- def send_telegram(msg: str):
- try:
- requests.post("https://api.telegram.org/bot{BOT_TOKEN}/sendMessage", json={"chat_id": CHAT_ID, "text": msg})
- return True
- except Exception as e:
- return False
- def notify(code: str, desc: str):
- msg = f"{code}\n{desc}"
- print(msg)
- #send_telegram(msg)
- def notify_err(e: Exception):
- msg = "".join(traceback.format_exception(type(e), e, e.__traceback__))
- print(msg)
- #send_telegram(msg)
- def control_loop():
- done = load_done()
- while True:
- try:
- published = get_hirings(get_page())
- new_published = published.keys() - done
- for new_code in new_published:
- notify(new_code, published[new_code])
- done.add(new_code)
- save_done(done)
- except Exception as e:
- notify_err(e)
- time.sleep(4*60*60)
- control_loop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement