Skip to content

Instantly share code, notes, and snippets.

@esmevane
Last active October 13, 2015 00:07
Show Gist options
  • Save esmevane/1e15b6938af5fda92c1e to your computer and use it in GitHub Desktop.
Save esmevane/1e15b6938af5fda92c1e to your computer and use it in GitHub Desktop.
[ Ruby / API / Scraping ]: Build a local json file of music videos sourced from imvdb.com
#! /usr/bin/env ruby
require 'ansi'
require 'faraday'
require 'json'
require 'thor'
require 'pry'
class Video
class Repo
class FileNotFound < StandardError; end
attr_reader :store
def initialize
@store = File.join("./videos.json")
end
def browse
fail FileNotFound, "videos.json file not found" unless File.exist?(store)
contents = "[#{File.read(store).lines.join(",")}]"
videos = JSON.parse(contents, symbolize_names: true)
pry videos
end
def drop
fail FileNotFound, "videos.json file not found" unless File.exist?(store)
FileUtils.rm(store)
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 if 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 "browse", "Browse a videos.json file".ansi(:blue)
def browse
puts "Entering videos.json contents".ansi(:blue)
repo = Video::Repo.new
repo.browse
rescue Video::Repo::FileNotFound
puts "No videos.json file found".ansi(:red)
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
end
CLI.start(ARGV)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment