Skip to content

Commit 44239cf

Browse files
committed
feat: implement async IO for Typing-Speed-Tester.py quote fetching (steam-bell-92#971)
1 parent 7485c79 commit 44239cf

2 files changed

Lines changed: 46 additions & 33 deletions

File tree

requirements.txt

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
# Game dependencies
2-
pygame==2.5.2
3-
4-
# Math dependencies
5-
numpy==1.26.4
6-
matplotlib==3.8.3
7-
pillow==12.2.0
8-
9-
# Test dependencies
10-
pytest>=8.0.0
11-
12-
# Utilities dependencies
13-
nltk==3.9.4
14-
requests==2.33.0
1+
# Game dependencies
2+
pygame==2.5.2
3+
4+
# Math dependencies
5+
numpy==1.26.4
6+
matplotlib==3.8.3
7+
pillow==12.2.0
8+
9+
# Test dependencies
10+
pytest>=8.0.0
11+
12+
# Utilities dependencies
13+
nltk==3.9.4
14+
requests==2.33.0
15+
aiohttp==3.11.11

utilities/Typing-Speed-Tester/Typing-Speed-Tester.py

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
import random
33
import urllib.request
44
import json
5+
import sys
6+
import os
7+
import asyncio
8+
import aiohttp
59

610
# Local fallback sentences
711
fallback_sentences = [
@@ -22,32 +26,40 @@
2226
"The best way to predict the future is to create it"
2327
]
2428

29+
async def fetch_url(session, url):
30+
try:
31+
async with session.get(url, headers={'User-Agent': 'Mozilla/5.0'}, timeout=3) as resp:
32+
if resp.status == 200:
33+
data = await resp.json()
34+
phrase = data.get('quote') or data.get('content')
35+
if phrase:
36+
return phrase
37+
except Exception:
38+
pass
39+
return None
40+
41+
async def fetch_phrase_async():
42+
urls = [
43+
"https://dummyjson.com/quotes/random",
44+
"https://api.quotable.io/random"
45+
]
46+
async with aiohttp.ClientSession() as session:
47+
tasks = [fetch_url(session, url) for url in urls]
48+
# Gather them concurrently
49+
results = await asyncio.gather(*tasks)
50+
for res in results:
51+
if res:
52+
return res
53+
return None
54+
2555
print("\n⌨️ ===============================")
2656
print(" WELCOME TO TYPING SPEED TESTER")
2757
print("================================ ⌨️\n")
2858

2959
while True:
3060
print("🌐 Fetching a fresh phrase for you...")
3161

32-
# Try multiple APIs for robustness
33-
urls = [
34-
"https://dummyjson.com/quotes/random",
35-
"https://api.quotable.io/random"
36-
]
37-
38-
sentence = None
39-
for url in urls:
40-
try:
41-
req = urllib.request.Request(url, headers={'User-Agent': 'Mozilla/5.0'})
42-
with urllib.request.urlopen(req, timeout=3) as response:
43-
data = json.loads(response.read().decode())
44-
# dummyjson uses 'quote', quotable uses 'content'
45-
phrase = data.get('quote') or data.get('content')
46-
if phrase:
47-
sentence = phrase
48-
break
49-
except Exception:
50-
continue
62+
sentence = asyncio.run(fetch_phrase_async())
5163

5264
if not sentence:
5365
print("📴 Offline or API unavailable. Using a classic phrase instead.")

0 commit comments

Comments
 (0)