Class: Jekyll::GoogleScholarCitationsTag
- Inherits:
-
Liquid::Tag
- Object
- Liquid::Tag
- Jekyll::GoogleScholarCitationsTag
- Defined in:
- lib/al_citations/google_scholar.rb
Constant Summary collapse
- Citations =
{ }
- CITED_BY_REGEX =
/Cited by (\d+[,\d]*)/
Instance Method Summary collapse
-
#initialize(tag_name, params, tokens) ⇒ GoogleScholarCitationsTag
constructor
A new instance of GoogleScholarCitationsTag.
- #render(context) ⇒ Object
Constructor Details
#initialize(tag_name, params, tokens) ⇒ GoogleScholarCitationsTag
Returns a new instance of GoogleScholarCitationsTag.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/al_citations/google_scholar.rb', line 10 def initialize(tag_name, params, tokens) super splitted = params.split(" ").map(&:strip) @scholar_id = splitted[0] @article_id = splitted[1] if @scholar_id.nil? || @scholar_id.empty? puts "Invalid scholar_id provided" end if @article_id.nil? || @article_id.empty? puts "Invalid article_id provided" end end |
Instance Method Details
#render(context) ⇒ Object
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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/al_citations/google_scholar.rb', line 25 def render(context) article_id = context[@article_id.strip] scholar_id = context[@scholar_id.strip] article_url = "https://scholar.google.com/citations?view_op=view_citation&hl=en&user=#{scholar_id}&citation_for_view=#{scholar_id}:#{article_id}" begin # If the citation count has already been fetched, return it if GoogleScholarCitationsTag::Citations[article_id] return GoogleScholarCitationsTag::Citations[article_id] end # Sleep for a random amount of time to avoid being blocked sleep(rand(1.5..3.5)) # Fetch the article page doc = Nokogiri::HTML(URI.open(article_url, "User-Agent" => "Ruby/#{RUBY_VERSION}")) # Attempt to extract the "Cited by n" string from the meta tags citation_count = 0 # Look for meta tags with "name" attribute set to "description" = doc.css('meta[name="description"]') = doc.css('meta[property="og:description"]') if !.empty? cited_by_text = [0]['content'] matches = cited_by_text.match(CITED_BY_REGEX) if matches citation_count = matches[1].sub(",", "").to_i end elsif !.empty? cited_by_text = [0]['content'] matches = cited_by_text.match(CITED_BY_REGEX) if matches citation_count = matches[1].sub(",", "").to_i end end 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 #{article_id} in #{article_url}: #{e.class} - #{e.}" end GoogleScholarCitationsTag::Citations[article_id] = citation_count return "#{citation_count}" end |