Last active
March 5, 2020 20:02
-
-
Save lord-alfred/304994e01757502dc258ea89b56db1ea to your computer and use it in GitHub Desktop.
readability_cli.exe --url=https://www.nytimes.com/2019/02/20/climate/climate-national-security-threat.html --fin=C:\GoProjects\fin.html --fout=C:\GoProjects\fout.txt
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
package main | |
import ( | |
"fmt" | |
"log" | |
"os" | |
readability "github.com/go-shiori/go-readability" | |
"github.com/urfave/cli" | |
) | |
func main() { | |
app := &cli.App{ | |
Flags: []cli.Flag{ | |
&cli.StringFlag{ | |
Name: "url", | |
Usage: "URL (only for statistics)", | |
}, | |
&cli.StringFlag{ | |
Name: "fin", | |
Usage: "Input filepath with full http response (html without headers)", | |
}, | |
&cli.StringFlag{ | |
Name: "fout", | |
Usage: "Result filepath for save article text", | |
}, | |
}, | |
Action: func(c *cli.Context) error { | |
url := c.String("url") | |
if url == "" { | |
fmt.Print("--url param is empty") | |
return nil | |
} | |
fin := c.String("fin") | |
_, err := os.Stat(fin) | |
if os.IsNotExist(err) { | |
fmt.Print("Input file from --fin param not exists") | |
return nil | |
} | |
fout := c.String("fout") | |
f, err := os.Open(fin) | |
defer f.Close() | |
if err != nil { | |
panic(err) | |
} | |
article, err := readability.FromReader(f, url) | |
if err != nil { | |
panic(err) | |
} | |
dstTxtFile, _ := os.Create(fout) | |
defer dstTxtFile.Close() | |
dstTxtFile.WriteString(article.TextContent) | |
fmt.Printf("SuccessReadability") | |
return nil | |
}, | |
} | |
err := app.Run(os.Args) | |
if err != nil { | |
log.Fatal(err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment