forked from alshedivat/al-folio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinspirehep-citations.rb
57 lines (44 loc) · 1.7 KB
/
inspirehep-citations.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
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
52
53
54
55
56
57
require "active_support/all"
require 'net/http'
require 'json'
require 'uri'
module Helpers
extend ActiveSupport::NumberHelper
end
module Jekyll
class InspireHEPCitationsTag < Liquid::Tag
Citations = { }
def initialize(tag_name, params, tokens)
super
@recid = params.strip
end
def render(context)
recid = context[@recid.strip]
api_url = "https://inspirehep.net/api/literature/?fields=citation_count&q=recid:#{recid}"
begin
# If the citation count has already been fetched, return it
if InspireHEPCitationsTag::Citations[recid]
return InspireHEPCitationsTag::Citations[recid]
end
# Fetch the citation count from the API
uri = URI(api_url)
response = Net::HTTP.get(uri)
data = JSON.parse(response)
# # Log the response for debugging
# puts "API Response: #{data.inspect}"
# Extract citation count from the JSON data
citation_count = data["hits"]["hits"][0]["metadata"]["citation_count"].to_i
# Format the citation count for readability
citation_count = Helpers.number_to_human(citation_count, format: '%n%u', precision: 2, units: { thousand: 'K', million: 'M', billion: 'B' })
rescue Exception => e
# Handle any errors that may occur during fetching
citation_count = "N/A"
# Print the error message including the exception class and message
puts "Error fetching citation count for #{recid}: #{e.class} - #{e.message}"
end
InspireHEPCitationsTag::Citations[recid] = citation_count
return "#{citation_count}"
end
end
end
Liquid::Template.register_tag('inspirehep_citations', Jekyll::InspireHEPCitationsTag)