Sample Code

If you're looking for some quick examples to get you started, you've come to the right place! Below you'll find code samples to demonstrate working with movie data and images: 

Want a step-by-step guide to getting started?  Check out our Quickstart Guide
Have questions about using our program IDs?  Read Important Gracenote Terms


JavaScript example:   Movies playing in local theatres

Use client-side javascript to display posters, titles, and ratings for movies playing in local theatres. 


<html>
   <head>
      <style type="text/css">
         .tile {display: inline-block; border: 1px solid grey; background: silver; padding: 4px; text-align: center; font-size: 15px;width:250px; }	
      </style>
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
      <script>
         
         // construct the url with parameter values
         var apikey = "your key";
         var baseUrl = "http://data.tmsapi.com/v1.1";
         var showtimesUrl = baseUrl + '/movies/showings';
         var zipCode = "78701";
         var d = new Date();
         var today = d.getFullYear() + '-' + (d.getMonth()+1) + '-' + d.getDate();
         
         $(document).ready(function() {
         
           // send off the query
           $.ajax({
         	url: showtimesUrl,
                data: {	startDate: today,
         	        zip: zipCode,
         	        jsonp: "dataHandler",
         	        api_key: apikey
                   },			
         	dataType: "jsonp",
           });
         });
         
         // callback to handle the results
         function dataHandler(data) {
          $(document.body).append('<p>Found ' + data.length + ' movies showing within 5 miles of ' + zipCode+':</p>');
          var movies = data.hits;
          $.each(data, function(index, movie) {
         	var movieData = '<div class="tile"><img src="http://developer.tmsimg.com/' + movie.preferredImage.uri + '?api_key='+apikey+'"><br/>';
         	movieData += movie.title;
         	if (movie.ratings) { movieData += ' (' + movie.ratings[0].code + ') </div>' };
         	$(document.body).append(movieData);
          });
         }
           
      </script>
   </head>
   <body>
   </body>
</html>

Code should render a page similar to this:

 


 

Ruby on Rails example:  Movie Details with online video and cast links

Use a combination of API methods to display movie description, cast, awards, and upcoming showings (on TV and online)
The tmsID for a movie or TV program can be found using Program Search, Lineup Airings (TV Grid), or Movie Showtimes responses

For a fully-functioning version of this screen, see sample application here.


#prepare the http client
proxy = ENV['HTTP_PROXY']
clnt = HTTPClient.new(proxy)

# form the url that will retrieve movie information (Program Details)
theURL = "http://data.tmsapi.com/v1.1/programs/" +   params[:tmsId] 
content = clnt.get_content(theURL + "?imageSize=Sm&api_key=" + APP_CONFIG['api_key'])
@aMovie = JSON.parse(content, {'object_class' => 'Hash'})

datetime = DateTime.now();
tz = TZInfo::Timezone.get('America/North_Dakota/Center')
tz1 = TZInfo::Timezone.get('Etc/GMT')
hoursAdd = tz1.now() - tz.now() 
hoursAdd =  hoursAdd / 3600 #time difference in hours 

startDateTime = datetime.strftime("%Y-%m-%d") + " 00:00"
origToday = DateTime.parse(startDateTime).advance(:hours => hoursAdd)
#set the start time and end time for airings we are interested in. 
#this takes into consideration timezone
startDateTime = origToday.strftime("%Y-%m-%dT%H:%MZ")
endDateTime = origToday.advance(:days => 14).strftime("%Y-%m-%dT%H:%MZ")

#form the url that will retrieve airing information (Program Airings)
theURL = "http://data.tmsapi.com/v1.1/programs/" +   params[:tmsId] + "/airings"  
content = clnt.get_content(theURL + "?lineupId=" +   APP_CONFIG['lineup_id'] + "&api_key=" + APP_CONFIG['api_key'] +
"&startDateTime=" + startDateTime +"&endDateTime=" + endDateTime) 

@movieAirings = JSON.parse(content, {'object_class' => 'Hash'}) 

#form the social information url (Online Social)
theURL = "http://feeds.tmsapi.com/social/" + @aMovie['rootId']  + ".xml?api_key=" + APP_CONFIG['ovd_api_key'];

socialContent = clnt.get_content(theURL) 
tempSocialInfo = Hash.from_xml(socialContent)
#tempSocialInfo = Hash.new()
@socialInfo = Hash.new()
@socialInfo[""] = ""
#form the end data structure that will contain the social information
if tempSocialInfo != nil && tempSocialInfo.class == Hash && tempSocialInfo.has_key?('social') && 
tempSocialInfo['social'].class == Hash &&  tempSocialInfo['social'].has_key?('movie') &&  
tempSocialInfo['social']['movie'].class == Hash &&  tempSocialInfo['social']['movie'].has_key?('links') && 
tempSocialInfo['social']['movie']['links'].class == Hash &&  tempSocialInfo['social']['movie']['links'].has_key?('link')  
tempSocialInfo = tempSocialInfo['social']['movie']['links']['link']
 if tempSocialInfo.class == Array
	for aSocial in tempSocialInfo
	  @socialInfo[aSocial['host']] = aSocial['url']
	end
 elsif tempSocialInfo.class == Hash
	 @socialInfo[tempSocialInfo['host']] = tempSocialInfo['url']
 end
else 
  @socialInfo = nil
end

#get the movie ovd information (Online Video)
theURL = "http://feeds.tmsapi.com/v2/movies/" + @aMovie['rootId'] + ".xml?api_key=" + APP_CONFIG['ovd_api_key'] ;
user_agent =  request.env['HTTP_USER_AGENT'].downcase
if /.*ipad.*/.match(user_agent)
  theURL += "&config=ios"
end

content = clnt.get_content(theURL)
@onlineAirings = Hash.from_xml(content)

Code will construct a page similar to this (screen sections outlined to show API requests corresponding to each data set):