Class: BibSync::Actions::FetchFromArXiv

Inherits:
Object
  • Object
show all
Includes:
Log, Utils
Defined in:
lib/bibsync/actions/fetch_from_arxiv.rb

Constant Summary collapse

SliceSize =
20

Constants included from Log

Log::Blue, Log::Level, Log::Red, Log::Reset, Log::Yellow

Instance Method Summary collapse

Methods included from Utils

#arxiv_download, #arxiv_id, #fetch, #fetch_xml, #name_without_ext

Methods included from Log

#log

Constructor Details

#initialize(options) ⇒ FetchFromArXiv

Returns a new instance of FetchFromArXiv.



9
10
11
12
# File 'lib/bibsync/actions/fetch_from_arxiv.rb', line 9

def initialize(options)
  raise 'Option --fetch is required' unless @fetch = options[:fetch]
  raise 'Option --dir is required' unless @dir = options[:dir]
end

Instance Method Details

#runObject



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
# File 'lib/bibsync/actions/fetch_from_arxiv.rb', line 14

def run
  arxivs = []
  urls = []

  @fetch.each do |url|
    if url =~ /\A(\d+\.\d+)(v\d+)?\Z/
      arxivs << $1
    elsif url =~ %r{\Ahttp://arxiv.org/abs/(\d+\.\d+)(v\d+)?\Z}
      arxivs << $1
    else
      urls << url
    end
  end

  unless urls.empty?
    notice 'Starting browser for non-arXiv urls'
    urls.each do |url|
      info "Opening #{url}"
      `xdg-open #{Shellwords.escape url}`
    end
  end

  unless arxivs.empty?
    notice 'Downloading from arXiv'
    arxivs.each_slice(SliceSize) do |ids|
      begin
        xml = fetch_xml('http://export.arxiv.org/api/query', id_list: ids.join(','), max_results: SliceSize)
        xml.each_element('//entry/id') do |id|
          id = id.text.gsub('http://arxiv.org/abs/', '')
          info 'arXiv download', key: id
          arxiv_download(@dir, id)
        end
      rescue => ex
        error('arXiv query failed', ex: ex)
      end
    end
  end
end