Skip to content

Commit

Permalink
Move image downloading to FiMFic class
Browse files Browse the repository at this point in the history
AO3 ships images in the epub, so no downloading is required there.
  • Loading branch information
creideiki committed Dec 30, 2023
1 parent 1ce6868 commit 839c668
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 30 deletions.
31 changes: 31 additions & 0 deletions lib/fimfic2pdf/fimfic/transformer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,37 @@ def visit_body(node, file)

node.children[3..].map { |c| visit(c, file) }
end

def download_image(url)
# We need a supported file name extension, or \includegraphics{}
# doesn't work. But it doesn't care if the data format matches
# the extension, and we don't know the data format until we've
# asked the server, which we want to avoid if possible. So just
# call everything .png.

# Also, FiMFiction runs all external references through a URL
# proxy, making it very difficult to determine the actual file
# name. Just use the hex MD5 of the URL and hope there are no
# collisions.
filename = 'img-' + Digest::MD5.hexdigest(url.split('/')[-1]) + '.png' # rubocop:disable Style/StringConcatenation
path = @options.id + File::SEPARATOR + filename

if File.exist? path
@logger.debug "Image #{url} already downloaded"
else
@logger.info "Fetching image from #{url}"
response = HTTParty.get(url,
headers: {
'User-Agent' => "FiMFic2PDF/#{FiMFic2PDF::VERSION}"
})
raise "Failed to download image #{url}: #{response.code} #{response.message}" unless response.code == 200

@logger.debug "Saving image as: #{path}"
File.binwrite(path, response.body)
end

filename
end
end
end
end
32 changes: 2 additions & 30 deletions lib/fimfic2pdf/transformer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -405,36 +405,8 @@ def visit_div(node, file)
end
# rubocop:enable Style/StringConcatenation

def download_image(url)
# We need a supported file name extension, or \includegraphics{}
# doesn't work. But it doesn't care if the data format matches
# the extension, and we don't know the data format until we've
# asked the server, which we want to avoid if possible. So just
# call everything .png.

# Also, FiMFiction runs all external references through a URL
# proxy, making it very difficult to determine the actual file
# name. Just use the hex MD5 of the URL and hope there are no
# collisions.
filename = 'img-' + Digest::MD5.hexdigest(url.split('/')[-1]) + '.png' # rubocop:disable Style/StringConcatenation
path = @options.id + File::SEPARATOR + filename

if File.exist? path
@logger.debug "Image #{url} already downloaded"
else
@logger.info "Fetching image from #{url}"
response = HTTParty.get(url,
headers: {
'User-Agent' => "FiMFic2PDF/#{FiMFic2PDF::VERSION}"
})
raise "Failed to download image #{url}: #{response.code} #{response.message}" unless response.code == 200

@logger.debug "Saving image as: #{path}"
File.binwrite(path, response.body)
end

filename
end
# Download an image and return the saved file name.
# def download_image(url) end

def visit_sub(node, file)
file.write '\textsubscript{'
Expand Down

0 comments on commit 839c668

Please sign in to comment.