Adds support for final journal downloads
Diff
src/fetch.cr | 5 +++++
src/infoparser.cr | 4 ++++
src/issue.cr | 30 +++++++++++++++++++++++++++---
src/journal.cr | 8 ++++++--
src/muse-dl.cr | 26 ++++++++++++++++++++------
src/parser.cr | 4 ++++
6 files changed, 55 insertions(+), 22 deletions(-)
@@ -27,6 +27,11 @@
File.delete(fns) if File.exists?(fns)
end
def self.cleanup_articles(tmp_path : String, id : String)
fns = article_file_name(id, tmp_path)
File.delete(fns) if File.exists?(fns)
end
def self.save_url(url : String, referer : String, file_name : String, tmp_path : String, cookie : String | Nil = nil, bookmark_title : String | Nil = nil, strip_first_page = true)
tmp_pdf_file = "#{file_name}.tmp"
if File.exists? file_name
@@ -42,6 +42,10 @@
end
end
def self.journal_title(myhtml : Myhtml::Parser)
myhtml.css("#journal_about_info .title").map(&.inner_text).to_a[0].strip
end
def self.author(myhtml : Myhtml::Parser)
myhtml.css("#book_about_info .author").map(&.inner_text).to_a[0].strip.gsub("<BR>", ", ").gsub("\n", " ")
end
@@ -16,6 +16,8 @@
date : String | Nil,
journal_title : String | Nil
setter :journal_title
def initialize(id : String, response : String | Nil = nil)
@id = id
@url = "https://muse.jhu.edu/issue/#{id}"
@@ -29,6 +31,11 @@
return @info["Open Access"] == "Yes"
end
false
end
def parse
html = Crest.get(@url).to_s
parse(html)
end
def parse(html : String)
@@ -47,25 +54,30 @@
@volume = /Volume (\d+)/.match(t).try &.[1]
@number = /Number (\d+)/.match(t).try &.[1]
@number = /Issue (\d+)/.match(t).try &.[1] unless @number
@date = /((January|February|March|April|May|June|July|August|September|October|November|December) (\d+))/.match(t).try &.[1]
@date = /((January|February|March|April|May|June|July|August|September|October|November|December|Sring|Winter|Fall|Summer) (\d+))/.match(t).try &.[1]
@date = /(\d{4})/.match(t).try &.[1] unless @date
end
end
def parse_contents(myhtml : Myhtml::Parser)
journal_title_a = myhtml.css("#journal_banner_title a").first
if journal_title_a
@journal_title = journal_title_a.inner_text
unless @journal_title
journal_title_a = myhtml.css("#journal_banner_title a").first
if journal_title_a
@journal_title = journal_title_a.inner_text
end
end
myhtml.css(".articles_list_text ol").each do |ol|
link = ol.css("li.title a").first
title = link.inner_text
pages = ol.css("li.pg").first.try &.inner_text
matches = /(\d+)-(\d+)/.match pages
if matches
start_page = matches[1].to_i
end_page = matches[2].to_i
pages = ol.css("li.pg")
if pages.size > 0
p = pages.first.try &.inner_text
matches = /(\d+)-(\d+)/.match p
if matches
start_page = matches[1].to_i
end_page = matches[2].to_i
end
end
ol.css("a").each do |l|
@@ -1,13 +1,14 @@
require "./infoparser.cr"
require "./issue.cr"
module Muse::Dl
class Journal
getter :info, :summary, :publisher, :issues
getter :info, :summary, :publisher, :issues, :title
@info = Hash(String, String).new
@summary : String
@publisher : String
@issues = [] of Muse::Dl::Issue
@title : String
private getter :h
@@ -16,6 +17,7 @@
@info = InfoParser.infobox(h)
@summary = InfoParser.summary(h)
@publisher = InfoParser.journal_publisher(h)
@title = InfoParser.journal_title(h)
parse_volumes(h)
end
@@ -32,7 +34,9 @@
matches = /\/issue\/(\d+)/.match link
if matches
@issues.push Muse::Dl::Issue.new matches[1]
issue = Muse::Dl::Issue.new matches[1]
issue.journal_title = @title
@issues.push issue
end
end
end
@@ -12,6 +12,7 @@
class Main
def self.dl(parser : Parser)
url = parser.url
puts "Downloading #{url}"
thing = Fetch.get_info(url) if url
return unless thing
@@ -78,7 +79,7 @@
FileUtils.rm source if parser.cleanup
elsif thing.is_a? Muse::Dl::Issue
parser.output = Util.slug_filename "#{thing.journal_title} - #{thing.title}.pdf"
parser.force_set_output Util.slug_filename "#{thing.journal_title} - #{thing.title}.pdf"
if File.exists?(parser.output) && parser.clobber == false
@@ -88,7 +89,6 @@
temp_stitched_file = nil
pdf_builder = Pdftk.new(parser.tmp)
thing.articles.each do |article|
begin
Fetch.save_article(parser.tmp, article.id, parser.cookie, article.title, parser.strip_first)
@@ -101,21 +101,25 @@
temp_stitched_file = pdf_builder.stitch_articles article_ids
pdf_builder.add_metadata(temp_stitched_file, parser.output, thing)
puts "--dont-strip-first-page was on. Please validate PDF file for any errors." unless parser.strip_first
puts "DL: #{url}. Saved final output to #{parser.output}"
if parser.cleanup
thing.articles.each do |a|
Fetch.cleanup_articles(parser.tmp, a.id)
end
end
elsif thing.is_a? Muse::Dl::Journal
thing.issues.each do |issue|
issue.parse
parser.url = issue.url
Main.dl parser
end
end
end
@@ -26,6 +26,10 @@
@output = output_file unless @output != DEFAULT_FILE_NAME
end
def force_set_output(output_file : String)
@output = output_file
end
def reset_output_file
@output = DEFAULT_FILE_NAME
end