#!/usr/bin/python # Toggle me for debugging debug = 1 # Import the libraries we will use from datetime import datetime from gtts import gTTS import re import sys import random import platform import argparse import os # check to see if termcolor is installed, we need it for color to work try: from termcolor import colored except ImportError: print("termcolor is not installed! Please install termcolor with" '\n', '\n', "pip install termcolor", '\n','\n'+"Note: You may need to run pip as root") exit() if debug == 1: print("termcolor is installed!") # ArgsParce def ArgsParce(): parser = argparse.ArgumentParser() parser.add_argument("-s", "--setup", help="Explains how to setup .txt file", action="store_true") args = parser.parse_args() if args.setup == True: sys.exit("If you want to include your own MadLibs story, you need to do the following:"+'\n'+"1. Open "+"\"stories.txt\""+'\n'+"2. Put the title of the story on all of the odd lines"+'\n'+"3. Put the entire story on one line, and put words you wish to replace in <>. Use the example as a reference."+'\n'+"4. When you are done, run me with the -c or --story flag to update how many stories are in stories.txt.") ArgsParce() if platform.system() == 'Linux': print('Linux master race! XD') # Introduce yourself print (colored("<>", 'red'), '\n' "Project started on July 13, 2019") print("I pull txt files in the directory you place me in for stories!" '\n' '\n' "Run me with the --setup flag for instructions on setting a story up!" '\n') print("Final Build*", '\n') # Notify if verbose if debug == 1: print("Debug mode is enabled! Being verbose!", '\n') else: print('\n') # Now on to business! # Load files #Declare vars storyContentStr = [] storyNameStr = [] # Alright, let's get the data from stories.txt i = 1 with open('stories.txt', 'r') as f: for line in f: if i % 2 == 0 : storyContent = line storyContentStr.append(storyContent.rstrip()) else: storyName = line storyNameStr.append(storyName.rstrip()) i+=1 IntStoryCount = len(storyNameStr) print("Detected", int(IntStoryCount), "stories") # Randomly pick what story we will use story = random.randint(1, IntStoryCount) print(storyNameStr) # Print current story title, but remove the brackets first filteredTitle = re.findall(r'<(.*?)>', storyNameStr[story-1]) # print the first result print("Current story title is", '"'+filteredTitle[0]+'"','\n') # Alright, now onto the tricky part. We need to filter out all of the bracketed words in stories.txt, putting them into a list, replacing them with incremental strings. We also need to count how many there are for later. # Pull all of the items with the <> brackets filtered = re.findall(r'<(.*?)>', storyContentStr[story-1]) # We got them! if debug == 1: print(filtered, '\n') # Now we need to count them replacedNumber = len(filtered) # Run a loop to get the words replaceList = [] replaceList.append("") print("Type a noun, verb, adjective, or adverb depending on what it asks you, followed by enter.", '\n') for loopCount in range(replacedNumber): replaceVar = input("Give me a(n) "+colored(filtered[loopCount], 'blue')+": ") replaceList.append(replaceVar) print(replaceList) # Run a loop to replace the words print("Replacing Words...") # Split the Story Content into a list storyContentList = re.split(r'<.*?>', storyContentStr[story-1]) # Count the items in the list storyContentCount = len(storyContentList) x = 0 for loopCount in range(storyContentCount): #print(storyContentList[loopCount]) storyContentList.insert(x, replaceList[loopCount]) x = x+2 # To get colored words for our output, we need to add the appropiate commands to our variable. storyContentListColored = re.split(r'<.*?>', storyContent) x = 0 # Merge lists into a string generatedStory = "" generatedStory = generatedStory.join(storyContentList) print(generatedStory) #Alright! We're done! Let's save the story to a file now = datetime.now() if os.path.exists("saved stories"): pass else: os.system("mkdir \"saved stories\"") currentDate = now.strftime("%d-%m-%Y-%H:%M:%S") saveFile = 'saved stories/generatedStory-'+currentDate print("Saving story to .txt file") with open(saveFile+'.txt', 'w+') as file_object: line_offset = [] offset = 0 for line in file_object: line_offset.append(offset) offset += len(line) file_object.seek(0) file_object.write(filteredTitle[0]+'\n'+'\n') file_object.write(generatedStory) file_object.write('\n'+"Generated by Caleb Fontenot\'s madlibs.py") file_object.close() # say the tts print('\n'+"Processing Text-To-Speech, please wait..."+'\n') tts = gTTS(text=generatedStory+"This story was generated by Caleb Fontenot's MadLibs.py", lang='en') tts.save("TTS.mp3") os.system("play TTS.mp3") os.system("mv TTS.mp3 "+"\""+saveFile+".mp3"+"\"")