Skip to content

Commit

Permalink
Merge pull request #95 from yann0917/develop
Browse files Browse the repository at this point in the history
🎨 支持扫码登录
  • Loading branch information
yann0917 authored Jul 15, 2022
2 parents 08c2d2e + fa372ad commit eb98109
Show file tree
Hide file tree
Showing 17 changed files with 816 additions and 562 deletions.
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ docker images|grep none|awk '{print $3 }'|xargs docker rmi

## 使用方法

* 电脑端登录 [得到](https://www.dedao.cn),以便生成 cookie ☆☆☆☆☆
* 使用 `dedao-dl login -q` **同时支持「得到App」和「微信」扫码扫码登录**,或者电脑端登录 [得到](https://www.dedao.cn) 生成 cookie 使用 `dedao-dl login -c "xxxxxxxx"` 登录 ☆☆☆☆☆

`dedao-dl -h` 可查看帮助说明,每个命令都有 `-h` 参数可查看该命令的用法

Expand All @@ -96,7 +96,6 @@ Available Commands:
who 查看当前登录的用户
```

`dedao-dl login` 登录,不带任何参数的情况下,默认使用 [`go-rod/rod`](https://github.com/go-rod/rod) 从浏览器获取cookie. 如果无法自动获取 cookie,则使用 `dedao-dl login -c "xxxxxxxx"` 登录

`dedao-dl cat` 获取课程分类

Expand Down Expand Up @@ -240,7 +239,6 @@ Available Commands:

* [geektime-dl](https://github.com/mmzou/geektime-dl)
* [annie](https://github.com/iawia002/annie)
* [m3u8](https://github.com/oopsguy/m3u8)

## Stargazers over time

Expand Down
40 changes: 40 additions & 0 deletions cmd/app/login.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package app

import (
"fmt"
"strings"
"time"

"github.com/go-rod/rod"
"github.com/pkg/errors"
"github.com/yann0917/dedao-dl/config"
"github.com/yann0917/dedao-dl/services"
"github.com/yann0917/dedao-dl/utils"
Expand All @@ -28,7 +31,44 @@ func GetCookie() (cookie string) {
_ = rod.Try(func() {
cookie = utils.Get(config.BaseURL)
if !strings.Contains(cookie, "ISID=") {
return
}
})
return
}

func LoginByQr() error {
token, err := getService().LoginAccessToken()
if err != nil {
return err
}
code, err := getService().GetQrcode(token)
if err != nil {
return err
}

ticker := time.NewTicker(time.Duration(1) * time.Second)
fmt.Println("同时支持「得到App」和「微信」扫码")
for {
select {
case <-ticker.C:
check, cookie, err := getService().CheckLogin(token, code.Data.QrCodeString)
if err != nil {
return err
}
if check.Data.Status == 1 {
LoginByCookie(cookie)
fmt.Println("扫码成功")
return nil
} else if check.Data.Status == 2 {
err = errors.New("登录失败,二维码已过期")
return err
}
case <-ticker.C:
//10分钟后二维码失效
case <-time.After(600 * time.Second):
err = errors.New("登录失败,二维码已过期")
return err
}
}
}
11 changes: 1 addition & 10 deletions cmd/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,16 +96,7 @@ func download(cType string, id, aid int) error {
if err := downloader.Download(datum, stream, path); err != nil {
errors = append(errors, err)
}
// use m3u8 downloader
// downloader, err := downloader.NewTask(path, datum.M3U8URL)
// if err != nil {
// fmt.Println(err)
// errors = append(errors, err)
// }
// outName := datum.Title + ".mp3"
// if err := downloader.Start(20, outName); err != nil {
// errors = append(errors, err)
// }

}
if len(errors) > 0 {
return errors[0]
Expand Down
13 changes: 11 additions & 2 deletions cmd/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,30 @@ import (

// Cookie cookie from https://www.dedao.cn
var Cookie string
var qr bool

// Login login
var loginCmd = &cobra.Command{
Use: "login",
Short: "登录得到 pc 端 https://www.dedao.cn",
Long: `使用 dedao-dl login to login https://www.dedao.cn`,
RunE: func(cmd *cobra.Command, args []string) error {
if qr {
err := app.LoginByQr()
return err
}
if Cookie == "" {
defaultCookie := app.GetCookie()
if defaultCookie == "" {
return errors.New("自动获取 cookie 失败,请使用参数设置 cookie")
}
Cookie = defaultCookie
} else {
err := app.LoginByCookie(Cookie)
return err
}
err := app.LoginByCookie(Cookie)
return err
return nil

},

PostRun: func(cmd *cobra.Command, args []string) {
Expand Down Expand Up @@ -82,6 +90,7 @@ func init() {
rootCmd.AddCommand(usersCmd)
rootCmd.AddCommand(suCmd)
loginCmd.Flags().StringVarP(&Cookie, "cookie", "c", "", "cookie from https://www.dedao.cn")
loginCmd.Flags().BoolVarP(&qr, "qrcode", "q", false, "scan qrcode login from https://www.dedao.cn")
}

// LoginedCookies cookie sting to map for chromedp print pdf
Expand Down
16 changes: 14 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package config

import (
"fmt"
"io"
"io/ioutil"
"os"
"path"
Expand Down Expand Up @@ -66,6 +68,7 @@ func (c *ConfigsData) Init() error {
return nil
}

fmt.Printf("xxxxxx %#v\n", c.activeUser)
if c.activeUser != nil {
c.service = c.activeUser.New()
}
Expand Down Expand Up @@ -93,6 +96,10 @@ func (c *ConfigsData) initActiveUser() error {
}
}

if c.AcitveUID == "" && len(c.Users) == 0 {
c.activeUser = new(Dedao)
}

if len(c.Users) > 0 {
return errors.New("存在登录的用户,可以进行切换登录用户")
}
Expand All @@ -104,6 +111,7 @@ func (c *ConfigsData) initActiveUser() error {
func (c *ConfigsData) Save() error {
err := c.lazyOpenConfigFile()
if err != nil {
fmt.Println(err)
return err
}

Expand All @@ -124,16 +132,19 @@ func (c *ConfigsData) Save() error {
// 减掉多余的部分
err = c.configFile.Truncate(int64(len(data)))
if err != nil {
fmt.Println(err)
return err
}

_, err = c.configFile.Seek(0, os.SEEK_SET)
_, err = c.configFile.Seek(0, io.SeekStart)
if err != nil {
fmt.Println(err)
return err
}

_, err = c.configFile.Write(data)
if err != nil {
fmt.Println(err)
return err
}

Expand All @@ -142,6 +153,7 @@ func (c *ConfigsData) Save() error {

func (c *ConfigsData) initDefaultConfig() {
// todo 默认配置
c.Save()
}

func (c *ConfigsData) loadConfigFromFile() error {
Expand All @@ -162,7 +174,7 @@ func (c *ConfigsData) loadConfigFromFile() error {
c.fileMu.Lock()
defer c.fileMu.Unlock()

_, err = c.configFile.Seek(0, os.SEEK_SET)
_, err = c.configFile.Seek(0, io.SeekStart)
if err != nil {
return nil
}
Expand Down
Loading

0 comments on commit eb98109

Please sign in to comment.