b-jokes/helpers/jokes.py
Administrator 149c251bfa initial
2022-10-03 16:32:43 +03:00

32 lines
791 B
Python

from time import sleep
from requests import get
from helpers.mongo import mongo
def fetch_jokes():
i = 1
while True:
info = get(f"https://baneks.ru/{i}")
if info.status_code == 200:
print(i)
content = info.text
anek = content.split("<p>")[1].split("</p>")[0].replace("<br />", "")
if '<a href="random">' in anek:
break
if mongo.jokes_collection.find_one({"id": i}) is None:
mongo.jokes_collection.insert_one({
"id": i,
"text": anek
})
i += 1
def poll_jokes():
while True:
print("start fetching jokes")
fetch_jokes()
print("finished fetching jokes")
sleep(60 * 60 * 24)