diff --git a/cmd/jaroid/main.go b/cmd/jaroid/main.go index 7a15bb1..c2038db 100644 --- a/cmd/jaroid/main.go +++ b/cmd/jaroid/main.go @@ -3,10 +3,6 @@ package main import ( "flag" - "net/http" - "os" - "path/filepath" - "github.com/bwmarrin/discordgo" "github.com/eientei/cookiejarx" "github.com/eientei/jaroid/discordbot/bot" @@ -24,9 +20,13 @@ import ( "github.com/eientei/jaroid/discordbot/modules/reply" "github.com/eientei/jaroid/discordbot/modules/rolereact" "github.com/eientei/jaroid/integration/nicovideo" + "github.com/eientei/jaroid/mediaservice" "github.com/eientei/jaroid/util/httputil/middleware" redis "github.com/go-redis/redis/v7" "github.com/sirupsen/logrus" + "net/http" + "os" + "path/filepath" ) func readConfig(log *logrus.Logger, configPath string) *botConfig.Root { @@ -55,6 +55,7 @@ func main() { log := logrus.New() configPath := flag.String("c", "config.yml", "Configuration file") + login := flag.Bool("l", false, "cache login cookies") flag.Parse() @@ -149,6 +150,16 @@ func main() { log.Fatal(err) } + if *login { + reporter := mediaservice.NewReporter(0, 16, os.Stdin) + err = b.Nicovideo.CacheAuth(reporter) + if err != nil { + panic(err) + } + + os.Exit(0) + } + err = b.Serve() if err != nil { log.Fatal(err) diff --git a/cmd/jaroidfedi/main.go b/cmd/jaroidfedi/main.go index a0aa71b..650d1b6 100644 --- a/cmd/jaroidfedi/main.go +++ b/cmd/jaroidfedi/main.go @@ -28,6 +28,7 @@ var opts struct { 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"` + NicovideoLogin bool `short:"n" long:"nicologin" description:"Nicovideo login and exit"` NicovideoUsername *string `short:"u" long:"username" description:"Nicovideo username"` NicovideoPassword *string `short:"p" long:"password" description:"Nicovideo password"` Acccount struct { @@ -54,6 +55,7 @@ type binconfig struct { post bool preview bool list bool + nicologin bool } func parseRest(p *flags.Parser, rest []string, c *binconfig) error { @@ -76,7 +78,7 @@ func parseRest(p *flags.Parser, rest []string, c *binconfig) error { } } - if opts.Default || c.command != "" { + if opts.Default || c.command != "" || c.nicologin { return nil } @@ -122,6 +124,8 @@ func parseConfig() (c binconfig) { c.command = p.Active.Name } + c.nicologin = opts.NicovideoLogin + if err == nil { err = parseRest(p, rest, &c) } @@ -133,7 +137,7 @@ func parseConfig() (c binconfig) { } } - if err == nil && c.videourl == "" && !opts.Default && p.Active == nil { + if err == nil && c.videourl == "" && !opts.Default && p.Active == nil && !opts.NicovideoLogin { p.WriteHelp(os.Stdout) err = &flags.Error{ @@ -394,6 +398,16 @@ func main() { panic(err) } + if c.nicologin { + reporter := mediaservice.NewReporter(0, 16, os.Stdin) + err = fedipost.Client.CacheAuth(reporter) + if err != nil { + panic(err) + } + + os.Exit(0) + } + ctx := context.Background() if opts.Default { diff --git a/discordbot/bot/handler.go b/discordbot/bot/handler.go index 72c51c6..e285d27 100644 --- a/discordbot/bot/handler.go +++ b/discordbot/bot/handler.go @@ -2,6 +2,7 @@ package bot import ( "github.com/bwmarrin/discordgo" + "time" ) func (bot *Bot) handlerMessageCreate(session *discordgo.Session, messageCreate *discordgo.MessageCreate) { @@ -25,6 +26,11 @@ func (bot *Bot) handlerMessageUpdate(session *discordgo.Session, messageUpdate * guild := bot.guild(messageUpdate.GuildID) + if messageUpdate.Message.EditedTimestamp == nil { + t := time.Now() + messageUpdate.Message.EditedTimestamp = &t + } + _ = bot.Router.Dispatch(session, guild.prefixes, session.State.User.ID, messageUpdate.Message) } diff --git a/discordbot/modules/nico/api.go b/discordbot/modules/nico/api.go index 0352376..b8195a2 100644 --- a/discordbot/modules/nico/api.go +++ b/discordbot/modules/nico/api.go @@ -289,6 +289,10 @@ func (mod *module) executeFeed(ctx context.Context, feed *feed) error { } func (mod *module) commandDownload(ctx *router.Context) error { + if ctx.Message.EditedTimestamp != nil { + return nil + } + if len(ctx.Args) < 2 { return ErrInvalidArgumentNumber } diff --git a/integration/nicovideo/api.go b/integration/nicovideo/api.go index e62293a..05101c6 100644 --- a/integration/nicovideo/api.go +++ b/integration/nicovideo/api.go @@ -14,7 +14,7 @@ import ( ) const ( - baseVideoURI = "https://api.search.nicovideo.jp/api/v2/snapshot/video/contents/search" + baseVideoURI = "https://snapshot.search.nicovideo.jp/api/v2/snapshot/video/contents/search" thumbURI = "https://ext.nicovideo.jp/api/getthumbinfo/" loginURI = "https://account.nicovideo.jp/api/v1/login" ) diff --git a/integration/nicovideo/download.go b/integration/nicovideo/download.go index 32d0723..496126f 100644 --- a/integration/nicovideo/download.go +++ b/integration/nicovideo/download.go @@ -25,7 +25,7 @@ import ( ) var ( - dataAPIDataRegex = regexp.MustCompile(`data-api-data="([^"]+)"`) + dataAPIDataRegex = regexp.MustCompile(`name="server-response"\s+content="([^"]+)"`) otpRegexp = regexp.MustCompile(`action="(/mfa[^"]*)"`) ) @@ -391,22 +391,6 @@ func (client *Client) methodPage( return nil, err } - if resp.StatusCode/100 != 2 { - defer func() { - _ = resp.Body.Close() - }() - - l, _ := strconv.Atoi(resp.Header.Get("Content-Language")) - - if l < 2048 { - bs, _ := io.ReadAll(resp.Body) - - return nil, fmt.Errorf("http response %d: %s", resp.StatusCode, string(bs)) - } - - return nil, fmt.Errorf("http response %d", resp.StatusCode) - } - return resp, nil } @@ -473,12 +457,19 @@ func (client *Client) postPage(ctx context.Context, url string, body io.Reader, return io.ReadAll(resp.Body) } +func (client *Client) CacheAuth(reporter mediaservice.Reporter) error { + _, err := client.auth(reporter) + + return err +} + func (client *Client) fetchInitPage(ctx context.Context, url string, reporter mediaservice.Reporter) ([]byte, error) { reporter.Submit("Downloading video metadata...", false) bs, err := client.getPage(ctx, url) - anonymous := strings.Contains(string(bs), "'not_login'") || strings.Contains(string(bs), "NEED_LOGIN") + anonymous := strings.Contains(string(bs), "'not_login'") || + strings.Contains(string(bs), "NEED_LOGIN") if anonymous && client.Auth != nil && !client.Auth.invalid { var succ bool @@ -623,16 +614,20 @@ func (client *Client) fetchAPIData(ctx context.Context, url string, reporter med return nil, fmt.Errorf("no api data") } - data := &APIData{} + var resdata struct { + Data struct { + Response APIData `json:"response"` + } `json:"data"` + } - err = json.Unmarshal(([]byte)(html.UnescapeString(parts[0][1])), data) + err = json.Unmarshal(([]byte)(html.UnescapeString(parts[0][1])), &resdata) if err != nil { return nil, err } - data.Created = time.Now() + resdata.Data.Response.Created = time.Now() - return data, nil + return &resdata.Data.Response, nil } // ListFormats returns available media format list for api response