Extract publication year from content
(self, soup: BeautifulSoup, title: str = '')
| 1042 | return None |
| 1043 | |
| 1044 | def _extract_year_from_content(self, soup: BeautifulSoup, title: str = '') -> Optional[int]: |
| 1045 | """Extract publication year from content""" |
| 1046 | try: |
| 1047 | # First try to find year in title |
| 1048 | if title: |
| 1049 | year_match = re.search(r'\b(19|20)\d{2}\b', title) |
| 1050 | if year_match: |
| 1051 | return int(year_match.group()) |
| 1052 | |
| 1053 | # Look for year in specific elements |
| 1054 | year_selectors = [ |
| 1055 | '[class*="year"]', |
| 1056 | '[class*="date"]', |
| 1057 | '.publication-date', |
| 1058 | '.pub-date' |
| 1059 | ] |
| 1060 | |
| 1061 | for selector in year_selectors: |
| 1062 | elements = soup.select(selector) |
| 1063 | for elem in elements: |
| 1064 | text = elem.get_text() |
| 1065 | year_match = re.search(r'\b(19|20)\d{2}\b', text) |
| 1066 | if year_match: |
| 1067 | return int(year_match.group()) |
| 1068 | |
| 1069 | # Try to find year in the first few paragraphs |
| 1070 | paragraphs = soup.find_all('p')[:5] |
| 1071 | for p in paragraphs: |
| 1072 | text = p.get_text() |
| 1073 | year_match = re.search(r'\b(19|20)\d{2}\b', text) |
| 1074 | if year_match: |
| 1075 | year = int(year_match.group()) |
| 1076 | # Only accept reasonable years for academic papers |
| 1077 | if 1950 <= year <= 2030: |
| 1078 | return year |
| 1079 | |
| 1080 | except Exception as e: |
| 1081 | self.logger.warning(f"Failed to extract year from content: {str(e)}") |
| 1082 | |
| 1083 | return None |
| 1084 | |
| 1085 | def _extract_from_pdf_content(self, content: bytes) -> Optional[Dict]: |
| 1086 | """Extract metadata from PDF content""" |
no outgoing calls
no test coverage detected