Skip to content

Commit

Permalink
Feature/v0.10.7 (#177)
Browse files Browse the repository at this point in the history
* fix #154  pdf code block missing bug

* fix cookie expire problem

* update log format

* Add more logs

* fix #176  support not encrypt video
  • Loading branch information
nicoxiang authored Mar 16, 2024
1 parent a25ec30 commit 6b30cf3
Show file tree
Hide file tree
Showing 8 changed files with 149 additions and 80 deletions.
1 change: 0 additions & 1 deletion cmd/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ func checkError(err error) {
errors.Is(err, promptui.ErrInterrupt) {
os.Exit(1)
} else if errors.As(err, &eg) {
logger.Error(err, eg.ResponseString)
exitWithMsg(err.Error())
} else if errors.Is(err, geektime.ErrWrongPassword) ||
errors.Is(err, geektime.ErrTooManyLoginAttemptTimes) {
Expand Down
40 changes: 21 additions & 19 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,31 +36,35 @@ func ReadCookieFromConfigFile(phone string) ([]*http.Cookie, error) {
if len(files) == 0 {
return nil, nil
}
now := time.Now()
for _, fi := range files {
if fi.IsDir() {
continue
}
if strings.HasPrefix(fi.Name(), phone) {
fullName := filepath.Join(userConfigDir, GeektimeDownloaderFolder, fi.Name())
var cookies []*http.Cookie
oneyear := time.Now().Add(180 * 24 * time.Hour)

data, err := ioutil.ReadFile(fullName)
if err != nil {
return nil, err
}

for _, line := range strings.Split(string(data), "\n") {
s := strings.SplitN(line, " ", 2)
if len(s) != 2 {
s := strings.SplitN(line, " ", 3)
if len(s) != 3 {
continue
}
t, err := time.Parse("2006-01-02 15:04:05.999999999 -0700 MST", s[2])
if err != nil || t.Before(now) {
break
}
cookies = append(cookies, &http.Cookie{
Name: s[0],
Value: s[1],
Domain: geektime.GeekBangCookieDomain,
HttpOnly: true,
Expires: oneyear,
Expires: t,
})
}
return cookies, nil
Expand All @@ -75,24 +79,16 @@ func WriteCookieToConfigFile(phone string, cookies []*http.Cookie) error {
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
return err
}
files, err := ioutil.ReadDir(dir)
if err != nil {
return err
}
for _, fi := range files {
// config file already exists
if strings.HasPrefix(fi.Name(), phone) {
return nil
}
}
removeConfig(dir, phone)

file, err := ioutil.TempFile(dir, phone)
if err != nil {
return err
}
defer file.Close()
var sb strings.Builder
for _, v := range cookies {
sb = writeOnelineConfig(sb, v.Name, v.Value)
sb = writeOnelineConfig(sb, v)
}
if _, err := file.Write([]byte(sb.String())); err != nil {
return err
Expand All @@ -103,6 +99,10 @@ func WriteCookieToConfigFile(phone string, cookies []*http.Cookie) error {
// RemoveConfig remove specified users' config
func RemoveConfig(phone string) error {
dir := filepath.Join(userConfigDir, GeektimeDownloaderFolder)
return removeConfig(dir, phone)
}

func removeConfig(dir, phone string) error {
files, err := ioutil.ReadDir(dir)
if err != nil {
return err
Expand All @@ -121,13 +121,15 @@ func RemoveConfig(phone string) error {
}
}
}
return nil
return nil;
}

func writeOnelineConfig(sb strings.Builder, key string, value string) strings.Builder {
sb.WriteString(key)
func writeOnelineConfig(sb strings.Builder, cookie *http.Cookie) strings.Builder {
sb.WriteString(cookie.Name)
sb.WriteString(" ")
sb.WriteString(cookie.Value)
sb.WriteString(" ")
sb.WriteString(value)
sb.WriteString(cookie.Expires.String())
sb.WriteString("\n")
return sb
}
57 changes: 48 additions & 9 deletions internal/geektime/geektime.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,12 +167,21 @@ func (c *Client) Login(phone, password string) ([]*http.Cookie, error) {
},
&res,
)

logger.Infof("Login request start")
resp, err := r.Execute(r.Method, r.URL)

if err != nil {
return nil, err
}

if resp.RawResponse.StatusCode != 200 || res.Code != 0 {
logger.Warnf("Login request end, status code: %d, response body: %s",
resp.RawResponse.StatusCode,
resp.String(),
)
}

if res.Code == 0 {
var cookies []*http.Cookie
for _, c := range resp.Cookies() {
Expand Down Expand Up @@ -206,13 +215,20 @@ func (c *Client) Auth(cs []*http.Cookie) error {
res,
)
r.SetHeader(Origin, DefaultBaseURL)

logger.Infof("Auth request start")
resp, err := r.Execute(r.Method, r.URL)

if err != nil {
return err
}

if resp.RawResponse.StatusCode == 452 || res.Code != 0 {
if resp.RawResponse.StatusCode != 200 || res.Code != 0 {
logger.Warnf("Auth request end, status code: %d, response body: %s",
resp.RawResponse.StatusCode,
resp.String(),
)

// result Code -1
// {\"error\":{\"msg\":\"未登录\",\"code\":-2000}
return ErrAuthFailed
Expand Down Expand Up @@ -394,10 +410,10 @@ func (c *Client) MyClassProduct(classID int) (Product, error) {
}

p = Product{
Access: true,
ID: classID,
Title: res.Data.Title,
Type: "",
Access: true,
ID: classID,
Title: res.Data.Title,
Type: "",
IsVideo: true,
}
var articles []Article
Expand Down Expand Up @@ -433,16 +449,24 @@ func (c *Client) newRequest(method, url string, params map[string]string, body i
}

func do(r *resty.Request) (*resty.Response, error) {
logger.Infof("Http request start, method: %s, url: %s",
r.Method,
r.URL,
)
resp, err := r.Execute(r.Method, r.URL)

if err != nil {
return nil, err
}

if resp.RawResponse.StatusCode == 451 {
return nil, ErrGeekTimeRateLimit
} else if resp.RawResponse.StatusCode == 452 {
return nil, ErrAuthFailed
statusCode := resp.RawResponse.StatusCode
if statusCode != 200 {
logNotOkResponse(resp)
if statusCode == 451 {
return nil, ErrGeekTimeRateLimit
} else if statusCode == 452 {
return nil, ErrAuthFailed
}
}

rv := reflect.ValueOf(r.Result)
Expand All @@ -453,5 +477,20 @@ func do(r *resty.Request) (*resty.Response, error) {
return resp, nil
}

logNotOkResponse(resp)
//未登录或者已失效
if code == -3050 || code == -2000 {
return nil, ErrAuthFailed
}

return nil, ErrGeekTimeAPIBadCode{r.URL, resp.String()}
}

func logNotOkResponse(resp *resty.Response) {
logger.Warnf("Http request end, method: %s, url: %s, status code: %d, response body: %s",
resp.RawResponse.Request.Method,
resp.RawResponse.Request.URL,
resp.RawResponse.StatusCode,
resp.String(),
)
}
10 changes: 9 additions & 1 deletion internal/pdf/pdf.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,14 @@ func hideRedundantElements(downloadComments bool) chromedp.ActionFunc {
return chromedp.ActionFunc(func(ctx context.Context) error {
s :=
`
var headMain = document.getElementsByClassName('main')[0];
if(headMain){
headMain.style.display="none";
}
var bottomWrapper = document.getElementsByClassName('sub-bottom-wrapper')[0];
if(bottomWrapper){
bottomWrapper.style.display="none";
}
var openAppdiv = document.getElementsByClassName('openApp')[0];
if(openAppdiv){
openAppdiv.parentNode.parentNode.parentNode.style.display="none";
Expand Down Expand Up @@ -177,4 +185,4 @@ func printToPDF(res *[]byte) chromedp.ActionFunc {
*res = data
return nil
})
}
}
2 changes: 1 addition & 1 deletion internal/pkg/downloader/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func retry(attempts int, sleep time.Duration, f func() error) (err error) {
time.Sleep(sleep)
sleep *= 2

logger.Info("retry hanppen, times: " + strconv.Itoa(i))
logger.Infof("retry hanppen, times: %s", strconv.Itoa(i))
}
err = f()
if err == nil || errors.Is(err, context.Canceled) {
Expand Down
53 changes: 34 additions & 19 deletions internal/pkg/logger/logger.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package logger

import (
"fmt"
"os"
"path/filepath"
"runtime"

"github.com/sirupsen/logrus"
)
Expand All @@ -16,13 +18,36 @@ var (
logger = logrus.New()
)

type customFormatter struct {
}

// Format custom logrus log format
func (f *customFormatter) Format(entry *logrus.Entry) ([]byte, error) {
// Get the file and line number where the log was called
_, filename, line, _ := runtime.Caller(7)

// Get the script name from the full file path
fullPathName := filepath.Base(filename)

// Format the log message
message := fmt.Sprintf("[%s] [%s] [%s:%d] %s\n",
entry.Time.Format("2006-01-02 15:04:05"), // Date-time
entry.Level.String(), // Log level
fullPathName, // Full path name
line, // Line number
entry.Message, // Log message
)

return []byte(message), nil
}

func init(){
userConfigDir, _ := os.UserConfigDir()
logFilePath := filepath.Join(userConfigDir, GeektimeLogFolder, GeektimeLogFolder + ".log")

logger.SetFormatter(&logrus.TextFormatter{})
logger.SetReportCaller(false)
logger.SetLevel(logrus.WarnLevel)
logger.SetReportCaller(true)
logger.SetLevel(logrus.InfoLevel)
logger.SetFormatter(&customFormatter{})
logFile, err := os.OpenFile(logFilePath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err == nil {
logger.Out = logFile
Expand All @@ -32,24 +57,14 @@ func init(){
logger.SetOutput(logFile)
}

// Trace wrapper logrus log.Trace
func Trace(args ...interface{}) {
logger.Log(logrus.TraceLevel, args...)
}

// Debug wrapper logrus log.Debug
func Debug(args ...interface{}) {
logger.Log(logrus.DebugLevel, args...)
}

// Info wrapper logrus log.Info
func Info(args ...interface{}) {
logger.Log(logrus.InfoLevel, args...)
// Infof wrapper logrus log.Infof
func Infof(format string, args ...interface{}) {
logger.Logf(logrus.InfoLevel, format, args...)
}

// Warn wrapper logrus log.Warn
func Warn(args ...interface{}) {
logger.Log(logrus.WarnLevel, args...)
// Warnf wrapper logrus log.Warnf
func Warnf(format string, args ...interface{}) {
logger.Logf(logrus.WarnLevel, format, args...)
}

// Error wrapper logrus log.Error
Expand Down
8 changes: 4 additions & 4 deletions internal/pkg/m3u8/m3u8.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ var (
linePattern = regexp.MustCompile(`([a-zA-Z-]+)=("[^"]+"|[^",]+)`)
)

// Parse do m3u8 url GET request, and extract ts file names and decrypt key from that
func Parse(client *geektime.Client, m3u8url string) (tsFileNames []string, keyURI string, err error) {
// Parse do m3u8 url GET request, and extract ts file names and check if it's encrypt video
func Parse(client *geektime.Client, m3u8url string) (tsFileNames []string, isVodEncryptVideo bool, err error) {
m3u8Resp, err := client.HTTPClient.R().SetDoNotParseResponse(true).Get(m3u8url)
if err != nil {
return nil, "", err
return nil, false, err
}
defer m3u8Resp.RawBody().Close()
s := bufio.NewScanner(m3u8Resp.RawBody())
Expand All @@ -33,7 +33,7 @@ func Parse(client *geektime.Client, m3u8url string) (tsFileNames []string, keyUR
if strings.HasPrefix(line, "#EXT-X-KEY") && !gotKeyURI {
// ONLY Method and URI, IV not present
params := parseLineParameters(line)
keyURI, gotKeyURI = params["URI"], true
isVodEncryptVideo, gotKeyURI = params["MEATHOD"] == "AES-128", true
}
if !strings.HasPrefix(line, "#") && strings.HasSuffix(line, ".ts") {
tsFileNames = append(tsFileNames, line)
Expand Down
Loading

0 comments on commit 6b30cf3

Please sign in to comment.