|
| 1 | +#! /usr/bin/env node |
| 2 | + |
| 3 | +// here we will convert the search.sh script into JS |
| 4 | +import { format, sub } from 'date-fns'; |
| 5 | +import fs from 'fs/promises'; |
| 6 | +import { download, search, sendOutput } from './cli.js'; |
| 7 | + |
| 8 | +const CATEGORY_ID = 'alzheimers-disease'; |
| 9 | +const DATA_DIRECTORY = 'example-data'; |
| 10 | + |
| 11 | +const MEDRXIV_SOURCE = 'medrxiv'; |
| 12 | +const BIORXIV_SOURCE = 'biorxiv'; |
| 13 | + |
| 14 | +const now = new Date(); |
| 15 | +const startOffset = { days: 1 }; |
| 16 | +const START_DATE = format(sub(now, startOffset), 'yyyy-MM-dd'); |
| 17 | +const END_DATE = format(now, 'yyyy-MM-dd'); |
| 18 | + |
| 19 | +const BIORXIV_FILE = `${DATA_DIRECTORY}/${END_DATE}_${BIORXIV_SOURCE}.json`; |
| 20 | +const MEDRXIV_FILE = `${DATA_DIRECTORY}/${END_DATE}_${MEDRXIV_SOURCE}.json`; |
| 21 | +const COMBINED_FILE = `${DATA_DIRECTORY}/${END_DATE}.json`; |
| 22 | +const OUTPUT_FILE = `${DATA_DIRECTORY}/${CATEGORY_ID}.json`; |
| 23 | + |
| 24 | +// Getting all latest articles from BiorXiv |
| 25 | +console.log(`Fetching from ${BIORXIV_SOURCE} between ${START_DATE} and ${END_DATE}`); |
| 26 | +fs.open(BIORXIV_FILE, 'w'); |
| 27 | +const bioOptions = { |
| 28 | + source: BIORXIV_SOURCE, |
| 29 | + output: BIORXIV_FILE |
| 30 | +}; |
| 31 | +const bioData = await download(START_DATE, END_DATE, bioOptions); |
| 32 | + |
| 33 | +// Getting all latest articles from MedrXiv |
| 34 | +console.log(`Fetching from ${MEDRXIV_SOURCE} between ${START_DATE} and ${END_DATE}`); |
| 35 | +fs.open(MEDRXIV_FILE, 'w'); |
| 36 | +const medOptions = { |
| 37 | + source: MEDRXIV_SOURCE, |
| 38 | + output: MEDRXIV_FILE |
| 39 | +}; |
| 40 | +const medData = await download(START_DATE, END_DATE, medOptions); |
| 41 | + |
| 42 | +// Creating a JSON with all the results, both sources combined |
| 43 | +console.log('Combining results...'); |
| 44 | +fs.open(COMBINED_FILE, 'w'); |
| 45 | +const combinedData = bioData.concat(medData); |
| 46 | +const combinedOptions = { |
| 47 | + output: COMBINED_FILE |
| 48 | +}; |
| 49 | +await sendOutput(combinedData, combinedOptions); |
| 50 | + |
| 51 | +// Search for the QUERY keyword in all the downloaded articles & compile the related articles |
| 52 | +const QUERY = 'alzheimer'; |
| 53 | +fs.open(OUTPUT_FILE, 'w'); |
| 54 | +const outputOptions = { |
| 55 | + input: COMBINED_FILE, |
| 56 | + output: OUTPUT_FILE |
| 57 | +}; |
| 58 | +console.log(`Searching for ${QUERY}`); |
| 59 | +const searchHits = await search(QUERY, outputOptions); |
| 60 | +const numSearchHits = searchHits.length; |
| 61 | +console.log(`Found ${numSearchHits} hits`); |
0 commit comments