Files
MediaFusion/mediafusion_scrapy/extensions.py
T
Mohamed Zumair 52d8956034 Add ScrapeOps monitoring, Add zilean filtered endpoint, Fix Prowlarr scraping with download link & Fix PikPak login error etc (#319)
* Refactor ZileanScraper to use parallel requests for searching and filtering streams with new endpoints

* Fix DLHD scraping & enable DLHD without MediaFlow

* Switch to httpx for async HTTP requests

* Implement caching for PikPak token to reduce login error

* Refactor torrent cleanup logic

* Add inactivity monitor extension to close idle spiders

Introduced `InactivityMonitor` to automatically close spiders that remain inactive for a specified time. This extension checks activity at regular intervals and uses configurable settings for check intervals and inactivity timeouts. If no items are scraped within the timeout period, the spider is closed to free resources.

* Handle TypeError in dynamic sorting of streams

* Refactor torrent info scraper to support pre-processing.

Introduced a pre-processing function mapping to handle specific indexer requirements before parsing the HTML. Added a custom pre-processing function for "TheRARBG" to handle URL adjustments, improving modularity and readability in the `get_torrent_info` function.

* Refine error logging and fix hash key in spider

* Fix Prowlarr not stop on max process limit

* #315: Integrate ScrapeOps logging into all scrapers & spiders

Added ScrapeOps logging to Zilean, Torrentio, Prowlarr, and Prowlarr Feed scrapers to enhance request tracking and error handling. Configured ScrapeOps API key in settings and updated Pipfile/Pipfile.lock with scrapeops-python-requests and scrapeops-scrapy dependencies.

* Refactor scraper cache status handler

* verify torrent before parsing on prowlarr & prioritize magnet on badass_torrents

* Add support for provide locally hosted mediaflow proxy public address and reduce the time leg on private ip address checking

* handle RD exception cases

* do not setup scrapeops when api key is none

* Enhanced dynamic sorting of torrent streams

Revised the dynamic_sort_key function to handle different key types more efficiently with match-case. Simplified error handling and improved logging to capture sorting data in the case of exceptions.

* add missing last update date for metadata

* update domain for nowmesports
2024-10-14 05:58:23 +05:30

64 lines
2.4 KiB
Python

import time
from datetime import datetime, timedelta
from scrapy import signals
from scrapy.exceptions import NotConfigured, CloseSpider
from scrapy.utils.log import logger
from twisted.internet import task
class InactivityMonitor:
"""Monitor spider for inactivity and close if inactive for too long"""
def __init__(self, crawler, interval=60.0, inactivity_timeout=15):
self.crawler = crawler
self.stats = crawler.stats
self.interval = interval
self.inactivity_timeout = inactivity_timeout
self.task = None
self.last_scraped_time = time.monotonic()
@classmethod
def from_crawler(cls, crawler):
interval = crawler.settings.getfloat("INACTIVITY_CHECK_INTERVAL", 60.0)
inactivity_timeout = crawler.settings.getint("INACTIVITY_TIMEOUT_MINUTES", 15)
if not interval or not inactivity_timeout:
raise NotConfigured
ext = cls(crawler, interval, inactivity_timeout)
crawler.signals.connect(ext.spider_opened, signal=signals.spider_opened)
crawler.signals.connect(ext.spider_closed, signal=signals.spider_closed)
crawler.signals.connect(ext.item_scraped, signal=signals.item_scraped)
return ext
def spider_opened(self, spider):
self.last_scraped_time = time.monotonic()
self.task = task.LoopingCall(self.check_inactivity, spider)
self.task.start(self.interval)
def check_inactivity(self, spider):
time_since_last_scrape_seconds = time.monotonic() - self.last_scraped_time
time_since_last_scrape = timedelta(seconds=time_since_last_scrape_seconds)
logger.debug(
"Checking inactivity for spider %s, last scraped %s ago",
spider.name,
time_since_last_scrape,
)
if time_since_last_scrape > timedelta(minutes=self.inactivity_timeout):
msg = f"No items scraped in the last {self.inactivity_timeout} minutes. Closing spider."
logger.info(msg, extra={"spider": spider})
self.crawler.engine.close_spider(
spider, reason=f"Inactivity timeout: {msg}"
)
raise CloseSpider(f"Inactivity timeout: {msg}")
def item_scraped(self, item, response, spider):
self.last_scraped_time = datetime.now()
def spider_closed(self, spider, reason):
if self.task and self.task.running:
self.task.stop()