About The Radio Perl (Server Side) Program: The server side program begins by printing the required heading so that the user can interpret the file as Json data. Then it uses the most important part of the program, the “should_change” function. This function determines if the current Json file is outdated, or good to be used by the client. If the current file needs to be changed then it uses the function “write_file” to create a new file. However, if it doesn’t need to be changed then it simply reads the Json data off of a file from the server. Then, regardless of whether or not the Json data was changed the program prints this for the user to retrieve. This is the function that determines wether or not the file should change. The program gets the current time from another function and gets the change time of the file using a series of calculations using the stat function on the file that stores the Json data. If the file was changed before the current playlist then the function returns 1 signifying that the file is outdated, if not then it returns 0 meaning the file is intact. This is the get_current_time function, which just like in java script converts the current time to seconds and returns it. However, in this function it adds a time shift because the server is hosted in CST, but the site is set to EST. Next is the function for writing the song file. This begins by finding what the current playlist is using the function “find_playlist” and then pulling in song data from a text file using the function “get_song_data” also stored on the server. Then the function “shuffle_songs” is called which shuffles the array of songs that was taken from the text file. Next the file_content variable is defined be using the “populate_json_data” method on the shuffled array. After that this content is stored in a file on the server and then returned so it can be given to the user. The first function called by the previous write file function is the “find_playlist” function. This gets the current time and then compares it to the playlist times to return a string containing the name of the current playlist. The next function that is called in “write_songs” is “get_song_data”. This function reads in a text file of songs and converts it to an array containing objects with 5 different attributes. This array is then returned to the function call. After the songs are converted to an array then the function “shuffle_songs” is called on it. This function starts by checking if the playlist is “Interview” and if there are any songs labeled interview in the array. If there aren’t any it changes the playlist to “all”. Then if the playlist is “all” the array copy is made up of all of the songs from the original array. If the playlist isn’t all then the array copy is made up of all the songs that match the current playlist. Then the function “getExtraClips” is called to make an array of non-song videos labeled “Ad” to be used in the radio. If the current section is “Interview” then only 1 of copy of all the ads is put into the array, but if it isn’t 4 copies are put in the array. After that the array is looped through and a random element is selected to be added to the result array. After each element has been randomly added to the result array it is returned. In the previous function “getExtraClips” is called. This works ver similar to “get_song_data”. This function takes in a list of videos from a text file and creates objects for them. These are then put into an array and returned to the method call. The next function called by the “write_file” function is the “populate_json_data” function. This creates a string of Json data created from the shuffled song array. This starts by getting the introductory object for the current playlist form the additional clips text file. next to determine if this should actually be added to the Json data. This is done by checking if the current time is less than the time that the into clip ends. If this is true then it is added to the string and the “current_play_time” variable is set to the current time and how much of the into clip theoretically already played, if not the intro clip is not added to the string and the “current_play_time” stays set to the current time. Then all of the songs are looped through until the current song will run past the next playlist and their attributes are added to the string, incrementing the play time by the length of the song each time. However, the song is not added to the string if it is an ad that was added immediately before. Then if the current song is the last one to be added, meaning that it will run past the next playlist the end for the Json data string is inserted and the string is returned. This is the “next_playlist” function used in the “populate_json_data” function. It compares the current time to the playlist times and determines which will come next. In the case of the last return the next playlist will start the next day at 6:45, but the songs should be populated like that is just later on the same day. This is the “read_file” that is called as an alternative to “write_file”. This takes the file content of the pre-created Json file of song data. It then returns this to be printed out to the user. Pages:Previous
Recent Comments