Module: CLI
- Defined in:
- lib/CLI.rb
Overview
All CLI-side concerns for the ‘ZMediumToMarkdown` executable. Pulled out of bin/ so it can be exercised by unit tests without spawning processes.
Constant Summary collapse
- COOKIE_SETUP_URL =
'https://github.com/ZhgChgLi/ZMediumToMarkdown/wiki/Setting-Up-Medium-Cookies-and-a-Cloudflare-Worker-Proxy'.freeze
- DEFAULT_MEDIUM_HOST =
'https://medium.com/_/graphql'.freeze
- DEFAULT_MIRO_MEDIUM_HOST =
'https://miro.medium.com'.freeze
Class Method Summary collapse
-
.buildSetupBanner(missingCookies:, missingProxy:, missingImageProxy:) ⇒ Object
One-line warning.
- .cookieMissing?(name) ⇒ Boolean
- .cookiesPresent? ⇒ Boolean
- .imageProxyConfigured? ⇒ Boolean
-
.loadCookies! ⇒ Object
Cookie precedence (highest → lowest): 1.
- .loadCookiesFromCache! ⇒ Object
- .loadCookiesFromEnv! ⇒ Object
- .main(argv, output: $stdout, errput: $stderr, cwd: ENV['PWD'] || ::Dir.pwd) ⇒ Object
- .parseArgs(argv, errput: $stderr) ⇒ Object
-
.pathPolicyFor(cwd, isForJekyll) ⇒ Object
Jekyll mode writes into the cwd (so files land in ‘_posts/…` and `assets/…` of an existing Jekyll site).
-
.proxyConfigured? ⇒ Boolean
Worker proxy is “configured” when MEDIUM_HOST is set to something other than the default upstream Medium URL — i.e.
- .run(options, cwd, output: $stdout, errput: $stderr) ⇒ Object
-
.warnAboutMissingSetup(options, errput: $stderr) ⇒ Object
Only warn when the invocation will actually hit Medium — skip for –version, –clean, –help, –new.
- .willHitMedium?(options) ⇒ Boolean
Class Method Details
.buildSetupBanner(missingCookies:, missingProxy:, missingImageProxy:) ⇒ Object
One-line warning. The wiki has the actual setup steps; we just nudge the user toward it instead of dumping a wall of guidance.
193 194 195 196 197 198 199 200 201 |
# File 'lib/CLI.rb', line 193 def buildSetupBanner(missingCookies:, missingProxy:, missingImageProxy:) missing = [] missing << 'Medium cookies (sid / uid)' if missingCookies missing << 'Cloudflare Worker proxy (MEDIUM_HOST)' if missingProxy missing << 'Cloudflare image proxy (MIRO_MEDIUM_HOST)' if missingImageProxy return '' if missing.empty? "⚠ Missing #{missing.join(' / ')}. Medium / Cloudflare may block the run. Setup guide: #{COOKIE_SETUP_URL}" end |
.cookieMissing?(name) ⇒ Boolean
149 150 151 152 |
# File 'lib/CLI.rb', line 149 def (name) return true unless defined?($cookies) && $cookies.is_a?(Hash) $cookies[name].to_s.empty? end |
.cookiesPresent? ⇒ Boolean
154 155 156 |
# File 'lib/CLI.rb', line 154 def !('sid') || !('uid') end |
.imageProxyConfigured? ⇒ Boolean
166 167 168 169 |
# File 'lib/CLI.rb', line 166 def imageProxyConfigured? host = ENV['MIRO_MEDIUM_HOST'].to_s !host.empty? && host != DEFAULT_MIRO_MEDIUM_HOST end |
.loadCookies! ⇒ Object
Cookie precedence (highest → lowest):
1. CLI flags (already written to $cookies in parseArgs)
2. Env vars (MEDIUM_COOKIE_*)
3. On-disk cache (~/.config/ZMediumToMarkdown/cookies.json)
Each layer only fills slots the higher layer left empty.
127 128 129 130 |
# File 'lib/CLI.rb', line 127 def loadCookies! loadCookiesFromEnv! loadCookiesFromCache! end |
.loadCookiesFromCache! ⇒ Object
139 140 141 142 143 144 145 146 147 |
# File 'lib/CLI.rb', line 139 def loadCookiesFromCache! cached = CookieCache.load return if cached.empty? ChromeAuth::TARGET_COOKIES.each do |name| value = cached[name] next if value.to_s.empty? $cookies[name] = value if (name) end end |
.loadCookiesFromEnv! ⇒ Object
132 133 134 135 136 137 |
# File 'lib/CLI.rb', line 132 def loadCookiesFromEnv! $cookies['sid'] = ENV['MEDIUM_COOKIE_SID'] if ('sid') && !ENV['MEDIUM_COOKIE_SID'].to_s.empty? $cookies['uid'] = ENV['MEDIUM_COOKIE_UID'] if ('uid') && !ENV['MEDIUM_COOKIE_UID'].to_s.empty? $cookies['cf_clearance'] = ENV['MEDIUM_COOKIE_CF_CLEARANCE'] if ('cf_clearance') && !ENV['MEDIUM_COOKIE_CF_CLEARANCE'].to_s.empty? $cookies['_cfuvid'] = ENV['MEDIUM_COOKIE_CFUVID'] if ('_cfuvid') && !ENV['MEDIUM_COOKIE_CFUVID'].to_s.empty? end |
.main(argv, output: $stdout, errput: $stderr, cwd: ENV['PWD'] || ::Dir.pwd) ⇒ Object
21 22 23 24 25 26 27 28 29 |
# File 'lib/CLI.rb', line 21 def main(argv, output: $stdout, errput: $stderr, cwd: ENV['PWD'] || ::Dir.pwd) argv = argv.dup argv << '-h' if argv.empty? = parseArgs(argv, errput: errput) loadCookies! warnAboutMissingSetup(, errput: errput) run(, cwd, output: output, errput: errput) end |
.parseArgs(argv, errput: $stderr) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/CLI.rb', line 31 def parseArgs(argv, errput: $stderr) = {} parser = OptionParser.new do |opts| opts. = "Usage: ZMediumToMarkdown [options]" opts.on('-s', '--cookie_sid SID', 'Medium logged-in cookie sid value (or set $MEDIUM_COOKIE_SID)') do |v| $cookies['sid'] = v end opts.on('-d', '--cookie_uid UID', 'Medium logged-in cookie uid value (or set $MEDIUM_COOKIE_UID)') do |v| $cookies['uid'] = v end opts.on('--cookie_cf_clearance VALUE', 'Cloudflare cf_clearance cookie value (or set $MEDIUM_COOKIE_CF_CLEARANCE)') do |v| $cookies['cf_clearance'] = v end opts.on('--cookie_cfuvid VALUE', 'Cloudflare _cfuvid cookie value (or set $MEDIUM_COOKIE_CFUVID)') do |v| $cookies['_cfuvid'] = v end opts.on('-x', '--medium_host URL', 'Cloudflare Worker proxy URL for Medium GraphQL (or set $MEDIUM_HOST). Strongly recommended for CI / bulk runs — see the wiki setup guide.') do |v| ENV['MEDIUM_HOST'] = v end opts.on('--miro_medium_host URL', 'Cloudflare Worker proxy URL for Medium image CDN (or set $MIRO_MEDIUM_HOST). Optional companion to --medium_host.') do |v| ENV['MIRO_MEDIUM_HOST'] = v end opts.on('-u', '--username USERNAME', 'Download all posts from a Medium username') do |v| [:username] = v end opts.on('-p', '--postURL POST_URL', 'Download a single post URL') do |v| [:postURL] = v end opts.on('--jekyll', 'Emit Jekyll-friendly output (combine with -u or -p)') do [:jekyll] = true end opts.on('-j', '--jekyllUsername USERNAME', 'DEPRECATED: use `--jekyll -u USERNAME`') do |v| [:username] = v [:jekyll] = true errput.puts '[deprecated] -j/--jekyllUsername is deprecated; use `--jekyll -u USERNAME`.' end opts.on('-k', '--jekyllPostURL POST_URL', 'DEPRECATED: use `--jekyll -p POST_URL`') do |v| [:postURL] = v [:jekyll] = true errput.puts '[deprecated] -k/--jekyllPostURL is deprecated; use `--jekyll -p POST_URL`.' end opts.on('--stdout', 'Render Markdown of -p/-u directly to stdout. Skips all image/asset downloads (image links stay as remote URLs). Logs and banners go to stderr so stdout stays pure markdown.') do [:stdout] = true end opts.on('--list', 'With -u <username>, emit one NDJSON line per post (title, url, creator, dates, tags) to stdout. Skips bodies and image downloads.') do [:list] = true end opts.on('--limit N', Integer, 'Cap the number of posts processed when used with -u (in --stdout or --list mode).') do |v| [:limit] = v end opts.on('-n', '--new', 'Update to latest version') do [:upgrade] = true end opts.on('-c', '--clean', 'Remove all downloaded posts data under cwd') do [:clean] = true end opts.on('-v', '--version', 'Print current ZMediumToMarkdown version') do [:version] = true end opts.on('--non-interactive', 'Never prompt or open a browser. CI runners auto-detect this; use the flag to force the same behavior on a TTY.') do [:nonInteractive] = true ENV['MEDIUM_NO_AUTO_BROWSER'] = '1' end opts.on('-h', '--help', 'Show this help message') do [:help] = opts.to_s end end parser.parse!(argv) end |
.pathPolicyFor(cwd, isForJekyll) ⇒ Object
Jekyll mode writes into the cwd (so files land in ‘_posts/…` and `assets/…` of an existing Jekyll site). Plain mode nests under `Output/` to keep the user’s cwd tidy.
281 282 283 284 285 286 287 |
# File 'lib/CLI.rb', line 281 def pathPolicyFor(cwd, isForJekyll) if isForJekyll PathPolicy.new(cwd, "") else PathPolicy.new("#{cwd}/Output", "Output") end end |
.proxyConfigured? ⇒ Boolean
Worker proxy is “configured” when MEDIUM_HOST is set to something other than the default upstream Medium URL — i.e. user pointed it at their own Cloudflare Worker (or another proxy).
161 162 163 164 |
# File 'lib/CLI.rb', line 161 def proxyConfigured? host = ENV['MEDIUM_HOST'].to_s !host.empty? && host != DEFAULT_MEDIUM_HOST end |
.run(options, cwd, output: $stdout, errput: $stderr) ⇒ Object
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 |
# File 'lib/CLI.rb', line 203 def run(, cwd, output: $stdout, errput: $stderr) if [:help] output.puts [:help] return end if [:version] output.puts "Version:#{Helper.getLocalVersion()}" Helper.printNewVersionMessageIfExists() return end if [:clean] outputFilePath = PathPolicy.new(cwd, "") FileUtils.rm_rf(Dir[outputFilePath.getAbsolutePath(nil)]) output.puts "All downloaded posts data has been removed." Helper.printNewVersionMessageIfExists() return end if [:upgrade] remote = Helper.getRemoteVersionFromGithub() local = Helper.getLocalVersion() if remote && local && remote > local Helper.downloadLatestVersion() else output.puts "You're using the latest version :)" end return end # --stdout / --list path: render to the given output stream, skip # all filesystem writes and asset downloads. Progress goes to errput # so stdout stays pure markdown / NDJSON for embedding callers. # Handled before willHitMedium? so the --list-without-username guard # surfaces an error instead of silently no-op'ing. if [:stdout] || [:list] if [:list] && [:username].nil? errput.puts '--list requires -u/--username' return end return unless willHitMedium?() fetcher = ZMediumFetcher.new fetcher.isForJekyll = [:jekyll] == true fetcher.stdoutIO = output fetcher.stdoutMode = true fetcher.progress.io = errput if [:list] fetcher.listPostsByUsername([:username], [:limit]) elsif [:postURL] fetcher.downloadPost([:postURL], nil, nil) elsif [:username] fetcher.downloadPostsByUsername([:username], nil, limit: [:limit]) end return end return unless willHitMedium?() fetcher = ZMediumFetcher.new fetcher.isForJekyll = [:jekyll] == true targetPolicy = pathPolicyFor(cwd, fetcher.isForJekyll) if [:postURL] fetcher.downloadPost([:postURL], targetPolicy, nil) elsif [:username] fetcher.downloadPostsByUsername([:username], targetPolicy, limit: [:limit]) end Helper.printNewVersionMessageIfExists() end |
.warnAboutMissingSetup(options, errput: $stderr) ⇒ Object
Only warn when the invocation will actually hit Medium — skip for –version, –clean, –help, –new.
174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/CLI.rb', line 174 def warnAboutMissingSetup(, errput: $stderr) return unless willHitMedium?() missingCookies = ! missingProxy = !proxyConfigured? missingImageProxy = !imageProxyConfigured? return if !missingCookies && !missingProxy && !missingImageProxy errput.puts buildSetupBanner(missingCookies: missingCookies, missingProxy: missingProxy, missingImageProxy: missingImageProxy) end |
.willHitMedium?(options) ⇒ Boolean
187 188 189 |
# File 'lib/CLI.rb', line 187 def willHitMedium?() ![:postURL].nil? || ![:username].nil? end |