From 9447c8e30a2de6bb5827abc96e94675ac2812296 Mon Sep 17 00:00:00 2001 From: Nemo Date: Sun, 29 Mar 2020 00:59:47 +0530 Subject: [PATCH] Initial commit --- .editorconfig | 9 +++++++++ .gitignore | 5 +++++ .travis.yml | 6 ++++++ LICENSE | 7 +++++++ README.md | 23 +++++++++++++++++++++++ shard.lock | 14 ++++++++++++++ shard.yml | 18 ++++++++++++++++++ spec/parser_spec.cr | 19 +++++++++++++++++++ spec/pdftk_spec.cr | 10 ++++++++++ spec/spec_helper.cr | 2 ++ src/fetch.cr | 18 ++++++++++++++++++ src/muse-dl.cr | 10 ++++++++++ src/parser.cr | 26 ++++++++++++++++++++++++++ src/pdftk.cr | 18 ++++++++++++++++++ 14 files changed, 185 insertions(+) create mode 100644 .editorconfig create mode 100644 .gitignore create mode 100644 .travis.yml create mode 100644 LICENSE create mode 100644 README.md create mode 100644 shard.lock create mode 100644 shard.yml create mode 100644 spec/parser_spec.cr create mode 100644 spec/pdftk_spec.cr create mode 100644 spec/spec_helper.cr create mode 100644 src/fetch.cr create mode 100644 src/muse-dl.cr create mode 100644 src/parser.cr create mode 100644 src/pdftk.cr diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..163eb75 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,9 @@ +root = true + +[*.cr] +charset = utf-8 +end_of_line = lf +insert_final_newline = true +indent_style = space +indent_size = 2 +trim_trailing_whitespace = true diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0bb75ea --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +/docs/ +/lib/ +/bin/ +/.shards/ +*.dwarf diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..765f0e9 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,6 @@ +language: crystal + +# Uncomment the following if you'd like Travis to run specs and check code formatting +# script: +# - crystal spec +# - crystal tool format --check diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..2bd1d6a --- /dev/null +++ b/LICENSE @@ -0,0 +1,7 @@ +Copyright 2020 Abhay Rana + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..f9a9ee3 --- /dev/null +++ b/README.md @@ -0,0 +1,23 @@ +# muse-dl + +# Installation + +TODO + +## Usage + +``` +muse-dl --help + +USAGE: muse-dl [FLAGS] URL + +FLAGS: + --no-bookmarks: Don't add bookmarks + --no-cleanup: Don't cleanup temporary files + --tmp-dir /path: Use /path as temporary directory + --output file.pdf: Use file.pdf as output filename +``` + +## License + +Licensed under the [MIT License](https://nemo.mit-license.org/). See LICENSE file for details. \ No newline at end of file diff --git a/shard.lock b/shard.lock new file mode 100644 index 0000000..087059c --- /dev/null +++ b/shard.lock @@ -0,0 +1,14 @@ +version: 1.0 +shards: + crest: + github: mamantoha/crest + version: 0.24.0 + + http-client-digest_auth: + github: mamantoha/http-client-digest_auth + version: 0.3.0 + + myhtml: + github: kostya/myhtml + version: 1.5.1 + diff --git a/shard.yml b/shard.yml new file mode 100644 index 0000000..455af33 --- /dev/null +++ b/shard.yml @@ -0,0 +1,18 @@ +name: muse-dl +version: 0.1.0 + +authors: + - Nemo + +description: Downloads and stitches ebooks from Project MUSE +license: MIT + +targets: + muse-dl: + main: src/muse-dl.cr + +dependencies: + myhtml: + github: kostya/myhtml + crest: + github: mamantoha/crest \ No newline at end of file diff --git a/spec/parser_spec.cr b/spec/parser_spec.cr new file mode 100644 index 0000000..f838323 --- /dev/null +++ b/spec/parser_spec.cr @@ -0,0 +1,19 @@ +require "./spec_helper" + +describe Muse::Dl::Parser do + it "should parse options" do + parser = Muse::Dl::Parser.new(["--no-bookmarks", "--tmp-dir", "/tmp", "--no-cleanup", "--output", "file.pdf"]) + parser.bookmarks.should eq false + parser.tmp.should eq "/tmp" + parser.cleanup.should eq false + parser.output.should eq "file.pdf" + end + + it "should have reasonable defaults" do + parser = Muse::Dl::Parser.new + parser.bookmarks.should eq true + parser.cleanup.should eq true + parser.tmp.should eq "/tmp" + parser.output.should eq "tempfilename.pdf" + end +end diff --git a/spec/pdftk_spec.cr b/spec/pdftk_spec.cr new file mode 100644 index 0000000..1b7903a --- /dev/null +++ b/spec/pdftk_spec.cr @@ -0,0 +1,10 @@ +require "./spec_helper" + +describe Muse::Dl::Pdftk do + it "should find pdftk binary" do + pdftk = Muse::Dl::Pdftk.new + if pdftk.binary + pdftk.binary.should eq "/usr/sbin/pdftk" + end + end +end diff --git a/spec/spec_helper.cr b/spec/spec_helper.cr new file mode 100644 index 0000000..0a940dd --- /dev/null +++ b/spec/spec_helper.cr @@ -0,0 +1,2 @@ +require "spec" +require "../src/muse-dl" diff --git a/src/fetch.cr b/src/fetch.cr new file mode 100644 index 0000000..2b4ad69 --- /dev/null +++ b/src/fetch.cr @@ -0,0 +1,18 @@ +require "crest" + +module Muse::Dl + class Fetch + def get_info(url : String) : (Muse::Dl::Book | Muse::Dl::Issue) + match = /https:\/\/muse.jhu.edu\/(book|journal)\/(\d+)/.match url + if match + begin + response = Crest.get url + rescue ex : Crest::NotFound + raise Muse::Dl::InvalidLink + end + else + raise Muse::Dl::InvalidLink + end + end + end +end diff --git a/src/muse-dl.cr b/src/muse-dl.cr new file mode 100644 index 0000000..3f9fa24 --- /dev/null +++ b/src/muse-dl.cr @@ -0,0 +1,10 @@ +require "./parser.cr" +require "./pdftk.cr" +require "./fetch.cr" + +# TODO: Write documentation for `Muse::Dl` +module Muse::Dl + VERSION = "0.1.0" + + # TODO: Put your code here +end diff --git a/src/parser.cr b/src/parser.cr new file mode 100644 index 0000000..1700640 --- /dev/null +++ b/src/parser.cr @@ -0,0 +1,26 @@ +module Muse::Dl + class Parser + @bookmarks : Bool + @tmp : String + @cleanup : Bool + @output : String + + getter :bookmarks, :tmp, :cleanup, :output + + def find_next(arg : Array(String), flag : String, default) + search = arg.index flag + if search + return arg[search + 1] + end + + return default + end + + def initialize(arg : Array(String) = [] of String) + @bookmarks = !arg.index "--no-bookmarks" + @cleanup = !arg.index "--no-cleanup" + @tmp = find_next(arg, "--tmp-dir", "/tmp") + @output = find_next(arg, "--output", "tempfilename.pdf") + end + end +end diff --git a/src/pdftk.cr b/src/pdftk.cr new file mode 100644 index 0000000..7eadd73 --- /dev/null +++ b/src/pdftk.cr @@ -0,0 +1,18 @@ +require "process" + +module Muse::Dl + class Pdftk + PDFTK_BINARY_NAME = "pdftk" + @binary : String | Nil + + getter :binary + + def initialize + @binary = Process.find_executable(Pdftk::PDFTK_BINARY_NAME) + if !@binary + puts "Could not find pdftk binary, exiting" + Process.exit(1) + end + end + end +end