<commit_before>package Gemstone;

import java.util.Arrays;
import java.util.Random;
        
import org.apache.log4j.Logger;
import sagex.api.Database;
import sagex.api.MediaFileAPI;
import sagex.api.AiringAPI;
import sagex.api.ShowAPI;
import sagex.api.PlaylistAPI;
import sagex.api.Utility;

/**
 *
 * @author SBANTA
 * @author jusjoken
 * - 04/04/2012 - updated for Gemstone
 */

public class airing
{
    static private final Logger LOG = Logger.getLogger(Source.class);
    public static String GetAiringTitle(Object MediaObject)
    //Returns the airing title for the MediaObject.  If the MediaObject is a TV file it returns GetAiringTitle().
    //If it is not a TV file it returns the "MediaTitle" from the custom metadata fields. Returns "Unknown"
    //if the title is not known.
    {
        //String MediaTitle = new String();	
        String MediaTitle;	
        if (MediaFileAPI.IsTVFile(MediaObject)) {
            MediaTitle = AiringAPI.GetAiringTitle(MediaObject);		
        }
        else {
            MediaTitle = MetadataCalls.GetMediaTitle(MediaObject);
        }	
        if (MediaTitle.length() == 0) {
            return "Unknown";
        }
        else {
            return MediaTitle;
        }
    }		
	
//	public static String GetShowEpisode(Object MediaObject)
//	{
//		if (MediaFileAPI.IsTVFile(MediaObject)) {
//			return ShowAPI.GetShowEpisode(MediaObject);
//		}
//		else {
//			String EpisodeTitle = MetadataCalls.GetEpisodeTitle(MediaObject);
//
//			if (EpisodeTitle.length() == 0) {
//				return MediaFileAPI.GetMediaTitle(MediaObject);
//			}
//			return EpisodeTitle;
//		}
//	}

    public static String GetShowEpisode(Object MediaObject){
        if (MediaObject==null){
            return "";
        }
        MediaObject = phoenix.media.GetSageMediaFile(MediaObject);
        return ShowAPI.GetShowEpisode(MediaObject);
    }
    
    public static String GetShowDescription(Object MediaObject){
        if (MediaObject==null){
            return "";
        }
        MediaObject = phoenix.media.GetSageMediaFile(MediaObject);
        return ShowAPI.GetShowDescription(MediaObject);
    }
    
    public static long GetOriginalAiringDate(Object MediaObject) 
    {
        if (MediaFileAPI.IsTVFile(MediaObject)){
            return ShowAPI.GetOriginalAiringDate(MediaObject);
        } else{
            return MetadataCalls.GetOriginalAirDate(MediaObject);
        }
    }
	
    public static String GetAiringTitlePostpend(Object MediaObject)
    {
        String s1 = airing.GetAiringTitle(MediaObject);
        String s2 = s1.toLowerCase();

        if (s2.startsWith("the ") || s2.startsWith("the.") || s2.startsWith("the_"))
        {
            s1 = s1.substring(4) + ", The";
        }
        if (s2.startsWith("a ") || s2.startsWith("a.") || s2.startsWith("a_"))
        {
            s1 = s1.substring(2) + ", A";
        }
        if (s2.startsWith("an ") || s2.startsWith("an.") || s2.startsWith("an_"))
        {
            s1 = s1.substring(3) + ", An";
        }
        return s1;
    }
	
    public static Object MakePlaylist(Object MediaObjects, String NewPlaylistName)
    /* 
     * returns a Sage Playlist object containing the airings in MediaObjects
     * If NewPlaylistName already exists, it will be removed without prompt and recreated.
     * 
     * @param MediaObjects, a sage MediaFile, Airing, or Show Object in an Array, list, or vector
     * @param NewPlaylistName, string for the title of the new playlist
     */
    {
        LOG.debug("MakePlaylist: START");

        Object[] AllPlaylists = PlaylistAPI.GetPlaylists();

        Object[] MediaObjectsArray = FanartCaching.toArray(MediaObjects);

        for (Object TempPlaylist : AllPlaylists)
        {
            String TempPlaylistName = PlaylistAPI.GetName(TempPlaylist);
            //System.out.println("TempPlaylist: '" + TempPlaylistName + "'");

            if (TempPlaylistName.equals(NewPlaylistName)){
                //System.out.println("Removing TempPlaylist: '" + TempPlaylistName + "'");
                PlaylistAPI.RemovePlaylist(TempPlaylist);
            }
        }

        //System.out.println("Adding Playlist: '" + NewPlaylistName +"'");
        Object NewPlaylist = PlaylistAPI.AddPlaylist(NewPlaylistName);

        for ( Object MediaObject : MediaObjectsArray){
            LOG.debug("MakePlaylist: Adding To Playlist: '" + MediaFileAPI.GetMediaTitle(MediaObject)+"::"+ MediaFileAPI.GetMediaFileID(MediaObject)+"'");
            PlaylistAPI.AddToPlaylist(NewPlaylist, AiringAPI.GetMediaFileForAiring(MediaObject));
        }
        LOG.debug("MakePlaylist: END");
        return NewPlaylist;
    }
	
    public static boolean IsWatchedPartial(Object MediaObject)
    /* 
     * returns true if the MediaObjects  the IsWatched() flag is false and it has been partially watched.
     * 
     * @param MediaObject, a sage MediaFile, Airing, or Show object
     */
    {
        if (!AiringAPI.IsWatched(MediaObject) && ( AiringAPI.GetWatchedDuration(MediaObject) != 0 ) ){
            return true;
        } else {
            return false;
        }
    }
	
    public static Object GetLastWatched(Object MediaObjects)
    /*
     * Given an array of Airings will return the last watched (in real time) object.
     * 
     * @param MediaObjects - sage MediaFiles, Airings, or Shows Objects in an Array, list, or vector.
     */
    {
        MediaObjects = Database.Sort(MediaObjects, true, "Gemstone_airing_GetRealWatchedStartTime");
        return Utility.GetElement(MediaObjects, 0);
    }

    public static Long GetRealWatchedStartTime(Object MediaObject){
        return sagex.api.AiringAPI.GetRealWatchedStartTime(phoenix.media.GetMediaObject(MediaObject));
    }

    public static Object GetNextShow(Object MediaObjects)
    /*
     * Given an array of Airings will return the next episode to watch by AiringDate (in real time).
     * Returns Null if Airings not found after last watched. (end of series)
     * 
     * @param MediaObjects - sage MediaFiles, Airings, or Shows Objects in an Array, list, or vector.
     */
    {
        //LOG.debug("GetNextShow: for '" + MediaObjects + "'");
        Object LastWatched = airing.GetLastWatched(MediaObjects);
        //LOG.debug("GetNextShow: LastWatched '" + LastWatched + "'");
        if(phoenix.media.IsWatched(LastWatched)){
            MediaObjects = Database.Sort(MediaObjects, false, "phoenix_metadata_GetOriginalAirDate");
            int index = Utility.FindElementIndex(MediaObjects, LastWatched);

            if(index >= Utility.Size(MediaObjects)){	
                return null;
            }else{
                return Utility.GetElement(MediaObjects, index+1);}
        }else{
            return LastWatched;	
        }
    }

    public static Object GetShowNextPrev(Object MediaObjects, Object CurrentShow, Boolean DirectionNext)
    /*
     * Given an array of Airings will return the Next or Previous episode by AiringDate (in real time).
     * Returns Null if Airings not found after or before Current Show. (end or begin of series hit)
     * 
     * @param MediaObjects - sage MediaFiles, Airings, or Shows Objects in an Array, list, or vector.
     * @param CurrentShow - sage MediaFile, Airing, or Show Object.
     * @param DirectionNext - true gets Next, false gets Previous.
     */
    {<commit_msg>New/Last/Next fix in progress - version 1.0175<commit_after>package Gemstone;

import java.util.Arrays;
import java.util.Random;
        
import org.apache.log4j.Logger;
import sagex.api.Database;
import sagex.api.MediaFileAPI;
import sagex.api.AiringAPI;
import sagex.api.ShowAPI;
import sagex.api.PlaylistAPI;
import sagex.api.Utility;

/**
 *
 * @author SBANTA
 * @author jusjoken
 * - 04/04/2012 - updated for Gemstone
 */

public class airing
{
    static private final Logger LOG = Logger.getLogger(Source.class);
    public static String GetAiringTitle(Object MediaObject)
    //Returns the airing title for the MediaObject.  If the MediaObject is a TV file it returns GetAiringTitle().
    //If it is not a TV file it returns the "MediaTitle" from the custom metadata fields. Returns "Unknown"
    //if the title is not known.
    {
        //String MediaTitle = new String();	
        String MediaTitle;	
        if (MediaFileAPI.IsTVFile(MediaObject)) {
            MediaTitle = AiringAPI.GetAiringTitle(MediaObject);		
        }
        else {
            MediaTitle = MetadataCalls.GetMediaTitle(MediaObject);
        }	
        if (MediaTitle.length() == 0) {
            return "Unknown";
        }
        else {
            return MediaTitle;
        }
    }		
	
//	public static String GetShowEpisode(Object MediaObject)
//	{
//		if (MediaFileAPI.IsTVFile(MediaObject)) {
//			return ShowAPI.GetShowEpisode(MediaObject);
//		}
//		else {
//			String EpisodeTitle = MetadataCalls.GetEpisodeTitle(MediaObject);
//
//			if (EpisodeTitle.length() == 0) {
//				return MediaFileAPI.GetMediaTitle(MediaObject);
//			}
//			return EpisodeTitle;
//		}
//	}

    public static String GetShowEpisode(Object MediaObject){
        if (MediaObject==null){
            return "";
        }
        MediaObject = phoenix.media.GetSageMediaFile(MediaObject);
        return ShowAPI.GetShowEpisode(MediaObject);
    }
    
    public static String GetShowDescription(Object MediaObject){
        if (MediaObject==null){
            return "";
        }
        MediaObject = phoenix.media.GetSageMediaFile(MediaObject);
        return ShowAPI.GetShowDescription(MediaObject);
    }
    
    public static long GetOriginalAiringDate(Object MediaObject) 
    {
        if (MediaFileAPI.IsTVFile(MediaObject)){
            return ShowAPI.GetOriginalAiringDate(MediaObject);
        } else{
            return MetadataCalls.GetOriginalAirDate(MediaObject);
        }
    }
	
    public static String GetAiringTitlePostpend(Object MediaObject)
    {
        String s1 = airing.GetAiringTitle(MediaObject);
        String s2 = s1.toLowerCase();

        if (s2.startsWith("the ") || s2.startsWith("the.") || s2.startsWith("the_"))
        {
            s1 = s1.substring(4) + ", The";
        }
        if (s2.startsWith("a ") || s2.startsWith("a.") || s2.startsWith("a_"))
        {
            s1 = s1.substring(2) + ", A";
        }
        if (s2.startsWith("an ") || s2.startsWith("an.") || s2.startsWith("an_"))
        {
            s1 = s1.substring(3) + ", An";
        }
        return s1;
    }
	
    public static Object MakePlaylist(Object MediaObjects, String NewPlaylistName)
    /* 
     * returns a Sage Playlist object containing the airings in MediaObjects
     * If NewPlaylistName already exists, it will be removed without prompt and recreated.
     * 
     * @param MediaObjects, a sage MediaFile, Airing, or Show Object in an Array, list, or vector
     * @param NewPlaylistName, string for the title of the new playlist
     */
    {
        LOG.debug("MakePlaylist: START");

        Object[] AllPlaylists = PlaylistAPI.GetPlaylists();

        Object[] MediaObjectsArray = FanartCaching.toArray(MediaObjects);

        for (Object TempPlaylist : AllPlaylists)
        {
            String TempPlaylistName = PlaylistAPI.GetName(TempPlaylist);
            //System.out.println("TempPlaylist: '" + TempPlaylistName + "'");

            if (TempPlaylistName.equals(NewPlaylistName)){
                //System.out.println("Removing TempPlaylist: '" + TempPlaylistName + "'");
                PlaylistAPI.RemovePlaylist(TempPlaylist);
            }
        }

        //System.out.println("Adding Playlist: '" + NewPlaylistName +"'");
        Object NewPlaylist = PlaylistAPI.AddPlaylist(NewPlaylistName);

        for ( Object MediaObject : MediaObjectsArray){
            LOG.debug("MakePlaylist: Adding To Playlist: '" + MediaFileAPI.GetMediaTitle(MediaObject)+"::"+ MediaFileAPI.GetMediaFileID(MediaObject)+"'");
            PlaylistAPI.AddToPlaylist(NewPlaylist, AiringAPI.GetMediaFileForAiring(MediaObject));
        }
        LOG.debug("MakePlaylist: END");
        return NewPlaylist;
    }
	
    public static boolean IsWatchedPartial(Object MediaObject)
    /* 
     * returns true if the MediaObjects  the IsWatched() flag is false and it has been partially watched.
     * 
     * @param MediaObject, a sage MediaFile, Airing, or Show object
     */
    {
        if (!AiringAPI.IsWatched(MediaObject) && ( AiringAPI.GetWatchedDuration(MediaObject) != 0 ) ){
            return true;
        } else {
            return false;
        }
    }
	
    public static Object GetLastWatched(Object MediaObjects)
    /*
     * Given an array of Airings will return the last watched (in real time) object.
     * 
     * @param MediaObjects - sage MediaFiles, Airings, or Shows Objects in an Array, list, or vector.
     */
    {
        MediaObjects = Database.Sort(MediaObjects, true, "Gemstone_airing_GetRealWatchedStartTime");
        return Utility.GetElement(MediaObjects, 0);
    }

    public static Long GetRealWatchedStartTime(Object MediaObject){
        return sagex.api.AiringAPI.GetRealWatchedStartTime(phoenix.media.GetMediaObject(MediaObject));
    }

    public static Object GetNextShow(Object MediaObjects)
    /*
     * Given an array of Airings will return the next episode to watch by AiringDate (in real time).
     * Returns Null if Airings not found after last watched. (end of series)
     * 
     * @param MediaObjects - sage MediaFiles, Airings, or Shows Objects in an Array, list, or vector.
     */
    {
        //LOG.debug("GetNextShow: for '" + MediaObjects + "'");
        Object LastWatched = airing.GetLastWatched(MediaObjects);
        LOG.debug("GetNextShow: LastWatched '" + LastWatched + "'");
        if(phoenix.media.IsWatched(LastWatched)){
            //MediaObjects = Database.Sort(MediaObjects, false, "phoenix_metadata_GetOriginalAirDate");
            MediaObjects = Database.Sort(MediaObjects, false, "Gemstone_airing_GetRealWatchedStartTime");
            int index = Utility.FindElementIndex(MediaObjects, LastWatched);
            LOG.debug("GetNextShow: Watched so finding index '" + index + "' size '" + Utility.Size(MediaObjects) + "'");

            if(index+1 >= Utility.Size(MediaObjects)){	
                //return the last item
                return Utility.GetElement(MediaObjects, Utility.Size(MediaObjects)-1);
            }else if(index==-1){
                //return the last item
                return Utility.GetElement(MediaObjects, Utility.Size(MediaObjects)-1);
            }else{
                return Utility.GetElement(MediaObjects, index+1);
            }
//            if(index >= Utility.Size(MediaObjects)){	
//                return null;
//            }else{
//                return Utility.GetElement(MediaObjects, index+1);
//            }
        }else{
            return LastWatched;	
        }
    }

    public static Object GetShowNextPrev(Object MediaObjects, Object CurrentShow, Boolean DirectionNext)
    /*
     * Given an array of Airings will return the Next or Previous episode by AiringDate (in real time).
     * Returns Null if Airings not found after or before Current Show. (end or begin of series hit)
     * 
     * @param MediaObjects - sage MediaFiles, Airings, or Shows Objects in an Array, list, or vector.
     * @param CurrentShow - sage MediaFile, Airing, or Show Object.
     * @param DirectionNext - true gets Next, false gets Previous.
     */
    {
{'fraction_non_alphanumeric': 0.10371617247183737, 'fraction_numerical': 0.005308817816910527, 'mean_word_length': 2.538021071919377, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1535476', 'n_tokens_mistral': 4498, 'n_tokens_neox': 4382, 'n_words': 1097}