Adding a new flag #2
|
@ -2,6 +2,7 @@ package cmdline
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
|
@ -31,69 +32,78 @@ func recurseNodes(top *html.Node, sb *strings.Builder) {
|
|||
}
|
||||
}
|
||||
|
||||
func searchGoogle(song string) error {
|
||||
func searchGoogle(song string) (err error) {
|
||||
runOption := &playwright.RunOptions{
|
||||
SkipInstallBrowsers: true,
|
||||
}
|
||||
err := playwright.Install(runOption)
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not install playwright dependencies: %v", err)
|
||||
tempErr := playwright.Install(runOption)
|
||||
if tempErr != nil {
|
||||
err = fmt.Errorf("could not install playwright dependencies: %v", tempErr)
|
||||
return err
|
||||
}
|
||||
pw, err := playwright.Run()
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not start playwright: %v", err)
|
||||
pw, tempErr := playwright.Run()
|
||||
if tempErr != nil {
|
||||
err = fmt.Errorf("could not start playwright: %v", tempErr)
|
||||
return err
|
||||
}
|
||||
defer func(pw *playwright.Playwright) error {
|
||||
err := pw.Stop()
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not stop Playwright: %v", err)
|
||||
defer func(pw *playwright.Playwright) {
|
||||
tempErr := pw.Stop()
|
||||
if tempErr != nil {
|
||||
e := fmt.Errorf("could not stop Playwright: %v", tempErr)
|
||||
err = errors.Join(err, e)
|
||||
}
|
||||
return nil
|
||||
}(pw)
|
||||
|
||||
option := playwright.BrowserTypeLaunchOptions{
|
||||
Channel: playwright.String("chrome"),
|
||||
Headless: playwright.Bool(false),
|
||||
}
|
||||
browser, err := pw.Chromium.Launch(option)
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not launch browser: %v", err)
|
||||
browser, tempErr := pw.Chromium.Launch(option)
|
||||
if tempErr != nil {
|
||||
err = fmt.Errorf("could not launch browser: %v", tempErr)
|
||||
return err
|
||||
}
|
||||
|
||||
defer func(browser playwright.Browser) error {
|
||||
err = browser.Close()
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not close browser: %v", err)
|
||||
defer func(browser playwright.Browser) {
|
||||
tempErr = browser.Close()
|
||||
if tempErr != nil {
|
||||
e := fmt.Errorf("could not close browser: %v", tempErr)
|
||||
err = errors.Join(err, e)
|
||||
}
|
||||
return nil
|
||||
}(browser)
|
||||
page, err := browser.NewPage()
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not create page: %v", err)
|
||||
page, tempErr := browser.NewPage()
|
||||
if tempErr != nil {
|
||||
err = fmt.Errorf("could not create page: %v", tempErr)
|
||||
return err
|
||||
}
|
||||
if _, err := page.Goto(fmt.Sprintf("https://www.google.com/search?q=%ss+lyrics", song),
|
||||
if _, tempErr := page.Goto(fmt.Sprintf("https://www.google.com/search?q=%ss+lyrics", song),
|
||||
playwright.PageGotoOptions{
|
||||
WaitUntil: playwright.WaitUntilStateLoad,
|
||||
}); err != nil {
|
||||
return fmt.Errorf("could not goto: %v", err)
|
||||
}); tempErr != nil {
|
||||
err = fmt.Errorf("could not goto: %v", tempErr)
|
||||
return err
|
||||
}
|
||||
err = page.Locator("body").WaitFor(playwright.LocatorWaitForOptions{
|
||||
tempErr = page.Locator("body").WaitFor(playwright.LocatorWaitForOptions{
|
||||
State: playwright.WaitForSelectorStateVisible,
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not wait for body: %v", err)
|
||||
if tempErr != nil {
|
||||
err = fmt.Errorf("could not wait for body: %v", tempErr)
|
||||
return err
|
||||
}
|
||||
html, err := page.Locator("html").InnerHTML()
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not get innerHtml: %v", err)
|
||||
html, tempErr := page.Locator("html").InnerHTML()
|
||||
if tempErr != nil {
|
||||
err = fmt.Errorf("could not get innerHtml: %v", tempErr)
|
||||
return err
|
||||
}
|
||||
doc, err := htmlquery.Parse(bytes.NewReader([]byte(html)))
|
||||
doc, tempErr := htmlquery.Parse(bytes.NewReader([]byte(html)))
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not parse the innerHtml: %v", err)
|
||||
err = fmt.Errorf("could not parse the innerHtml: %v", tempErr)
|
||||
return err
|
||||
}
|
||||
nodes, err := htmlquery.QueryAll(doc, "//div[@data-lyricid]/div")
|
||||
nodes, tempErr := htmlquery.QueryAll(doc, "//div[@data-lyricid]/div")
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not get the nodes: %v", err)
|
||||
err = fmt.Errorf("could not get the nodes: %v", tempErr)
|
||||
return err
|
||||
}
|
||||
|
||||
var sb strings.Builder
|
||||
|
@ -105,9 +115,10 @@ func searchGoogle(song string) error {
|
|||
mainLog.Println("Writing lyrics from Google...")
|
||||
}
|
||||
filename := fmt.Sprintf("%s_google.txt", outputFile)
|
||||
err = os.WriteFile(filename, []byte(sb.String()), os.ModePerm)
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not write to %s: %v", filename, err)
|
||||
tempErr = os.WriteFile(filename, []byte(sb.String()), os.ModePerm)
|
||||
if tempErr != nil {
|
||||
err = fmt.Errorf("could not write to %s: %v", filename, err)
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
mainLog.Println("Lyrics cannot be found...")
|
||||
|
@ -149,7 +160,10 @@ func Main() int {
|
|||
useGoogleFlag := flags.Bool("google", false, "Optional. Use google.")
|
||||
useGeniusFlag := flags.Bool("genius", false, "Optional. Use genius")
|
||||
|
||||
flags.Parse(os.Args[1:])
|
||||
err := flags.Parse(os.Args[1:])
|
||||
if err != nil {
|
||||
return 1
|
||||
}
|
||||
|
||||
if len(flags.Args()) > 1 {
|
||||
errorLog.Println("Error: too many command-line arguments")
|
||||
|
|
Loading…
Reference in New Issue