Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert images links from external to internal #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

snowman
Copy link
Contributor

@snowman snowman commented Jan 12, 2021

Here's the script that converts image links to internal.

#!/usr/bin/env ruby

require 'open-uri'
require 'uri'
require 'pry'

Dir.mkdir('images') unless File.exist?('images')

def transform_url(url)
  url.sub('/optimized/', '/original/').sub(/_\d_\d+x\d+(\.\w+)$/, "\\1")
end

def url_to_filename(url)
  url.split('/')[-1]
end

def download_file(url, filename)
  URI.open(url) do |image|
    p "downloading #{filename}..."
    File.open(filename, 'wb') { |file| file.write(image.read) }
  end
end

def process_line(line)
  urls =
    URI.extract(line).filter { |url| url.include? 'emacs-china.org/uploads' }

  urls.each do |url|
    url = url.sub(/\)$/, "")

    transformed_url = transform_url(url)
    filename = 'images/' + url_to_filename(transformed_url)
    download_file(transformed_url, filename) unless File.exist?(filename)

    p "renaming #{url} to #{filename}"
    line.sub!(url, filename) if File.exist?(filename)
  end

  line
end

Dir['*.md'].each do |filename|
  lines = open(filename).each_line.map {|line| process_line(line) }
  File.open(filename, 'w') { |file| file.write(lines.join) }
end

@condy0919
Copy link
Owner

condy0919 commented Jan 12, 2021

Maybe we can abuse GitHub issues to store images? e.g. condy0919/.emacs.d#37

@snowman
Copy link
Contributor Author

snowman commented Jan 13, 2021

Why have to use external links? It's a slow and bad user experience.

How I read the .md file?

I clone the repos and read it with markdown-mode, and use
imenu and narrow-to-subtree to focus on one section, and
delete the read section later with git commit per day.

To make the images links internal, we can view images locally with
image viewer like https://github.com/muennich/sxiv seamlessly with
zoom in/out, stop frame animation of GIF and manually jump between
frames, etc and without bear network latency.

And also this means integrity, rely on external service is unstable,
and GitHub is slow without using proxies like v2ray.

Use single same repos with internal images, you can deploy them
anywhere you want without limited by Image Hotlinking protection.

I think you have the original images files with good naming instead of
hash-numbering file, so by using external images links, you don't have
to host the images yourself. If you want to automatically rename those
files in markdown file, here is the utility that finds visually
similar or duplicate images (https://github.com/jhnc/findimagedupes).

Anyway, that's my two cents. You can close this pull request and
reopen it and merge it someday later when you think about it again or
not.

Have a good day! :)

@snowman snowman closed this Jan 14, 2021
@condy0919
Copy link
Owner

condy0919 commented Jan 14, 2021

The original images are lost forever...

And also this means integrity, rely on external service is unstable, and
GitHub is slow without using proxies like v2ray.

Yes, it shouldn't reply on external services.

@condy0919 condy0919 self-assigned this Jan 14, 2021
@condy0919 condy0919 reopened this Feb 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants