Skip to content

Instantly share code, notes, and snippets.

@Willamin
Created September 11, 2020 02:35
Show Gist options
  • Save Willamin/99e082e214a8651a6510cc8577fc9d8e to your computer and use it in GitHub Desktop.
Save Willamin/99e082e214a8651a6510cc8577fc9d8e to your computer and use it in GitHub Desktop.
#!/usr/bin/env cashrun
require "uri"
require "json"
require "http/client"
class SearchResults
include JSON::Serializable
property searchType : String
property expression : String
property errorMessage : String
property results : Array(ResultItem)
end
class ResultItem
include JSON::Serializable
property id : String
property resultType : String
property image : String
property title : String
property description : String
end
scheme = "https"
host = "imdb-api.com"
path = "/en/API/SearchMovie/"
api_key = ENV["IMDB_API_KEY"]?
query = URI.encode(ARGV.join(" ").chomp)
unless api_key
puts("You need an api key stored in IMDB_API_KEY")
end
if query.empty?
puts("You must provide a query")
exit(1)
end
response : SearchResults? = nil
String
.build { |s|
s << scheme
s << "://"
s << host
s << path
s << api_key
s << "/"
s << query
}
.try { |u| HTTP::Client.get(u) }
.body
.try { |b| SearchResults.from_json(b) }
.try { |r| response = r }
response.try &.results.sort_by { |item| item.description }.each { |item|
[
item.id,
item.title,
item.description,
]
.join("|")
.try { |column| puts(column) }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment