Skip to content

Instantly share code, notes, and snippets.

@yymao
Last active November 4, 2019 12:22
Show Gist options
  • Save yymao/c4547a2c433550231e1e to your computer and use it in GitHub Desktop.
Save yymao/c4547a2c433550231e1e to your computer and use it in GitHub Desktop.
astro portal: search for arXiv, DOI, ADS bibcode, and first author + year, without typing syntax keywords!
<?php
if(isset($_GET['q'])){
$url_base = 'https://ui.adsabs.harvard.edu/#search/q=';
#doi
$re = '/\b10\.\d{4,}(?:\.\d+)*\/(?:(?![\'"&<>])\S)+/';
if (preg_match($re, $_GET['q'], $matches)){
if (isset($_GET['rd'])){
$url = $url_base . 'doi%3A' . urlencode($matches[0]);
}
else {
$url = 'https://dx.doi.org/' . $matches[0];
}
header("Location: $url"); die();
}
#ads bibcode
$re = '/\b\d{4}\D\S{13}[A-Z.:]/';
if (preg_match($re, $_GET['q'], $matches)){
$url = 'https://ui.adsabs.harvard.edu/#abs/' . $matches[0] . '/abstract';
header("Location: $url"); die();
}
#arxiv
$re = '/\b(?:\d{4}\.\d{4,5}|[a-z-]+(?:\.[A-Za-z-]+)?\/\d{7})/';
if (preg_match($re, $_GET['q'], $matches)){
if (isset($_GET['rd'])){
$url = $url_base . 'arxiv%3A' . urlencode($matches[0]);
}
else {
$url = 'https://arxiv.org/abs/' . $matches[0];
}
header("Location: $url"); die();
}
#first author and year
$re = '/(?:\D|^)(\d{4}(?:-\d{4})?)(?=\D|$)/';
if (preg_match($re, $_GET['q'], $matches)){ //first check year
$year = $matches[1];
$q = str_replace(' et al', ':', strtolower($_GET['q']));
$re = "/(?:[^a-z]|^)([a-z-'\h]+(?=[^a-z\h]|$)(?:,\h*[a-z])?)/";
if (preg_match($re, $q, $matches)){
$url = "${url_base}author%3A%22%5E${matches[1]}%22+year%3A$year&db_f=astronomy&sort=classic_factor%20desc%2C%20bibcode%20desc";
header("Location: $url"); die();
}
}
$url = $url_base . $_GET['q'];
header("Location: $url"); die();
}
?>
<!DOCTYPE html>
<html>
<head>
<title>astro portal</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://unpkg.com/purecss@1.0.0/build/pure-min.css">
<style>
div#main{
padding: 1em;
max-width: 600px;
margin-left: auto;
margin-right: auto;
}
input#q{
width: 100%;
}
.footer{
font-size: small;
margin-top: 42px;
}
</style>
</head>
<script>
function fill(q) {
document.getElementById('q').value = q;
}
</script>
<body>
<div id="main">
<center>
<h1>astro portal</h1>
<form class="pure-form" action="astro-portal.php" method="get">
<fieldset>
<p><input id="q" name="q" required placeholder="arXiv, DOI, ADS bibcode, first author & year, or any ADS 2 query"></p>
<p><button type="submit" class="pure-button pure-button-primary">Search</button></p>
<p><label for="rd" class="pure-checkbox"><input id="rd" name="rd" type="checkbox"> Redirect arXiv and DOI searches to ADS 2.0</label></p>
</fieldset>
</form>
<p class="footer">Simply type in an <a href="javascript:fill('1304.6401');">arXiv ID</a>,
a <a href="javascript:fill('10.1103/PhysRevD.89.063513');">DOI</a>,
an <a href="javascript:fill('2014PhRvD..89f3513M');">ADS bibcode</a>,
or <a href="javascript:fill('Mao, Y et al. (2013)');">a first author and an year</a>.<br>
(Click those links to see an example.) <br><br>
Note that you do <b>not</b> need any syntax keyword.
This means that you can simply copy and paste the citation from the paper you are reading.
There's no need to format your query!!</p>
<p class="footer"><a href="hep-portal.php">Oops! You're a hep person and like INSPIRE more?</a><p>
<p class="footer">By <a href="http://web.stanford.edu/~yymao/">Yao-Yuan Mao</a> (2015). Source code <a href="https://gist.github.com/yymao/c4547a2c433550231e1e">on Gist</a>.</p>
</center>
</div>
</body>
</html>
<?php
if(isset($_GET['q'])){
$url_base = 'https://inspirehep.net/search?p=';
#doi
$re = '/\b10\.\d{4,}(?:\.\d+)*\/(?:(?![\'"&<>])\S)+/';
if (preg_match($re, $_GET['q'], $matches)){
if (isset($_GET['rd'])){
$url = $url_base . 'doi%3A' . urlencode($matches[0]);
}
else {
$url = 'https://dx.doi.org/' . $matches[0];
}
header("Location: $url"); die();
}
#ads bibcode
$re = '/\b\d{4}\D\S{13}[A-Z.:]/';
if (preg_match($re, $_GET['q'], $matches)){
$url = 'https://ui.adsabs.harvard.edu/#abs/' . $matches[0] . '/abstract';
header("Location: $url"); die();
}
#arxiv
$re = '/\b(?:\d{4}\.\d{4,5}|[a-z-]+(?:\.[A-Za-z-]+)?\/\d{7})/';
if (preg_match($re, $_GET['q'], $matches)){
if (isset($_GET['rd'])){
$url = $url_base . (strpos($matches[0], '/')?'eprint+':'arxiv%3A')
. urlencode($matches[0]);
}
else {
$url = 'https://arxiv.org/abs/' . $matches[0];
}
header("Location: $url"); die();
}
#first author and year
$re = '/(?:\D|^)(\d{4}(?:-\d{4})?)(?=\D|$)/';
if (preg_match($re, $_GET['q'], $matches)){ //first check year
$year = str_replace('-', '-%3E', $matches[1]);
$q = str_replace(' et al', ':', strtolower($_GET['q']));
$re = "/(?:[^a-z]|^)([a-z-'\h]+(?=[^a-z\h]|$)(?:,\h*[a-z])?)/";
if (preg_match($re, $q, $matches)){
$url = "${url_base}fa%22${matches[1]}%22+year%3A$year";
header("Location: $url"); die();
}
}
$url = $url_base . $_GET['q'];
header("Location: $url"); die();
}
?>
<!DOCTYPE html>
<html>
<head>
<title>hep portal</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://unpkg.com/purecss@1.0.0/build/pure-min.css">
<style>
div#main{
padding: 1em;
max-width: 600px;
margin-left: auto;
margin-right: auto;
}
input#q{
width: 100%;
}
.footer{
font-size: small;
margin-top: 42px;
}
</style>
</head>
<script>
function fill(q) {
document.getElementById('q').value = q;
}
</script>
<body>
<div id="main">
<center>
<h1>hep portal</h1>
<form class="pure-form" action="hep-portal.php" method="get">
<fieldset>
<p><input id="q" name="q" required placeholder="arXiv, DOI, ADS bibcode, first author & year, or any INSPIRE query"></p>
<p><button type="submit" class="pure-button pure-button-primary">Search</button></p>
<p><label for="rd" class="pure-checkbox"><input id="rd" name="rd" type="checkbox"> Redirect arXiv and DOI searches to INSPIRE</label></p>
</fieldset>
</form>
<p class="footer">Simply type in an <a href="javascript:fill('1304.6401');">arXiv ID</a>,
a <a href="javascript:fill('10.1103/PhysRevD.89.063513');">DOI</a>,
an <a href="javascript:fill('2014PhRvD..89f3513M');">ADS bibcode</a>,
or <a href="javascript:fill('Mao, Y et al. (2013)');">a first author and an year</a>.<br>
(Click those links to see an example.) <br><br>
Note that you do <b>not</b> need any syntax keyword.
This means that you can simply copy and paste the citation from the paper you are reading.
There's no need to format your query!!</p>
<p class="footer"><a href="astro-portal.php">Oops! You're an astro person and like ADS more?</a><p>
<p class="footer">By <a href="http://web.stanford.edu/~yymao/">Yao-Yuan Mao</a> (2015). Source code <a href="https://gist.github.com/yymao/c4547a2c433550231e1e">on Gist</a>.</p>
</center>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment