Advertisement
Guest User

Untitled

a guest
Jan 18th, 2023
379
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.62 KB | None | 0 0
  1. from bs4 import BeautifulSoup
  2. import requests
  3. import json
  4. import time
  5. from typing import Set
  6. import traceback
  7.  
  8. PAGE_URL = "http://www.unina.it/ateneo/concorsi/concorsi-personale/concorsi-pubblici"
  9. BOT_TOKEN = "<TELEGRAM-TOKEN>"
  10. CHAT_ID = "<TELEGRAM-CHAT-ID>"
  11.  
  12. def get_page():
  13.     return requests.get(PAGE_URL).content
  14.    
  15. def get_hirings(page: bytes) -> {str: str}:
  16.     soup = BeautifulSoup(page, "html.parser")
  17.    
  18.     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"})}
  19.    
  20. def load_done() -> Set[str]:
  21.     try:
  22.         return set(json.load(open("done.json")))
  23.     except:
  24.         return set()
  25.        
  26. def save_done(codes: Set[str]) -> bool:
  27.     try:
  28.         json.dump(list(codes), open("done.json", "w"))
  29.         return True
  30.     except Exception as e:
  31.         return False
  32.            
  33. def send_telegram(msg: str):
  34.     try:
  35.         requests.post("https://api.telegram.org/bot{BOT_TOKEN}/sendMessage", json={"chat_id": CHAT_ID, "text": msg})
  36.         return True
  37.     except Exception as e:
  38.         return False           
  39.            
  40. def notify(code: str, desc: str):
  41.     msg = f"{code}\n{desc}"
  42.     print(msg)
  43.     #send_telegram(msg)
  44.    
  45. def notify_err(e: Exception):
  46.     msg = "".join(traceback.format_exception(type(e), e, e.__traceback__))
  47.     print(msg)
  48.     #send_telegram(msg)
  49.    
  50.  
  51. def control_loop():
  52.     done = load_done()
  53.    
  54.     while True:
  55.         try:
  56.             published = get_hirings(get_page())
  57.             new_published = published.keys() - done
  58.             for new_code in new_published:
  59.                 notify(new_code, published[new_code])
  60.                 done.add(new_code)
  61.                 save_done(done)
  62.         except Exception as e:
  63.             notify_err(e)
  64.         time.sleep(4*60*60)
  65.        
  66. control_loop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement