Skip to content

Commit

Permalink
Fixups (#191)
Browse files Browse the repository at this point in the history
* add events and event specs

* add back new relic free plan

* fixups
  • Loading branch information
estiens authored Nov 15, 2022
1 parent a854ee5 commit 6aa46d6
Show file tree
Hide file tree
Showing 12 changed files with 159 additions and 115 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ gem 'good_job', '~> 3.4'
gem 'haml-rails', '~> 2.1'
gem 'httparty', '~> 0.20'
gem 'jbuilder', '~> 2.11'
gem 'newrelic_rpm'
gem 'puma', '~> 5.6'
gem 'rack-attack', '~> 6.6'
gem 'rack-cors', '~> 1.1', require: 'rack/cors'
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ GEM
timeout
net-smtp (0.3.2)
net-protocol
newrelic_rpm (8.12.0)
nio4r (2.5.8)
nokogiri (1.13.9-arm64-darwin)
racc (~> 1.4)
Expand Down Expand Up @@ -277,6 +278,7 @@ DEPENDENCIES
haml-rails (~> 2.1)
httparty (~> 0.20)
jbuilder (~> 2.11)
newrelic_rpm
pg (~> 1.4)
pry-rails (~> 0.3)
puma (~> 5.6)
Expand Down
141 changes: 72 additions & 69 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,28 @@

We are super up and running for 2022. Note URL change! Please remember to make a PR with your nice apps, raspberry pi projects, etc. And my favorite part of this whole thing, the students who get super excited about coding something.


todo:

- make sure nothing changes in what we parse
- maybe find match stats
- make sure we detect when match starts/stops


Setup: usual rails setup then
`Setup2022.setup_teams`
`Setup2022.setup_groups`
`AllMatchesService.setup`

And if you have 64 matches you are good to go! From there on out you can call jobs directly or use the
`app/jobs/scheduler.rb` to update them
`app/jobs/scheduler.rb` to update them. If you want to run the scraping tasks automatically, you'll need another tab open and run `ENABLE_CRON=true bundle exec good_jobs start`

https://worldcupjson.net

Main response endpoint:
https://worldcupjson.net/matches/today

## DETAILED EXAMPLE RESPONSE

[detailed_response](https://gist.github.com/estiens/8c8de685fd74821c273cc9160ad9d765)

** note if match statistics make it in, they are all ints - but I have to find them!
Expand All @@ -40,6 +41,7 @@ Special thanks to my former employer [Software For Good](https://softwareforgood
The current rate limit is 10 requests every 60 seconds. This is open to change at anytime depending on load, but I'll always keep it so a few requests can fire off in parallel. Please keep your polling down to once a minute or so, 30 seconds if you are feeling greedy, you're not going to get updated information any quicker than that.

## ENDPOINTS

(some of the teams endpoints were never used may be deprecated for 2022)

`[url]/teams/` for a list of teams with group ID and FIFA code
Expand Down Expand Up @@ -67,116 +69,116 @@ You can also retrieve the matches for any team if you know their FIFA code (get
Example: [url]/matches/country?fifa_code=ISL

## Other Params
* You can append a start date or a start date and and end date to get the matches for those dates. Example `/matches?start_date=2018-06-19&end_date=2018-06-21` Please use YYYY-MM-DD as your param. `end_date` is optional.

* You can append `?callback=foo` to get a JSONP response
- You can append a start date or a start date and and end date to get the matches for those dates. Example `/matches?start_date=2018-06-19&end_date=2018-06-21` Please use YYYY-MM-DD as your param. `end_date` is optional.

- You can append `?callback=foo` to get a JSONP response

Example: `https://worldcupjson.net/matches/today?callback=bar`
Example: `https://worldcupjson.net/matches/today?callback=bar`

* You can append `?by_date=desc` to any query to sort the matches by furthest in the future to furthest in the past. `?by_date=asc` does past to future. (ASC is default sort with no params)
- You can append `?by_date=desc` to any query to sort the matches by furthest in the future to furthest in the past. `?by_date=asc` does past to future. (ASC is default sort with no params)

Example:`[url]/matches/today/?by_date=DESC`
Example:`[url]/matches/today/?by_date=DESC`

You can also use the by param to get some other sortings of the match list.

* `total_goals` will sort matches with the largest number of total goals to the least
* `home_team_goals` will sort matches with the largest number of home team goals to the least
* `away_team_goals` will sort matches with the largest number of away team goals to the least
* `closest_scores` will sort matches with draws first to largest winning margin
- `total_goals` will sort matches with the largest number of total goals to the least
- `home_team_goals` will sort matches with the largest number of home team goals to the least
- `away_team_goals` will sort matches with the largest number of away team goals to the least
- `closest_scores` will sort matches with draws first to largest winning margin

Example:`[url]/matches/current/?by=closest_scores`
Example:`[url]/matches/current/?by=closest_scores`

#### JSONP
<details><summary>JSONP old school</summary>

JSONP old school
The API also supports [JSONP](http://en.wikipedia.org/wiki/JSONP) style output via the `callback` parameter, e.g. `[url]/teams/results?callback=processData`

The response includes the same data output as the regular GET call without parameters, but wrapped inside a function. This can be used to get around cross origin issues.
</details>

## EXAMPLE RESPONSES

[Detailed Match View](https://gist.github.com/estiens/8c8de685fd74821c273cc9160ad9d765)



## PROJECTS USING THIS API IN 2022

(Feel free to submit a PR with your project!)
### 2022 projects

* https://github.com/cyrusDev1/qatar-worldcup
(Vue Js Web App to find the matches, scores and rankings of the Qatar 2022 world cup in real time.)
- https://github.com/cyrusDev1/qatar-worldcup
(Vue Js Web App to find the matches, scores and rankings of the Qatar 2022 world cup in real time.)

## PROJECTS USING THIS API IN 2018

<details><summary>2018 projects submitted</summary>

* http://fifa-worldcup.herokuapp.com
(NodeJS and Express Web App to keep you updated with the FIFA World Cup 2018)
- http://fifa-worldcup.herokuapp.com
(NodeJS and Express Web App to keep you updated with the FIFA World Cup 2018)

* http://meisam-dodangeh.ir/worldcup (A simple web Page to show today games and games events)
* https://m.me/244560172984721 (Facebook Messenger bot that shows games for today and tomorrow as well as allowing you to follow along with live matches)
- http://meisam-dodangeh.ir/worldcup (A simple web Page to show today games and games events)
- https://m.me/244560172984721 (Facebook Messenger bot that shows games for today and tomorrow as well as allowing you to follow along with live matches)

* https://github.com/jthomas/goalbot
(Twitter bot ([@WC2018_Goals](https://twitter.com/WC2018_Goals)) which tweets out every goal from the 2018 FIFA World Cup.)
* https://github.com/riceluxs1t/EloSoccerPrediction
(React.js + Django app that shows (live) game data and match outcome predictions using an ELO based Poisson model.)
- https://github.com/jthomas/goalbot
(Twitter bot ([@WC2018_Goals](https://twitter.com/WC2018_Goals)) which tweets out every goal from the 2018 FIFA World Cup.)
- https://github.com/riceluxs1t/EloSoccerPrediction
(React.js + Django app that shows (live) game data and match outcome predictions using an ELO based Poisson model.)

* https://github.com/justcallmelarry/sportsball
(slack integration for updates of goals, cards and results)
- https://github.com/justcallmelarry/sportsball
(slack integration for updates of goals, cards and results)

* https://github.com/selfish/worldcup-slack
(Node.js Slack game status announcer, updated for 2018 games)
- https://github.com/selfish/worldcup-slack
(Node.js Slack game status announcer, updated for 2018 games)

* https://github.com/dg01d/bitbar-worldcup
(BitBar plugin to show current/daily scores and results)
- https://github.com/dg01d/bitbar-worldcup
(BitBar plugin to show current/daily scores and results)

* https://github.com/nicolopignatelli/wc2018-slack-bot
(Slack bot for updates about the current match)
- https://github.com/nicolopignatelli/wc2018-slack-bot
(Slack bot for updates about the current match)

* https://github.com/wildlifehexagon/node-world-cup
(Node.js command line app to display results and standings)
- https://github.com/wildlifehexagon/node-world-cup
(Node.js command line app to display results and standings)

* https://github.com/iricigor/FIFA2018
(PowerShell wrapper, compatible with both Linux and Windows versions)
- https://github.com/iricigor/FIFA2018
(PowerShell wrapper, compatible with both Linux and Windows versions)

* https://github.com/pedsm/liveCup
(React.js based dashboard with live updates designed for TVs and Computers)
- https://github.com/pedsm/liveCup
(React.js based dashboard with live updates designed for TVs and Computers)

* https://github.com/johnbfox/world-cup-scores-cli
(Command line tool for getting the day's scores and goals)
- https://github.com/johnbfox/world-cup-scores-cli
(Command line tool for getting the day's scores and goals)

* https://github.com/cedricblondeau/world-cup-2018-cli-dashboard
(CLI Dashboard that displays live updates of the current game, today's schedule and groups, built with react-blessed)
- https://github.com/cedricblondeau/world-cup-2018-cli-dashboard
(CLI Dashboard that displays live updates of the current game, today's schedule and groups, built with react-blessed)

* https://github.com/sazap10/world-cup-discord-bot
(Discord bot to display schedule, match information and standings)
- https://github.com/sazap10/world-cup-discord-bot
(Discord bot to display schedule, match information and standings)

* https://github.com/luridarmawan/Carik/
([Carik](https://github.com/luridarmawan/Carik/) ChatBot for Facebook Messenger, Telegram, Line, Slack. just type "info world cup".) See screenshots [1](https://cl.ly/102h2A1a3S46) [2](https://cl.ly/1p123j342A3v) [3](https://cl.ly/1T0i1E1P410B)
- https://github.com/luridarmawan/Carik/
([Carik](https://github.com/luridarmawan/Carik/) ChatBot for Facebook Messenger, Telegram, Line, Slack. just type "info world cup".) See screenshots [1](https://cl.ly/102h2A1a3S46) [2](https://cl.ly/1p123j342A3v) [3](https://cl.ly/1T0i1E1P410B)

* https://github.com/arghgr/golbot
(Twitter bot that tweets the word GOL, with varying numbers of Os, whenever a goal is scored - [@worldcupgolbot](https://twitter.com/worldcupgolbot))
- https://github.com/arghgr/golbot
(Twitter bot that tweets the word GOL, with varying numbers of Os, whenever a goal is scored - [@worldcupgolbot](https://twitter.com/worldcupgolbot))

* https://github.com/kadinho/worldcup-notifications
(A project to fetch FIFA World Cup matches and send Slack event notifications)
- https://github.com/kadinho/worldcup-notifications
(A project to fetch FIFA World Cup matches and send Slack event notifications)

* https://github.com/gk4m/world-cup-scores
(Simple vue.js based app for displaying results from World Cup 2018.)
- https://github.com/gk4m/world-cup-scores
(Simple vue.js based app for displaying results from World Cup 2018.)

* https://apps.lametric.com/apps/fifa_world_cup_2018/6624
(Display the latest score of the current match(es), or the recent and next matches on a [LaMetric Time](https://lametric.com) device)
- https://apps.lametric.com/apps/fifa_world_cup_2018/6624
(Display the latest score of the current match(es), or the recent and next matches on a [LaMetric Time](https://lametric.com) device)

* https://world-cup-basecamp.herokuapp.com/
[Github Link](https://github.com/dskoda1/world-cup-basecamp) - SPA using React/Redux/Material for displaying data from this API
- https://world-cup-basecamp.herokuapp.com/
[Github Link](https://github.com/dskoda1/world-cup-basecamp) - SPA using React/Redux/Material for displaying data from this API

* https://spapas.github.io/wc2018/
(A WC2018 dashboard using vue.js. Source @ https://github.com/spapas/vue-wc2018)
- https://spapas.github.io/wc2018/
(A WC2018 dashboard using vue.js. Source @ https://github.com/spapas/vue-wc2018)

* https://github.com/eeddaann/ElastiCup
- https://github.com/eeddaann/ElastiCup
(Loads World Cup data into Elasticsearch)
</details>

## PROJECTS USING THIS API IN 2014

<details><summary>Some 2014 projects submitted</summary>
* http://alexb.ninja/wc
* https://github.com/fatiherikli/worldcup (displays World Cup results in the terminal)
Expand Down Expand Up @@ -210,29 +212,30 @@ Some people have asked if they can make donations. I'd love for you to donate so
[https://worldcupjson.net/teams](https://worldcupjson.net/teams)

[https://worldcupjson.net/teams/USA](https://worldcupjson.net/teams/USA)

## UPDATES

### Updates June 19, 2018

* New rate limit -- some of you are really hammering the server. You can now make 10 requests
- New rate limit -- some of you are really hammering the server. You can now make 10 requests
every 60 seconds, requests after that will return 429
with your reset time. Please try to limit polling to once every minute or so and if you are building a SPA please do some cacheing/storing on your side and don't send a request with every user interaction. If you need a whitelist for more requests, please let me know and we can do it, but right now fully half of the requests get throttled and it's a fair amount of load to deal with that many requests coming in more often than 10s apart.

* Better cacheing/fixed broken JSONP cacheing
- Better cacheing/fixed broken JSONP cacheing

* Scrapers almost finished being reworked
- Scrapers almost finished being reworked

* New param added to the matches endpoint. You can pass `?start_date=2018-06-21` or `?start_date=2018-06-19&end_date=2018-06-24`
- New param added to the matches endpoint. You can pass `?start_date=2018-06-21` or `?start_date=2018-06-19&end_date=2018-06-24`

(If you pass one date, you'll get matches for that day, otherwise for the range of days specified. Please use YYYY-MM-DD format even though it is weird for the rest of the world not to use YYYY-DD-MM)

### Updates June 18, 2018

* We now retrieve match statistics as well (attempts on goal, saves, etc). This will show up at all the matches endpoints. If you want a more truncated view of a match (no events or stats) please pass `?details=false` to any match endpoint
- We now retrieve match statistics as well (attempts on goal, saves, etc). This will show up at all the matches endpoints. If you want a more truncated view of a match (no events or stats) please pass `?details=false` to any match endpoint

* You can now retrieve a match by fifa_id if you only want one specific match, just use `/matches/fifa_id/300331499` or as a shortcut just `/matches/300331499`. You'll get a 404 error back if no match has that id.
- You can now retrieve a match by fifa_id if you only want one specific match, just use `/matches/fifa_id/300331499` or as a shortcut just `/matches/300331499`. You'll get a 404 error back if no match has that id.

* Scrapers are being refactored to actually have readable methods, memoize parsed information, etc. Views not calculating anything should increase response time considerably, which is good as we are now at about 30-40 rps.
- Scrapers are being refactored to actually have readable methods, memoize parsed information, etc. Views not calculating anything should increase response time considerably, which is good as we are now at about 30-40 rps.

## WARNING

Expand Down
27 changes: 11 additions & 16 deletions app/controllers/matches_controller.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
class MatchesController < BaseApiController
before_action :detail_level
before_action :load_matches, except: %i[show country today tomorrow]
before_action :load_matches, except: %i[show]

def index
@details = false unless params[:details]&.downcase == 'true'
order_by_params
@details = false unless params[:details] == true
end

def current
Expand All @@ -31,21 +31,19 @@ def country
render json: { 'error': 'country code not_found' }
return
end
@matches = @team.matches
@matches = @matches.where('home_team_id = ? OR away_team_id = ?', @team.id, @team.id)
order_by_params
render :index
end

def today
@matches = Match.today.includes(:match_statistics)
.includes(:home_team).includes(:away_team).includes(:events)
@matches = @matches.today
order_by_params
render :index
end

def tomorrow
@matches = Match.tomorrow.includes(:match_statistics)
.includes(:home_team).includes(:away_team).includes(:events)
@matches = @matches.tomorrow
order_by_params
render :index
end
Expand All @@ -57,7 +55,8 @@ def show
private

def load_matches
@matches = Match.all.includes(:match_statistics, :events).order('datetime ASC')
@matches = Match.all
@matches = @matches.includes(:match_statistics, :events) if @details
end

def order_by_params
Expand Down Expand Up @@ -99,20 +98,16 @@ def order_by_scores

case @order_by.downcase
when 'total_goals'
@matches = @matches.reorder(nil).order('home_team_score + away_team_score DESC')
@matches = @matches.reorder(nil).order(Arel.sql('home_team_score + away_team_score DESC'))
when 'home_team_goals'
@matches = @matches.reorder(nil).order('home_team_score DESC')
@matches = @matches.reorder(nil).order(Arel.sql('home_team_score DESC'))
when 'away_team_goals'
@matches = @matches.reorder(nil).order('away_team_score DESC')
@matches = @matches.reorder(nil).order(Arel.sql('away_team_score DESC'))
when 'closest_scores'
@matches = @matches.reorder(nil).order('abs(home_team_score - away_team_score) ASC')
@matches = @matches.reorder(nil).order(Arel.sql('abs(home_team_score - away_team_score) ASC'))
end
end

def index_detail_level
@details = params[:details] || 'false'
end

def detail_level
@details = params[:details] || 'true'
end
Expand Down
8 changes: 6 additions & 2 deletions app/models/match.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,15 @@ def name
end

def home_team_events
events.where(team: home_team)
return [] unless home_team

events.where(team: home_team).sort_by { |e| e.time.to_i }
end

def away_team_events
events.where(team: away_team)
return [] unless away_team

events.where(team: away_team).sort_by { |e| e.time.to_i }
end

def home_stats
Expand Down
6 changes: 6 additions & 0 deletions app/services/match_writer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def complete_match!

def write_current_match
write_score_info
write_time_info
write_home_stats
write_away_stats
write_events
Expand Down Expand Up @@ -152,6 +153,11 @@ def write_away_stats
stats.save
end

def write_time_info
match.time = @json_match.current_time_info[:current_time]
match.detailed_time = @json_match.current_time_info
end

def write_score_info
match.home_team_score = @json_match.score_info[:home_score]
match.away_team_score = @json_match.score_info[:away_score]
Expand Down
Loading

0 comments on commit 6aa46d6

Please sign in to comment.