This repository was archived by the owner on Jul 20, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
fix: backfill district case_type from detail page #85
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -122,6 +122,17 @@ def start_requests(self): | |
| or_( | ||
| CourtCase.status == "pending", | ||
| CourtCase.status.is_(None), | ||
| # Revisit already-enriched rows whose case_type never | ||
| # got populated: the daily cause-list only carries the | ||
| # (often blank) "मुद्दा विषय" subject column, so these | ||
| # need the detail page's "मुद्दाको किसिम" to backfill. | ||
| and_( | ||
| CourtCase.status == "enriched", | ||
| or_( | ||
| CourtCase.case_type.is_(None), | ||
| CourtCase.case_type == "", | ||
| ), | ||
| ), | ||
| ), | ||
| ) | ||
| ) | ||
|
|
@@ -258,9 +269,27 @@ def parse_case_detail(self, response): | |
| self.logger.warning(f"Case {case_number} not found in database") | ||
| return | ||
|
|
||
| if case.status == "enriched": | ||
| already_enriched = case.status == "enriched" | ||
| has_case_type = bool(case.case_type) | ||
|
|
||
| if already_enriched: | ||
| if has_case_type: | ||
| self.logger.info(f"Case {case_number} already enriched, skipping") | ||
| return | ||
| # Already enriched but missing case_type: backfill just that field | ||
| # from the detail page without re-touching entities/hearings (whose | ||
| # rebuild deletes rows that may have downstream linkages). | ||
| case_type = self._extract_enrichment_data(soup).get("case_type") | ||
| if case_type: | ||
| self._backfill_case_type(case_number, code_name, case_type) | ||
| self.logger.info( | ||
| f"Backfilled case_type for {case_number} ({code_name}): {case_type}" | ||
| ) | ||
| else: | ||
| self.logger.info( | ||
| f"Case {case_number} ({code_name}) has no case_type on detail page" | ||
| ) | ||
| return | ||
|
|
||
| # Extract enrichment data | ||
| enrichment_data = self._extract_enrichment_data(soup) | ||
|
|
@@ -296,6 +325,11 @@ def _extract_enrichment_data(self, soup: BeautifulSoup) -> Dict: | |
| # Map Nepali labels to database fields | ||
| if label == "रजिष्ट्रेशन नं" and value: | ||
| data["registration_number"] = value[:100] | ||
| elif label == "मुद्दाको किसिम" and value: | ||
| # True case type, only available on the detail page; the | ||
| # cause-list listing has no such column (it carries the | ||
| # subject), which is why district case_type is often blank. | ||
| data["case_type"] = value[:200] | ||
| elif label == "मुद्दाको बिषय" and value: | ||
| data["case_subject"] = value | ||
| elif label == "मुद्दाको स्थिति" and value: | ||
|
|
@@ -395,6 +429,26 @@ def _extract_hearings_timeline(self, soup: BeautifulSoup) -> Dict[str, List[Dict | |
|
|
||
| return data | ||
|
|
||
| def _backfill_case_type(self, case_number: str, code_name: str, case_type: str): | ||
| """Set case_type on an already-enriched case, leaving everything else.""" | ||
| now = datetime.now(KATHMANDU_TZ).replace(tzinfo=None) | ||
|
|
||
| with self.session.begin(): | ||
| case = ( | ||
| self.session.query(CourtCase) | ||
| .filter( | ||
| and_( | ||
| CourtCase.case_number == case_number, | ||
| CourtCase.court_identifier == code_name, | ||
| ) | ||
| ) | ||
| .first() | ||
| ) | ||
|
|
||
| if case and not case.case_type: | ||
| case.case_type = case_type[:200] | ||
| case.updated_at = now | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| def _save_enrichment( | ||
| self, | ||
| case_number: str, | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Combining the status check and the backfill logic inside the existing transaction block avoids querying the database twice for the same
CourtCaserow and opening/closing multiple transactions.Additionally, if a case is already enriched but has no
case_typeon the detail page, leaving it empty will cause it to be repeatedly queried and scraped on every subsequent run of the spider (due to the query instart_requests). Setting a placeholder like"-"prevents this infinite re-scraping loop.