120 lines
4.5 KiB
Python
120 lines
4.5 KiB
Python
# Toggle me for debugging
|
|
debug = 1
|
|
|
|
# Import the libraries we will use
|
|
import datetime
|
|
import re
|
|
import sys
|
|
import random
|
|
import platform
|
|
import argparse
|
|
# 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!")
|
|
# If we are on Windows, we need to do a little more to get color to work
|
|
if platform.system() == 'Windows':
|
|
import os
|
|
os.system('color')
|
|
# ArgSparce
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument("-s", "--setup", help="Explains how to setup .txt file", action="store_true")
|
|
parser.add_argument("-c", "--story", type=int, help="Write story count to file")
|
|
args = parser.parse_args()
|
|
# 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':
|
|
print('Linux master race! XD')
|
|
# Introduce yourself
|
|
print (colored("<<madlibs.", 'red')+colored("p", 'yellow')+colored("y", 'blue'), colored("- Written by Caleb Fontenot>>", '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')
|
|
|
|
# Notify if verbose
|
|
if debug == 1:
|
|
print("Debug mode is enabled! Being verbose!", '\n')
|
|
else:
|
|
print('\n')
|
|
# Now on to business!
|
|
# Load files
|
|
f = open('storyCount.txt', 'r')
|
|
StoryCount = f.read()
|
|
IntStoryCount = int(StoryCount)
|
|
print("Detected", IntStoryCount, "stories")
|
|
# Randomly pick what story we will use
|
|
story = random.randint(1, IntStoryCount)
|
|
|
|
# Alright, let's get the data from stories.txt
|
|
i = 1
|
|
f = open('stories.txt', 'r')
|
|
for line in f.readlines():
|
|
if i % 2 == 0 :
|
|
storyContent = line
|
|
else:
|
|
storyName = line
|
|
i+=1
|
|
f.close()
|
|
# Print current story title, but remove the brackets first
|
|
filteredTitle = re.findall(r'<(.*?)>', storyName)
|
|
# 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'<(.*?)>', storyContent)
|
|
# 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
|
|
#This normally is just "replaceList = []"
|
|
replaceList = ['1', '2', '43', '', '2643', '62', '36', '236', '23', '6234', '623', '623', '46', '2346', '234', '6234', '623', '4', '234', '6234', '2346', '2345124', '12', '1']
|
|
#for loopCount in range(replacedNumber):
|
|
# replaceVar = input("Give me a(n) "+filtered[loopCount]+": ")
|
|
# replaceList.append(replaceVar)
|
|
print(replaceList)
|
|
# Run a loop to replace the words
|
|
|
|
print("Replacing Words...")
|
|
|
|
for loopCount in range(replacedNumber):
|
|
currentReplace = replaceList[loopCount]
|
|
print(re.finditer(r'<.*?>',storyContent))
|
|
print("Replacing", currentReplace)
|
|
filteredReplaced = re.sub(r'<.*?>',currentReplace, storyContent, loopCount)
|
|
print(filteredReplaced)
|
|
#This exit was intensional, just to prevent it from executing the rest of the program while I work on it...
|
|
exit()
|
|
currentDate1 = datetime.date
|
|
currentDate2 = str(currentDate1)
|
|
print("Saving story to .txt file")
|
|
f = open('generatedStory'+currentDate2+'.txt', 'a')
|
|
f.write(filteredReplaced)
|
|
f.seek(3)
|
|
f.write("Generated by Caleb Fontenot\'s madlibs.py")
|
|
f.close()
|