-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
52 lines (38 loc) · 1.32 KB
/
utils.py
File metadata and controls
52 lines (38 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
"""
Utils to be used by other modules, including title capitalization, etc.
"""
# Non-local imports
import requests
# Local imports
import enum
# Project modules
import keys
from config import Config
# ---- Language ----
class CapitalizationMethod(enum.Enum):
SMART = enum.auto()
DEFAULT = enum.auto()
def capitalize_title(title: str, method_force: str = None) -> str:
if not isinstance(title, str):
raise Exception("You can only capitalize string titles.")
# Determine method
if not method_force:
method = Config.smart_cap_preference
else:
if method_force.lower() == "smart" or Config.smart_cap_preference:
method = CapitalizationMethod.SMART
else:
method = CapitalizationMethod.DEFAULT
# If smart capitalization is disabled
if method == CapitalizationMethod.DEFAULT:
return title.title()
# Separate words with %20
title = title.replace(' ', '%20')
# Make the request
url = f"https://capitalize-my-title.p.rapidapi.com/title/{title}"
headers = {
"X-RapidAPI-Key": keys.RapidAPI.api_key,
"X-RapidAPI-Host": "capitalize-my-title.p.rapidapi.com"
}
response = requests.request("GET", url, headers=headers)
return response.json()['data']['output']