Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.savvasdalkitsis.jtmdb;
  
  import java.io.File;
  import java.net.URL;
  import java.util.List;
 
 
 import  org.json.JSONArray;
 import  org.json.JSONException;
 import  org.json.JSONObject;
 
This class provides methods that links movie information with media formats such as DVDs.

Author(s):
Savvas Dalkitsis
 
 public class Media {

The constructor is private for now until a stateful Media object makes sense in the site API.
 
 	private Media() {
 
 	}

This enumeration provides the type of Media available for tagging Movies with.

Author(s):
Savvas Dalkitsis
 
 	public static enum MediaType {
The Media is a DVD.
 
 		DVD("dvd"),
The Media is a file.
 
 		FILE("file");
 
 		private String value;
 
 		private MediaType(String value) {
 			this. = value;
 		}
 
 		protected String getValue() {
 			return ;
 		}
 	}

This method allows you to add a file id to the TMDb database. These file id's can be useful for identifying movies without having to worry about their file name.The method will calculate the hash of the supplied file for you.Requires an authenticated session.

Parameters:
movieID The ID of the TMDb movie you are tagging with the hash.
mediaFile The file that is the movie to be tagged.
fps The frames per second of the media.
session The session to use. Must be authenticated.
Returns:
The response from the server.
Throws:
IOException
 
 	public static ServerResponse addID(int movieIDFile mediaFilefloat fps,
 			Session sessionthrows IOException {
 		String mediaHash = generateFileHash(mediaFile);
 		return addID(movieIDmediaHash.mediaFile.length(),
 				fpssession);
 	}

This method allows you to add a file id to the TMDb database. These file id's can be useful for identifying movies without having to worry about their file name.The method will calculate the hash of the supplied file for you.Requires an authenticated session.

Parameters:
movieID The ID of the TMDb movie you are tagging with the hash.
mediaFile The file that is the movie to be tagged.
fps The frames per second of the media.
volumeLabel The label of the media.
session The session to use. Must be authenticated.
Returns:
The response from the server.
Throws:
IOException
	public static ServerResponse addID(int movieIDFile mediaFilefloat fps,
			String volumeLabelSession sessionthrows IOException {
		String mediaHash = generateFileHash(mediaFile);
		return addID(movieIDmediaHash.mediaFile.length(),
				fpsvolumeLabelsession);
	}

This method allows you to add a file or disc id to the TMDb database. These file id's can be useful for identifying movies without having to worry about their file name. Currently only 2 kinds of id's are supported. More about these id's and hashes can be found here. Requires an authenticated session.

Parameters:
movieID The ID of the TMDb movie you are tagging with the hash.
mediaHash This is the computed id (hash) for the Movie.
mediaType The type of the media.
byteSize The total size of the media file.
fps The frames per second of the media.
session The session to use. Must be authenticated.
Returns:
The response from the server.
Throws:
IOException
	public static ServerResponse addID(int movieIDString mediaHash,
			MediaType mediaTypelong byteSizefloat fpsSession session)
			throws IOException {
		return addID(movieIDmediaHashmediaTypebyteSizefpsnull,
				session);
	}

This method allows you to add a file or disc id to the TMDb database. These file id's can be useful for identifying movies without having to worry about their file name. Currently only 2 kinds of id's are supported. More about these id's and hashes can be found here. Requires an authenticated session.

Parameters:
movieID The ID of the TMDb movie you are tagging with the hash.
mediaHash This is the computed id (hash) for the Movie.
mediaType The type of the media.
byteSize The total size of the media file.
fps The frames per second of the media.
volumeLabel The label of the media.
session The session to use. Must be authenticated.
Returns:
The response from the server.
Throws:
IOException
	public static ServerResponse addID(int movieIDString mediaHash,
			MediaType mediaTypelong byteSizefloat fpsString volumeLabel,
			Session sessionthrows IOException {
		Log
				.log("Adding Media ID for Movie with ID" + movieID,
		if (byteSize < 1) {
			Log.log("Byte size must be a positive number.".);
			return null;
		}
		if (GeneralSettings.getApiKey() != null
				&& !GeneralSettings.getApiKey().equals("")) {
			if (session != null && session.getSession() != null
					&& !session.getSession().equals("")) {
				try {
					String jsonString;
					if (volumeLabel == null) {
						jsonString = Utilities.postToUrl(call"type""json",
								"api_key", GeneralSettings.getApiKey(),
								"session_key"session.getSession(), "id"""
movieID"media_id"mediaHash,
								"media_type"mediaType.getValue(),
								"total_size""" + byteSize"fps""" + fps);
else {
						jsonString = Utilities.postToUrl(call"type""json",
								"api_key", GeneralSettings.getApiKey(),
								"session_key"session.getSession(), "id"""
movieID"media_id"mediaHash,
								"media_type"mediaType.getValue(),
								"total_size""" + byteSize"fps""" + fps,
								"volume_label"volumeLabel);
					}
					try {
						JSONObject responseJson = new JSONObject(jsonString);
						if (responseJson.has("code")) {
							int code = responseJson.getInt("code");
							return ServerResponse.forID(code);
else {
							Log
											"Unknown error while adding Media ID for movie",
						}
catch (JSONException e) {
					}
catch (IOException e) {
					throw e;
				}
else {
				Log.log("Session was null or empty".);
			}
else {
			Log.log("Error with the API key".);
		}
		return null;
	}

Gets the info for the movie that is the media file provided. Returns a list of Movie objects with the normal form (see class description Movie and method isReduced()). Will return null if a valid API key was not supplied to the GeneralSettings or if the supplied media file was not correspond to a movie in the database.

Parameters:
mediaFile The file to get the Movie info for.
Returns:
A list of Movie objects with the normal form (see class description Movie and method isReduced()). Will return null if a valid API key was not supplied to the GeneralSettings or if the supplied media file was not correspond to a movie in the database.
Throws:
IOException
JSONException
	public static List<MoviegetInfo(File mediaFilethrows IOException,
			JSONException {
		return getInfo(generateFileHash(mediaFile), mediaFile.length());
	}

Gets the info for a specific Movie with the provided DVD ID. Returns a Movie object with the normal form (see class description Movie and method isReduced()). Will return null if a valid API key was not supplied to the GeneralSettings or if the supplied DVD ID did not correspond to a movie.

Parameters:
dvdID The ID of the DVD.
Returns:
A Movie object with the normal form (see class description Movie and method isReduced()). Will return null if a valid API key was not supplied to the GeneralSettings or if the supplied DVD ID did not correspond to a movie.
Throws:
IOException
JSONException
	public static Movie getInfo(String dvdIDthrows IOException, JSONException {
		Log
				.log("Getting info for movie with DVD ID " + dvdID,
		if (GeneralSettings.getApiKey() != null
				&& !GeneralSettings.getApiKey().equals("")) {
			try {
						+ GeneralSettings.getAPILocaleFormatted() + "/"
						+ GeneralSettings.getApiKey() + "/" + dvdID);
				String jsonString = Utilities.readUrlResponse(call).trim();
				if ((jsonString.startsWith("[") || jsonString.startsWith("{"))
						&& !jsonString.equals("[\"Nothing found.\"]")) {
					JSONArray jsonArray = new JSONArray(jsonString.toString());
					return new Movie(jsonArray);
else {
					Log.log("Getting info for Movie with DVD ID " + dvdID
" returned no results".);
				}
catch (IOException e) {
				throw e;
catch (JSONException e) {
				throw e;
			}
else {
			Log.log("Error with the API key".);
		}
		return null;
	}

Gets the info for a specific Movie with the provided hash key and byte size of the file. Returns a list of Movie objects with the normal form (see class description Movie and method isReduced()). Will return null if a valid API key was not supplied to the GeneralSettings or if the supplied hash and byte size did not correspond to a movie.

Parameters:
hash The hash of the file.
byteSize The byte size of the file.
Returns:
A list of Movie objects with the normal form (see class description Movie and method isReduced()). Will return null if a valid API key was not supplied to the GeneralSettings or if the supplied hash and byte size did not correspond to a movie.
Throws:
IOException
JSONException
	public static List<MoviegetInfo(String hashlong byteSize)
			throws IOException, JSONException {
		Log.log("Getting info for movie with hash " + hash + " and bytesize "
byteSize.);
		if (GeneralSettings.getApiKey() != null
				&& !GeneralSettings.getApiKey().equals("")) {
			try {
						+ GeneralSettings.getAPILocaleFormatted() + "/"
						+ GeneralSettings.getApiKey() + "/" + hash + "/"
byteSize);
				String jsonString = Utilities.readUrlResponse(call).trim();
				if ((jsonString.startsWith("[") || jsonString.startsWith("{"))
						&& !jsonString.equals("[\"Nothing found.\"]")) {
					JSONArray jsonArray = new JSONArray(jsonString.toString());
					List<Moviemovies = new LinkedList<Movie>();
					for (int i = 0; i < jsonArray.length(); i++) {
						movies.add(new Movie(jsonArray.getJSONObject(i)));
					}
					return movies;
else {
					Log.log("Getting info for Movie with hash " + hash
" and bytesize " + byteSize
" returned no results".);
				}
catch (IOException e) {
				throw e;
catch (JSONException e) {
				throw e;
			}
else {
			Log.log("Error with the API key".);
		}
		return null;
	}

This method calculates the hash of the file based on the size + 64bit checksum of the first and last 64k.

Parameters:
f The file to calculate the checksum for.
Returns:
The hex value of the checksum.
Throws:
IOException
	public static String generateFileHash(File fthrows IOException {
		long byteSize = f.length();
		long index = Math.min(byteSize, 1024 * 64);
		long head = 0;
		long tail = 0;
		long read = 0;
		byte[] longBuffer = new byte[8];
		int b = 0;
		while ((b = in.read(longBuffer)) != -1 && read < index) {
			long l = 0;
			for (int i = 7; i >= 8 - bi--) {
				l = l << 8;
				long newL = longBuffer[i];
				l = l | (newL & 255);
			}
			for (int i = 1; i <= 8 - bi++) {
				l = l << 8;
			}
			read += b;
			head += l;
		}
		in.close();
		in.skip(byteSize - 1024 * 64);
		b = 0;
		while ((b = in.read(longBuffer)) != -1) {
			long l = 0;
			for (int i = 7; i >= 8 - bi--) {
				l = l << 8;
				long newL = longBuffer[i];
				l = l | (newL & 255);
			}
			for (int i = 1; i <= 8 - bi++) {
				l = l << 8;
			}
			tail += l;
		}
		in.close();
		return Long.toHexString(byteSize + head + tail);
	}
New to GrepCode? Check out our FAQ X