Skip to content

Instantly share code, notes, and snippets.

@Mte90
Created December 20, 2021 10:48
Show Gist options
  • Save Mte90/0c13c759ace5526e35675f2568f5157b to your computer and use it in GitHub Desktop.
Save Mte90/0c13c759ace5526e35675f2568f5157b to your computer and use it in GitHub Desktop.
Convert mIRC trivia.txt to LakeYS/Discord-Trivia-Bot
#!/usr/bin/env python3
import yaml
with open('trivia.txt', mode="r", encoding="latin-1") as f:
lines = f.readlines()
questions = {}
count = 0
for line in lines:
count += 1
if ':' in line:
category = line.split(':', 1)
question = category[1].split('*')
answer = question[1]
question = question[0]
category = category[0].replace('"', '').strip()
else:
question = line.split('*')
answer = question[1]
question = question[0]
category = 'Generica'
category = category.strip()
answer = answer.strip()
question = question.strip().replace('"', '')
if category not in questions:
questions[category] = []
questions[category].append({'question': question, 'correct_answer': answer, 'difficulty': 'medium'})
print("Elaborate "+ str(count) + " domande")
count = -1
categories = {'trivia_categories':[]}
clean_questions = {}
clean_questions['Altre'] = []
for cat in questions:
if len(questions[cat]) < 20:
clean_questions['Altre'] += questions[cat]
else:
clean_questions[cat] = questions[cat]
for cat in clean_questions:
count += 1
with open('./tmp/questions_' + str(count) + '.yml', 'w') as yaml_file:
yaml.dump({'questions': clean_questions[cat]}, yaml_file, default_flow_style=False, sort_keys=False, line_break=None, encoding='utf-8')
categories['trivia_categories'].append({'id':count, 'name':cat})
print("Categorie "+ str(count) + " trovate")
with open('./tmp/categories.yml', 'w') as yaml_file:
yaml.dump(categories, yaml_file, default_flow_style=False, sort_keys=False, line_break=None, encoding='utf-8')
Category: Question with text here *Answer
Question with no category * answer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment