ZMediumToMarkdown 1.1.0 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/bin/ZMediumToMarkdown +59 -0
- data/lib/Helper.rb +143 -0
- data/{bin/ZMediumFetcher → lib/ZMediumFetcher.rb} +6 -43
- metadata +5 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 837c899a0d017fa83febc0f96e84a89527a517754cbee44f6e91320c7b2cd292
|
4
|
+
data.tar.gz: cb5c49e7a67f99ab888cb0797edaf4c0ce237da3e8c41bfda963395bd606b1cd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f9339415de8183d6c255bf28db437c61e8913c242e5ed3270d070285b1282717187511a974fdc6c0ef391c37a6308bcf3d7f866dd0fa508a7e00fc004d05ca87
|
7
|
+
data.tar.gz: 832198d0d8cf5851aedfa7f69a9193cad42f71f21f06fb29a832957140fdfd6d8e3099429b8085824eae2f19d0d05c6c2615f73d012fa16679fcf49945918862
|
@@ -0,0 +1,59 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# -*- coding: utf-8 -*-
|
3
|
+
|
4
|
+
$lib = File.expand_path('../lib', File.dirname(__FILE__))
|
5
|
+
$LOAD_PATH.unshift($lib)
|
6
|
+
|
7
|
+
require "ZMediumFetcher"
|
8
|
+
require "Helper"
|
9
|
+
require "optparse"
|
10
|
+
|
11
|
+
class Main
|
12
|
+
def initialize
|
13
|
+
fetcher = ZMediumFetcher.new
|
14
|
+
ARGV << '-h' if ARGV.empty?
|
15
|
+
OptionParser.new do |opts|
|
16
|
+
opts.banner = "Usage: ZMediumFetcher [options]"
|
17
|
+
|
18
|
+
opts.on('-uUSERNAME', '--username=USERNAME', 'test') do |username|
|
19
|
+
Helper.checkNewVersion()
|
20
|
+
|
21
|
+
pathPolicy = PathPolicy.new("#{File.expand_path('../', File.dirname(__FILE__))}", "Output")
|
22
|
+
fetcher.downloadPostsByUsername(username, pathPolicy)
|
23
|
+
end
|
24
|
+
|
25
|
+
opts.on('-pPOST_URL', '--postURL=POST_URL', 'test') do |postURL|
|
26
|
+
Helper.checkNewVersion()
|
27
|
+
|
28
|
+
pathPolicy = PathPolicy.new("#{File.expand_path('../', File.dirname(__FILE__))}", "Output")
|
29
|
+
fetcher.downloadPost(postURL, pathPolicy)
|
30
|
+
end
|
31
|
+
|
32
|
+
opts.on('-n', '--new', 'Update to latest version') do |postURL|
|
33
|
+
if Helper.compareVersion(Helper.getRemoteVersionFromGithub(), Helper.getLocalVersionFromGemspec())
|
34
|
+
Helper.downloadLatestVersion()
|
35
|
+
else
|
36
|
+
puts "You're using the latest version :)"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
end.parse!
|
41
|
+
|
42
|
+
Helper.logLatestRunVersion()
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
begin
|
47
|
+
puts "#https://github.com/ZhgChgLi/ZMediumToMarkdown"
|
48
|
+
puts "You have read and agree with the Disclaimer."
|
49
|
+
Main.new()
|
50
|
+
puts "Execute Successfully!!!"
|
51
|
+
puts "#https://github.com/ZhgChgLi/ZMediumToMarkdown"
|
52
|
+
puts "#Thanks for using this tool."
|
53
|
+
puts "#If this is helpful, please help to star the repo or recommend it to your friends."
|
54
|
+
rescue => e
|
55
|
+
puts "#Error: #{e.class} #{e.message}\n"
|
56
|
+
puts e.backtrace
|
57
|
+
puts "#Please feel free to open an Issue or submit a fix/contribution via Pull Request on:\n"
|
58
|
+
puts "#https://github.com/ZhgChgLi/ZMediumToMarkdown\n"
|
59
|
+
end
|
data/lib/Helper.rb
CHANGED
@@ -1,9 +1,30 @@
|
|
1
1
|
$lib = File.expand_path('../lib', File.dirname(__FILE__))
|
2
2
|
|
3
|
+
require 'fileutils'
|
3
4
|
require 'date'
|
5
|
+
require 'PathPolicy'
|
4
6
|
require 'Post'
|
7
|
+
require "Request"
|
8
|
+
require 'json'
|
9
|
+
require 'open-uri'
|
10
|
+
require 'zip'
|
5
11
|
|
6
12
|
class Helper
|
13
|
+
|
14
|
+
class Version
|
15
|
+
attr_accessor :major, :minor, :patch
|
16
|
+
|
17
|
+
def initialize(major, minor, patch)
|
18
|
+
@major = major.to_i
|
19
|
+
@minor = minor.to_i
|
20
|
+
@patch = patch.to_i
|
21
|
+
end
|
22
|
+
|
23
|
+
def to_string()
|
24
|
+
"#{major.to_s}.#{minor.to_s}.#{patch.to_s}"
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
7
28
|
def self.createDirIfNotExist(dirPath)
|
8
29
|
dirs = dirPath.split("/")
|
9
30
|
currentDir = ""
|
@@ -24,6 +45,45 @@ class Helper
|
|
24
45
|
puts "####################################################\n"
|
25
46
|
end
|
26
47
|
|
48
|
+
def self.downloadLatestVersion()
|
49
|
+
apiPath = 'https://api.github.com/repos/ZhgChgLi/ZMediumToMarkdown/releases'
|
50
|
+
versions = JSON.parse(Request.URL(apiPath).body).sort { |a,b| b["id"] <=> a["id"] }
|
51
|
+
|
52
|
+
version = nil
|
53
|
+
index = 0
|
54
|
+
while version == nil do
|
55
|
+
thisVersion = versions[index]
|
56
|
+
if thisVersion["prerelease"] == false
|
57
|
+
version = thisVersion
|
58
|
+
next
|
59
|
+
end
|
60
|
+
index += 1
|
61
|
+
end
|
62
|
+
|
63
|
+
zipFilePath = version["zipball_url"]
|
64
|
+
puts "Downloading latest version from github..."
|
65
|
+
open('latest.zip', 'wb') do |fo|
|
66
|
+
fo.print open(zipFilePath).read
|
67
|
+
end
|
68
|
+
|
69
|
+
puts "Unzip..."
|
70
|
+
Zip::File.open("latest.zip") do |zipfile|
|
71
|
+
zipfile.each do |file|
|
72
|
+
fileNames = file.name.split("/")
|
73
|
+
fileNames.shift
|
74
|
+
filePath = fileNames.join("/")
|
75
|
+
if filePath != ''
|
76
|
+
puts "Unzinp...#{filePath}"
|
77
|
+
zipfile.extract(file, filePath) { true }
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
File.delete("latest.zip")
|
82
|
+
|
83
|
+
tagName = version["tag_name"]
|
84
|
+
puts "Update to version #{tagName} successfully!"
|
85
|
+
end
|
86
|
+
|
27
87
|
def self.createPostInfo(postInfo)
|
28
88
|
result = "---\n"
|
29
89
|
result += "title: #{postInfo.title}\n"
|
@@ -36,6 +96,89 @@ class Helper
|
|
36
96
|
result
|
37
97
|
end
|
38
98
|
|
99
|
+
def self.checkNewVersion()
|
100
|
+
if Helper.compareVersion(Helper.getRemoteVersionFromGithub(), Helper.getLocalVersionFromGemspec())
|
101
|
+
puts "##########################################################"
|
102
|
+
puts "##### New Version Available!!! #####"
|
103
|
+
puts "##### Please type `bin/ZMediumToMarkdown -n` to update!!##"
|
104
|
+
puts "##########################################################"
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
def self.getLocalVersionFromGemspec()
|
109
|
+
rootPath = File.expand_path('../', File.dirname(__FILE__))
|
110
|
+
gemspecContent = File.read("#{rootPath}/ZMediumToMarkdown.gemspec")
|
111
|
+
result = gemspecContent[/(gem\.version){1}\s+(\=)\s+(\'){1}(\d+(\.){1}\d+(\.){1}\d+){1}(\'){1}/, 4]
|
112
|
+
|
113
|
+
if !result.nil?
|
114
|
+
versions = result.split(".")
|
115
|
+
Version.new(versions[0],versions[1],versions[2])
|
116
|
+
else
|
117
|
+
nil
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
def self.logLatestRunVersion()
|
122
|
+
version = Helper.getLocalVersionFromGemspec()
|
123
|
+
rootPath = File.expand_path('../', File.dirname(__FILE__))
|
124
|
+
|
125
|
+
File.open("#{rootPath}/.latestRunVersion" , 'w+') do |file|
|
126
|
+
file.puts(version.to_string())
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
def self.getLocalVersionFromFile()
|
131
|
+
rootPath = File.expand_path('../', File.dirname(__FILE__))
|
132
|
+
result = File.read("#{rootPath}/.latestRunVersion")
|
133
|
+
|
134
|
+
if !result.nil?
|
135
|
+
versions = result.split(".")
|
136
|
+
Version.new(versions[0],versions[1],versions[2])
|
137
|
+
else
|
138
|
+
nil
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
def self.getRemoteVersionFromGithub()
|
143
|
+
apiPath = 'https://api.github.com/repos/ZhgChgLi/ZMediumToMarkdown/releases'
|
144
|
+
versions = JSON.parse(Request.URL(apiPath).body).sort { |a,b| b["id"] <=> a["id"] }
|
145
|
+
|
146
|
+
tagName = nil
|
147
|
+
index = 0
|
148
|
+
while tagName == nil do
|
149
|
+
thisVersion = versions[index]
|
150
|
+
if thisVersion["prerelease"] == false
|
151
|
+
tagName = thisVersion["tag_name"]
|
152
|
+
next
|
153
|
+
end
|
154
|
+
index += 1
|
155
|
+
end
|
156
|
+
|
157
|
+
if !tagName.nil?
|
158
|
+
versions = (tagName.downcase.gsub! 'v','') .split(".")
|
159
|
+
Version.new(versions[0],versions[1],versions[2])
|
160
|
+
else
|
161
|
+
nil
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
def self.compareVersion(version1, version2)
|
166
|
+
if version1.major > version2.major
|
167
|
+
true
|
168
|
+
else
|
169
|
+
if version1.minor > version2.minor
|
170
|
+
true
|
171
|
+
else
|
172
|
+
if version1.patch > version2.patch
|
173
|
+
true
|
174
|
+
else
|
175
|
+
false
|
176
|
+
end
|
177
|
+
end
|
178
|
+
end
|
179
|
+
end
|
180
|
+
|
181
|
+
|
39
182
|
def self.createWatermark(postURL)
|
40
183
|
text = "\r\n\r\n\r\n"
|
41
184
|
text += "+-----------------------------------------------------------------------------------+"
|
@@ -1,13 +1,8 @@
|
|
1
|
-
|
2
|
-
# -*- coding: utf-8 -*-
|
1
|
+
|
3
2
|
|
4
3
|
$lib = File.expand_path('../lib', File.dirname(__FILE__))
|
5
|
-
$LOAD_PATH.unshift($lib)
|
6
4
|
|
7
|
-
require "
|
8
|
-
require 'json'
|
9
|
-
require 'optparse'
|
10
|
-
require 'fileutils'
|
5
|
+
require "fileutils"
|
11
6
|
|
12
7
|
require "Parsers/H1Parser"
|
13
8
|
require "Parsers/H2Parser"
|
@@ -32,26 +27,6 @@ require "Request"
|
|
32
27
|
require "Post"
|
33
28
|
require "User"
|
34
29
|
|
35
|
-
class Main
|
36
|
-
def initialize
|
37
|
-
fetcher = ZMediumFetcher.new
|
38
|
-
ARGV << '-h' if ARGV.empty?
|
39
|
-
OptionParser.new do |opts|
|
40
|
-
opts.banner = "Usage: ZMediumFetcher [options]"
|
41
|
-
|
42
|
-
opts.on('-uUSERNAME', '--username=USERNAME', 'test') do |username|
|
43
|
-
pathPolicy = PathPolicy.new("#{File.expand_path('../', File.dirname(__FILE__))}", "Output")
|
44
|
-
fetcher.downloadPostsByUsername(username, pathPolicy)
|
45
|
-
end
|
46
|
-
|
47
|
-
opts.on('-pPOST_URL', '--postURL=POST_URL', 'test') do |postURL|
|
48
|
-
pathPolicy = PathPolicy.new("#{File.expand_path('../', File.dirname(__FILE__))}", "Output")
|
49
|
-
fetcher.downloadPost(postURL, pathPolicy)
|
50
|
-
end
|
51
|
-
end.parse!
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
30
|
class ZMediumFetcher
|
56
31
|
|
57
32
|
attr_accessor :progress, :linkParser
|
@@ -237,9 +212,12 @@ class ZMediumFetcher
|
|
237
212
|
progress.printLog()
|
238
213
|
|
239
214
|
absolutePath = postPathPolicy.getAbsolutePath("#{postPath}.md")
|
215
|
+
|
216
|
+
# if user latest use's version is < latest local version than force download (don't skip, make sure bug fixed can be influenced)
|
217
|
+
forceDownload = Helper.compareVersion(Helper.getLocalVersionFromGemspec(), Helper.getLocalVersionFromFile())
|
240
218
|
|
241
219
|
# if markdown file is exists and last modification time is >= latestPublishedAt(last update post time on medium)
|
242
|
-
if File.file?(absolutePath) && File.mtime(absolutePath) >= postInfo.latestPublishedAt
|
220
|
+
if forceDownload == false && File.file?(absolutePath) && File.mtime(absolutePath) >= postInfo.latestPublishedAt
|
243
221
|
# Already downloaded and nothing has changed!, Skip!
|
244
222
|
progress.currentPostParagraphIndex = paragraphs.length
|
245
223
|
progress.message = "Skip, Post already downloaded and nothing has changed!"
|
@@ -323,19 +301,4 @@ class ZMediumFetcher
|
|
323
301
|
progress.message = "All posts has been downloaded!, Total posts: #{postURLS.length}"
|
324
302
|
progress.printLog()
|
325
303
|
end
|
326
|
-
end
|
327
|
-
|
328
|
-
begin
|
329
|
-
puts "#https://github.com/ZhgChgLi/ZMediumToMarkdown"
|
330
|
-
puts "You have read and agree with the Disclaimer."
|
331
|
-
Main.new()
|
332
|
-
puts "Execute Successfully!!!"
|
333
|
-
puts "#https://github.com/ZhgChgLi/ZMediumToMarkdown"
|
334
|
-
puts "#Thanks for using this tool."
|
335
|
-
puts "#If this is helpful, please help to star the repo or recommend it to your friends."
|
336
|
-
rescue => e
|
337
|
-
puts "#Error: #{e.class} #{e.message}\n"
|
338
|
-
puts e.backtrace
|
339
|
-
puts "#Please feel free to open an Issue or submit a fix/contribution via Pull Request on:\n"
|
340
|
-
puts "#https://github.com/ZhgChgLi/ZMediumToMarkdown\n"
|
341
304
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ZMediumToMarkdown
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- ZhgChgLi
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-05-
|
11
|
+
date: 2022-05-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: nokogiri
|
@@ -56,11 +56,11 @@ description: ZMediumToMarkdown lets you download Medium post and convert it to m
|
|
56
56
|
format easily.
|
57
57
|
email:
|
58
58
|
executables:
|
59
|
-
-
|
59
|
+
- ZMediumToMarkdown
|
60
60
|
extensions: []
|
61
61
|
extra_rdoc_files: []
|
62
62
|
files:
|
63
|
-
- bin/
|
63
|
+
- bin/ZMediumToMarkdown
|
64
64
|
- lib/ErrorHandle.rb
|
65
65
|
- lib/Helper.rb
|
66
66
|
- lib/ImageDownloader.rb
|
@@ -87,6 +87,7 @@ files:
|
|
87
87
|
- lib/Post.rb
|
88
88
|
- lib/Request.rb
|
89
89
|
- lib/User.rb
|
90
|
+
- lib/ZMediumFetcher.rb
|
90
91
|
homepage: https://github.com/ZhgChgLi/ZMediumToMarkdown
|
91
92
|
licenses:
|
92
93
|
- MIT
|