Skip to content

Commit

Permalink
support nicovideo API changes
Browse files Browse the repository at this point in the history
  • Loading branch information
iamtakingiteasy committed Nov 16, 2024
1 parent 4a8b209 commit 94c8f3a
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 29 deletions.
19 changes: 15 additions & 4 deletions cmd/jaroid/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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)
Expand Down
18 changes: 16 additions & 2 deletions cmd/jaroidfedi/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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
}

Expand Down Expand Up @@ -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)
}
Expand All @@ -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{
Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 6 additions & 0 deletions discordbot/bot/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package bot

import (
"github.com/bwmarrin/discordgo"
"time"
)

func (bot *Bot) handlerMessageCreate(session *discordgo.Session, messageCreate *discordgo.MessageCreate) {
Expand All @@ -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)
}

Expand Down
4 changes: 4 additions & 0 deletions discordbot/modules/nico/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion integration/nicovideo/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down
39 changes: 17 additions & 22 deletions integration/nicovideo/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -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[^"]*)"`)
)

Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 94c8f3a

Please sign in to comment.