def VerifyPids(pid_base, log): """Find pid's from pid_files and check if processes are still running. Remove pid_files for any processes that are no longer active. Args: pid_base: Path and beginning of pid_file used in setting pid_files. log: Log file location. """ pid_wildcard = pid_base + "_*.pid" for pid_file in glob.glob(pid_wildcard): pid = ReadFileData(pid_file, log) try: os.getpgid(int(list(pid)[0])) except: try: logger.logMessage(log, "Stale pid: %s" % pid_file) os.unlink(pid_file) except: logger.logMessage(log, "Unable to clear stale pid: %s" % pid_file)
def SetPid(thread_name, pid_base, log): """Function to handle checking and setting pid files. If pid file doesn't exist this function will set it and return the path to the parent script. If the pid file already exists this function will exit out of the script. Args: thread_name: Identifier for each thread/function. Returns: path of pid file. """ pid = str(os.getpid()) pid_file = pid_base + "_" + thread_name + ".pid" if os.path.isfile(pid_file): logger.logMessage(log, "%s exists, exiting." % pid_file) sys.exit() else: file(pid_file, 'w').write(pid) return pid_file
def ReadFileData(target_file_name, log): """Read data from a file ignoring commented out lines. Args: target_file_name: Filename of file to read. Returns: Contents of file as a set. """ file_contents = set() try: target_file = open(target_file_name, 'r') for line in target_file: string = line.decode('unicode_escape').encode('ascii','ignore') if string.encode('utf-8')[:1] != '#': file_contents.add(string.encode('utf-8').replace('\n', '')) target_file.close() except: logger.logMessage(log, "WARNING: Unspecified error processing %s" % target_file_name) return file_contents
def Cleanup(thread_name, pid_file, log): """pid file cleanup for abnormal script termination. Args: thread_name: Human readable name for thread being cleaned up. pid_file: File to check during cleanup. """ logger.logMessage(log, "%s: Starting cleanup." % thread_name) try: os.unlink(pid_file) logger.logMessage(log, "%s pid file successfully removed." % thread_name) except: logger.logMessage(log, "WARNING: Unable to remove %s pid file." % thread_name)
tempBuffer.pop( ) # Remove the left over apostraphe from the split buffer array ircReadBuffer = tempBuffer # ircReadBuffer is ready for processing for line in ircReadBuffer: # This for loop allows "break" to be used for saving time checking for if statements if "PING :tmi.twitch.tv" in line: # PONGs on request of the server to prevent disconnecting sendPongToServer(server) break user = getUser(line) message = getMessage(line) wordList = message.split(" ") print(str(getTime() + user + ": " + message)) # Prints messages from all users to console if LOG_TO_FILE: logMessage(str(getTime() + user + ": " + message)) ########## Add custom functions below ########## # Current configuration is modular. Just copy the design patterns as seen below if "!level" in wordList[0]: sendMessage(server, getLevel(user, message, wordList)) break if "!lvl" in wordList[0]: sendMessage(server, getLevel(user, message, wordList)) break if "!commands" in wordList[0]: sendMessage( server, "@" + user +
myFile = open(fileTouched, 'r') for line in myFile: arrTouched.append(line.rstrip('\n')) myFile.close() def WriteTouched(): myFile = open(fileTouched, 'wb') for line in arrTouched: myFile.write(line + '\n') myFile.close() filer.tailFile(fileTouched, lengthTouched) #/Functions #Main if __name__ == "__main__": logger.logMessage(fileLog, 'Reading state...') try: ReadTouched() except: logger.logMessage(fileLog, ' Oops! Unable to read file.') for Subreddit in colSubreddits: logger.logMessage(fileLog, 'Entering %s...' % Subreddit) thisSub = r.get_subreddit(Subreddit) hot = thisSub.get_hot(limit=numPosts) for thing in hot: if not(thing.id in arrTouched): logger.logMessage(fileLog, 'Post: %s' % thing.title) try: for redComment in thing.comments: try: strComment = redComment.body
def sendPongToServer(server): # Twitch servers will ping the bot every 5 minutes. Failure to pong will close the connection server.send("PONG\r\n".encode()) print(getTime() + "PONG sent to server") if LOG_TO_FILE: logMessage(str(getTime() + "PONG sent to server"))
def sendMessage(server, message): messageToSend = "PRIVMSG #" + CHANNEL + " :" + message server.send((messageToSend + "\r\n").encode()) print(getTime() + NICK +": " + message) if LOG_TO_FILE: logMessage(str(getTime() + NICK +": " + message))