Skip to content

Instantly share code, notes, and snippets.

@milanaleksic
Created July 3, 2011 22:01
Show Gist options
  • Save milanaleksic/1062657 to your computer and use it in GitHub Desktop.
Save milanaleksic/1062657 to your computer and use it in GitHub Desktop.
Fetching possible IMDB titles based on search phrase
package misc
import groovy.json.JsonSlurper
// Pretraga za "Bat" gadja naredni URL: http://sg.media-imdb.com/suggests/b/bat.json
def getMatchesForInput(input) {
String content
try {
InputStream stream = new URL("http://sg.media-imdb.com/suggests/${input[0]}/${input}.json").openStream()
List<String> lines = new InputStreamReader(stream).readLines()
content = lines.join("")
} catch (Exception e) {
println "Error on server: $e.message"
return null
}
def values = []
content.eachMatch(~/\((.*)\)/) { all, match ->
def retval = new JsonSlurper().parseText(match)
retval.d.each { elem ->
if (!elem.id.startsWith('tt'))
return
values << "$elem.l (${elem.y ? elem.y : '?'}) id=$elem.id"
}
}
return values
}
while (true) {
System.out.println("\nPlease enter string for movie search on IMDB (empty string for exit): ")
System.out.print("-> ")
String search = new BufferedReader(new InputStreamReader(System.in)).readLine()
if (!search)
return
def matches = getMatchesForInput(search.toLowerCase())
if (matches) {
println 'Matches: '
matches.each { match ->
println "\t$match"
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment