titlebot.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. # -*- coding: utf-8 -*-
  2. import re
  3. import irc
  4. import requests
  5. from bs4 import BeautifulSoup
  6. ## IRC Config
  7. server = "127.0.0.1"
  8. port = 11070
  9. channels = ["#test", "#test1"]
  10. botnick = "website-title"
  11. ircc = irc.IRC()
  12. ircc.connect(server, port, channels, botnick)
  13. while True:
  14. text = ircc.get_response()
  15. text_list = text.split(' ')
  16. if text_list[1] == "PRIVMSG":
  17. channel = text_list[2]
  18. msg = ' '.join(text_list[3:])
  19. url = re.findall(r'(https?://[^\s]+)', msg)
  20. for i in url:
  21. reqs = requests.get(i)
  22. soup = BeautifulSoup(reqs.text, 'html.parser')
  23. for title in soup.find_all('title'):
  24. title_text = title.get_text()
  25. if not len(title_text) > 0:
  26. print("Error: Title not found!")
  27. continue
  28. title_text = title_text.split('\n')
  29. title_msg = []
  30. # remove empty lines from tweet body
  31. for line in title_text:
  32. if not line.strip():
  33. continue
  34. title_msg.append(line)
  35. title_msg = " ".join(title_msg)
  36. print(f"Title: {title_msg}")
  37. ircc.send(channel, f"Title: {title_msg}")