<commit_before>/*****************************************************************************
 * videoportals.c: Convert video webportal HTML urls to the appropriate video
 *                 stream url.
 *****************************************************************************
 * Copyright (C) 2007 the VideoLAN team
 * $Id$
 *
 * Authors: <NAME> <dionoea @t videolan d.t org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include <vlc/vlc.h>
#include <vlc_demux.h>
#include <vlc_url.h>

#include <errno.h>                                                 /* ENOMEM */
#include "playlist.h"

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
static int Demux( demux_t *p_demux);
static int Control( demux_t *p_demux, int i_query, va_list args );

struct demux_sys_t
{
    char *psz_url;
};

/*****************************************************************************
 * Import_VideoPortal: main import function
 *****************************************************************************/
int E_(Import_VideoPortal)( vlc_object_t *p_this )
{
    demux_t *p_demux = (demux_t *)p_this;
    const char *psz_path = p_demux->psz_path;
    char *psz_cur;
    char *psz_url = NULL;

    byte_t *p_peek;
    int i_peek;

    /* YouTube */
    if( ( psz_cur = strstr( psz_path, "youtube.com" ) ) )
    {
        psz_cur += strlen( "youtube.com" );
        psz_cur = strchr( psz_cur, '/' );
        if( psz_cur )
        {
            psz_cur++;
            if( !strncmp( psz_cur, "watch?v=", strlen( "watch?v=" ) ) )
            {
                /* This is the webpage's url */
                psz_cur += strlen( "watch?v=" );
                asprintf( &psz_url, "http://www.youtube.com/v/%s", psz_cur );
            }
            else if( !strncmp( psz_cur, "p.swf", strlen( "p.swf" ) ) )
            {
                /* This is the swf flv player url (which we get after a
                 * redirect from the http://www.youtube.com/v/video_id url */
                char *video_id = strstr( psz_cur, "video_id=" );
                char *t = strstr( psz_cur, "t=" );
                if( video_id && t )
                {
                    char *psz_buf;
                    video_id += strlen( "video_id=" );
                    t += strlen( "t=" );
                    psz_buf = strchr( video_id, '&' );
                    if( psz_buf ) *psz_buf = '\0';
                    psz_buf = strchr( t, '&' );
                    if( psz_buf ) *psz_buf = '\0';
                    asprintf( &psz_url, "http://www.youtube.com/"
                              "get_video.php?video_id=%s&t=%s",
                              video_id, t );
                }
            }
        }
    }
    /* Daily motion */
    else if( ( psz_cur = strstr( psz_path, "dailymotion.com" ) ) )
    {
        i_peek = stream_Peek( p_demux->s, &p_peek, strlen( "<!DOCTYPE" ) );
        if( !strncmp( (char*)p_peek, "<!DOCTYPE", strlen( "!<DOCTYPE" ) ) )
        {
            /* This looks like a (daily motion) webpage */
            char *psz_line;
            while( ( psz_line = stream_ReadLine( p_demux->s ) ) )
            {
                if( ( psz_cur = strstr( psz_line,
                      "param name=\"flashvars\" value=\"url=" ) ) )
                {
                    char *psz_tmp;
                    psz_cur += strlen( "param name=\"flashvars\" value=\"url=" );
                    psz_tmp = strchr( psz_cur, '&' );
                    *psz_tmp = 0;
                    psz_url = strdup( psz_cur );
                    decode_URI( psz_url );
                    *psz_tmp = '&';
                    break;
                }
            }
        }
    }
    /* Google video */
    else if( ( psz_cur = strstr( psz_path, "video.google.com" ) )
             && strstr( psz_cur, "videoplay" ) )
    {
        char *docid = strstr( psz_cur, "docid=" );
        if( docid )
        {
            docid += strlen( "docid=" );
            /* This doesn't always work ... */
            asprintf( &psz_url,
                      "http://video.google.com/videogvp?docid=%s",
                      docid );
        }
    }
    /* metacafe */
    else if( ( psz_cur = strstr( psz_path, "metacafe.com" ) ) )
    {
        if( strstr( psz_cur, "watch/" ) )
        {
            psz_cur = strstr( psz_cur, "watch/" );
            psz_cur += strlen( "watch/" );
            if( psz_cur[strlen(psz_cur)-1] == '/' )
                psz_cur[strlen(psz_cur)-1] = '\0';
            asprintf( &psz_url, "http://www.metacafe.com/fplayer/%s.swf",
                      psz_cur );
        }
        else if( ( psz_cur = strstr( psz_path, "images.metacafe.com" ) ) )
        {
            if( ( psz_cur = strstr( psz_cur, "mediaURL=" ) ) )
            {
                char *psz_tmp;
                psz_cur += strlen( "mediaURL=" );
                psz_tmp = strchr( psz_cur, '&' );
                *psz_tmp = 0;
                psz_url = strdup( psz_cur );
                *psz_tmp = '&';
            }
        }
    }

    if( !psz_url )
    {
        return VLC_EGENERIC;
    }

    p_demux->p_sys = (demux_sys_t*)malloc( sizeof( demux_sys_t ) );
    if( !p_demux->p_sys )
    {
        free( psz_url );
        return VLC_ENOMEM;
    }
    p_demux->p_sys->psz_url = psz_url;

    p_demux->pf_control = Control;
    p_demux->pf_demux = Demux;

    return VLC_SUCCESS;
}

/*****************************************************************************
 * Deactivate: frees unused data
 *****************************************************************************/
void E_(Close_VideoPortal)( vlc_object_t *p_this )
{
    demux_t *p_demux = (demux_t *)p_this;
    free( p_demux->p_sys->psz_url );
    free( p_demux->p_sys );
}

static int Demux( demux_t *p_demux )
{
    char *psz_url = p_demux->p_sys->psz_url;
    input_item_t *p_input;

    msg_Dbg( p_demux, "Redirecting %s to %s", p_demux->psz_path, psz_url );

    INIT_PLAYLIST_STUFF;

    p_input = input_ItemNewExt( p_playlist, psz_url, psz_url, 0, NULL, -1 );
    playlist_BothAddInput( p_playlist, p_input,
                           p_item_in_category,
                           PLAYLIST_APPEND | PLAYLIST_SPREPARSE,
                           PLAYLIST_END, NULL, NULL, VLC_FALSE );

    HANDLE_PLAY_AND_RELEASE;

    return -1; /* Needed for correct operation of go back */
}

static int Control( demux_t *p_demux, int i_query, va_list args )
{
    return VLC_EGENERIC;
}<commit_msg>* videoportals.c: Allow using fullscreen youtube URLs and retreive the video's title when possible (still youtube).<commit_after>/*****************************************************************************
 * videoportals.c: Convert video webportal HTML urls to the appropriate video
 *                 stream url.
 *****************************************************************************
 * Copyright (C) 2007 the VideoLAN team
 * $Id$
 *
 * Authors: <NAME> <dionoea @t videolan d.t org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include <vlc/vlc.h>
#include <vlc_demux.h>
#include <vlc_url.h>
#include <vlc_strings.h>

#include <errno.h>                                                 /* ENOMEM */
#include "playlist.h"

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
static int Demux( demux_t *p_demux);
static int Control( demux_t *p_demux, int i_query, va_list args );

struct demux_sys_t
{
    char *psz_url;
    char *psz_title;
};

/*****************************************************************************
 * Import_VideoPortal: main import function
 *****************************************************************************/
int E_(Import_VideoPortal)( vlc_object_t *p_this )
{
    demux_t *p_demux = (demux_t *)p_this;
    const char *psz_path = p_demux->psz_path;
    char *psz_cur;
    char *psz_url = NULL;
    char *psz_title = NULL;

    byte_t *p_peek;
    int i_peek;

    /* YouTube */
    if( ( psz_cur = strstr( psz_path, "youtube.com" ) ) )
    {
        psz_cur += strlen( "youtube.com" );
        psz_cur = strchr( psz_cur, '/' );
        if( psz_cur )
        {
            psz_cur++;
            if( !strncmp( psz_cur, "watch?v=", strlen( "watch?v=" ) ) )
            {
                /* This is the webpage's url */
                char *psz_line;
                char *psz_id;
                psz_cur += strlen( "watch?v=" );
                psz_id = psz_cur;
                psz_cur = strchr( psz_cur, '&' );
                if( psz_cur ) *psz_cur = '\0';
                /* Retreive the stream's title from the HTML */
                while( ( psz_line = stream_ReadLine( p_demux->s ) ) )
                {
                    if( ( psz_cur = strstr( psz_line,
                          "<meta name=\"title\" content=\"" ) ) )
                    {
                        char *psz_buf;
                        psz_cur += strlen( "<meta name=\"title\" content=\"" );
                        psz_buf = strchr( psz_cur, '"' );
                        if( psz_buf ) *psz_buf = '\0';
                        psz_title = strdup( psz_cur );
                        resolve_xml_special_chars( psz_title );
                        if( psz_buf ) *psz_buf = '"';
                        break;
                    }
                }
                asprintf( &psz_url, "http://www.youtube.com/v/%s", psz_id );
            }
            else if( !strncmp( psz_cur, "watch_fullscreen?video_id=",
                               strlen( "watch_fullscreen?video_id=" ) )
           || !strncmp( psz_cur, "p.swf", strlen( "p.swf" ) ) /* Normal size */
           || !strncmp( psz_cur, "player2.swf", strlen( "player2.swf" ) )/* Fullscreen urls */ )
            {
                /* This is the swf flv player url (which we get after a
                 * redirect from the http://www.youtube.com/v/video_id url),
                 * or the fullscreen swf flv player url,
                 * or the fullscreen webpage url */
                char *video_id = strstr( psz_cur, "video_id=" );
                char *t = strstr( psz_cur, "t=" );
                char *title = strstr( psz_cur, "title=" );
                if( video_id && t )
                {
                    char *psz_buf;
                    video_id += strlen( "video_id=" );
                    t += strlen( "t=" );
                    psz_buf = strchr( video_id, '&' );
                    if( psz_buf ) *psz_buf = '\0';
                    psz_buf = strchr( t, '&' );
                    if( psz_buf ) *psz_buf = '\0';
                    asprintf( &psz_url, "http://www.youtube.com/"
                              "get_video.php?video_id=%s&t=%s",
                              video_id, t );
                    if( title ) /* Comes with fullscreen urls */
                    {
                        title += strlen( "title=" );
                        psz_buf = strchr( title, '&' );
                        if( psz_buf ) *psz_buf = '\0';
                        psz_title = strdup( title );
                        decode_URI( psz_title );
                    }
                }
            }
        }
    }
    /* Daily motion */
    else if( ( psz_cur = strstr( psz_path, "dailymotion.com" ) ) )
    {
        i_peek = stream_Peek( p_demux->s, &p_peek, strlen( "<!DOCTYPE" ) );
        if( !strncmp( (char*)p_peek, "<!DOCTYPE", strlen( "!<DOCTYPE" ) ) )
        {
            /* This looks like a (daily motion) webpage */
            char *psz_line;
            while( ( psz_line = stream_ReadLine( p_demux->s ) ) )
            {
                if( ( psz_cur = strstr( psz_line,
                      "param name=\"flashvars\" value=\"url=" ) ) )
                {
                    char *psz_tmp;
                    psz_cur += strlen( "param name=\"flashvars\" value=\"url=" );
                    psz_tmp = strchr( psz_cur, '&' );
                    *psz_tmp = 0;
                    psz_url = strdup( psz_cur );
                    decode_URI( psz_url );
                    *psz_tmp = '&';
                    break;
                }
            }
        }
    }
    /* Google video */
    else if( ( psz_cur = strstr( psz_path, "video.google.com" ) )
             && strstr( psz_cur, "videoplay" ) )
    {
        char *docid = strstr( psz_cur, "docid=" );
        if( docid )
        {
            docid += strlen( "docid=" );
            /* This doesn't always work ... */
            asprintf( &psz_url,
                      "http://video.google.com/videogvp?docid=%s",
                      docid );
        }
    }
    /* metacafe */
    else if( ( psz_cur = strstr( psz_path, "metacafe.com" ) ) )
    {
        if( strstr( psz_cur, "watch/" ) )
        {
            psz_cur = strstr( psz_cur, "watch/" );
            psz_cur += strlen( "watch/" );
            if( psz_cur[strlen(psz_cur)-1] == '/' )
                psz_cur[strlen(psz_cur)-1] = '\0';
            asprintf( &psz_url, "http://www.metacafe.com/fplayer/%s.swf",
                      psz_cur );
        }
        else if( ( psz_cur = strstr( psz_path, "images.metacafe.com" ) ) )
        {
            if( ( psz_cur = strstr( psz_cur, "mediaURL=" ) ) )
            {
                char *psz_tmp;
                psz_cur += strlen( "mediaURL=" );
                psz_tmp = strchr( psz_cur, '&' );
                *psz_tmp = 0;
                psz_url = strdup( psz_cur );
                *psz_tmp = '&';
            }
        }
    }

    if( !psz_url )
    {
        return VLC_EGENERIC;
    }

    p_demux->p_sys = (demux_sys_t*)malloc( sizeof( demux_sys_t ) );
    if( !p_demux->p_sys )
    {
        free( psz_url );
        free( psz_title );
        return VLC_ENOMEM;
    }
    p_demux->p_sys->psz_url = psz_url;
    p_demux->p_sys->psz_title = psz_title;

    p_demux->pf_control = Control;
    p_demux->pf_demux = Demux;

    return VLC_SUCCESS;
}

/*****************************************************************************
 * Deactivate: frees unused data
 *****************************************************************************/
void E_(Close_VideoPortal)( vlc_object_t *p_this )
{
    demux_t *p_demux = (demux_t *)p_this;
    free( p_demux->p_sys->psz_url );
    free( p_demux->p_sys->psz_title );
    free( p_demux->p_sys );
}

static int Demux( demux_t *p_demux )
{
    char *psz_url = p_demux->p_sys->psz_url;
    char *psz_title = p_demux->p_sys->psz_title;
    input_item_t *p_input;

    msg_Dbg( p_demux, "Redirecting %s to %s", p_demux->psz_path, psz_url );

    INIT_PLAYLIST_STUFF;
    if( !psz_title )
    {
        psz_title = p_current_input->psz_name;
    }

    p_input = input_ItemNewExt( p_playlist, psz_url, psz_title, 0, NULL, -1 );
    playlist_BothAddInput( p_playlist, p_input,
                           p_item_in_category,
                           PLAYLIST_APPEND | PLAYLIST_SPREPARSE,
                           PLAYLIST_END, NULL, NULL, VLC_FALSE );

    HANDLE_PLAY_AND_RELEASE;

    return -1; /* Needed for correct operation of go back */
}

static int Control( demux_t *p_demux, int i_query, va_list args )
{
    return VLC_EGENERIC;
}
{'fraction_non_alphanumeric': 0.21109165207238761, 'fraction_numerical': 0.003327495621716287, 'mean_word_length': 1.8376677157528574, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 54, 'https://': 0, 'lorem ipsum': 0, 'www.': 8, 'xml': 1}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4108044', 'n_tokens_mistral': 5429, 'n_tokens_neox': 5210, 'n_words': 1201}