Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
finviz/finviz
fixtures/*
test_data/*
testdata/*
vendor/

# Testing
Expand Down
169 changes: 169 additions & 0 deletions calendar/calendar.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
// Copyright (c) 2022 James Bury. All rights reserved.
// Project site: https://github.com/d3an/finviz
// Use of this source code is governed by a MIT-style license that
// can be found in the LICENSE file for the project.

package calendar

import (
"fmt"
"io/ioutil"
"net"
"net/http"
"strconv"
"sync"
"time"

"github.com/PuerkitoBio/goquery"
"github.com/corpix/uarand"
"github.com/dnaeon/go-vcr/recorder"
"github.com/go-gota/gota/dataframe"

"github.com/d3an/finviz/utils"
)

const (
APIURL = "https://finviz.com/calendar.ashx"
YEAR = 2022
)

var (
once sync.Once
instance *Client
)

type Config struct {
userAgent string
recorder *recorder.Recorder
}

type Client struct {
*http.Client
config Config
}

func New(config *Config) *Client {
once.Do(func() {
transport := &http.Transport{
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
}).DialContext,
TLSHandshakeTimeout: 30 * time.Second,
}
client := &http.Client{
Timeout: 30 * time.Second,
Transport: transport,
}
if config != nil {
instance = &Client{Client: client, config: *config}
}
instance = &Client{
Client: client,
config: Config{userAgent: uarand.GetRandom()},
}
})

return instance
}

func (c *Client) Do(req *http.Request) (*http.Response, error) {
req.Header.Set("User-Agent", c.config.userAgent)
return c.Client.Do(req)
}

func (c *Client) GetCalendar() (*dataframe.DataFrame, error) {
req, err := http.NewRequest(http.MethodGet, APIURL, http.NoBody)
if err != nil {
return nil, err
}

resp, err := c.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}

if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("error getting calendar, status code: '%d', body: '%s'", resp.StatusCode, string(body))
}

doc, err := utils.GenerateDocument(body)
if err != nil {
return nil, err
}

results, err := Scrape(doc)
if err != nil {
return nil, err
}

df := dataframe.LoadRecords(results)
return &df, nil
}

func Scrape(doc *goquery.Document) ([][]string, error) {
var year int
var err error

year, err = strconv.Atoi(doc.Find(".copyright").Text()[89:93])
if err != nil {
year = YEAR
}

data := doc.Find("tr .calendar-header")

var dow string
var calendarDataSlice []map[string]interface{}
data.Each(func(i int, day *goquery.Selection) {
dow = day.Children().Eq(0).Text()

day.Parent().Children().Each(func(j int, event *goquery.Selection) {
if j == 0 {
return
}
var calendarEvent = make(map[string]interface{})
event.Children().Each(func(k int, eventDetails *goquery.Selection) {
switch k {
case 0:
date, err := time.Parse("Mon Jan 02 2006 3:04 PM MST", fmt.Sprintf("%s %d %s EST", dow, year, eventDetails.Text()))
if err != nil {
calendarEvent["Date"] = eventDetails.Text()
}
calendarEvent["Date"] = date.Format(time.RFC3339)
case 1:
return
case 2:
calendarEvent["Release"] = eventDetails.Text()
case 3:
switch eventDetails.Find("img").Eq(0).AttrOr("src", "") {
default:
calendarEvent["Impact"] = "-"
case "gfx/calendar/impact_3.gif":
calendarEvent["Impact"] = "critical"
case "gfx/calendar/impact_2.gif":
calendarEvent["Impact"] = "moderate"
case "gfx/calendar/impact_1.gif":
calendarEvent["Impact"] = "low"
}
case 4:
calendarEvent["For"] = eventDetails.Text()
case 5:
calendarEvent["Actual"] = eventDetails.Text()
case 6:
calendarEvent["Expected"] = eventDetails.Text()
case 7:
calendarEvent["Prior"] = eventDetails.Text()
}
})
calendarDataSlice = append(calendarDataSlice, calendarEvent)
})
})

headers := []string{"Date", "Release", "Impact", "For", "Actual", "Expected", "Prior"}
return utils.GenerateRows(headers, calendarDataSlice)
}
57 changes: 57 additions & 0 deletions calendar/calendar_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright (c) 2022 James Bury. All rights reserved.
// Project site: https://github.com/d3an/finviz
// Use of this source code is governed by a MIT-style license that
// can be found in the LICENSE file for the project.

package calendar

import (
"net"
"net/http"
"testing"
"time"

"github.com/corpix/uarand"
"github.com/dnaeon/go-vcr/recorder"
"github.com/stretchr/testify/require"

"github.com/d3an/finviz/utils"
)

func newTestClient(config *Config) *Client {
if config != nil {
return &Client{
Client: &http.Client{
Timeout: 30 * time.Second,
Transport: utils.AddHeaderTransport(config.recorder),
},
}
}
return &Client{
Client: &http.Client{
Timeout: 30 * time.Second,
Transport: &http.Transport{
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
}).DialContext,
TLSHandshakeTimeout: 30 * time.Second,
},
},
}
}

func TestGetCalendar(t *testing.T) {
func() {
r, err := recorder.New("cassettes/calendar")
require.Nil(t, err)
defer func() {
err = r.Stop()
require.Nil(t, err)
}()
client := newTestClient(&Config{recorder: r, userAgent: uarand.GetRandom()})

calendar, err := client.GetCalendar()
require.Nil(t, err)
utils.PrintFullDataFrame(calendar)
}()
}
Loading