Here is my Python script that will search Gravitational Wave(GW) related papers on arXiv based on the given input inputs(dates) and it will extract title, abstruct, arxiv id and author name; and it will create a txt file containing those information. It can be customized for different fields by changing the URL. Default search url
is:
# Default :
## Search terms - gravitation wave (all fields) or, eccentric signals(all fields)
## search subject - physics - including cross-listed papers
## Search date - <accoding to input> - submission date (most recent)
## Abstruct - show, size - 200
# For any other field:
## Visit `https://arxiv.org/search/advanced` and for the first time,
## edit the search engine with the required terms and subjects and dates then,
## search and copy the url and paste the url below and replace dates in the url as `{}` to format custom dates there. (as below,)
Main Script
#!/path/anaconda3/bin/python
#This script will search extract gw-related paper summery from arxiv based on input dates and make a text from from that data
# By- Anik Mandal
from bs4 import BeautifulSoup # You need to have this library in your conda environment
import requests
import datetime as dt
import numpy as np
import os
print("--------------->>>>>|GW NEWS|<<<<<--------------------------------------------------------------------")
print("(This script will search gw papers on arxiv based on input dates)\n")
print("Write a period type [last_day, last_week] (Press <Enter> for custom date search)")
action = input(">>>")
if action == "last_day":
to_ = dt.date.today()
from_ = to_ - dt.timedelta(days=1)
file_name = "arxiv_gwdaily_{}.txt".format(from_)
elif action == "last_week":
to_ = dt.date.today()
from_ = to_ - dt.timedelta(days=7)
file_name = "arxiv_gwweekly_{}_{}.txt".format(from_, to_)
else:
from_ = input("From date [YYYY-MM-DD] : ").split("-")
to_ = input("To date [YYYY-MM-DD] : ").split("-")
from_ = dt.date(int(from_[0]), int(from_[1]), int(from_[2]))
to_ = dt.date(int(to_[0]), int(to_[1]), int(to_[2]))
file_name = "arxiv_gw_{}_{}.txt".format(from_, to_)
file = open(file_name, "w")
file.write( "--------------->>>>>|GW NEWS|<<<<<--------------------------------------------------------------------\n")
file.write("-----|From : "+str(from_)+"\t To: "+str(to_)+"|--------------------------------------------------------------\n\n\n\n")
#
def gw_news(from_, to_):
url = "https://arxiv.org/search/advanced?advanced=1&terms-0-operator=AND&terms-0-term=gravitational+wave&terms-0-field=all&terms-1-operator=OR&terms-1-term=eccentric+signal+&terms-1-field=all&classification-physics=y&classification-physics_archives=all&classification-include_cross_list=include&date-year=&date-filter_by=date_range&date-from_date={}&date-to_date={}&date-date_type=submitted_date&abstracts=show&size=200&order=-announced_date_first".format(from_, to_)
try:
source = requests.get(url)
source.raise_for_status()
soup = BeautifulSoup(source.text, "html.parser")
papers = soup.find("ol", class_="breathe-horizontal").find_all("li", class_="arxiv-result")
file.write("No. of papers : {}\n".format(len(papers)))
file.write("_______________________________________________________________________________________________________\n")
i = 1
for paper in papers:
title = paper.find("p", class_="title is-5 mathjax").text[8:-10]
abstract = paper.find("p", class_="abstract mathjax").find("span", class_="abstract-full has-text-grey-dark mathjax").text[:-7]
id_ = paper.find("p", class_="list-title is-inline-block").a.text
author = paper.find("p", class_="authors").text[10:].replace("\n","").replace(" ", "")
file.write("#"+str(i)+"."+title+"\n")
file.write(abstract+"\n")
file.write(id_+"\t\t"+author+"\n \n")
file.write("_______________________________________________________________________________________________________\n")
i=i+1
print("Done! Data stored in /arXiv/{}".format(file_name))
except:
print("No new paper during this period!")
os.remove(file_name)
return url
url = gw_news(from_, to_)
file.write("\n\n")
file.write("_______________________________________________________________________________________________________\n")
file.write("Site url : "+url+"\n")
file.write("_______________________________________________________________________________________________________\n")
print("\nSite url : ", url)
But make sure your pc is connected to the internet while the script is running.
Automation using Crontab
This script can be run daily automatically by setting the action
to last_date
and by adding the script to crontab. There is a way you can do it:
Open the crontab window,
$ crontab -e
add a new cronjob and save it.
0 9 * * * /path/arXiv/gwarxiv_search.py
This script will run automatically daily exactly at 9:00 AM and stores the output text file in the respective folder.
Using a Desktop application
For that, you have to write a shell command which will run the script and open the output(most recent) file. Here is the example code,
#!/bin/bash
#-----RUN THE SEARCH SCRIPTS---------------------------------------------------------------------------
source /path/anaconda3/etc/profile.d/conda.sh
conda activate <conda_environment>
# Get the current day of the week (0-6 where Sunday is 0)
day_of_week=$(date +%w)
# Check if it's Sunday (day_of_week equals 0)
if [ "$day_of_week" -eq 0 ]; then
# Execute your desired commands or script here
python /path/arXiv/gw_weekly/gwweekly.py
fi
python /path/arXiv/gw_daily/gwdaily.py
conda deactivate
#-----OPEN THE MOST RECENT FILE------------------------------------------------------------------------
# Define the paths to the folders
folder1="/path/arXiv/gw_daily"
folder2="/path/arXiv/gw_weekly"
# Find the most recent .txt files in each folder
most_recent_file1=$(ls -t "$folder1"/*.txt | head -n 1)
most_recent_file2=$(ls -t "$folder2"/*.txt | head -n 1)
# Open the most recent .txt files in gedit
gedit "$most_recent_file1" "$most_recent_file2"
#-----END----------------------------------------------------------------------------------------------
Make the shell command executable by,
$ chmod +x /path/gwarxiv_search.sh
Then add a new desktop application (/home/Desktop/gwarxiv_search.desktop
), which will run the above shell command(gwarxiv_search.sh
) on click
.
[Desktop Entry]
Name=GW NEWS
Comment=GW NEWS on arXiv
Exec=/path/arXiv/utils/gwarxiv_search.sh
Icon=/path/arXiv/utils/arxiv_400x400.png # add icon of your choice
Terminal=false
Type=Application
Categories=Utility;
Also, make this desktop application executable by,
$ chmod +x /home/Desktop/gwarxiv_search.desktop
and right click on the application to Allow Launching
and it is done!