mirror of
https://github.com/arnaucube/link2epub.git
synced 2026-02-07 03:26:44 +01:00
Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ff6e43c247 | ||
|
|
19cd78ec15 |
14
README.md
14
README.md
@@ -1,6 +1,8 @@
|
|||||||
# link2epub [](https://goreportcard.com/report/github.com/arnaucube/link2epub)
|
# link2epub [](https://goreportcard.com/report/github.com/arnaucube/link2epub)
|
||||||
Very simple tool to download articles and convert it to `.epub`/`.mobi` files.
|
Very simple tool to download articles and convert it to `.epub`/`.mobi` files.
|
||||||
|
|
||||||
|
It gets the text content, simplifies its html, downloads the images, and builds the `.epub`/`.mobi` file.
|
||||||
|
|
||||||
## Download
|
## Download
|
||||||
- Binary can be:
|
- Binary can be:
|
||||||
- downloaded from [releases section](https://github.com/arnaucube/link2epub/releases)
|
- downloaded from [releases section](https://github.com/arnaucube/link2epub/releases)
|
||||||
@@ -9,12 +11,18 @@ Very simple tool to download articles and convert it to `.epub`/`.mobi` files.
|
|||||||
## Usage
|
## Usage
|
||||||
Needs [calibre](https://calibre-ebook.com/) in order to convert to `.epub` and `.mobi`.
|
Needs [calibre](https://calibre-ebook.com/) in order to convert to `.epub` and `.mobi`.
|
||||||
|
|
||||||
|
Putting the binary in the `~/bin` directory will be more comfortable.
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
./link2epub -l https://link.com/to-the-article
|
link2epub -l https://link.com/to-the-article
|
||||||
|
|
||||||
// optionally add extension (by default .mobi)
|
// optionally add extension (by default .mobi)
|
||||||
./link2epub -l https://link.com/to-the-article -type mobi
|
link2epub -l https://link.com/to-the-article -type mobi
|
||||||
./link2epub -l https://link.com/to-the-article -type epub
|
link2epub -l https://link.com/to-the-article -type epub
|
||||||
|
|
||||||
|
// see help for all the available flags
|
||||||
|
link2epub --help
|
||||||
```
|
```
|
||||||
|
|
||||||
Thanks to [@dhole](https://github.com/dhole) for the advisment.
|
Thanks to [@dhole](https://github.com/dhole) for the advisment.
|
||||||
|
|
||||||
|
|||||||
28
main.go
28
main.go
@@ -15,21 +15,32 @@ import (
|
|||||||
readability "github.com/go-shiori/go-readability"
|
readability "github.com/go-shiori/go-readability"
|
||||||
)
|
)
|
||||||
|
|
||||||
const tmpDir = "tmp"
|
const version = "v0_20221002"
|
||||||
|
const tmpDir = "link2epubtmpdir"
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// var typeFlag string
|
versionFlag := flag.Bool("v", false, "version")
|
||||||
linkFlag := flag.String("l", "", "Link to download")
|
linkFlag := flag.String("l", "", "Link to download")
|
||||||
typeFlag := flag.String("type", "mobi", "Type of epub. Available: mobi (default), epub")
|
typeFlag := flag.String("type", "mobi", "Type of epub. Available: mobi (default), epub")
|
||||||
|
titleFlag := flag.String("title", "", "Title is automatically getted from article, if want to change it, use this flag")
|
||||||
|
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
|
fmt.Println("link2epub version:", version)
|
||||||
|
if *versionFlag {
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
||||||
|
|
||||||
if *typeFlag != "mobi" && *typeFlag != "epub" {
|
if *typeFlag != "mobi" && *typeFlag != "epub" {
|
||||||
log.Fatal("not valid type")
|
log.Fatal("not valid type")
|
||||||
}
|
}
|
||||||
err := os.Mkdir(tmpDir, os.ModePerm)
|
err := os.Mkdir(tmpDir, os.ModePerm)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("error creating tmp dir %s: %v\n", tmpDir, err)
|
log.Printf("error creating tmp dir %s: %v\nRemoving it and continuing.", tmpDir, err)
|
||||||
|
err = os.RemoveAll(tmpDir)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("err removing %s: %v\n", tmpDir, err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// get link
|
// get link
|
||||||
@@ -70,6 +81,17 @@ func main() {
|
|||||||
article.Content = strings.Replace(article.Content, string(img[4]), filename, -1)
|
article.Content = strings.Replace(article.Content, string(img[4]), filename, -1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if *titleFlag != "" {
|
||||||
|
article.Title = *titleFlag
|
||||||
|
}
|
||||||
|
|
||||||
|
// add title to content
|
||||||
|
article.Content = `
|
||||||
|
<h1>` + article.Title + `</h1>
|
||||||
|
<h2 style="text-align:right;">` + article.Byline + `</h2>
|
||||||
|
<br>
|
||||||
|
` + article.Content
|
||||||
|
|
||||||
// store html file
|
// store html file
|
||||||
filename := article.Title + " - " + article.Byline
|
filename := article.Title + " - " + article.Byline
|
||||||
out, err := os.Create(tmpDir + "/" + filename + ".html")
|
out, err := os.Create(tmpDir + "/" + filename + ".html")
|
||||||
|
|||||||
Reference in New Issue
Block a user