Skip to content

Commit

Permalink
[Feat] support auto upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
zhgchgli0718 committed May 30, 2022
1 parent c805f30 commit 2b34ab1
Show file tree
Hide file tree
Showing 8 changed files with 229 additions and 52 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
/tmp/
/vendor/
/Output/
/bin/.DS_Store
.DS_Store
.latestRunVersion
latest.zip

# Used by dotenv library to load environment variables.
# .env
Expand Down
3 changes: 2 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
source 'https://rubygems.org'
gem 'net-http', '~> 0.1.0'
gem 'nokogiri', '~> 1.13.1'
gem 'reverse_markdown', '~> 2.1.1'
gem 'reverse_markdown', '~> 2.1.1'
gem 'rubyzip', '~> 2.3.2'
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ GEM
racc (1.6.0)
reverse_markdown (2.1.1)
nokogiri
rubyzip (2.3.2)
timeout (0.2.0)
uri (0.11.0)

Expand All @@ -21,6 +22,7 @@ DEPENDENCIES
net-http (~> 0.1.0)
nokogiri (~> 1.13.1)
reverse_markdown (~> 2.1.1)
rubyzip (~> 2.3.2)

BUNDLED WITH
2.3.13
18 changes: 12 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ You can also use [Github Action](https://github.com/features/actions) as the aut
- [X] Auto skip when post has been downloaded and last modification date from Medium doesn't changed (convenient for auto-sync or auto-backup service, to save server's bandwidth and execution time)
- [X] Highly optimized markdown format for Medium

## Example
- [Original post on Medium](https://medium.com/pinkoi-engineering/%E5%AF%A6%E6%88%B0%E7%B4%80%E9%8C%84-4-%E5%80%8B%E5%A0%B4%E6%99%AF-7-%E5%80%8B-design-patterns-78507a8de6a5)
- [Downloaded & Converted Output Result](example/實戰紀錄-4-個場景-7-個-design-patterns-78507a8de6a5.md)

## Setup
### If you are familiar with ruby:
1. make sure you have Ruby in your environment (I use `2.6.5p114`)
Expand All @@ -41,29 +45,31 @@ You can also use [Github Action](https://github.com/features/actions) as the aut
10. use `bundle exec ruby [USAGE Command]` in the furture (USAGE Command write down below)

## Usage
Execute File: `./bin/ZMediumFetcher`
Execute File: `./bin/ZMediumToMarkdown`

### Downloading all posts from any user
```
bundle exec ruby bin/ZMediumFetcher -u [USEERNAME]
bundle exec ruby bin/ZMediumToMarkdown -u [USEERNAME]
```
![image](https://user-images.githubusercontent.com/33706588/170810772-ec7cd618-d208-4fca-9fe5-9ae6ee745951.png)

### Downloading single post
```
bundle exec ruby bin/ZMediumFetcher -p [MEDIUM POST URL]
bundle exec ruby bin/ZMediumToMarkdown -p [MEDIUM POST URL]
```
![image](https://user-images.githubusercontent.com/33706588/170810799-7da207ff-0642-4beb-9b3a-6af11d6e918d.png)

### Update to latest version
```
bundle exec ruby bin/ZMediumToMarkdown -n
```

## Output
### Where can I find the results of the downloaded post?
The default path of the downloaded post will be in the `./Output` directory.
- Downloading all posts from user:`./Ouput/users/[USERNAME]/posts/[POST_PATH_NAME]`
- Downloading single post:`./Ouput/posts/[POST_PATH_NAME]`
- Post's images:`[POST_PATH_NAME]/images/[POST_ID]/[IMAGE_PATH_NAME]`
### Example
- [Original post on Medium](https://medium.com/pinkoi-engineering/%E5%AF%A6%E6%88%B0%E7%B4%80%E9%8C%84-4-%E5%80%8B%E5%A0%B4%E6%99%AF-7-%E5%80%8B-design-patterns-78507a8de6a5)
- [Downloaded & Converted Output Result](example/實戰紀錄-4-個場景-7-個-design-patterns-78507a8de6a5.md)

## Disclaimer
This repository is for research purposes only, the use of this code is your responsibility.
Expand Down
4 changes: 2 additions & 2 deletions ZMediumToMarkdown.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ Gem::Specification.new do |gem|
gem.summary = 'This project can help you to make an auto-sync or auto-backup service from Medium, like auto-sync Medium posts to Jekyll or other static markdown blog engines or auto-backup Medium posts to the Github page.'
gem.homepage = 'https://github.com/ZhgChgLi/ZMediumToMarkdown'
gem.files = Dir['lib/**/*.*']
gem.executables = ['ZMediumFetcher']
gem.executables = ['ZMediumToMarkdown']
gem.name = 'ZMediumToMarkdown'
gem.version = '1.1.0'
gem.version = '1.2.0'

gem.license = "MIT"

Expand Down
59 changes: 59 additions & 0 deletions bin/ZMediumToMarkdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-

$lib = File.expand_path('../lib', File.dirname(__FILE__))
$LOAD_PATH.unshift($lib)

require "ZMediumFetcher"
require "Helper"
require "optparse"

class Main
def initialize
fetcher = ZMediumFetcher.new
ARGV << '-h' if ARGV.empty?
OptionParser.new do |opts|
opts.banner = "Usage: ZMediumFetcher [options]"

opts.on('-uUSERNAME', '--username=USERNAME', 'test') do |username|
Helper.checkNewVersion()

pathPolicy = PathPolicy.new("#{File.expand_path('../', File.dirname(__FILE__))}", "Output")
fetcher.downloadPostsByUsername(username, pathPolicy)
end

opts.on('-pPOST_URL', '--postURL=POST_URL', 'test') do |postURL|
Helper.checkNewVersion()

pathPolicy = PathPolicy.new("#{File.expand_path('../', File.dirname(__FILE__))}", "Output")
fetcher.downloadPost(postURL, pathPolicy)
end

opts.on('-n', '--new', 'Update to latest version') do |postURL|
if Helper.compareVersion(Helper.getRemoteVersionFromGithub(), Helper.getLocalVersionFromGemspec())
Helper.downloadLatestVersion()
else
puts "You're using the latest version :)"
end
end

end.parse!

Helper.logLatestRunVersion()
end
end

begin
puts "#https://github.com/ZhgChgLi/ZMediumToMarkdown"
puts "You have read and agree with the Disclaimer."
Main.new()
puts "Execute Successfully!!!"
puts "#https://github.com/ZhgChgLi/ZMediumToMarkdown"
puts "#Thanks for using this tool."
puts "#If this is helpful, please help to star the repo or recommend it to your friends."
rescue => e
puts "#Error: #{e.class} #{e.message}\n"
puts e.backtrace
puts "#Please feel free to open an Issue or submit a fix/contribution via Pull Request on:\n"
puts "#https://github.com/ZhgChgLi/ZMediumToMarkdown\n"
end
143 changes: 143 additions & 0 deletions lib/Helper.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,30 @@
$lib = File.expand_path('../lib', File.dirname(__FILE__))

require 'fileutils'
require 'date'
require 'PathPolicy'
require 'Post'
require "Request"
require 'json'
require 'open-uri'
require 'zip'

class Helper

class Version
attr_accessor :major, :minor, :patch

def initialize(major, minor, patch)
@major = major.to_i
@minor = minor.to_i
@patch = patch.to_i
end

def to_string()
"#{major.to_s}.#{minor.to_s}.#{patch.to_s}"
end
end

def self.createDirIfNotExist(dirPath)
dirs = dirPath.split("/")
currentDir = ""
Expand All @@ -24,6 +45,45 @@ def self.makeWarningText(message)
puts "####################################################\n"
end

def self.downloadLatestVersion()
apiPath = 'https://api.github.com/repos/ZhgChgLi/ZMediumToMarkdown/releases'
versions = JSON.parse(Request.URL(apiPath).body).sort { |a,b| b["id"] <=> a["id"] }

version = nil
index = 0
while version == nil do
thisVersion = versions[index]
if thisVersion["prerelease"] == false
version = thisVersion
next
end
index += 1
end

zipFilePath = version["zipball_url"]
puts "Downloading latest version from github..."
open('latest.zip', 'wb') do |fo|
fo.print open(zipFilePath).read
end

puts "Unzip..."
Zip::File.open("latest.zip") do |zipfile|
zipfile.each do |file|
fileNames = file.name.split("/")
fileNames.shift
filePath = fileNames.join("/")
if filePath != ''
puts "Unzinp...#{filePath}"
zipfile.extract(file, filePath) { true }
end
end
end
File.delete("latest.zip")

tagName = version["tag_name"]
puts "Update to version #{tagName} successfully!"
end

def self.createPostInfo(postInfo)
result = "---\n"
result += "title: #{postInfo.title}\n"
Expand All @@ -36,6 +96,89 @@ def self.createPostInfo(postInfo)
result
end

def self.checkNewVersion()
if Helper.compareVersion(Helper.getRemoteVersionFromGithub(), Helper.getLocalVersionFromGemspec())
puts "##########################################################"
puts "##### New Version Available!!! #####"
puts "##### Please type `bin/ZMediumToMarkdown -n` to update!!##"
puts "##########################################################"
end
end

def self.getLocalVersionFromGemspec()
rootPath = File.expand_path('../', File.dirname(__FILE__))
gemspecContent = File.read("#{rootPath}/ZMediumToMarkdown.gemspec")
result = gemspecContent[/(gem\.version){1}\s+(\=)\s+(\'){1}(\d+(\.){1}\d+(\.){1}\d+){1}(\'){1}/, 4]

if !result.nil?
versions = result.split(".")
Version.new(versions[0],versions[1],versions[2])
else
nil
end
end

def self.logLatestRunVersion()
version = Helper.getLocalVersionFromGemspec()
rootPath = File.expand_path('../', File.dirname(__FILE__))

File.open("#{rootPath}/.latestRunVersion" , 'w+') do |file|
file.puts(version.to_string())
end
end

def self.getLocalVersionFromFile()
rootPath = File.expand_path('../', File.dirname(__FILE__))
result = File.read("#{rootPath}/.latestRunVersion")

if !result.nil?
versions = result.split(".")
Version.new(versions[0],versions[1],versions[2])
else
nil
end
end

def self.getRemoteVersionFromGithub()
apiPath = 'https://api.github.com/repos/ZhgChgLi/ZMediumToMarkdown/releases'
versions = JSON.parse(Request.URL(apiPath).body).sort { |a,b| b["id"] <=> a["id"] }

tagName = nil
index = 0
while tagName == nil do
thisVersion = versions[index]
if thisVersion["prerelease"] == false
tagName = thisVersion["tag_name"]
next
end
index += 1
end

if !tagName.nil?
versions = (tagName.downcase.gsub! 'v','') .split(".")
Version.new(versions[0],versions[1],versions[2])
else
nil
end
end

def self.compareVersion(version1, version2)
if version1.major > version2.major
true
else
if version1.minor > version2.minor
true
else
if version1.patch > version2.patch
true
else
false
end
end
end
end


def self.createWatermark(postURL)
text = "\r\n\r\n\r\n"
text += "+-----------------------------------------------------------------------------------+"
Expand Down
Loading

0 comments on commit 2b34ab1

Please sign in to comment.