Guest User

a

a guest
Jul 15th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.20 KB | None | 0 0
  1. import praw
  2. import requests
  3. import bs4
  4. import time
  5.  
  6. reddit = praw.Reddit(client_id = '*********',
  7.                     client_secret = '*******************',
  8.                     username = 'IsLinkDownBot',
  9.                     password = '********',
  10.                     user_agent = 'IsLinkDown v1.0')
  11.  
  12.  
  13. def is_link_down(link):
  14.     """
  15.    Checks downforeveryoneorjustme.com to find out if the link is down or not.
  16.    If an error occurs, waits for 30 minutes and tries again.
  17.    """
  18.     try:
  19.         link = link.lower()
  20.         url = "http://www.downforeveryoneorjustme.com/{}".format(link)
  21.         response = requests.get(url)
  22.         soup = bs4.BeautifulSoup(response.text, 'html.parser')
  23.         for p_tag in soup.find_all('p'):
  24.             result = str(p_tag.contents[0]).strip()
  25.             if result == "It's just you.":
  26.                 result += link + " is up for everybody else."
  27.             elif result == "It's not just you!":
  28.                 result += link + " is down for everybody else."
  29.             break
  30.         return result
  31.     except:
  32.         time.sleep(1800)
  33.         is_link_down(link)
  34.  
  35.  
  36.  
  37. while True:
  38.     """
  39.    Checks every 30 minutes for new mentions of /u/IsLinkDownBot by any Redditor.
  40.    If it has been mentioned, replies with the message.
  41.    """
  42.     for mention in reddit.inbox.mentions():
  43.         with open("mentions_replied_to.txt", "r") as f:
  44.             mentions_replied_to = f.read()
  45.             mentions_replied_to = mentions_replied_to.split("\n")
  46.             mentions_replied_to = list(filter(None, mentions_replied_to))
  47.  
  48.             if mention not in mentions_replied_to:
  49.                 #mention body = '/u/IsLinkDownBot google.com'
  50.                 link = str(mention.body).replace('/u/IsLinkDownBot','') #' google.com'
  51.                 link = link.strip() #'google.com'
  52.  
  53.                 message = is_link_down(link)
  54.                 mention.reply(message)
  55.                 #Adding mention ID to the list.
  56.                 mentions_replied_to.append(mention)
  57.  
  58.         with open("mentions_replied_to.txt", "w") as f:
  59.             for mentionid in mentions_replied_to:
  60.                 f.write(str(mentionid) + "\n")
  61.             f.close()
  62.         time.sleep(1800)
Add Comment
Please, Sign In to add comment