Cleaned up code
This commit is contained in:
parent
1e7b5643aa
commit
f4f5d78f94
@ -1,7 +1,6 @@
|
|||||||
#!/usr/bin/python
|
#!/usr/bin/python
|
||||||
# Toggle me for debugging
|
# Toggle me for debugging
|
||||||
debug = 1
|
debug = 1
|
||||||
|
|
||||||
# Import the libraries we will use
|
# Import the libraries we will use
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from gtts import gTTS
|
from gtts import gTTS
|
||||||
@ -11,8 +10,6 @@ import random
|
|||||||
import platform
|
import platform
|
||||||
import argparse
|
import argparse
|
||||||
import os
|
import os
|
||||||
# init tts
|
|
||||||
|
|
||||||
# check to see if termcolor is installed, we need it for color to work
|
# check to see if termcolor is installed, we need it for color to work
|
||||||
try:
|
try:
|
||||||
from termcolor import colored
|
from termcolor import colored
|
||||||
@ -21,35 +18,14 @@ except ImportError:
|
|||||||
exit()
|
exit()
|
||||||
if debug == 1:
|
if debug == 1:
|
||||||
print("termcolor is installed!")
|
print("termcolor is installed!")
|
||||||
# If we are on Windows, we need to do a little more to get color to work
|
# ArgsParce
|
||||||
if platform.system() == 'Windows':
|
def ArgsParce():
|
||||||
os.system('color')
|
parser = argparse.ArgumentParser()
|
||||||
# ArgSparce
|
parser.add_argument("-s", "--setup", help="Explains how to setup .txt file", action="store_true")
|
||||||
parser = argparse.ArgumentParser()
|
args = parser.parse_args()
|
||||||
parser.add_argument("-s", "--setup", help="Explains how to setup .txt file", action="store_true")
|
if args.setup == True:
|
||||||
parser.add_argument("-c", "--story", type=int, help="Write story count to file")
|
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.")
|
||||||
args = parser.parse_args()
|
ArgsParce()
|
||||||
# convert the integer to a string because pickiness
|
|
||||||
StoryCount = str(args.story)
|
|
||||||
|
|
||||||
#if statements for ArgSparce
|
|
||||||
|
|
||||||
# line 36 fails if args.story reads as "None", so we need to clear that string if it reads as such.
|
|
||||||
if args.story == None:
|
|
||||||
exec('args.story = int(0)')
|
|
||||||
# args.story should now read as 0
|
|
||||||
if args.story > 0:
|
|
||||||
f = open('storyCount.txt', "r+")
|
|
||||||
IntStoryCount = f.read()
|
|
||||||
print("There are currently", IntStoryCount, "in stories.txt")
|
|
||||||
f.seek(0)
|
|
||||||
f.write(StoryCount)
|
|
||||||
f.close()
|
|
||||||
print("Writing", StoryCount, "to txt file!")
|
|
||||||
exit()
|
|
||||||
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.")
|
|
||||||
# Linux easter egg
|
|
||||||
if platform.system() == 'Linux':
|
if platform.system() == 'Linux':
|
||||||
print('Linux master race! XD')
|
print('Linux master race! XD')
|
||||||
# Introduce yourself
|
# Introduce yourself
|
||||||
@ -68,16 +44,15 @@ storyContentStr = []
|
|||||||
storyNameStr = []
|
storyNameStr = []
|
||||||
# Alright, let's get the data from stories.txt
|
# Alright, let's get the data from stories.txt
|
||||||
i = 1
|
i = 1
|
||||||
f = open('stories.txt', 'r')
|
with open('stories.txt', 'r') as f:
|
||||||
for line in f.readlines():
|
for line in f:
|
||||||
if i % 2 == 0 :
|
if i % 2 == 0 :
|
||||||
storyContent = line
|
storyContent = line
|
||||||
storyContentStr.append(storyContent)
|
storyContentStr.append(storyContent.rstrip())
|
||||||
else:
|
else:
|
||||||
storyName = line
|
storyName = line
|
||||||
storyNameStr.append(storyName)
|
storyNameStr.append(storyName.rstrip())
|
||||||
i+=1
|
i+=1
|
||||||
f.close()
|
|
||||||
IntStoryCount = len(storyNameStr)
|
IntStoryCount = len(storyNameStr)
|
||||||
print("Detected", int(IntStoryCount), "stories")
|
print("Detected", int(IntStoryCount), "stories")
|
||||||
# Randomly pick what story we will use
|
# Randomly pick what story we will use
|
||||||
@ -85,7 +60,6 @@ story = random.randint(1, IntStoryCount)
|
|||||||
print(storyNameStr)
|
print(storyNameStr)
|
||||||
# Print current story title, but remove the brackets first
|
# Print current story title, but remove the brackets first
|
||||||
filteredTitle = re.findall(r'<(.*?)>', storyNameStr[story-1])
|
filteredTitle = re.findall(r'<(.*?)>', storyNameStr[story-1])
|
||||||
|
|
||||||
# print the first result
|
# print the first result
|
||||||
print("Current story title is", '"'+filteredTitle[0]+'"','\n')
|
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.
|
# 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.
|
||||||
@ -96,22 +70,16 @@ if debug == 1:
|
|||||||
print(filtered, '\n')
|
print(filtered, '\n')
|
||||||
# Now we need to count them
|
# Now we need to count them
|
||||||
replacedNumber = len(filtered)
|
replacedNumber = len(filtered)
|
||||||
|
|
||||||
# Run a loop to get the words
|
# Run a loop to get the words
|
||||||
|
|
||||||
replaceList = []
|
replaceList = []
|
||||||
#replaceList =['', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24']
|
|
||||||
replaceList.append("")
|
replaceList.append("")
|
||||||
print("Type a noun, verb, adjective, or adverb depending on what it asks you, followed by enter.", '\n')
|
print("Type a noun, verb, adjective, or adverb depending on what it asks you, followed by enter.", '\n')
|
||||||
|
|
||||||
for loopCount in range(replacedNumber):
|
for loopCount in range(replacedNumber):
|
||||||
replaceVar = input("Give me a(n) "+colored(filtered[loopCount], 'blue')+": ")
|
replaceVar = input("Give me a(n) "+colored(filtered[loopCount], 'blue')+": ")
|
||||||
replaceList.append(replaceVar)
|
replaceList.append(replaceVar)
|
||||||
print(replaceList)
|
print(replaceList)
|
||||||
# Run a loop to replace the words
|
# Run a loop to replace the words
|
||||||
|
|
||||||
print("Replacing Words...")
|
print("Replacing Words...")
|
||||||
|
|
||||||
# Split the Story Content into a list
|
# Split the Story Content into a list
|
||||||
storyContentList = re.split(r'<.*?>', storyContentStr[story-1])
|
storyContentList = re.split(r'<.*?>', storyContentStr[story-1])
|
||||||
# Count the items in the list
|
# Count the items in the list
|
||||||
@ -124,43 +92,34 @@ for loopCount in range(storyContentCount):
|
|||||||
# To get colored words for our output, we need to add the appropiate commands to our variable.
|
# To get colored words for our output, we need to add the appropiate commands to our variable.
|
||||||
storyContentListColored = re.split(r'<.*?>', storyContent)
|
storyContentListColored = re.split(r'<.*?>', storyContent)
|
||||||
x = 0
|
x = 0
|
||||||
|
|
||||||
# Merge lists into a string
|
# Merge lists into a string
|
||||||
generatedStory = ""
|
generatedStory = ""
|
||||||
generatedStory = generatedStory.join(storyContentList)
|
generatedStory = generatedStory.join(storyContentList)
|
||||||
|
|
||||||
print(generatedStory)
|
print(generatedStory)
|
||||||
#exit()
|
|
||||||
#Alright! We're done! Let's save the story to a file
|
#Alright! We're done! Let's save the story to a file
|
||||||
now = datetime.now()
|
now = datetime.now()
|
||||||
|
|
||||||
if os.path.exists("saved stories"):
|
if os.path.exists("saved stories"):
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
os.system("mkdir \"saved stories\"")
|
os.system("mkdir \"saved stories\"")
|
||||||
|
|
||||||
currentDate = now.strftime("%d-%m-%Y-%H:%M:%S")
|
currentDate = now.strftime("%d-%m-%Y-%H:%M:%S")
|
||||||
saveFile = 'saved stories/generatedStory-'+currentDate
|
saveFile = 'saved stories/generatedStory-'+currentDate
|
||||||
print("Saving story to .txt file")
|
print("Saving story to .txt file")
|
||||||
file = open(saveFile+'.txt', 'w+')
|
with open(saveFile+'.txt', 'w+') as file_object:
|
||||||
|
line_offset = []
|
||||||
line_offset = []
|
offset = 0
|
||||||
offset = 0
|
for line in file_object:
|
||||||
for line in file:
|
line_offset.append(offset)
|
||||||
line_offset.append(offset)
|
offset += len(line)
|
||||||
offset += len(line)
|
file_object.seek(0)
|
||||||
|
file_object.write(filteredTitle[0]+'\n'+'\n')
|
||||||
file.seek(0)
|
file_object.write(generatedStory)
|
||||||
file.write(filteredTitle[0]+'\n'+'\n')
|
file_object.write('\n'+"Generated by Caleb Fontenot\'s madlibs.py")
|
||||||
file.write(generatedStory)
|
file_object.close()
|
||||||
file.write('\n'+"Generated by Caleb Fontenot\'s madlibs.py")
|
|
||||||
file.close()
|
|
||||||
|
|
||||||
# say the tts
|
# say the tts
|
||||||
print('\n'+"Processing Text-To-Speech, please wait..."+'\n')
|
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 = gTTS(text=generatedStory+"This story was generated by Caleb Fontenot's MadLibs.py", lang='en')
|
||||||
tts.save("TTS.mp3")
|
tts.save("TTS.mp3")
|
||||||
os.system("play TTS.mp3")
|
os.system("play TTS.mp3")
|
||||||
os.system("mv TTS.mp3 "+"\""+saveFile+".mp3"+"\"")
|
os.system("mv TTS.mp3 "+"\""+saveFile+".mp3"+"\"")
|
||||||
|
|
||||||
|
|
BIN
madlibs/saved stories/generatedStory-23-03-2021-08:32:35.mp3
Normal file
BIN
madlibs/saved stories/generatedStory-23-03-2021-08:32:35.mp3
Normal file
Binary file not shown.
@ -0,0 +1,4 @@
|
|||||||
|
Three Little Pigs
|
||||||
|
|
||||||
|
Once upon a time, there were three a pigs. One day, their mother said, "You are all grown up and must a on your own." So they left to a their houses. The first little pig wanted only to a all day and quickly built his house out of a. The second little pig wanted to a and a all day so he a his house with a. The third a pig knew the wolf lived nearby and worked hard to a his house out of a. One day, the wolf knocked on the first pig's a. "Let me in or I'll a your house down!" The pig didn't, so the wolf a down the a. The wolf knocked on the second pig's a. "Let me in or I'll blow your aa down!" The pig didn't, so the wolf a down the house. Then the wolf knocked on the third a pig's door. "Let me in or I'll blow your house down!" The little pig didn't, so the wolf a and a.He could not blow the house down. All the pigs went to live in the a house and they all a happily ever after.
|
||||||
|
Generated by Caleb Fontenot's madlibs.py
|
Loading…
Reference in New Issue
Block a user