initial
This commit is contained in:
98
import_taylor_swift_2.py
Normal file
98
import_taylor_swift_2.py
Normal file
@@ -0,0 +1,98 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Import additional Taylor Swift trivia questions"""
|
||||
|
||||
import json
|
||||
import urllib.request
|
||||
import urllib.error
|
||||
|
||||
# Backend URL
|
||||
BASE_URL = "http://localhost:5001/api"
|
||||
|
||||
# Questions as list of tuples (question, answer)
|
||||
questions_data = [
|
||||
("Who is Taylor Swift engaged to?", "Travis Kelce"),
|
||||
("Which song did Taylor Swift help write under the pseudonym Nils Sjoberg?", "This is what you came for"),
|
||||
('Which ex was featured in Taylor Swift\'s "I can see you" music video?', "Taylor Lautner"),
|
||||
("Taylor was releasing Taylor's Versions for all the masters she didn't have. What was the last album she released a TV for before she bought back her masters?", "1989"),
|
||||
("In the TV's, there's usually a few newly released songs at the end. What's the name she gave these songs?", "Vault/From the vault"),
|
||||
("What is Taylor Swift's lucky number?", "13"),
|
||||
("Who did Taylor Swift beef with that birthed the Reputation album?", "Kanye/Kim"),
|
||||
("Which country paid Taylor Swift a lot of money to not perform in neighboring countries?", "Singapore"),
|
||||
("Hamlet is having a year! Which Hamlet character was featured on Taylor Swift's latest album?", "Ophelia"),
|
||||
("Which Taylor Swift song was re-released to have a ten-minute version?", "All Too Well"),
|
||||
("Taylor has had months in her song titles before, but she has mentioned a particular one in two song names. Which month is it?", "December (Back to December, Last December)"),
|
||||
('Identify the song: "She wears high heels, I wear sneakers"', "You Belong With Me"),
|
||||
("What was Taylor Swift's first released song, also the name of a country singer?", "Tim McGraw"),
|
||||
]
|
||||
|
||||
|
||||
def parse_questions():
|
||||
"""Convert question tuples to API format"""
|
||||
questions = []
|
||||
for question_text, answer_text in questions_data:
|
||||
questions.append({
|
||||
'question_content': question_text,
|
||||
'answer': answer_text,
|
||||
'category': 'Taylor Swift',
|
||||
'type': 'text'
|
||||
})
|
||||
return questions
|
||||
|
||||
|
||||
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("Additional Taylor Swift Questions Import")
|
||||
print("=" * 60)
|
||||
|
||||
# Parse questions
|
||||
print("\n1. Parsing questions...")
|
||||
questions = parse_questions()
|
||||
print(f" Parsed {len(questions)} questions")
|
||||
|
||||
# Show a preview
|
||||
print("\n Preview:")
|
||||
for i, q in enumerate(questions[:3], 1):
|
||||
print(f" {i}. {q['question_content'][:50]}...")
|
||||
|
||||
# Import questions
|
||||
print("\n2. 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()
|
||||
Reference in New Issue
Block a user