Skip to content

Commit d58c75d

Browse files
committed
Refactor spider name extracto into a new utility function
1 parent 23d3bb8 commit d58c75d

File tree

4 files changed

+13
-3
lines changed

4 files changed

+13
-3
lines changed

spidermon/contrib/scrapy/extensions.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from itemadapter import ItemAdapter
88

99
from spidermon import MonitorSuite
10+
from spidermon.contrib.utils.spider import get_spider_name
1011
from spidermon.contrib.scrapy.runners import SpiderMonitorRunner
1112
from spidermon.python import factory
1213
from spidermon.python.monitors import ExpressionsMonitor
@@ -240,6 +241,6 @@ def _generate_data_for_spider(self, spider):
240241
else [],
241242
"crawler": self.crawler,
242243
"spider": spider,
243-
"sc_spider_name": os.getenv("SHUB_VIRTUAL_SPIDER", spider.name),
244+
"sc_spider_name": get_spider_name(spider),
244245
"job": self.client.job if self.client.available else None,
245246
}

spidermon/contrib/stats/statscollectors/local_storage.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
from scrapy.statscollectors import StatsCollector
66
from scrapy.utils.project import data_path
77

8+
from spidermon.contrib.utils.spider import get_spider_name
9+
810

911
class LocalStorageStatsHistoryCollector(StatsCollector):
1012
def _stats_location(self, spider):
1113
statsdir = data_path("stats", createdir=True)
12-
spider_name = os.getenv("SHUB_VIRTUAL_SPIDER", spider.name)
14+
spider_name = get_spider_name(spider)
1315
return os.path.join(statsdir, f"{spider_name}_stats_history")
1416

1517
def open_spider(self, spider):

spidermon/contrib/stats/statscollectors/sc_collections.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import scrapinghub
66
from sh_scrapy.stats import HubStorageStatsCollector
77

8+
from spidermon.contrib.utils.spider import get_spider_name
9+
810
logger = logging.getLogger(__name__)
911

1012

@@ -18,7 +20,7 @@ def _open_collection(self, spider):
1820

1921
project = sh_client.get_project(proj_id)
2022
collections = project.collections
21-
spider_name = os.getenv("SHUB_VIRTUAL_SPIDER", spider.name)
23+
spider_name = get_spider_name(spider)
2224
stats_location = f"{spider_name}_stats_history"
2325
store = collections.get_store(stats_location)
2426
return store

spidermon/contrib/utils/spider.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import os
2+
3+
4+
def get_spider_name(spider):
5+
return os.getenv("SHUB_VIRTUAL_SPIDER") or spider.name

0 commit comments

Comments
 (0)