#! /usr/bin/env ruby require 'ansi' require 'bundler/setup' require 'faraday' require 'json' require 'pry' require 'thor' class Video class Repo class FileNotFound < StandardError; end attr_reader :store def initialize @store = File.join("./demo/videos.json") end def all fail FileNotFound, "videos.json file not found" unless File.exist?(store) contents = "[#{File.read(store).lines.join(",")}]" JSON.parse(contents, symbolize_names: true) end def drop fail FileNotFound, "videos.json file not found" unless File.exist?(store) FileUtils.rm(store) end def exemplar all.sample end def insert(document) File.open(store, "a+") { |file| file.puts(document) } end end attr_reader :attributes, :repo def initialize(attributes) @attributes = attributes @repo = Repo.new end def save repo.insert(attributes.to_json) end end class Client ENDPOINT = "http://imvdb.com/api/v1" attr_reader :service def initialize @service = Faraday.new(ENDPOINT) end def results(term, page = 0) resource = "search/videos?q=#{term}&per_page=5000" resource = "#{resource}&page=#{page}" if page > 0 JSON.parse(service.get(resource).body, symbolize_names: true) end end class CLI < Thor desc "build", "Build a videos.json file with IMVDB.com data".ansi(:blue) def build client = Client.new puts "Building videos.json file".ansi(:blue) ('a'..'z').each do |character| puts " --> Getting results for `#{character}`".ansi(:magenta) results = client.results(character).fetch(:results) results.each { |result| Video.new(result).save } sleep 0.5 end end desc "drop", "Remove a videos.json file".ansi(:blue) def drop puts "Dropping videos.json file".ansi(:blue) repo = Video::Repo.new repo.drop rescue Video::Repo::FileNotFound puts "No videos.json file found".ansi(:red) end desc "random", "Get a random music video sample".ansi(:magenta) def random repo = Video::Repo.new documents = repo.all $stdout.puts documents.shuffle.first(10).to_json rescue Video::Repo::FileNotFound puts "No videos.json file found".ansi(:red) end end CLI.start(ARGV)