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