muse-dl/src/issue.cr

61 lines
1.6 KiB
Crystal
Raw Normal View History

require "./thing.cr"
2020-04-07 20:18:48 +00:00
require "./fetch.cr"
require "./article.cr"
module Muse::Dl
2020-04-07 19:18:36 +00:00
class Issue
@id : String
2020-04-07 20:18:48 +00:00
@title : String | Nil
@articles : Array(Muse::Dl::Article)
@url : String
2020-04-07 20:20:40 +00:00
@info : Hash(String, String)
2020-04-07 20:18:48 +00:00
@summary : String | Nil
@publisher : String | Nil
@volume : String | Nil
@number : String | Nil
@date : String | Nil
2020-06-30 08:38:28 +00:00
@issues : Array(Muse::Dl::Issue)
2020-04-07 19:18:36 +00:00
getter :id, :title, :articles, :url, :summary, :publisher, :info, :volume, :number, :date
2020-04-07 19:18:36 +00:00
def initialize(id : String)
@id = id
2020-04-07 20:18:48 +00:00
@url = "https://muse.jhu.edu/issue/#{id}"
2020-04-07 20:20:40 +00:00
@info = Hash(String, String).new
2020-04-07 20:18:48 +00:00
@articles = [] of Muse::Dl::Article
2020-06-30 08:38:28 +00:00
@issues = [] of Muse::Dl::Issue
2020-04-07 20:18:48 +00:00
end
def parse
html = Crest.get(url).to_s
h = Myhtml::Parser.new html
@info = InfoParser.infobox(h)
2020-04-07 20:22:07 +00:00
@title = InfoParser.issue_title(h)
2020-04-07 20:18:48 +00:00
@summary = InfoParser.summary(h)
@publisher = InfoParser.journal_publisher(h)
parse_title
2020-06-30 08:38:28 +00:00
parse_contents(h)
end
def parse_title
t = @title
unless t.nil?
@volume = /Volume (\d+)/.match(t).try &.[1]
@number = /Number (\d+)/.match(t).try &.[1]
@date = /((January|February|March|April|May|June|July|August|September|October|November|December) (\d+))/.match(t).try &.[1]
end
2020-04-07 19:18:36 +00:00
end
2020-06-30 08:38:28 +00:00
def parse_contents(myhtml : Myhtml::Parser)
myhtml.css("#available_issues_list_text a").each do |a|
link = a.attribute_by("href").to_s
matches = /\/issue\/(\d+)/.match link
if matches
@issues.push Muse::Dl::Issue.new matches[1]
end
end
end
end
end