Skip to content

Commit

Permalink
feat: code optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
app-harry committed Aug 11, 2024
1 parent 85d3b9a commit 25376d1
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 49 deletions.
2 changes: 1 addition & 1 deletion ZMediumToMarkdown.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Gem::Specification.new do |gem|
gem.files = Dir['lib/**/*.*']
gem.executables = ['ZMediumToMarkdown']
gem.name = 'ZMediumToMarkdown'
gem.version = '2.4.0'
gem.version = '2.4.1'

gem.license = "MIT"

Expand Down
95 changes: 60 additions & 35 deletions bin/ZMediumToMarkdown
Original file line number Diff line number Diff line change
Expand Up @@ -16,73 +16,98 @@ class Main
fetcher = ZMediumFetcher.new
ARGV << '-h' if ARGV.empty?

options = {}
filePath = ENV['PWD'] || ::Dir.pwd

OptionParser.new do |opts|
opts.banner = "Usage: ZMediumFetcher [options]"

opts.on('-s', '--cookie_sid COOKIESID', 'Your logged-in Medium cookie sid value') do |cookie_sid|
$cookie_sid = cookie_sid
puts $cookie_sid
end

opts.on('-d', '--cookie_uid COOKIEUID', 'Your logged-in Medium cookie uid value') do |cookie_uid|
$cookie_uid = cookie_uid
end

opts.on('-u', '--username USERNAME', 'Downloading all posts from user') do |username|
outputFilePath = PathPolicy.new("#{filePath}/Output", "Output")
fetcher.downloadPostsByUsername(username, outputFilePath)

Helper.printNewVersionMessageIfExists()
options[:u] = username
end

opts.on('-p', '--postURL POST_URL', 'Downloading single post') do |postURL|
outputFilePath = PathPolicy.new("#{filePath}/Output", "Output")
fetcher.downloadPost(postURL, outputFilePath, nil)

Helper.printNewVersionMessageIfExists()
options[:p] = postURL
end

opts.on('-j', '--jekyllUsername USERNAME', 'Downloading all posts from user with Jekyll friendly') do |username|
outputFilePath = PathPolicy.new(filePath, "")
fetcher.isForJekyll = true
fetcher.downloadPostsByUsername(username, outputFilePath)

Helper.printNewVersionMessageIfExists()
options[:j] = username
end

opts.on('-k', '--jekyllPostURL POST_URL', 'Downloading single post with Jekyll friendly') do |postURL|
outputFilePath = PathPolicy.new(filePath, "")
fetcher.isForJekyll = true
fetcher.downloadPost(postURL, outputFilePath, nil)

Helper.printNewVersionMessageIfExists()
options[:k] = postURL
end

opts.on('-n', '--new', 'Update to latest version') do
if Helper.getRemoteVersionFromGithub() > Helper.getLocalVersion()
Helper.downloadLatestVersion()
else
puts "You're using the latest version :)"
end
options[:n] = true
end

opts.on('-c', '--clean', 'Remove all downloaded posts data') do
outputFilePath = PathPolicy.new(filePath, "")
FileUtils.rm_rf(Dir[outputFilePath.getAbsolutePath(nil)])
puts "All downloaded posts data has been removed."

Helper.printNewVersionMessageIfExists()
options[:c] = true
end

opts.on('-v', '--version', 'Print current ZMediumToMarkdown Version & Output Path') do
puts "Version:#{Helper.getLocalVersion().to_s}"

Helper.printNewVersionMessageIfExists()
options[:v] = true
end

end.parse!

#
if !options[:v].nil? && options[:v] == true
puts "Version:#{Helper.getLocalVersion().to_s}"

Helper.printNewVersionMessageIfExists()
elsif !options[:c].nil? && options[:c] == true
outputFilePath = PathPolicy.new(filePath, "")
FileUtils.rm_rf(Dir[outputFilePath.getAbsolutePath(nil)])
puts "All downloaded posts data has been removed."

Helper.printNewVersionMessageIfExists()
elsif !options[:n].nil? && options[:n] == true
if Helper.getRemoteVersionFromGithub() > Helper.getLocalVersion()
Helper.downloadLatestVersion()
else
puts "You're using the latest version :)"
end
elsif !options[:k].nil?
postURL = options[:k]

outputFilePath = PathPolicy.new(filePath, "")
fetcher.isForJekyll = true
fetcher.downloadPost(postURL, outputFilePath, nil)

Helper.printNewVersionMessageIfExists()
elsif !options[:j].nil?
username = options[:j]

outputFilePath = PathPolicy.new(filePath, "")
fetcher.isForJekyll = true
fetcher.downloadPostsByUsername(username, outputFilePath)

Helper.printNewVersionMessageIfExists()
elsif !options[:p].nil?
postURL = options[:p]

outputFilePath = PathPolicy.new("#{filePath}/Output", "Output")
fetcher.downloadPost(postURL, outputFilePath, nil)

Helper.printNewVersionMessageIfExists()
elsif !options[:u].nil?
username = options[:u]

outputFilePath = PathPolicy.new("#{filePath}/Output", "Output")
fetcher.downloadPostsByUsername(username, outputFilePath)

Helper.printNewVersionMessageIfExists()
end

end
end

Expand Down
18 changes: 17 additions & 1 deletion lib/Helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def self.downloadLatestVersion()
end
end

def self.createPostInfo(postInfo, isPin, isForJekyll)
def self.createPostInfo(postInfo, isPin, isLockedPreviewOnly, isForJekyll)
title = postInfo.title&.gsub("[","")
title = title&.gsub("]","")

Expand All @@ -123,6 +123,9 @@ def self.createPostInfo(postInfo, isPin, isForJekyll)
if !isPin.nil? && isPin == true
result += "pin: true\r\n"
end
if !isLockedPreviewOnly.nil? && isLockedPreviewOnly == true
result += "lockedPreviewOnly: true\r\n"
end

if isForJekyll
result += "render_with_liquid: false\n"
Expand Down Expand Up @@ -211,4 +214,17 @@ def self.createWatermark(postURL, isForJekyll)

text
end

def self.createViewFullPost(postURL, isForJekyll)
jekyllOpen = ""
if isForJekyll
jekyllOpen = "{:target=\"_blank\"}"
end

text = "\r\n\r\n\r\n"
text += "**This [post](#{postURL})#{jekyllOpen} is behind Medium's paywall, View the full [post](#{postURL})#{jekyllOpen} on Medium, converted by [ZMediumToMarkdown](https://github.com/ZhgChgLi/ZMediumToMarkdown)#{jekyllOpen}.**"
text += "\r\n"

text
end
end
39 changes: 27 additions & 12 deletions lib/ZMediumFetcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,6 @@ def downloadPost(postURL, pathPolicy, isPin)

isLockedPreviewOnly = contentInfo&.dig("isLockedPreviewOnly")

if isLockedPreviewOnly
puts "Skip: This post is listed in Medium's paywall. You need to provide Medium Member vaild logged-in cookies to access it (refer to Readme.md). PostURL: #{postURL}"
return
end

sourceParagraphs = contentInfo&.dig("bodyModel", "paragraphs")

if sourceParagraphs.nil?
Expand Down Expand Up @@ -249,6 +244,7 @@ def downloadPost(postURL, pathPolicy, isPin)

fileLatestPublishedAt = nil
filePin = false
fileLockedPreviewOnly = false
if File.file?(absolutePath)
lines = File.foreach(absolutePath).first(15)
if lines.first&.start_with?("---")
Expand All @@ -261,10 +257,15 @@ def downloadPost(postURL, pathPolicy, isPin)
if !pinLine.nil?
filePin = pinLine[/^(pin:)\s+(\S*)/, 2].downcase == "true"
end

lockedPreviewOnlyLine = lines.select { |line| line.start_with?("lockedPreviewOnly:") }.first
if !lockedPreviewOnlyLine.nil?
fileLockedPreviewOnly = lockedPreviewOnlyLine[/^(lockedPreviewOnly:)\s+(\S*)/, 2].downcase == "true"
end
end
end

if (!fileLatestPublishedAt.nil? && fileLatestPublishedAt >= postInfo.latestPublishedAt.to_i) && (!isPin.nil? && isPin == filePin)
if (!fileLatestPublishedAt.nil? && fileLatestPublishedAt >= postInfo.latestPublishedAt.to_i) && (!isPin.nil? && isPin == filePin) && (!isLockedPreviewOnly.nil? && isLockedPreviewOnly == fileLockedPreviewOnly)
# Already downloaded and nothing has changed!, Skip!
progress.currentPostParagraphIndex = paragraphs.length
progress.message = "Skip, Post already downloaded and nothing has changed!"
Expand All @@ -273,7 +274,7 @@ def downloadPost(postURL, pathPolicy, isPin)
Helper.createDirIfNotExist(postPathPolicy.getAbsolutePath(nil))
File.open(absolutePath, "w+") do |file|
# write postInfo into top
postMetaInfo = Helper.createPostInfo(postInfo, isPin, isForJekyll)
postMetaInfo = Helper.createPostInfo(postInfo, isPin, isLockedPreviewOnly, isForJekyll)
if !postMetaInfo.nil?
file.puts(postMetaInfo)
end
Expand All @@ -296,15 +297,29 @@ def downloadPost(postURL, pathPolicy, isPin)
progress.message = "Converting Post..."
progress.printLog()
end

postWatermark = Helper.createWatermark(postURL, isForJekyll)
if !postWatermark.nil?
file.puts(postWatermark)

if isLockedPreviewOnly
viewFullPost = Helper.createViewFullPost(postURL, isForJekyll)
if !viewFullPost.nil?
file.puts(viewFullPost)
end
else
postWatermark = Helper.createWatermark(postURL, isForJekyll)
if !postWatermark.nil?
file.puts(postWatermark)
end
end


end
FileUtils.touch absolutePath, :mtime => postInfo.latestPublishedAt

progress.message = "Post Successfully Downloaded!"
if isLockedPreviewOnly
progress.message = "This post is behind Medium's paywall. You need to provide valid Medium Member login cookies to download the full post."
else
progress.message = "Post Successfully Downloaded!"
end

progress.printLog()
end

Expand Down

0 comments on commit 25376d1

Please sign in to comment.