Created
July 3, 2011 22:01
-
-
Save milanaleksic/1062657 to your computer and use it in GitHub Desktop.
Fetching possible IMDB titles based on search phrase
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
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