Initial commit

This commit is contained in:
Nemo 2020-03-29 00:59:47 +05:30
commit 9447c8e30a
14 changed files with 185 additions and 0 deletions

9
.editorconfig Normal file
View File

@ -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

5
.gitignore vendored Normal file
View File

@ -0,0 +1,5 @@
/docs/
/lib/
/bin/
/.shards/
*.dwarf

6
.travis.yml Normal file
View File

@ -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

7
LICENSE Normal file
View File

@ -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.

23
README.md Normal file
View File

@ -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.

14
shard.lock Normal file
View File

@ -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

18
shard.yml Normal file
View File

@ -0,0 +1,18 @@
name: muse-dl
version: 0.1.0
authors:
- Nemo <muse.dl@captnemo.in>
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

19
spec/parser_spec.cr Normal file
View File

@ -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

10
spec/pdftk_spec.cr Normal file
View File

@ -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

2
spec/spec_helper.cr Normal file
View File

@ -0,0 +1,2 @@
require "spec"
require "../src/muse-dl"

18
src/fetch.cr Normal file
View File

@ -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

10
src/muse-dl.cr Normal file
View File

@ -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

26
src/parser.cr Normal file
View File

@ -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

18
src/pdftk.cr Normal file
View File

@ -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