Skip to content

Instantly share code, notes, and snippets.

@WakeupTsai
Created October 19, 2018 08:48
Show Gist options
  • Save WakeupTsai/96a60f7d39b77f7fff2806def7c77046 to your computer and use it in GitHub Desktop.
Save WakeupTsai/96a60f7d39b77f7fff2806def7c77046 to your computer and use it in GitHub Desktop.
package main
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"strings"
"github.com/gocolly/colly"
"github.com/manifoldco/promptui"
"github.com/olekukonko/tablewriter"
"github.com/tealeg/xlsx"
)
func main() {
list := []string{}
table := map[string]string{}
// Instantiate default collector
c := colly.NewCollector(
// Visit only domains: tfi.org.tw
colly.AllowedDomains("www.tfi.org.tw"),
)
// On every a element which has tr attribute call callback
c.OnHTML("tr", func(e *colly.HTMLElement) {
ch := e.DOM.Children()
if link, _ := ch.Eq(4).Children().Eq(0).Attr("href"); link != "" {
title := ch.Eq(1).Text()
list = append(list, title)
test, _ := ch.Eq(4).Children().Eq(0).Attr("onclick")
ok := strings.Split(test, "'")
table[title] = ok[1]
}
})
c.Visit("https://www.tfi.org.tw/BoxOfficeBulletin/weekly")
prompt := promptui.Select{
Label: "Select Week",
Items: list,
Size: 20,
}
_, result, err := prompt.Run()
if err != nil {
fmt.Printf("Prompt failed %v\n", err)
return
}
fileURL := "https://www.tfi.org.tw" + table[result]
err = DownloadFile("avatar.xlsx", fileURL)
if err != nil {
panic(err)
}
}
func print(xlFile *xlsx.File) {
data := [][]string{}
table := tablewriter.NewWriter(os.Stdout)
table.SetAutoMergeCells(true)
table.SetRowLine(true)
for _, sheet := range xlFile.Sheets {
table.SetHeader([]string{sheet.Rows[0].Cells[0].String(), sheet.Rows[0].Cells[1].String(), sheet.Rows[0].Cells[2].String(), sheet.Rows[0].Cells[3].String()})
for _, row := range sheet.Rows[1:] {
data = append(data, []string{row.Cells[0].String(), row.Cells[1].String(), row.Cells[2].String(), row.Cells[3].String()})
}
}
table.AppendBulk(data)
table.Render() // Send output
}
// DownloadFile will download a url to a local file. It's efficient because it will
// write as it downloads and not load the whole file into memory.
func DownloadFile(filepath string, url string) error {
// Get the data
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
xlFile, err := xlsx.OpenBinary(body)
if err != nil {
return nil
}
// gogogogo
old := os.Stdout // keep backup of the real stdout
r, w, _ := os.Pipe()
os.Stdout = w
//table.Render()
print(xlFile)
outC := make(chan string)
// copy the output in a separate goroutine so printing can't block indefinitely
go func() {
var buf bytes.Buffer
io.Copy(&buf, r)
outC <- buf.String()
}()
// back to normal state
w.Close()
os.Stdout = old // restoring the real stdout
out := <-outC
menu := []string{}
for _, line := range strings.Split(out, "\n") {
menu = append(menu, line)
}
prompt := promptui.Select{
Label: "Select Movie",
Items: menu,
Size: 20,
}
_, _, err = prompt.Run()
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment