isup.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. ## version 0.1 created by hlmtre ##
  2. ## version 0.2 updated by mech ##
  3. import sys
  4. import json
  5. from event import Event
  6. try:
  7. import requests
  8. except (ImportError, SystemError):
  9. print("Warning: isup module requires requests")
  10. requests = object
  11. if sys.version_info > (3, 0, 0):
  12. try:
  13. from .basemodule import BaseModule
  14. except (ImportError, SystemError):
  15. from modules.basemodule import BaseModule
  16. else:
  17. try:
  18. from basemodule import BaseModule
  19. except (ImportError, SystemError):
  20. from modules.basemodule import BaseModule
  21. class Isup(BaseModule):
  22. """ takes a url and determines if the site hosted there is up """
  23. def post_init(self):
  24. isup = Event("__.isup__")
  25. isup.define(msg_definition=r"^\.isup")
  26. isup.subscribe(self)
  27. self.help = ".isup <Valid website using *.com, *.net, etc.>"
  28. # register ourself to our new isup event
  29. self.bot.register_event(isup, self)
  30. self.url = "https://api.downfor.cloud/httpcheck/" # URL which outputs JSON data
  31. """
  32. Example to show json data parameters that can be pulled from with current URL get request:
  33. statusCode 200
  34. statusText "OK"
  35. isDown false
  36. returnedUrl "https://www.reddit.com/"
  37. requestedDomain "reddit.com"
  38. lastChecked 1557603603861
  39. """
  40. def handle(self, event):
  41. if len(event.msg.split()) == 2: # Looks for the command and hopefully a valid website (*.com,*.net, etc.)
  42. try:
  43. """Needed to set user agent so request would not be blocked, without this a 503 status code is returned"""
  44. headers = {
  45. 'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36',
  46. }
  47. r = requests.get(self.url + event.msg.split()[1], headers=headers)# Takes our static URL and appends your site to the end to make our get request
  48. j = json.loads(r.text) # Converts our JSON to python object
  49. if str(j["isDown"]) == "True": # Converts our parameter to a string to compare against our "isDown" parameter
  50. self.say(event.channel, "Site looks down; it's not just you.") # Once state is determined it will be spit out into the channel
  51. elif str(j["isDown"]) == "False":
  52. self.say(event.channel, "Site looks ok to me; it's just you.")
  53. except requests.ConnectionError:
  54. self.say("Connection error.")