mirror of
https://github.com/KeksPirates/SoftwareManager.git
synced 2026-08-03 17:39:42 +02:00
Refactor monkrus scraper to implement caching and improve post fetching logic
This commit is contained in:
@@ -1,16 +1,29 @@
|
|||||||
from bs4 import BeautifulSoup
|
from bs4 import BeautifulSoup
|
||||||
import requests
|
import requests
|
||||||
|
import time
|
||||||
from core.utils.general.logs import consoleLog
|
from core.utils.general.logs import consoleLog
|
||||||
|
|
||||||
def scrape_monkrus_telegram(query):
|
cache = {
|
||||||
|
"data": [],
|
||||||
|
"last_fetched": 0
|
||||||
|
}
|
||||||
|
|
||||||
|
cache_expiry = 300
|
||||||
|
|
||||||
|
def _get_telegram_posts():
|
||||||
post = 100
|
post = 100
|
||||||
max_posts = 250
|
max_posts = 250
|
||||||
posts: list[dict] = []
|
posts = []
|
||||||
added = set()
|
added = set()
|
||||||
|
|
||||||
|
current_time = time.time()
|
||||||
|
|
||||||
|
if cache["data"] and (current_time - cache["last_fetched"] < cache_expiry):
|
||||||
|
return cache["data"]
|
||||||
|
|
||||||
while post <= max_posts:
|
while post <= max_posts:
|
||||||
url = f"https://t.me/s/real_monkrus/{post}"
|
url = f"https://t.me/s/real_monkrus/{post}"
|
||||||
response = requests.get(url)
|
response = requests.get(url, timeout=10)
|
||||||
soup = BeautifulSoup(response.text, "html.parser")
|
soup = BeautifulSoup(response.text, "html.parser")
|
||||||
bubbles = soup.find_all("div", class_="tgme_widget_message_bubble")
|
bubbles = soup.find_all("div", class_="tgme_widget_message_bubble")
|
||||||
|
|
||||||
@@ -25,7 +38,7 @@ def scrape_monkrus_telegram(query):
|
|||||||
title = post_txt.b.text
|
title = post_txt.b.text
|
||||||
link = bubble.find("a", href=lambda x: x and x.startswith("https://uztracker.net"))
|
link = bubble.find("a", href=lambda x: x and x.startswith("https://uztracker.net"))
|
||||||
|
|
||||||
if query.lower() in title.lower() and link:
|
if link:
|
||||||
post_url = link["href"]
|
post_url = link["href"]
|
||||||
if post_url not in added:
|
if post_url not in added:
|
||||||
added.add(post_url)
|
added.add(post_url)
|
||||||
@@ -39,4 +52,20 @@ def scrape_monkrus_telegram(query):
|
|||||||
post += 22
|
post += 22
|
||||||
|
|
||||||
posts.reverse()
|
posts.reverse()
|
||||||
|
|
||||||
|
cache["data"] = posts
|
||||||
|
cache["last_fetched"] = current_time
|
||||||
|
|
||||||
return(posts)
|
return(posts)
|
||||||
|
|
||||||
|
def scrape_monkrus_telegram(query):
|
||||||
|
posts = _get_telegram_posts()
|
||||||
|
filtered_posts = []
|
||||||
|
|
||||||
|
for post in posts:
|
||||||
|
if query.lower() in post["title"].lower():
|
||||||
|
filtered_post = post.copy()
|
||||||
|
filtered_post["id"] = len(filtered_posts) + 1
|
||||||
|
filtered_posts.append(filtered_post)
|
||||||
|
|
||||||
|
return filtered_posts
|
||||||
Reference in New Issue
Block a user