#!/usr/bin/env python3 """Import Taylor Swift trivia questions""" import json import urllib.request import urllib.error # Backend URL BASE_URL = "http://localhost:5001/api" # Parse the questions from the pasted text questions_text = """1. What is Taylor Swift's middle name? - Answer: Alison 2. In which year was Taylor Swift born? - Answer: 1989 3. What is the name of Taylor Swift's debut single? - Answer: Tim McGraw 4. Which Taylor Swift album features the song "Shake It Off"? - Answer: 1989 5. What instrument did Taylor Swift learn to play at age 12? - Answer: Guitar 6. What is the name of Taylor Swift's third studio album? - Answer: Speak Now 7. Which city is Taylor Swift originally from? - Answer: West Reading, Pennsylvania 8. What is the title of Taylor Swift's documentary released in 2020? - Answer: Miss Americana 9. How many Grammy Awards has Taylor Swift won for Album of the Year as of 2023? - Answer: 4 10. What is the name of Taylor Swift's cat named after a character from Law & Order: SVU? - Answer: Olivia Benson 11. Which Taylor Swift album was entirely written by her without co-writers? - Answer: Speak Now 12. What is Taylor Swift's lucky number? - Answer: 13 13. Which Taylor Swift song begins with "I stay out too late"? - Answer: Shake It Off 14. What was the name of Taylor Swift's first record label? - Answer: Big Machine Records 15. In what year did Taylor Swift release the album "Folklore"? - Answer: 2020 16. What is the name of Taylor Swift's brother? - Answer: Austin 17. Which Taylor Swift album features the song "Love Story"? - Answer: Fearless 18. What is the name of the farm Taylor Swift grew up on? - Answer: Pine Ridge Farm 19. Which Taylor Swift music video features a British actor as her love interest? - Answer: Tom Hiddleston 20. What year did Taylor Swift win her first Grammy Award? - Answer: 2010""" def parse_questions(text): """Parse questions from the formatted text""" questions = [] for line in text.strip().split('\n'): if not line.strip(): continue # Split on " - Answer: " to separate question from answer parts = line.split(' - Answer: ') if len(parts) != 2: print(f"Skipping malformed line: {line}") continue # Remove the number prefix from the question question_part = parts[0].strip() answer_part = parts[1].strip() # Remove leading number and dot (e.g., "1. ") if '. ' in question_part: question_part = question_part.split('. ', 1)[1] questions.append({ 'question_content': question_part, 'answer': answer_part, 'category': 'Taylor Swift', 'type': 'text' }) return questions def create_category(category_name): """Create a category (optional, but good for organization)""" try: data = json.dumps({'name': category_name}).encode('utf-8') req = urllib.request.Request( f"{BASE_URL}/categories", data=data, headers={'Content-Type': 'application/json'} ) with urllib.request.urlopen(req) as response: print(f"✓ Created category: {category_name}") return json.loads(response.read().decode('utf-8')) except urllib.error.HTTPError as e: if e.code == 409: print(f"• Category '{category_name}' already exists") return None else: print(f"⚠ Failed to create category: {e.read().decode('utf-8')}") return None except Exception as e: print(f"⚠ Error creating category: {e}") return None def bulk_import_questions(questions): """Import questions using the bulk endpoint""" try: data = json.dumps({'questions': questions}).encode('utf-8') req = urllib.request.Request( f"{BASE_URL}/questions/bulk", data=data, headers={'Content-Type': 'application/json'} ) with urllib.request.urlopen(req) as response: result = json.loads(response.read().decode('utf-8')) print(f"✓ Successfully imported {result['created']} questions") if result['errors']: print(f"⚠ {len(result['errors'])} errors:") for error in result['errors']: print(f" - Question {error['index']}: {error['error']}") return result except urllib.error.HTTPError as e: print(f"✗ Failed to import questions: {e.read().decode('utf-8')}") return None except Exception as e: print(f"✗ Error importing questions: {e}") return None def main(): print("=" * 60) print("Taylor Swift Trivia Questions Import") print("=" * 60) # Create the category print("\n1. Creating category...") create_category("Taylor Swift") # Parse questions print("\n2. Parsing questions...") questions = parse_questions(questions_text) print(f" Parsed {len(questions)} questions") # Import questions print("\n3. Importing questions...") result = bulk_import_questions(questions) if result: print("\n" + "=" * 60) print("✓ Import complete!") print(f" Total questions imported: {result['created']}") print("=" * 60) else: print("\n✗ Import failed!") if __name__ == "__main__": main()