Coverage for src/crawler/by_source/ptm_crawler.py: 22%
121 statements
« prev ^ index » next coverage.py v7.8.2, created at 2025-06-16 07:44 +0000
« prev ^ index » next coverage.py v7.8.2, created at 2025-06-16 07:44 +0000
1import lingua
2import regex
3from bs4 import BeautifulSoup, Tag
4from lingua import LanguageDetectorBuilder
5from ptf.cmds.xml.jats.jats_parser import JatsBase
6from ptf.model_data import (
7 ArticleData,
8 create_abstract,
9 create_articledata,
10 create_contributor,
11 create_subj,
12)
14from crawler.base_crawler import BaseCollectionCrawler
15from crawler.utils import add_pdf_link_to_xarticle, cleanup_str, regex_to_dict
18class PtmCrawler(BaseCollectionCrawler):
19 source_name = "Annales Societatis Mathematicae Polonae Series "
20 source_domain = "PTM"
21 source_website = "https://wydawnictwa.ptm.org.pl/"
23 issue_re = (
24 r"(?:Vol|Tom) (?P<volume>\d+)(?:, (?:No|Nr) (?P<number>[\d\-\/]+))? \((?P<year>\d{4})\)"
25 )
27 language_detector = LanguageDetectorBuilder.from_languages(
28 lingua.Language.ENGLISH, lingua.Language.POLISH, lingua.Language.RUSSIAN
29 ).build()
31 def __init__(self, *args, **kwargs):
32 super().__init__(*args, **kwargs)
33 self.update_cookies()
35 def download_file(self, url, force_refresh=False):
36 content = super().download_file(url, force_refresh)
37 if (
38 "Access to this website is possible only using browser with JavaScript and Cookies enabled."
39 in content
40 ):
41 self.update_cookies()
42 return self.download_file(url, force_refresh=True)
43 return content
45 def update_cookies(self):
46 script_content = super().download_file(self.source_website)
47 cookie_search = regex.search(r"createCookie\('vjs','(?P<cookie>\d+)',60\)", script_content)
48 if not cookie_search: 48 ↛ 49line 48 didn't jump to line 49 because the condition on line 48 was never true
49 raise ValueError("Couldn't set cookie for ptm")
50 self.headers.update(
51 {
52 "Cookie": f"vjs={cookie_search.group(1)}",
53 # "Accept-Language": "en-US,en;q=0.5",
54 "Accept-Encoding": "gzip, deflate, br, zstd",
55 "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
56 "User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:134.0) Gecko/20100101 Firefox/134.0",
57 }
58 )
60 def parse_collection_content(self, content):
61 soup = BeautifulSoup(content, "html.parser")
62 xissues = []
63 for issue_tag in soup.select("#issue h4 a"):
64 issue_url = issue_tag.get("href")
65 if not isinstance(issue_url, str): 65 ↛ 66line 65 didn't jump to line 66 because the condition on line 65 was never true
66 raise ValueError("Couldn't parse issue url")
67 issue_data = regex_to_dict(
68 self.issue_re,
69 issue_tag.text,
70 error_msg="Couldn't parse issue data",
71 )
72 xissues.append(
73 self.create_xissue(
74 issue_url, issue_data["year"], issue_data["volume"], issue_data["number"]
75 )
76 )
77 return xissues
79 def parse_issue_content(self, content, xissue):
80 soup = BeautifulSoup(content, "html.parser")
81 for index, article in enumerate(soup.select(".tocTitle a")):
82 xarticle = create_articledata()
83 xarticle.pid = f"a{index}"
84 article_url = article.get("href")
85 if not isinstance(article_url, str):
86 raise ValueError("Couldn't parse article url")
87 xarticle.url = article_url
88 xissue.articles.append(xarticle)
90 def parse_article_content(self, content, xissue, xarticle, url):
91 soup = BeautifulSoup(content, "html.parser")
92 frame = soup.select_one("frameset frame:first-child")
93 if not frame:
94 raise ValueError("Couldn't parse article")
95 real_url = frame.get("src")
96 if not isinstance(real_url, str):
97 raise ValueError("Couldn't find article url")
99 content = self.download_file(real_url)
100 soup = BeautifulSoup(content, "html.parser")
102 # Title
103 title_tag = soup.select_one("#articleTitle")
104 if not title_tag:
105 print(f"Couldn't parse article : {xissue.pid}_{xarticle.pid} {real_url}. Skipping")
106 return None
107 raise ValueError("Couldn't parse title")
108 xarticle.title_tex = cleanup_str(title_tag.text)
110 # DOI
111 doi_header = soup.select_one(
112 "strong:-soup-contains-own('Digital Object Identifier (DOI):')"
113 )
114 if doi_header:
115 doi_tag = doi_header.parent
116 doi_header.decompose()
117 xarticle.doi = cleanup_str(doi_tag.text)
119 # Abstract
120 abstract_tag = soup.select_one("#articleAbstract div")
121 if abstract_tag:
122 abstract_text = cleanup_str(abstract_tag.text)
123 if len(abstract_text) > 0 and abstract_text not in (
124 "Artykuł nie zawiera streszczenia",
125 "-",
126 ):
127 xarticle.abstracts.append(
128 create_abstract(
129 tag="abstract",
130 value_tex=abstract_text,
131 lang=self.detect_language(abstract_text),
132 )
133 )
135 # Pages
136 pages_header = soup.select_one("strong:-soup-contains-own('Pages:')") or soup.select_one(
137 "strong:-soup-contains-own('Strony:')"
138 )
139 if pages_header:
140 pages_tag = pages_header.parent
141 pages_header.decompose()
142 pages_splitted = pages_tag.text.split("-")
143 if len(pages_splitted) > 0:
144 xarticle.fpage = pages_splitted[0]
145 if len(pages_splitted) > 1:
146 xarticle.lpage = pages_splitted[1]
148 # pdf
149 pdf_tag = soup.select_one("a.file")
150 if pdf_tag:
151 pdf_url = pdf_tag.get("href")
152 if not isinstance(pdf_url, str):
153 raise ValueError("Couldn't parse pdf url")
155 pdf_url = pdf_url.replace("/view/", "/download/")
156 add_pdf_link_to_xarticle(xarticle, pdf_url)
158 if "(ENGLISH)" in pdf_tag.text:
159 xarticle.lang = "en"
160 elif "(POLSKI)" in pdf_tag.text:
161 xarticle.lang = "pl"
162 else:
163 xarticle.lang = "pl"
164 else:
165 print(f"Couldn't find article pdf for article {xissue.pid}_{xarticle.pid}")
166 return None
168 # Authors
169 authors_tag = soup.select_one("#authorString")
170 if authors_tag:
171 for author in cleanup_str(authors_tag.text).split(", "):
172 xarticle.contributors.append(create_contributor(string_name=author, role="author"))
174 # msc
175 msc_header = soup.select_one(
176 "strong:-soup-contains-own('Subject classification:')"
177 ) or soup.select_one("strong:-soup-contains-own('Kklasyfikacja tematyczna:')")
178 if msc_header:
179 msc_tag = msc_header.parent
180 msc_header.decompose()
181 for msc in cleanup_str(msc_tag.text).split("; "):
182 xarticle.kwds.append(create_subj(type="msc", value=msc))
184 # Keywords
185 kwd_header = soup.select_one("strong:-soup-contains-own('Keywords:')") or soup.select_one(
186 "strong:-soup-contains-own('Słowa kluczowe:')"
187 )
188 if kwd_header:
189 kwd_tag = kwd_header.parent
190 kwd_header.decompose()
191 for kwd in cleanup_str(kwd_tag.text).split("; "):
192 xarticle.kwds.append(create_subj(value=kwd))
194 # References
195 # Disabling references for now : PTM doesn't have a "clean" way to display references (eg : https://wydawnictwa.ptm.org.pl/index.php/antiquitates-mathematicae/article/view/7321)
197 # refs_header = soup.select_one("h4:-soup-contains-own('References')") or soup.select_one(
198 # "h4:-soup-contains-own('Cytowania')"
199 # )
200 # if refs_header:
201 # refs_tag = refs_header.next_sibling.next_sibling
202 # if refs_tag and isinstance(refs_tag, Tag):
203 # self.parse_references(xarticle, refs_tag)
205 return xarticle
207 def parse_references(self, xarticle: ArticleData, references: Tag):
208 bibitems = []
209 # TODO : extensive parsing (authors, title etc...)
210 # Currently, only the text is inserted
211 for ref in references.get_text(strip=True, separator="\n").splitlines():
212 bibitem = JatsBase.bake_ref(cleanup_str(ref))
213 bibitems.append(bibitem)
214 if len(bibitems) > 0:
215 xarticle.abstracts.append(JatsBase.compile_refs(bibitems))