-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpylipsum.py
51 lines (37 loc) · 1.64 KB
/
pylipsum.py
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
import requests
from bs4 import BeautifulSoup
class LipsumAPI:
def __init__(self) -> None:
pass
def generate(self, count: int, content_type: str, default_start: bool = True) -> str:
# Validate type input
if content_type not in ['words', 'paragraphs', 'bytes', 'lists']:
raise ValueError("content_type must be 'words', 'paragraphs', 'bytes', or 'lists'")
url = "https://www.lipsum.com/feed/html"
if content_type == "paragraphs":
wtype = 'paras'
else:
wtype = content_type
data = {
'amount': count,
'what': wtype,
}
if default_start:
data["start"] = "yes"
try:
# Send the request
response = requests.post(url, data=data)
response.raise_for_status() # Raise an exception for 4xx and 5xx status codes
soup = BeautifulSoup(response.text, 'lxml')
# Extract text from <p> or <ul> tags
if content_type == 'lists':
text_list = [ul.get_text() for ul in soup.find_all('ul')]
else:
text_list = [p.get_text() for p in soup.find_all('p')]
# Concatenate the text into a single string
lorem_text = '\n'.join(text_list)
# Remove extra whitespace and newlines if content_type is 'lists'
lorem_text = lorem_text.replace("\n", "")
return lorem_text
except requests.RequestException as e:
raise Exception(f"Error fetching data from Lipsum.com: {e}")