File size: 12,847 Bytes
bf64b03
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
"""

VortexScienceScraper: Scrapes scientific content from open access sources.

Respects robots.txt and rate limits.

"""

import time
import requests
from typing import List, Dict, Optional
from urllib.robotparser import RobotFileParser
from pathlib import Path
import json


class VortexScienceScraper:
    """

    Scrapes scientific content from open access sources.

    Sources: arXiv, PubMed Central, Wikipedia, NIST, NASA.

    """

    SOURCES = {
        "arxiv": {
            "base_url": "https://arxiv.org",
            "search_url": "https://arxiv.org/search/",
            "rate_limit": 1.0,  # seconds between requests
            "robots": "https://arxiv.org/robots.txt",
        },
        "pubmed": {
            "base_url": "https://www.ncbi.nlm.nih.gov/pmc",
            "search_url": "https://www.ncbi.nlm.nih.gov/pmc/articles/",
            "rate_limit": 0.5,
            "robots": "https://www.ncbi.nlm.nih.gov/robots.txt",
        },
        "wikipedia": {
            "base_url": "https://en.wikipedia.org",
            "search_url": "https://en.wikipedia.org/w/api.php",
            "rate_limit": 0.1,
            "robots": "https://en.wikipedia.org/robots.txt",
        },
        "nist": {
            "base_url": "https://webbook.nist.gov",
            "search_url": "https://webbook.nist.gov/cgi/cbook.cgi",
            "rate_limit": 1.0,
            "robots": "https://webbook.nist.gov/robots.txt",
        },
        "nasa": {
            "base_url": "https://ntrs.nasa.gov",
            "search_url": "https://ntrs.nasa.gov/api/citations/search",
            "rate_limit": 1.0,
            "robots": "https://ntrs.nasa.gov/robots.txt",
        },
    }

    def __init__(

        self,

        output_dir: str = "./data/scraped",

        respect_robots: bool = True,

        user_agent: str = "VortexScientificBot/1.0",

    ):
        """

        Initialize scraper.



        Args:

            output_dir: Directory to save scraped data

            respect_robots: Whether to respect robots.txt

            user_agent: User agent string for requests

        """
        self.output_dir = Path(output_dir)
        self.output_dir.mkdir(parents=True, exist_ok=True)
        self.respect_robots = respect_robots
        self.user_agent = user_agent

        self.session = requests.Session()
        self.session.headers.update({"User-Agent": user_agent})

        # Cache for robots.txt
        self.robots_cache = {}

        # Rate limit tracking
        self.last_request_time = {}

    def _check_robots_allowed(self, url: str) -> bool:
        """Check if robots.txt allows scraping the URL."""
        if not self.respect_robots:
            return True

        # Extract base domain
        from urllib.parse import urlparse
        parsed = urlparse(url)
        base_url = f"{parsed.scheme}://{parsed.netloc}"

        if base_url not in self.robots_cache:
            rp = RobotFileParser()
            rp.set_url(base_url + "/robots.txt")
            try:
                rp.read()
                self.robots_cache[base_url] = rp
            except Exception as e:
                print(f"Could not read robots.txt for {base_url}: {e}")
                return False

        rp = self.robots_cache[base_url]
        return rp.can_fetch(self.user_agent, url)

    def _rate_limit(self, source: str):
        """Enforce rate limiting for a source."""
        now = time.time()
        last = self.last_request_time.get(source, 0)
        delay = self.SOURCES[source]["rate_limit"]
        if now - last < delay:
            time.sleep(delay - (now - last))
        self.last_request_time[source] = time.time()

    def scrape_arxiv(

        self,

        query: str,

        max_results: int = 100,

        categories: Optional[List[str]] = None,

    ) -> List[Dict]:
        """

        Scrape arXiv papers.



        Args:

            query: Search query

            max_results: Maximum number of results

            categories: Optional list of arXiv categories (e.g., ['physics', 'math'])



        Returns:

            List of paper metadata and abstracts

        """
        papers = []

        params = {
            "query": query,
            "searchtype": "all",
            "abstracts": "show",
            "size": min(max_results, 200),  # arXiv max per page
            "order": "-announced_date_first",
        }

        if categories:
            params["filter"] = "categories:" + "+OR+".join(categories)

        url = self.SOURCES["arxiv"]["search_url"]

        if not self._check_robots_allowed(url):
            print(f"Robots.txt disallows scraping {url}")
            return papers

        try:
            self._rate_limit("arxiv")
            response = self.session.get(url, params=params)
            response.raise_for_status()

            # Parse HTML (simplified - would use BeautifulSoup in practice)
            # For now, return placeholder
            print(f"Scraped arXiv query '{query}' - got response status {response.status_code}")

            # Placeholder: would extract paper titles, abstracts, PDF links
            for i in range(min(10, max_results)):
                papers.append({
                    "source": "arxiv",
                    "title": f"Paper {i}",
                    "abstract": "Abstract placeholder...",
                    "pdf_url": f"https://arxiv.org/pdf/{i}.pdf",
                })

        except Exception as e:
            print(f"Error scraping arXiv: {e}")

        return papers

    def scrape_pubmed(

        self,

        query: str,

        max_results: int = 100,

    ) -> List[Dict]:
        """Scrape PubMed Central articles."""
        articles = []

        # PubMed API endpoint
        url = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi"
        params = {
            "db": "pmc",
            "term": query,
            "retmax": max_results,
            "retmode": "json",
        }

        if not self._check_robots_allowed(url):
            print(f"Robots.txt disallows {url}")
            return articles

        try:
            self._rate_limit("pubmed")
            response = self.session.get(url, params=params)
            response.raise_for_status()

            data = response.json()
            pmc_ids = data.get("esearchresult", {}).get("idlist", [])

            for pmc_id in pmc_ids[:10]:  # Limit for demo
                articles.append({
                    "source": "pubmed",
                    "pmc_id": pmc_id,
                    "url": f"https://www.ncbi.nlm.nih.gov/pmc/articles/PMC{pmc_id}/",
                })

            print(f"Found {len(pmc_ids)} PubMed articles")

        except Exception as e:
            print(f"Error scraping PubMed: {e}")

        return articles

    def scrape_wikipedia(

        self,

        topic: str,

        max_pages: int = 10,

    ) -> List[Dict]:
        """Scrape Wikipedia science articles."""
        pages = []

        # Wikipedia API
        url = "https://en.wikipedia.org/w/api.php"
        params = {
            "action": "query",
            "format": "json",
            "prop": "extracts",
            "exintro": True,
            "titles": topic,
            "redirects": True,
        }

        if not self._check_robots_allowed(url):
            print(f"Robots.txt disallows {url}")
            return pages

        try:
            self._rate_limit("wikipedia")
            response = self.session.get(url, params=params)
            response.raise_for_status()

            data = response.json()
            pages_data = data.get("query", {}).get("pages", {})

            for page_id, page in pages_data.items():
                if "extract" in page:
                    pages.append({
                        "source": "wikipedia",
                        "title": page.get("title", ""),
                        "text": page.get("extract", ""),
                    })

        except Exception as e:
            print(f"Error scraping Wikipedia: {e}")

        return pages

    def scrape_nist(

        self,

        element: str,

    ) -> List[Dict]:
        """Scrape NIST chemistry webbook for element data."""
        data = []

        url = "https://webbook.nist.gov/cgi/cbook.cgi"
        params = {
            "Formula": element,
            "Units": "SI",
            "Submit": "Submit",
        }

        if not self._check_robots_allowed(url):
            print(f"Robots.txt disallows {url}")
            return data

        try:
            self._rate_limit("nist")
            response = self.session.get(url, params=params)
            response.raise_for_status()

            # Placeholder - would parse HTML tables
            data.append({
                "source": "nist",
                "element": element,
                "html": response.text[:1000],
            })

        except Exception as e:
            print(f"Error scraping NIST: {e}")

        return data

    def scrape_nasa(

        self,

        query: str,

        max_results: int = 50,

    ) -> List[Dict]:
        """Scrape NASA technical reports."""
        reports = []

        url = "https://ntrs.nasa.gov/api/citations/search"
        params = {
            "q": query,
            "page[size]": max_results,
        }

        if not self._check_robots_allowed(url):
            print(f"Robots.txt disallows {url}")
            return reports

        try:
            self._rate_limit("nasa")
            response = self.session.get(url, params=params)
            response.raise_for_status()

            data = response.json()
            for item in data.get("data", [])[:10]:
                reports.append({
                    "source": "nasa",
                    "title": item.get("attributes", {}).get("title", ""),
                    "abstract": item.get("attributes", {}).get("abstract", ""),
                    "download_url": item.get("attributes", {}).get("downloads", {}).get("pdf", ""),
                })

        except Exception as e:
            print(f"Error scraping NASA: {e}")

        return reports

    def save_results(

        self,

        results: List[Dict],

        filename: str,

    ):
        """Save scraped results to JSON."""
        output_path = self.output_dir / filename
        with open(output_path, "w", encoding="utf-8") as f:
            json.dump(results, f, indent=2, ensure_ascii=False)
        print(f"Saved {len(results)} results to {output_path}")

    def scrape_all_sources(

        self,

        queries: Dict[str, str],

        max_per_source: int = 50,

    ) -> Dict[str, List[Dict]]:
        """

        Scrape all sources with given queries.



        Args:

            queries: Dict mapping source name to query string

            max_per_source: Max results per source



        Returns:

            Dict mapping source to list of results

        """
        all_results = {}

        for source, query in queries.items():
            if source not in self.SOURCES:
                print(f"Unknown source: {source}")
                continue

            print(f"Scraping {source} with query: {query}")

            if source == "arxiv":
                results = self.scrape_arxiv(query, max_results=max_per_source)
            elif source == "pubmed":
                results = self.scrape_pubmed(query, max_results=max_per_source)
            elif source == "wikipedia":
                results = self.scrape_wikipedia(query, max_pages=max_per_source)
            elif source == "nist":
                results = self.scrape_nist(query)
            elif source == "nasa":
                results = self.scrape_nasa(query, max_results=max_per_source)
            else:
                results = []

            all_results[source] = results

            # Save intermediate results
            self.save_results(results, f"{source}_results.json")

        return all_results


def test_scraper():
    """Test the scraper (limited)."""
    scraper = VortexScienceScraper()

    # Test Wikipedia (lightweight)
    print("Testing Wikipedia scrape...")
    results = scraper.scrape_wikipedia("quantum mechanics", max_pages=2)
    print(f"Got {len(results)} Wikipedia pages")

    # Test arXiv (rate limited)
    print("Testing arXiv scrape...")
    results = scraper.scrape_arxiv("quantum", max_results=5)
    print(f"Got {len(results)} arXiv papers")

    print("Scraper test passed!")


if __name__ == "__main__":
    test_scraper()