Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[go] sample implementations #12

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
77 changes: 77 additions & 0 deletions implementations/go/data/data.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package data

import (
"github.com/FBDevCLagos/soccergist/implementations/go/data/football_data"
"github.com/FBDevCLagos/soccergist/implementations/go/data/reddit"
)

type League interface {
Table() *football_data.LeagueTable
PresentMatchday() int
TotalMatchdays() int
GetMatchdayFixtures(int) *football_data.MatchDayFixtures
}

type Highlight struct {
Title string
Name string
URLs []string
}

type LeagueTableTeamInfo struct {
Name, Crest string
Position, Points, MatchPlayed int
}

func PremierLeagueInfo() League {
competition := football_data.PremierLeague()
l := League(competition)
return l
}

func FirstFour(table *football_data.LeagueTable) []LeagueTableTeamInfo {
info := []LeagueTableTeamInfo{}
for i, team := range table.Standing {
info = append(info, LeagueTableTeamInfo{
Position: team.Position,
Name: team.TeamName,
Crest: substitueTeamLogo(team.TeamName),
Points: team.Points,
MatchPlayed: team.PlayedGames,
})

if i == 3 {
break
}
}

return info
}

func Highlights(after string) (highlights []Highlight) {
posts := reddit.GetHighlightPosts(after)

for _, post := range posts {
if len(post.URLs) == 0 {
continue
}

highlights = append(highlights, Highlight{
Title: post.Title,
Name: post.Name,
URLs: post.URLs,
})
}
return
}

var teamsLogo = map[string]string{
"Manchester City FC": "https://logoeps.com/wp-content/uploads/2011/08/manchester-city-logo-vector.png",
"Manchester United FC": "https://logoeps.com/wp-content/uploads/2011/08/manchester-united-logo-vector.png",
"Liverpool FC": "https://logoeps.com/wp-content/uploads/2011/08/liverpool-logo-vector.png",
"Chelsea FC": "https://logoeps.com/wp-content/uploads/2011/08/chelsea-logo-vector.png",
}

func substitueTeamLogo(teamName string) string {
return teamsLogo[teamName]
}
59 changes: 59 additions & 0 deletions implementations/go/data/football_data/api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package football_data

import (
"encoding/json"
"log"
"net/http"
"strings"

"github.com/FBDevCLagos/soccergist/implementations/go/utils"
)

const (
URL = "https://api.football-data.org/v1/competitions?season=2017"
PremierLeagueSymbol = "PL"
)

func PremierLeague() *Competition {
req, err := utils.APIRequest(URL, "GET", nil)
if err != nil || req.StatusCode != http.StatusOK {
log.Println("Error occurred in PremierLeague while making request to: ", URL, err)
}

return filterPremierLeague(req)
}

func filterPremierLeague(req *http.Response) (premierLeague *Competition) {
var competitions []Competition
err := json.NewDecoder(req.Body).Decode(&competitions)
if err != nil {
log.Println("Error occurred parsing json: ", err)
return
}

for _, competition := range competitions {
if competition.League == PremierLeagueSymbol {
premierLeague = &competition
break
}
}
return
}

func fetchLeagueTable(url string) *LeagueTable {
table := &LeagueTable{}
url = strings.Replace(url, "http://", "https://", 1)

req, err := utils.APIRequest(url, "GET", nil)
if err != nil || req.StatusCode != http.StatusOK {
log.Println("Error occurred in fetchLeagueTable while making request to: ", url, err)
return nil
}

err = json.NewDecoder(req.Body).Decode(table)
if err != nil {
log.Println("Error occurred parsing leagueTable json: ", err)
return nil
}
return table
}
157 changes: 157 additions & 0 deletions implementations/go/data/football_data/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
package football_data

import (
"encoding/json"
"fmt"
"log"
"net/http"
"strings"

"github.com/FBDevCLagos/soccergist/implementations/go/utils"
)

type Competition struct {
Links struct {
Fixtures struct {
Href string `json:"href"`
} `json:"fixtures"`
LeagueTable struct {
Href string `json:"href"`
} `json:"leagueTable"`
Self struct {
Href string `json:"href"`
} `json:"self"`
Teams struct {
Href string `json:"href"`
} `json:"teams"`
} `json:"_links"`
Caption string `json:"caption"`
CurrentMatchday int `json:"currentMatchday"`
ID int `json:"id"`
LastUpdated string `json:"lastUpdated"`
League string `json:"league"`
NumberOfGames int `json:"numberOfGames"`
NumberOfMatchdays int `json:"numberOfMatchdays"`
NumberOfTeams int `json:"numberOfTeams"`
Year string `json:"year"`
}

type LeagueTable struct {
Links struct {
Self struct {
Href string `json:"href"`
} `json:"self"`
Competition struct {
Href string `json:"href"`
} `json:"competition"`
} `json:"_links"`
LeagueCaption string `json:"leagueCaption"`
Matchday int `json:"matchday"`
Standing []struct {
Links struct {
Team struct {
Href string `json:"href"`
} `json:"team"`
} `json:"_links"`
Position int `json:"position"`
TeamName string `json:"teamName"`
CrestURI string `json:"crestURI"`
PlayedGames int `json:"playedGames"`
Points int `json:"points"`
Goals int `json:"goals"`
GoalsAgainst int `json:"goalsAgainst"`
GoalDifference int `json:"goalDifference"`
Wins int `json:"wins"`
Draws int `json:"draws"`
Losses int `json:"losses"`
Home struct {
Goals int `json:"goals"`
GoalsAgainst int `json:"goalsAgainst"`
Wins int `json:"wins"`
Draws int `json:"draws"`
Losses int `json:"losses"`
} `json:"home"`
Away struct {
Goals int `json:"goals"`
GoalsAgainst int `json:"goalsAgainst"`
Wins int `json:"wins"`
Draws int `json:"draws"`
Losses int `json:"losses"`
} `json:"away"`
} `json:"standing"`
}

type MatchDayFixtures struct {
Links struct {
Competition struct {
Href string `json:"href"`
} `json:"competition"`
Self struct {
Href string `json:"href"`
} `json:"self"`
} `json:"_links"`
Count int `json:"count"`
Fixtures []struct {
Links struct {
AwayTeam struct {
Href string `json:"href"`
} `json:"awayTeam"`
Competition struct {
Href string `json:"href"`
} `json:"competition"`
HomeTeam struct {
Href string `json:"href"`
} `json:"homeTeam"`
Self struct {
Href string `json:"href"`
} `json:"self"`
} `json:"_links"`
AwayTeamName string `json:"awayTeamName"`
Date string `json:"date"`
HomeTeamName string `json:"homeTeamName"`
Matchday int `json:"matchday"`
Odds interface{} `json:"odds"`
Result struct {
GoalsAwayTeam int `json:"goalsAwayTeam"`
GoalsHomeTeam int `json:"goalsHomeTeam"`
HalfTime struct {
GoalsAwayTeam int `json:"goalsAwayTeam"`
GoalsHomeTeam int `json:"goalsHomeTeam"`
} `json:"halfTime"`
} `json:"result"`
Status string `json:"status"`
} `json:"fixtures"`
}

func (c *Competition) Table() *LeagueTable {
url := c.Links.LeagueTable.Href
return fetchLeagueTable(url)
}

func (c *Competition) PresentMatchday() int {
return c.CurrentMatchday
}

func (c *Competition) TotalMatchdays() int {
return c.NumberOfMatchdays
}

func (c *Competition) GetMatchdayFixtures(matchday int) *MatchDayFixtures {
url := fmt.Sprintf("%s?matchday=%d", c.Links.Fixtures.Href, matchday)
matchDayFixtures := &MatchDayFixtures{}
url = strings.Replace(url, "http://", "https://", 1)

req, err := utils.APIRequest(url, "GET", nil)
if err != nil || req.StatusCode != http.StatusOK {
log.Println("Error occurred in GetMatchdayFixtures while making request to: ", url, err)
return nil
}

err = json.NewDecoder(req.Body).Decode(matchDayFixtures)
if err != nil {
log.Println("Error occurred parsing json: ", err)
return nil
}

return matchDayFixtures
}
67 changes: 67 additions & 0 deletions implementations/go/data/reddit/reddit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package reddit

import (
"fmt"
"log"
"regexp"
"strings"

"github.com/turnage/graw/reddit"
)

const (
subreddit = "/r/footballhighlights"
)

var (
redditBot reddit.Bot
videoURLRegex = regexp.MustCompile(`href="([\w\/:?\.-=]+|[\w\/:\.-]+)"`)
)

func init() {
var err error
redditBot, err = reddit.NewBotFromAgentFile("reddit.agent", 0)
if err != nil {
log.Fatal("Failed to create bot handle: ", err)
}
}

type Highlight struct {
*reddit.Post
URLs []string
}

// GetHighlightPosts returns posts in the highlights subreddit
// Filtering only posts that have 'Premier' league in their title
func GetHighlightPosts(after string) (highlights []*Highlight) {
params := map[string]string{"after": after}
harvest, err := redditBot.ListingWithParams(subreddit, params)
if err != nil {
log.Printf("Failed to fetch: %s %s\n", subreddit, err)
return
}

for _, post := range harvest.Posts {
if !strings.Contains(post.Title, "Premier") {
continue
}

highlight := &Highlight{Post: post, URLs: getHighlightVideoURLs(post)}
if len(highlight.URLs) == 0 {
fmt.Println(highlight.Title)
fmt.Println(post.SelfTextHTML)
}
highlights = append(highlights, highlight)
}

return
}

func getHighlightVideoURLs(post *reddit.Post) (urls []string) {
videoURLs := videoURLRegex.FindAllStringSubmatch(post.SelfTextHTML, -1)
for _, url := range videoURLs {
// TODO: handle unique videos
urls = append(urls, url[1])
}
return
}
Loading