Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move Zyte related code to own module #383

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 5 additions & 48 deletions spidermon/contrib/actions/jobs/tags.py
Original file line number Diff line number Diff line change
@@ -1,48 +1,5 @@
from spidermon.core.actions import Action
from spidermon.exceptions import NotConfigured


class JobTagsAction(Action):
tags = []
tag_settings = None

def __init__(self, tags=None):
super().__init__()
tags = tags or self.tags
self.tags = tags if isinstance(tags, list) else [tags]

@classmethod
def from_crawler_kwargs(cls, crawler):
kwargs = super().from_crawler_kwargs(crawler)
if cls.tag_settings:
kwargs.update({"tags": crawler.settings.get(cls.tag_settings)})
return kwargs

def run_action(self):
if self.tags:
if not self.data.job:
raise NotConfigured("Job not available!")
job_metadata = self.data.job.metadata
self.process_tags(job_metadata)
job_metadata.save()

def process_tags(self, job_metadata):
raise NotImplementedError


class AddJobTags(JobTagsAction):
tag_settings = "SPIDERMON_JOB_TAGS_TO_ADD"

def process_tags(self, job_metadata):
for tag in self.tags:
if tag not in job_metadata["tags"]:
job_metadata["tags"].append(tag)


class RemoveJobTags(JobTagsAction):
tag_settings = "SPIDERMON_JOB_TAGS_TO_REMOVE"

def process_tags(self, job_metadata):
for tag in self.tags:
if tag in job_metadata["tags"]:
job_metadata["tags"].remove(tag)
from spidermon.contrib.zyte.actions.jobs.tags import ( # noqa
JobTagsAction,
AddJobTags,
RemoveJobTags,
)
10 changes: 1 addition & 9 deletions spidermon/contrib/monitors/mixins/job.py
Original file line number Diff line number Diff line change
@@ -1,9 +1 @@
from spidermon.exceptions import NotConfigured


class JobMonitorMixin:
@property
def job(self):
if not self.data.job:
raise NotConfigured("Job not available!")
return self.data.job
from spidermon.contrib.zyte.monitors.mixins.job import JobMonitorMixin # noqa
Empty file.
Empty file.
Empty file.
48 changes: 48 additions & 0 deletions spidermon/contrib/zyte/actions/jobs/tags.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from spidermon.core.actions import Action
from spidermon.exceptions import NotConfigured


class JobTagsAction(Action):
tags = []
tag_settings = None

def __init__(self, tags=None):
super().__init__()
tags = tags or self.tags
self.tags = tags if isinstance(tags, list) else [tags]

@classmethod
def from_crawler_kwargs(cls, crawler):
kwargs = super().from_crawler_kwargs(crawler)
if cls.tag_settings:
kwargs.update({"tags": crawler.settings.get(cls.tag_settings)})
return kwargs

def run_action(self):
if self.tags:
if not self.data.job:
raise NotConfigured("Job not available!")
job_metadata = self.data.job.metadata
self.process_tags(job_metadata)
job_metadata.save()

def process_tags(self, job_metadata):
raise NotImplementedError


class AddJobTags(JobTagsAction):
tag_settings = "SPIDERMON_JOB_TAGS_TO_ADD"

def process_tags(self, job_metadata):
for tag in self.tags:
if tag not in job_metadata["tags"]:
job_metadata["tags"].append(tag)


class RemoveJobTags(JobTagsAction):
tag_settings = "SPIDERMON_JOB_TAGS_TO_REMOVE"

def process_tags(self, job_metadata):
for tag in self.tags:
if tag in job_metadata["tags"]:
job_metadata["tags"].remove(tag)
Empty file.
Empty file.
9 changes: 9 additions & 0 deletions spidermon/contrib/zyte/monitors/mixins/job.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from spidermon.exceptions import NotConfigured


class JobMonitorMixin:
@property
def job(self):
if not self.data.job:
raise NotConfigured("Job not available!")
return self.data.job
Empty file added spidermon/contrib/zyte/utils.py
Empty file.