Skip to content

Commit

Permalink
update documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
iamtakingiteasy committed Nov 16, 2021
1 parent 32706e5 commit 56b416e
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 102 deletions.
61 changes: 28 additions & 33 deletions README.jaroidfedipost.md
Original file line number Diff line number Diff line change
@@ -1,40 +1,46 @@
This utility provides CLI interface to authenticate in pleroma/mastadon
account using OAuth2 API and make posts with content of [nicovideo.jp](https://www.nicovideo.jp/)
videos using https://github.com/animelover1984/youtube-dl/tree/Niconico_download_fix script.
This CLI utility downloads videos from [nicovideo.jp](https://www.nicovideo.jp/) also optionally uploading them as
pleroma/mastodon instance posts, using OAuth2 API to authenticate under provided account.

USAGE
---

```
Usage:
jaroidfedi https://www.nicovideo.jp/watch/sm0000000 <size[!]|formatid|max|list> [sub[:<jp|en|cn>]] [post] [account]
jaroidfedi https://www.nicovideo.jp/watch/sm0000000 <size[!]|formatid|max|list> [post] [account]
Application Options:
-u, --url= Fediverse instance URL
-l, --login= Fediverse instance login
--dir= Download directory
--youtube-dl-path= Set youtube-dl path
--listen= Listen for authorization code
-q, --quiet Suppress extra output
--default Set specifid url/login/args as default
-f, --fediverse= Fediverse instance URL
-l, --login= Fediverse instance login
-d, --dir= Download directory
-o, --output= Output file
-c, --config= Config file location
-j, --cookie-jar= Cookie jar file
--listen= Listen for authorization code
-u, --username= Nicovideo username
-p, --password= Nicovideo password
-q, --quiet Suppress extra output
--default Set specifid url/login/args as default
Help Options:
-h, --help Show this help message
Available commands:
account
[account command options]
--code= OAuth2 code
```

- To add an account
- Using localhost HTTP server
```sh
./jaroidfedi account -u your.instance.domain -l yourlogin --listen
./jaroidfedi account -f your.instance.domain -l yourlogin --listen
```
this will start local HTTP server on random port.
To choose a specific port or host, you can use e.g. `--listen=:8080`
- To add an account manually
```sh
./jaroidfedi account -u your.instance.domain -l yourlogin
./jaroidfedi account -f your.instance.domain -l yourlogin
```

Either way, URL should be printed, which may look like
Expand All @@ -55,12 +61,12 @@ Available commands:
FE should display a token, which then can be passed to cli as
```sh
./jaroidfedi account -u your.instance.domain -l yourlogin -c yourcode
./jaroidfedi account -f your.instance.domain -l yourlogin --code yourcode
```
You only need to this once, for one account, unless you revoke this token (it should display as `jaroid`) in your account security options.
- To change default instace/account
```sh
./jaroidfedi -u your.instance.domain -l yourlogin --default
./jaroidfedi -f your.instance.domain -l yourlogin --default
```
- You can list available formats with
```sh
Expand All @@ -82,21 +88,13 @@ Available commands:
```sh
./jaroidfedi https://www.nicovideo.jp/watch/sm0000000 max
```
- To download also a danmaku subitles, add 'sub'
```sh
./jaroidfedi https://www.nicovideo.jp/watch/sm0000000 <size[!]|formatid|max> sub
```
- To select danmaku subitles language 'sub:<en|jp|cn>'
```sh
./jaroidfedi https://www.nicovideo.jp/watch/sm0000000 <size[!]|formatid|max> sub:jp
```
- To post a video, add 'post'
```sh
./jaroidfedi https://www.nicovideo.jp/watch/sm0000000 <size[!]|formatid|max> post
```
- To pass extra options to youtube-dl
```sh
./jaroidfedi https://www.nicovideo.jp/watch/sm0000000 -- -u nicovideologin -p nicovideopassword
./jaroidfedi https://www.nicovideo.jp/watch/sm0000000 -u nicovideologin -p nicovideopassword
```
Config
Expand Down Expand Up @@ -148,15 +146,12 @@ global:
user_agent: jaroid
default_instance: your.instance.domain
mediaservice:
default: youtube-dl
youtube_dl:
executable_path: youtube-dl
save_dir: /tmp/jaroid
common_args:
- -u
- [email protected]
- -p
- yournicopassword
save_dir: /tmp/jaroid
cookie_jar: /home/user/.config/jaroid/cookie.jar
keep_files: false
auth:
username:
password:
```
Template
Expand Down
104 changes: 75 additions & 29 deletions cmd/jaroidfedi/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,18 @@ import (
)

var opts struct {
URL *string `short:"u" long:"url" description:"Fediverse instance URL"`
Login *string `short:"l" long:"login" description:"Fediverse instance login"`
Dir *string `long:"dir" description:"Download directory"`
Listen *string `long:"listen" description:"Listen for authorization code" optional:"true" optional-value:":0"`
Acccount struct {
Code string `short:"c" long:"code" description:"OAuth2 code"`
URL *string `short:"f" long:"fediverse" description:"Fediverse instance URL"`
Login *string `short:"l" long:"login" description:"Fediverse instance login"`
Dir *string `short:"d" long:"dir" description:"Download directory (.)"`
Output *string `short:"o" long:"output" description:"Output file"`
Config *string `short:"c" long:"config" description:"Config file location (~/.config/jaroid/fedipost.yml)"`
CookieJar *string `short:"j" long:"cookie-jar" description:"Cookie jar file (~/.config/jaroid/cookie.jar)"`
Listen *string `long:"listen" optional:"true" optional-value:":0" description:"Listen for authorization code"`

NicovideoUsername *string `short:"u" long:"username" description:"Nicovideo username"`
NicovideoPassword *string `short:"p" long:"password" description:"Nicovideo password"`
Acccount struct {
Code string `long:"code" description:"OAuth2 code"`
} `command:"account"`
Quiet bool `short:"q" long:"quiet" description:"Suppress extra output"`
Default bool `long:"default" description:"Set specifid url/login/args as default"`
Expand Down Expand Up @@ -103,7 +109,7 @@ func parseConfig() (c binconfig) {

p := flags.NewParser(&opts, flags.Default)
p.SubcommandsOptional = true
p.Usage = "https://www.nicovideo.jp/watch/sm0000000 <size[!]|formatid|max|list> [sub[:<jp|en|cn>]] [post]"
p.Usage = "https://www.nicovideo.jp/watch/sm0000000 <size[!]|formatid|max|list> [post]"

rest, err := p.ParseArgs(preargs)

Expand Down Expand Up @@ -135,9 +141,9 @@ func parseConfig() (c binconfig) {
if t.Type == flags.ErrHelp {
_, _ = fmt.Printf(
"To add an account\n"+
"%s account -u your.instance.domain -l yourlogin [--listen]\n\n"+
"%s account -f your.instance.domain -l yourlogin [--listen]\n\n"+
"To change default instace/account\n"+
"%s -u your.instance.domain -l yourlogin --default\n\n"+
"%s -f your.instance.domain -l yourlogin --default\n\n"+
"You can list available formats with\n"+
"%s https://www.nicovideo.jp/watch/sm0000000 list\n\n"+
"To download a video with selected format use\n"+
Expand All @@ -148,16 +154,10 @@ func parseConfig() (c binconfig) {
"%s https://www.nicovideo.jp/watch/sm0000000 50m!\n\n"+
"Alternatively preselect a maximum available format\n"+
"%s https://www.nicovideo.jp/watch/sm0000000 max\n\n"+
"To download also a danmaku subitles, add 'sub'\n"+
"%s https://www.nicovideo.jp/watch/sm0000000 <size[!]|formatid|max> sub\n\n"+
"To select danmaku subitles language 'sub:<en|jp|cn>'\n"+
"%s https://www.nicovideo.jp/watch/sm0000000 <size[!]|formatid|max> sub:jp\n\n"+
"To post a video, add 'post'\n"+
"%s https://www.nicovideo.jp/watch/sm0000000 <size[!]|formatid|max> post\n\n"+
"To pass extra options to youtube-dl\n"+
"%s https://www.nicovideo.jp/watch/sm0000000 -- -u nicovideologin -p nicovideopassword\n\n",
os.Args[0],
os.Args[0],
"To provide authentication for nicoideo\n"+
"%s https://www.nicovideo.jp/watch/sm0000000 -u nicovideologin -p nicovideopassword\n\n",
os.Args[0],
os.Args[0],
os.Args[0],
Expand Down Expand Up @@ -258,7 +258,7 @@ func startLocalHTTP(addr string) (string, chan *resp, error) {

func handleAccount(ctx context.Context, c *binconfig, fedipost *app.Fedipost) {
if c.uri == "" || c.login == "" {
fmt.Println("both -u your.instance.domain and -l yourlogin are requied")
fmt.Println("both -f your.instance.domain and -l yourlogin are requied")

os.Exit(1)
}
Expand All @@ -274,7 +274,7 @@ func handleAccount(ctx context.Context, c *binconfig, fedipost *app.Fedipost) {

if c.redirect == "" {
fmt.Printf("\nthen use received authorization code as\n\n")
fmt.Printf("%s account -u %s -l %s -c yourcode\n", os.Args[0], c.uri, c.login)
fmt.Printf("%s account -f %s -l %s --code yourcode\n", os.Args[0], c.uri, c.login)
os.Exit(0)
}
}
Expand Down Expand Up @@ -357,10 +357,34 @@ func startReporter() mediaservice.Reporter {
return reporter
}

func overrides(f *app.Fedipost) {
if opts.CookieJar != nil {
f.Config.Mediaservice.CookieJar = *opts.CookieJar
}

if opts.NicovideoUsername != nil {
f.Config.Mediaservice.Auth.Username = *opts.NicovideoUsername
}

if opts.NicovideoPassword != nil {
f.Config.Mediaservice.Auth.Password = *opts.NicovideoPassword
}

if opts.Dir != nil {
f.Config.Mediaservice.SaveDir = *opts.Dir
}
}

func main() {
c := parseConfig()

fedipost, err := app.New("")
var configpath string

if opts.Config != nil {
configpath = *opts.Config
}

fedipost, err := app.New(configpath, overrides)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -407,21 +431,36 @@ func handleDownload(
) string {
fid := nicopost.FormatFileID(path.Base(c.videourl), c.format)

match, err := nicopost.GlobFind(mediaservicecopy.SaveDir, fid)
if err != nil {
panic(err)
var match string

var err error

if mediaservicecopy.SaveDir != "" {
match, err = nicopost.GlobFind(mediaservicecopy.SaveDir, fid)
if err != nil {
panic(err)
}
}

if match != "" {
if match != "" && opts.Output == nil {
return match
}

err = os.MkdirAll(mediaservicecopy.SaveDir, 0777)
if err != nil {
panic(err)
savedir := mediaservicecopy.SaveDir

if savedir == "" {
savedir, err = os.Getwd()
if err != nil {
panic(err)
}
} else {
err = os.MkdirAll(mediaservicecopy.SaveDir, 0777)
if err != nil {
panic(err)
}
}

match = nicopost.SaveFilepath(mediaservicecopy.SaveDir, c.videourl, c.format)
match = nicopost.SaveFilepath(savedir, c.videourl, c.format)

reporter := mediaservice.NewDummyReporter()

Expand All @@ -446,7 +485,14 @@ func handleDownload(
downopts.Subtitles = append(downopts.Subtitles, c.subs)
}

match, err = downloader.SaveFormat(ctx, c.videourl, c.format, match, downopts)
reuse := true

if opts.Output != nil {
match = *opts.Output
reuse = false
}

match, err = downloader.SaveFormat(ctx, c.videourl, c.format, match, reuse, downopts)
if err != nil {
panic(err)
}
Expand Down
2 changes: 1 addition & 1 deletion discordbot/modules/nico/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ func (mod *module) downloadVideo(ctx context.Context, id string, task *TaskDownl
}
}()

fmtname, err = mod.config.Nicovideo.SaveFormat(ctx, task.VideoURL, task.Format, output, opts)
fmtname, err = mod.config.Nicovideo.SaveFormat(ctx, task.VideoURL, task.Format, output, true, opts)
if err != nil {
opts.Reporter.Submit("ERROR: "+err.Error(), true)

Expand Down
13 changes: 10 additions & 3 deletions fedipost/app/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type Fedipost struct {
}

// New returns new fedipost app with given config path
func New(configpath string) (*Fedipost, error) {
func New(configpath string, overrides func(fp *Fedipost)) (*Fedipost, error) {
if configpath == "" {
homedir, err := os.UserHomeDir()
if err != nil {
Expand All @@ -46,7 +46,7 @@ func New(configpath string) (*Fedipost, error) {
Config: &config.Root{},
}

err := f.Reload()
err := f.Reload(overrides)
if err != nil {
return nil, err
}
Expand All @@ -55,7 +55,7 @@ func New(configpath string) (*Fedipost, error) {
}

// Reload fedipost from config
func (f *Fedipost) Reload() error {
func (f *Fedipost) Reload(overrides func(fp *Fedipost)) error {
err := f.Config.LoadFile(f.ConfigLocation)
if errors.Is(err, os.ErrNotExist) {
err = nil
Expand All @@ -70,6 +70,13 @@ func (f *Fedipost) Reload() error {
return err
}

overrides(f)

err = os.MkdirAll(filepath.Dir(f.Config.Mediaservice.CookieJar), 0777)
if err != nil {
return err
}

var auth *nicovideo.Auth

if f.Config.Mediaservice.Auth.Username != "" && f.Config.Mediaservice.Auth.Password != "" {
Expand Down
9 changes: 0 additions & 9 deletions fedipost/config/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,6 @@ func (r *Root) Save(writer io.Writer) error {
r.Global.UserAgent = DefaultUseragent
}

if r.Mediaservice.SaveDir == "" {
r.Mediaservice.SaveDir = filepath.Join(os.TempDir(), "jaroid")
}

if r.Mediaservice.CookieJar == "" {
homedir, err := os.UserHomeDir()
if err != nil {
Expand All @@ -99,11 +95,6 @@ func (r *Root) Save(writer io.Writer) error {
r.Mediaservice.CookieJar = filepath.Join(homedir, ".config", "jaroid", "cookie.jar")
}

err := os.MkdirAll(filepath.Dir(r.Mediaservice.CookieJar), 0777)
if err != nil {
return err
}

return yaml.NewEncoder(writer).Encode(r)
}

Expand Down
6 changes: 5 additions & 1 deletion integration/nicovideo/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,11 @@ type Client struct {
Config
}

func (client *Client) openCopyFile(f *os.File, outpath, fmtname string) error {
func (client *Client) openCopyFile(f *os.File, outpath, fmtname string, reuse bool) error {
if !reuse {
return nil
}

parts := strings.SplitN(filepath.Base(outpath), "-", 2)

matches, err := filepath.Glob(filepath.Join(filepath.Dir(outpath), parts[0]+"-*-"+fmtname+"*"))
Expand Down
Loading

0 comments on commit 56b416e

Please sign in to comment.