-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from nikhil1raghav/cobra
Autoclassify filetype, added cobra for command line options
- Loading branch information
Showing
89 changed files
with
12,227 additions
and
143 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
1.0.3-rc-2 | ||
2.0.0-rc-1 |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package classifier | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/nikhil1raghav/kindle-send/types" | ||
) | ||
|
||
func isUrl(u string) bool { | ||
for _, proto := range []string{"http://", "https://"} { | ||
if strings.HasPrefix(u, proto) { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func isUrlFile(u string) bool { | ||
file, err := os.Open(u) | ||
if err != nil { | ||
return false | ||
} | ||
defer file.Close() | ||
buf := make([]byte, 1024) | ||
n, _ := file.Read(buf) | ||
content := string(buf[:n]) | ||
lines := strings.Split(content, "\n") | ||
for _, line := range lines { | ||
line = strings.Trim(line, " ") | ||
if len(line) == 0 { | ||
continue | ||
} | ||
if !strings.HasPrefix(line, "http") { | ||
return false | ||
} | ||
} | ||
return true | ||
} | ||
|
||
func isBook(u string) bool { | ||
extension := filepath.Ext(u) | ||
// does file exist | ||
_, err := os.Stat(u) | ||
if err != nil { | ||
return false | ||
} | ||
for _, ext := range []string{".mobi", ".pdf", ".epub", ".azw3", ".txt"} { | ||
if extension == ext { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func Classify(args []string) []types.Request { | ||
var requests []types.Request | ||
for _, arg := range args { | ||
if isUrl(arg) { | ||
requests = append(requests, types.NewRequest(arg, types.TypeUrl, nil)) | ||
} else if isUrlFile(arg) { | ||
requests = append(requests, types.NewRequest(arg, types.TypeUrlFile, nil)) | ||
} else if isBook(arg) { | ||
requests = append(requests, types.NewRequest(arg, types.TypeFile, nil)) | ||
} | ||
} | ||
|
||
return requests | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package cmd | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/lithammer/dedent" | ||
"github.com/nikhil1raghav/kindle-send/classifier" | ||
"github.com/nikhil1raghav/kindle-send/config" | ||
"github.com/nikhil1raghav/kindle-send/handler" | ||
"github.com/nikhil1raghav/kindle-send/util" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func init() { | ||
rootCmd.AddCommand(downloadCmd) | ||
} | ||
|
||
var ( | ||
helpDownload = `Downloads the webpage or collection of webpages from given arguments | ||
that can be a standalone link or a text file containing multiple links. | ||
Supports multiple arguments. Each argument is downloaded as a separate file.` | ||
|
||
exampleDownload = dedent.Dedent(` | ||
# Download a single webpage | ||
kindle-send download "http://paulgraham.com/alien.html" | ||
# Download multiple webpages | ||
kindle-send download "http://paulgraham.com/alien.html" "http://paulgraham.com/hwh.html" | ||
# Download webpage and collection of webpages | ||
kindle-send download "http://paulgraham.com/alien.html" links.txt`, | ||
) | ||
) | ||
|
||
var downloadCmd = &cobra.Command{ | ||
Use: "download [LINK1] [LINK2] [FILE1] [FILE2]", | ||
Short: "Download the webpage as ebook and save locally", | ||
Long: helpDownload, | ||
Example: exampleDownload, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
configPath, _ := cmd.Flags().GetString("config") | ||
_, err := config.Load(configPath) | ||
if err != nil { | ||
util.Red.Println(err) | ||
return | ||
} | ||
|
||
downloadRequests := classifier.Classify(args) | ||
downloadedRequests := handler.Queue(downloadRequests) | ||
|
||
util.CyanBold.Printf("Downloaded %d files :\n", len(downloadRequests)) | ||
for idx, req := range downloadedRequests { | ||
fileInfo, _ := os.Stat(req.Path) | ||
util.Cyan.Printf("%d. %s\n", idx+1, fileInfo.Name()) | ||
} | ||
|
||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/nikhil1raghav/kindle-send/config" | ||
"github.com/nikhil1raghav/kindle-send/util" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func init() { | ||
var configPath string | ||
configPath, err := config.DefaultConfigPath() | ||
if err != nil { | ||
util.Red.Println("Error setting default config path: ", err) | ||
os.Exit(1) | ||
} | ||
rootCmd.PersistentFlags().StringVarP(&configPath, "config", "c", configPath, "Path to config file") | ||
|
||
} | ||
|
||
var rootCmd = &cobra.Command{ | ||
Use: "kindle-send", | ||
Short: "kindle-send sends documents, webpages and books to your ereader", | ||
Long: `kindle-send is a CLI tool to send file (books/documents) and webpages to your ereader | ||
It parses the webpage, optimizes it for reading on ereader, and then converts | ||
into an ebook. Then it emails the ebook to the ereader. | ||
Complete documentation is available at https://github.com/nikhil1raghav/kindle-send`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
configPath, _ := cmd.Flags().GetString("config") | ||
_, err := config.Load(configPath) | ||
if err != nil { | ||
util.Red.Println(err) | ||
return | ||
} | ||
}, | ||
} | ||
|
||
func Execute() { | ||
if err := rootCmd.Execute(); err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/lithammer/dedent" | ||
"github.com/nikhil1raghav/kindle-send/classifier" | ||
"github.com/nikhil1raghav/kindle-send/config" | ||
"github.com/nikhil1raghav/kindle-send/handler" | ||
"github.com/nikhil1raghav/kindle-send/util" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func init() { | ||
rootCmd.AddCommand(sendCmd) | ||
} | ||
|
||
var ( | ||
helpLong = `Sends the files to ereader. If a link or a file containing links is given | ||
it will first download the webpage, convert into ebook and then send. | ||
Each argument is sent as a separate file. | ||
kindle-send auto detects if argument is a link, collection of links or an ebook.` | ||
|
||
helpExample = dedent.Dedent(` | ||
# Send a single webpage | ||
kindle-send send "http://paulgraham.com/alien.html" | ||
# Send multiple webpages | ||
kindle-send send "http://paulgraham.com/alien.html" "http://paulgraham.com/hwh.html" | ||
# Send webpage, collection of webpages and an ebook | ||
kindle-send download "http://paulgraham.com/alien.html" links.txt "Some Book.epub"`, | ||
) | ||
) | ||
|
||
func init() { | ||
sendCmd.PersistentFlags().IntP("mail-timeout", "m", 120, "Mail timeout in seconds, increase it if sending lot of files") | ||
} | ||
|
||
var sendCmd = &cobra.Command{ | ||
Use: "send [LINK1] [LINK2] [FILE1] [FILE2]", | ||
Short: "Send the files, links, documents to ereader", | ||
Long: helpLong, | ||
Example: helpExample, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
configPath, _ := cmd.Flags().GetString("config") | ||
_, err := config.Load(configPath) | ||
if err != nil { | ||
util.Red.Println(err) | ||
return | ||
} | ||
|
||
downloadRequests := classifier.Classify(args) | ||
downloadedRequests := handler.Queue(downloadRequests) | ||
|
||
timeout, err := cmd.Flags().GetInt("mail-timeout") | ||
if err != nil { | ||
timeout = 0 | ||
} | ||
|
||
handler.Mail(downloadedRequests, timeout) | ||
|
||
}, | ||
} |
Oops, something went wrong.