Muttered3 commited on
Commit
6ff79d3
Β·
verified Β·
1 Parent(s): 13dd7ff

Update wordlist.py

Browse files
Files changed (1) hide show
  1. wordlist.py +28 -33
wordlist.py CHANGED
@@ -1,38 +1,33 @@
 
 
 
 
1
  import asyncio
2
  import urllib.request
3
- import urllib.error
4
  import config
5
 
6
- async def fetch_word_list() -> list[str]:
7
- """
8
- Downloads config.WORDLIST_URL (tab-separated word<TAB>freq file).
9
- Filters: length MIN_WORD_LEN..MAX_WORD_LEN, strictly alpha only.
10
- Sorts by frequency descending.
11
- Uses loop.run_in_executor so it does not block the event loop.
12
- """
13
- def _download():
14
- req = urllib.request.Request(config.WORDLIST_URL, headers={'User-Agent': 'Mozilla/5.0'})
15
- try:
16
- with urllib.request.urlopen(req) as response:
17
- return response.read().decode('utf-8').splitlines()
18
- except urllib.error.URLError as e:
19
- print(f"Failed to fetch word list: {e}")
20
- return []
21
-
22
- loop = asyncio.get_event_loop()
23
- lines = await loop.run_in_executor(None, _download)
 
 
 
24
 
25
- data = []
26
- for line in lines:
27
- parts = line.split('\t')
28
- if len(parts) == 2:
29
- word = parts[0].strip()
30
- if config.MIN_WORD_LEN <= len(word) <= config.MAX_WORD_LEN and word.isalpha():
31
- try:
32
- count = int(parts[1].strip())
33
- data.append((word, count))
34
- except ValueError:
35
- continue
36
-
37
- data.sort(key=lambda x: x[1], reverse=True)
38
- return [word for word, count in data]
 
1
+ # ════════════════════════════════════════
2
+ # FILE: wordlist.py
3
+ # ════════════════════════════════════════
4
+
5
  import asyncio
6
  import urllib.request
 
7
  import config
8
 
9
+ def _download_and_filter() -> list[str]:
10
+ try:
11
+ req = urllib.request.Request(config.WORDLIST_URL)
12
+ with urllib.request.urlopen(req) as response:
13
+ data = response.read().decode('utf-8')
14
+
15
+ words = []
16
+ for line in data.splitlines():
17
+ line = line.strip()
18
+ if not line:
19
+ continue
20
+ parts = line.split('\t')
21
+ if not parts:
22
+ continue
23
+ w = parts[0].strip().lower()
24
+ if w.isalpha() and config.MIN_WORD_LEN <= len(w) <= config.MAX_WORD_LEN:
25
+ words.append(w)
26
+ return words
27
+ except Exception as e:
28
+ print(f"Error fetching wordlist: {e}")
29
+ return []
30
 
31
+ async def fetch_word_list() -> list[str]:
32
+ loop = asyncio.get_running_loop()
33
+ return await loop.run_in_executor(None, _download_and_filter)