<commit_before>/*
 * ServeStream: A HTTP stream browser/player for Android
 * Copyright 2010 <NAME>
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.sourceforge.servestream.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

import javax.net.ssl.HttpsURLConnection;

public class PlaylistHandler {

	private URL targetURL = null;
    private ArrayList<String> playlistFiles = null;
    
	/**
	 * Default constructor
	 */
	public PlaylistHandler(String targetURL) {

		try {
		    this.targetURL = new URL(targetURL);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		
		playlistFiles = new ArrayList<String>();
	}
    
	/**
	 * Retrieves the files listed in a .m3u file
	 */
    public void buildPlaylist() {
    	
		HttpURLConnection conn = null;
        String line = null;
        BufferedReader reader = null;;
        
        try {
        	
        	if (targetURL.getProtocol().equals("http")) {
        		conn = (HttpURLConnection) targetURL.openConnection();
        	} else if (targetURL.getProtocol().equals("https")) {
        		conn = (HttpsURLConnection) targetURL.openConnection();        		
        	}
        	
		    conn.setRequestMethod("GET");
		    
		    // Start the query
		    reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
		    conn.connect();
        
		    while ((line = reader.readLine()) != null) {
		    	if (!(line.contains("#EXTM3U") || line.contains("#EXTINF"))) {
		    		playlistFiles.add(line);
		    	}           
            }

        } catch (Exception ex) {
        	ex.printStackTrace();
        } finally {
        	closeReader(reader);
        	closeHttpConnection(conn);
        }
    }
    
    /**
     * Returns the files in the playlist
     * 
     * @return ArrayList<String> An array containing the files from the playlist
     */
    public ArrayList<String> getPlayListFiles() {
    	return playlistFiles;
    }

	/**
	 * Closes a BufferedReader
	 * 
	 * @param reader The reader to close
	 */
    private void closeReader(BufferedReader reader) {<commit_msg>Added initial support for .pls playlist files<commit_after>/*
 * ServeStream: A HTTP stream browser/player for Android
 * Copyright 2010 <NAME>
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.sourceforge.servestream.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

import javax.net.ssl.HttpsURLConnection;

public class PlaylistHandler {
	public final static String TAG = "ServeStream.PlaylistHandler";

	private URL targetURL = null;
    private ArrayList<String> playlistFiles = null;
    
	/**
	 * Default constructor
	 */
	public PlaylistHandler(String targetURL) {

		try {
		    this.targetURL = new URL(targetURL);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		
		playlistFiles = new ArrayList<String>();
	}
    
	public void buildPlaylist() {
		
		String mediaFileName = targetURL.toString();
		
    	if (mediaFileName.length() > 4) {
    	    if (mediaFileName.substring(mediaFileName.length() - 4, mediaFileName.length()).equalsIgnoreCase(".m3u")) {
    	    	retrieveM3uFiles();
    	    }
    	    
    	    if (mediaFileName.contains(".pls")) {
    	    	retrievePsuFiles();
    	    }
    	}
	}
	
	/**
	 * Retrieves the files listed in a .m3u file
	 */
    public void retrieveM3uFiles() {
    	
		HttpURLConnection conn = null;
        String line = null;
        BufferedReader reader = null;;
        
        try {
        	
        	if (targetURL.getProtocol().equals("http")) {
        		conn = (HttpURLConnection) targetURL.openConnection();
        	} else if (targetURL.getProtocol().equals("https")) {
        		conn = (HttpsURLConnection) targetURL.openConnection();        		
        	}
        	
		    conn.setRequestMethod("GET");
		    
		    // Start the query
		    reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
		    conn.connect();
        
		    while ((line = reader.readLine()) != null) {
		    	if (!(line.contains("#EXTM3U") || line.contains("#EXTINF"))) {
		    		playlistFiles.add(line);
		    	}           
            }

        } catch (Exception ex) {
        	ex.printStackTrace();
        } finally {
        	closeReader(reader);
        	closeHttpConnection(conn);
        }
    }
    
    /**
	 * Retrieves the files listed in a .psu file
	 */
    public void retrievePsuFiles() {
    	
		HttpURLConnection conn = null;
        String line = null;
        BufferedReader reader = null;;
        
        try {
        	
        	if (targetURL.getProtocol().equals("http")) {
        		conn = (HttpURLConnection) targetURL.openConnection();
        	} else if (targetURL.getProtocol().equals("https")) {
        		conn = (HttpsURLConnection) targetURL.openConnection();        		
        	}
        	
		    conn.setRequestMethod("GET");
		    
		    // Start the query
		    reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
		    conn.connect();
        
		    while ((line = reader.readLine()) != null) {
		    	if (line.contains("File")) {
		    		String [] parsedLine = line.split("\\=");
		    		
		    		if (parsedLine.length == 2)
		    	        playlistFiles.add(parsedLine[1]);
		    	}           
            }

        } catch (Exception ex) {
        	ex.printStackTrace();
        } finally {
        	closeReader(reader);
        	closeHttpConnection(conn);
        }
    }
    
    /**
     * Checks if a file is a playlist file
     * 
     * @param mediaFileName The file to check
     * @return boolean True if the file is a playlist, false otherwise
     */
    public static boolean isPlaylist(String mediaFileName) {
    	if (mediaFileName.length() > 4) {
    	    if (mediaFileName.substring(mediaFileName.length() - 4, mediaFileName.length()).equalsIgnoreCase(".m3u")) {
    	    	return true;
    	    }
    	    
    	    if (mediaFileName.contains(".pls")) {
    	    	return true;
    	    }
    	}
    	
    	return false;
    }
    
    /**
     * Returns the files in the playlist
     * 
     * @return ArrayList<String> An array containing the files from the playlist
     */
    public ArrayList<String> getPlayListFiles() {
    	return playlistFiles;
    }

	/**
	 * Closes a BufferedReader
	 * 
	 * @param reader The reader to close
	 */
    private void closeReader(BufferedReader reader) {
{'fraction_non_alphanumeric': 0.10593331583092676, 'fraction_numerical': 0.0039380414807035965, 'mean_word_length': 2.3460693895476505, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3747832', 'n_tokens_mistral': 2285, 'n_tokens_neox': 2133, 'n_words': 579}