bibtex-search
Advanced tools
Comparing version 0.1.3 to 0.2.0
117
index.js
@@ -1,2 +0,1 @@ | ||
#!/usr/bin/env node | ||
'use strict'; | ||
@@ -7,16 +6,8 @@ | ||
const cheerio = require('cheerio'); | ||
const meow = require('meow'); | ||
const inquirer = require('inquirer'); | ||
const ora = require('ora'); | ||
const clipboardy = require('clipboardy'); | ||
const chalk = require('chalk'); | ||
const MAX_ARTICLES = 10; | ||
const ACM_SEARCH_URL = 'https://dl.acm.org/results.cfm'; | ||
const ACM_REFERENCE_URL = 'https://dl.acm.org/exportformats.cfm'; | ||
const SCHOLAR_SEARCH_URL = 'https://scholar.google.com/scholar'; | ||
/** | ||
* Searches ACM for the given query, returning an array of articles. | ||
*/ | ||
async function search(query) { | ||
async function searchAcm(query) { | ||
const res = await got(ACM_SEARCH_URL, { query: { query } }); | ||
@@ -46,6 +37,22 @@ const selector = cheerio.load(res.body); | ||
/** | ||
* Retrieves the BibTeX reference for a given ACM ID. | ||
*/ | ||
async function retrieveReference(id) { | ||
async function searchScholar(query) { | ||
const res = await got(SCHOLAR_SEARCH_URL, { | ||
query: { | ||
q: query, | ||
hl: 'en' | ||
} | ||
}); | ||
const selector = cheerio.load(res.body); | ||
const detailsSelector = selector('.gs_r.gs_or.gs_scl'); | ||
return detailsSelector.toArray().map(article => { | ||
const articleSelector = selector(article); | ||
const id = articleSelector.data('cid'); | ||
const title = articleSelector.find('.gs_rt > a').text(); | ||
const authors = articleSelector.find('.gs_a').text(); | ||
return { id, title, authors }; | ||
}); | ||
} | ||
async function retrieveAcm(id) { | ||
const query = { | ||
@@ -61,63 +68,35 @@ id, | ||
const cli = meow(` | ||
Searches for BibTeX references. | ||
async function retrieveScholar(id) { | ||
const query = { | ||
q: `info:${id}:scholar.google.com/`, | ||
output: 'cite' | ||
}; | ||
Usage: | ||
$ bibtex-search <query> | ||
`); | ||
function buildQuestions(articles) { | ||
const choices = articles.map(({ id, title, authors }, i) => ({ | ||
value: id, | ||
name: `${title} ${chalk.dim(`(${authors})`)}` | ||
})); | ||
return [ | ||
{ | ||
choices, | ||
pageSize: Infinity, | ||
type: 'list', | ||
name: 'article', | ||
message: 'Which article are you looking for?' | ||
} | ||
]; | ||
const res = await got(SCHOLAR_SEARCH_URL, { query }); | ||
const selector = cheerio.load(res.body); | ||
const url = selector(`a.gs_citi:contains(BibTeX)`).attr('href'); | ||
const refRes = await got(url); | ||
return refRes.body; | ||
} | ||
async function main() { | ||
const query = cli.input.join(' '); | ||
if (!query) cli.showHelp(); | ||
const spinner = ora(`Searching for '${query}'`).start(); | ||
let articles; | ||
try { | ||
articles = await search(query); | ||
articles = articles.slice(0, MAX_ARTICLES); | ||
spinner.stop(); | ||
} catch (e) { | ||
spinner.fail(`Something went wrong while searching: ${e}`); | ||
process.exit(1); | ||
/** | ||
* Retrieves the BibTeX reference for a given source and id. | ||
*/ | ||
exports.retrieve = async function retrieve(source, id) { | ||
if (source === 'google') { | ||
return retrieveScholar(id); | ||
} | ||
if (!articles.length) { | ||
spinner.info(`No results found for query '${query}'.`); | ||
process.exit(0); | ||
} | ||
return retrieveAcm(id); | ||
}; | ||
const questions = buildQuestions(articles); | ||
const { article } = await inquirer.prompt(questions); | ||
spinner.start('Retrieving BibTeX reference'); | ||
let reference; | ||
try { | ||
reference = await retrieveReference(article); | ||
} catch (e) { | ||
spinner.fail(`Something went wrong while retrieving reference: ${e}`); | ||
process.exit(1); | ||
/** | ||
* Searches the given source for a list of articles. | ||
*/ | ||
exports.search = async function search(source, query) { | ||
if (source === 'google') { | ||
return searchScholar(query); | ||
} | ||
spinner.stop(); | ||
console.log(reference); | ||
clipboardy.writeSync(reference); | ||
spinner.succeed('Copied to clipboard!'); | ||
} | ||
main(); | ||
return searchAcm(query); | ||
}; |
{ | ||
"name": "bibtex-search", | ||
"version": "0.1.3", | ||
"version": "0.2.0", | ||
"description": "Search for BibTeX references", | ||
"main": "index.js", | ||
"bin": "index.js", | ||
"bin": "cli.js", | ||
"repository": "https://github.com/ekmartin/bibtex-search", | ||
@@ -11,3 +11,3 @@ "author": "Martin Ek <mail@ekmartin.com>", | ||
"scripts": { | ||
"start": "node index.js", | ||
"start": "node cli.js", | ||
"test": "yarn lint", | ||
@@ -14,0 +14,0 @@ "lint": "yarn lint:prettier && yarn lint:eslint", |
@@ -7,6 +7,7 @@ # bibtex-search [](https://travis-ci.org/ekmartin/bibtex-search) | ||
Currently uses [ACM's Digital Library](https://dl.acm.org/) to find papers, but | ||
might support other sources in the future. | ||
Currently uses [ACM's Digital Library](https://dl.acm.org/) and | ||
[Google Scholar](https://scholar.google.com/) to find papers, but might support | ||
other sources in the future. | ||
## Installation and usage | ||
## Installation | ||
@@ -17,3 +18,15 @@ bibtex-search needs at least version 7.6 of Node.js installed. | ||
$ npm install --global bibtex-search | ||
``` | ||
## Usage | ||
```bash | ||
$ bibtex-search <query> | ||
Options: | ||
--source, -s Where to find papers from - valid options: [google, acm] | ||
Examples: | ||
$ bibtex-search bayou | ||
$ bibtex-search --source google zaharia spark | ||
``` |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
81424
11
168
31