Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- Reddit-SMS-Notifier.py
- Monitors a subreddit for new posts matching a search term, and sends an SMS
- alert via Twilio whenever a matching post is found. Keeps a local record of
- already-seen post IDs (idArray.p) to avoid duplicate notifications, and logs
- activity to a timestamped file in ./logs/.
- Requires: praw, twilio
- Fill in your Reddit API credentials, Twilio credentials, and search settings
- in the __main__ block before running.
- """
- import datetime
- import os, sys
- import pickle
- import praw
- import time
- from twilio.rest import Client
- def createLog(message):
- logFile.write(f"[{getCurrentTime()}] {message}")
- logFile.flush()
- def getCurrentTime():
- now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
- return now
- def loadArray():
- if os.path.isfile("./idArray.p"):
- fileIn = open("./idArray.p", "rb")
- idArray = pickle.load(fileIn)
- fileIn.close()
- else:
- idArray = []
- return idArray
- def saveArray():
- fileOut = open("./idArray.p", "wb")
- pickle.dump(idArray, fileOut)
- fileOut.close()
- def getNewSubmissions():
- createLog(f"Getting {postLimit} new posts\n")
- for submission in reddit.subreddit(subredditName).new(limit=postLimit):
- if searchQuery in submission.title.lower():
- if submission.id not in idArray:
- idArray.append(submission.id)
- saveArray()
- createLog(f"Found new post: {submission.title}\n")
- try:
- message = client.messages.create(
- body=f"New Update: {submission.title} \n\nVisit the link here: {submission.url}",
- from_=sendingPhoneNumber,
- to=destinationPhoneNumber,
- )
- time.sleep(5)
- createLog(f"Text sent successfully\n")
- createLog(f"Message ID: {message.sid}\n")
- except Exception as e:
- createLog(f"Text did not send successfully: {e}\n")
- else:
- createLog(f"Submission doesn't contain correct criteria\n")
- createLog(f"Sleeping for {1 * timeDelay} minutes\n\n")
- try:
- time.sleep(60 * timeDelay)
- except KeyboardInterrupt:
- createLog(f"Shutdown received\n")
- logFile.close()
- sys.exit()
- if __name__ == "__main__":
- accountSid = ""
- authToken = ""
- client = Client(accountSid, authToken)
- destinationPhoneNumber = ""
- logFile = open(f"./logs/log {getCurrentTime().replace(':', '')}.txt", "a+")
- postLimit = 15
- reddit = praw.Reddit(
- client_id="",
- client_secret="",
- user_agent=""
- )
- searchQuery = ""
- sendingPhoneNumber = ""
- subredditName = ""
- timeDelay = 5
- idArray = loadArray()
- while True:
- getNewSubmissions()
Advertisement
Comments
-
Comment was deleted
-
- Monitors a subreddit for new posts matching a search term and texts you via Twilio when a match is found.
Add Comment
Please, Sign In to add comment