-
-
Save davidpdrsn/ef0830e15695d672879120da67135d12 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require "oga" | |
require "rest-client" | |
require "attr_extras" | |
require "colorize" | |
require "active_support/core_ext/string" | |
class String | |
def print(out = STDOUT) | |
out.puts self | |
end | |
end | |
module Db | |
module Match | |
class << self | |
def seen?(match) | |
make_store_file | |
seen_video_ids.include?(match.video_id) | |
end | |
def seen!(match) | |
make_store_file | |
write_as_lines( | |
seen_store_file_path, | |
seen_video_ids + [match.video_id], | |
) | |
end | |
def opened?(match) | |
make_store_file | |
opened_video_ids.include?(match.video_id) | |
end | |
def opened!(match) | |
make_store_file | |
write_as_lines( | |
opened_store_file_path, | |
opened_video_ids + [match.video_id], | |
) | |
end | |
private | |
def write_as_lines(path, array) | |
File.write path, array.join("\n") + "\n" | |
end | |
def seen_video_ids | |
File.read(seen_store_file_path).lines.map(&:chomp) | |
end | |
def opened_video_ids | |
File.read(opened_store_file_path).lines.map(&:chomp) | |
end | |
def make_store_file | |
unless File.exists?(seen_store_file_path) | |
`touch #{seen_store_file_path}` | |
end | |
unless File.exists?(opened_store_file_path) | |
`touch #{opened_store_file_path}` | |
end | |
end | |
def seen_store_file_path | |
"/Users/davidpdrsn/.owl/matches.txt" | |
end | |
def opened_store_file_path | |
"/Users/davidpdrsn/.owl/matches_opened.txt" | |
end | |
end | |
end | |
end | |
class Day | |
pattr_initialize [:name!, :matches!] | |
attr_reader :name, :matches | |
def pretty_print | |
first, second = *name.split(" | ", 2) | |
"#{first.white.on_black} | #{second}" | |
end | |
end | |
class Match | |
pattr_initialize [:name!, :href!] | |
attr_reader :href | |
def video_id | |
"https://player2.majorleaguegaming.com/api/v2/player/embed/vod/owl-web?vid=OUz-fWqAyns&lang=en-us" | |
href.match(/vid=(?<vid>[^&]+)/)[:vid] | |
end | |
def pretty_print | |
" #{status_emoji} #{pretty_print_name}" | |
end | |
def pretty_print_name | |
first, second = *name.split(" vs ", 2) | |
"#{team_colorize(first)} #{'vs'} #{team_colorize(second)} (#{video_id})" | |
end | |
def name | |
@name | |
.sub("vs.", "vs") | |
.sub(" at ", " vs ") | |
end | |
def status_emoji | |
if seen? | |
"‚úÖ " | |
elsif opened? | |
"üëÄ " | |
else | |
" " | |
end | |
end | |
def seen? | |
Db::Match.seen?(self) | |
end | |
def seen! | |
Db::Match.seen!(self) | |
end | |
def open | |
Db::Match.opened!(self) | |
`open "#{href}"` | |
end | |
def opened? | |
Db::Match.opened?(self) | |
end | |
def team_colorize(team_name) | |
color = { | |
"Philadelphia Fusion" => :yellow, | |
"Boston Uprising" => :blue, | |
"Florida Mayhem" => :yellow, | |
"New York Excelsior" => :blue, | |
"London Spitfire" => :cyan, | |
"Houston Outlaws" => :green, | |
"Los Angeles Gladiators" => :magenta, | |
"Dallas Fuel" => :blue, | |
"Seoul Dynasty" => :yellow, | |
"San Francisco Shock" => :yellow, | |
"Los Angeles Valiant" => :green, | |
"Shanghai Dragons" => :red, | |
}.fetch(team_name) | |
team_name.send(color) | |
end | |
end | |
def parse_html_into_days | |
html = RestClient.get "https://overwatchleague.com/en-us/videos" | |
doc = Oga.parse_html html | |
days = doc.css("h3.ContentModule-title").map do |node| | |
next unless node.text.match? /Inaugural Season \|/ | |
sibling = node.next | |
unless sibling.attributes.any? { |attr| attr.name == "class" && attr.value.include?("MediaGallery") } | |
raise "HTML has changed" | |
end | |
matches = sibling.css(".MediaGallery-tile").map do |tile| | |
href = tile.css("a.Card-link").first.attributes.detect { |attr| attr.name == "data-mlg-embed" }.value | |
html_name = tile.css(".Card-title").text.squish | |
unless html_name.include?("Full Match |") | |
html_name = html_name.sub("Full Match ", "Full Match | ") | |
end | |
name = html_name.split(" | ").detect do |text| | |
text.include?("vs") || | |
text.include?(" at ") | |
end | |
Match.new name: name, href: href | |
end | |
Day.new name: node.text, matches: matches | |
end.compact | |
days | |
end | |
days = parse_html_into_days | |
if ARGV.first == "--open" || ARGV.first == "-o" | |
id = ARGV.last | |
matches = days.flat_map(&:matches) | |
match = matches.detect { |match| match.video_id == id } | |
match.open | |
exit 0 | |
end | |
if ARGV.first == "--seen" || ARGV.first == "-s" | |
id = ARGV.last | |
matches = days.flat_map(&:matches) | |
match = matches.detect { |match| match.video_id == id } | |
match.seen! | |
exit 0 | |
end | |
if ARGV.size == 0 || ARGV.first == "-a" || ARGV.first == "--all" | |
chosen_days = if ARGV.first == "-a" || ARGV.first == "--all" | |
days.reverse | |
else | |
days.reverse.last 9 | |
end | |
chosen_days.map do |day| | |
[ | |
day.pretty_print, | |
day.matches.map(&:pretty_print).join("\n") | |
].join("\n") | |
end.join("\n\n").print | |
exit 0 | |
end | |
STDERR.puts "No args" | |
exit 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment