Add quote validation
This will allow the bot to post a quote even if badly formated quotes are selected from the CSV
This commit is contained in:
parent
bebb4947fc
commit
02bbcefe7c
1 changed files with 29 additions and 1 deletions
|
@ -41,6 +41,34 @@ if not args.debug:
|
|||
to_file=username+".secret"
|
||||
)
|
||||
|
||||
"""
|
||||
get_random_quote: selects quotes randomly from a list until it finds one properly formated
|
||||
quotes: a list of comma separated values representing quotes
|
||||
max_attempts: the number of times a random quote should be selected if the previously selectd quote is formatted incorrectly
|
||||
return: the selected quote, or an empty list (if max_attempts was exceeded)
|
||||
"""
|
||||
def get_random_quote (quotes, max_attempts = 5):
|
||||
success = False
|
||||
attempts = 0
|
||||
quote = []
|
||||
while (success == False and attempts < max_attempts):
|
||||
success = True # will be set to false again if any values are bad
|
||||
attempts = attempts + 1
|
||||
print("Selecting a random quote... Attempt #{}".format(attempts))
|
||||
quote = random.choice(quotes)
|
||||
if len(quote) < 4:
|
||||
print("Error: CSV has fewer than 4 columns.")
|
||||
success = False
|
||||
else:
|
||||
for column in quote:
|
||||
if column == "":
|
||||
print("Error: CSV has at least one empty column.")
|
||||
success = False
|
||||
break # there is no need to continue looping over each column after the first failure
|
||||
|
||||
# TODO: remove badly formated quote and append it to a csv file with other badly formated quotes
|
||||
return quote
|
||||
|
||||
quotes = []
|
||||
for root, dirs, files in os.walk('quotes/'):
|
||||
for name in files:
|
||||
|
@ -53,7 +81,7 @@ for root, dirs, files in os.walk('quotes/'):
|
|||
|
||||
print('Found %d total quotes' % len(quotes))
|
||||
|
||||
row = random.choice(quotes)
|
||||
row = get_random_quote(quotes, 20)
|
||||
|
||||
text = '<p>{}</p> - {}, <a href={}>{}</a>'.format(row[0], row[1], row[2], row[3])
|
||||
print(text)
|
||||
|
|
Loading…
Reference in a new issue