<commit_before>--[[
 Allocine Extension for VLC media player 1.1
 French website only

 Copyright © 2010 VideoLAN and AUTHORS

 Authors:  <NAME> (<EMAIL>)

 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.
--]]

-- Lua modules
require "simplexml"

-- Global variables
dlg = nil     -- Dialog
title = nil   -- Text input widget
message = nil -- Label
list = nil    -- List widget
okay = nil    -- Okay button
html = nil    -- HTML box
spin = nil    -- spinning icon
films = {}

-- Extension description
function descriptor()
    return { title = "Allociné (France)" ;
             version = "1.0" ;
             author = "VideoLAN" ;
             url = 'http://www.allocine.fr/' ;
             shortdesc = "Allocine.com" ;
             description = "<center><b>ALLOCINE.COM</b></center>"
                        .. "Récupère des informations sur le média en cours "
                        .. "de lecture depuis Allocine.fr, telles que :<br />"
                        .. "- Casting,<br />- Résumé,<br />- Note des utilisateurs,"
                        .. "<br />- Lien direct vers la fiche du film sur "
                        .. "<a href=\"http://www.allocine.fr\">allocine.fr</a>." ;
             icon = icon_allocine ;
             capabilities = { "input-listener", "meta-listener" }}
end

-- Activation hook
function activate()
    vlc.msg.dbg("[ALLOCINE.COM] Welcome on Allocine.fr")
    create_dialog()
end

-- Deactivation hook
function deactivate()
    vlc.msg.dbg("[ALLOCINE.COM] Bye bye!")
end

-- Dialog close hook
function close()
    -- Deactivate this extension
    vlc.deactivate()
end

-- Input change hook
function input_changed()
    title:set_text(get_title())
end

-- Meta change hook
function meta_changed()
    title:set_text(get_title())
end

-- Create the dialog
function create_dialog()
    dlg = vlc.dialog("ALLOCINE.COM")
    dlg:add_label("<b>Titre du film:</b>", 1, 1, 1, 1)
    title = dlg:add_text_input(get_title(), 2, 1, 1, 1)
    dlg:add_button("Rechercher", click_chercher, 3, 1, 1, 1)
    spin = dlg:add_spin_icon(4, 1, 1, 1)
end

-- Get clean title from filename
function get_title(str)
    local item = vlc.item or vlc.input.item()
    if not item then
        return ""
    end
    local metas = item:metas()
    if metas["title"] then
        return metas["title"]
    else
        local filename = string.gsub(item:name(), "^(.+)%.%w+$", "%1")
        return trim(filename or item:name())
    end
end

-- Remove leading and trailing spaces
function trim(str)
    if not str then return "" end
    return string.gsub(str, "^%s*(.-)%s*$", "%1")
end

-- Lookup for this movie title
function click_chercher()
    -- Get title
    local name = title:get_text()
    if not name or name == "" then
        vlc.msg.dbg("[ALLOCINE.COM] No title")
        return
    end

    -- Update dialog
    if list then
        dlg:del_widget(list)
        list = nil
    end

    -- Transform spaces and dots into +
    name = string.gsub(string.gsub(name, "[%p%s%c]", "+"), "%++", "+")

    -- Build URL
    local url = "http://www.allocine.fr/recherche/?q=" .. name

    -- Please wait...
    local message_text = "Recherche <a href=\"" .. url .. "\">" .. string.gsub(name, "%+", " ") .. "</a> sur Allociné..."
    if not message then
        message = dlg:add_label(message_text, 1, 2, 4, 1)
    else
        message:set_text(message_text)
    end
    if list then dlg:del_widget(list) end
    if okay then dlg:del_widget(okay) end
    if html then dlg:del_widget(html) end
    list = nil
    okay = nil
    html = nil

    -- Show progress
    spin:animate()
    dlg:update()

    -- Open URL
    local s, msg = vlc.stream(url)
    if not s then
        vlc.msg.warn("[ALLOCINE.COM] " .. msg)
        spin:stop()
        return
    end
    vlc.keep_alive()

    -- Fetch HTML data (max 65 kb)
    local data = s:read(65535)
    vlc.keep_alive()

    -- Clean data
    data = string.gsub(data, "<b>", "")
    data = string.gsub(data, "</b>", "")
    data = string.gsub(data, "%s+", " ")

    -- Data storage
    films = {}

    -- Find categories
    for category in string.gmatch(data, "<[hH]2>%s*([^<]+)%s*</[hH]2>") do
        local category = trim(category)

        -- Split substring corresponding to this table
        local _, first = string.find(data, "<[hH]2>%s*" .. category .. "%s*</[hH]2>")
        first, _ = string.find(data, "<table", first)
        local _, last = string.find(data, "</table>", first)

        -- Find movies and TV shows
        if category == "Films" or category == "Séries TV" then
            -- Read <table> tag as xml<commit_msg>Lua: fix Allocine for large result pages<commit_after>--[[
 Allocine Extension for VLC media player 1.1
 French website only

 Copyright © 2010 VideoLAN and AUTHORS

 Authors:  <NAME> (<EMAIL>)

 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.
--]]

-- Lua modules
require "simplexml"

-- Global variables
dlg = nil     -- Dialog
title = nil   -- Text input widget
message = nil -- Label
list = nil    -- List widget
okay = nil    -- Okay button
html = nil    -- HTML box
spin = nil    -- spinning icon
films = {}

-- Extension description
function descriptor()
    return { title = "Allociné (France)" ;
             version = "1.0" ;
             author = "VideoLAN" ;
             url = 'http://www.allocine.fr/' ;
             shortdesc = "Allocine.com" ;
             description = "<center><b>ALLOCINE.COM</b></center>"
                        .. "Récupère des informations sur le média en cours "
                        .. "de lecture depuis Allocine.fr, telles que :<br />"
                        .. "- Casting,<br />- Résumé,<br />- Note des utilisateurs,"
                        .. "<br />- Lien direct vers la fiche du film sur "
                        .. "<a href=\"http://www.allocine.fr\">allocine.fr</a>." ;
             icon = icon_allocine ;
             capabilities = { "input-listener", "meta-listener" }}
end

-- Activation hook
function activate()
    vlc.msg.dbg("[ALLOCINE.COM] Welcome on Allocine.fr")
    create_dialog()
end

-- Deactivation hook
function deactivate()
    vlc.msg.dbg("[ALLOCINE.COM] Bye bye!")
end

-- Dialog close hook
function close()
    -- Deactivate this extension
    vlc.deactivate()
end

-- Input change hook
function input_changed()
    title:set_text(get_title())
end

-- Meta change hook
function meta_changed()
    title:set_text(get_title())
end

-- Create the dialog
function create_dialog()
    dlg = vlc.dialog("ALLOCINE.COM")
    dlg:add_label("<b>Titre du film:</b>", 1, 1, 1, 1)
    title = dlg:add_text_input(get_title(), 2, 1, 1, 1)
    dlg:add_button("Rechercher", click_chercher, 3, 1, 1, 1)
    spin = dlg:add_spin_icon(4, 1, 1, 1)
end

-- Get clean title from filename
function get_title(str)
    local item = vlc.item or vlc.input.item()
    if not item then
        return ""
    end
    local metas = item:metas()
    if metas["title"] then
        return metas["title"]
    else
        local filename = string.gsub(item:name(), "^(.+)%.%w+$", "%1")
        return trim(filename or item:name())
    end
end

-- Remove leading and trailing spaces
function trim(str)
    if not str then return "" end
    return string.gsub(str, "^%s*(.-)%s*$", "%1")
end

-- Lookup for this movie title
function click_chercher()
    -- Get title
    local name = title:get_text()
    if not name or name == "" then
        vlc.msg.dbg("[ALLOCINE.COM] No title")
        return
    end

    -- Update dialog
    if list then
        dlg:del_widget(list)
        list = nil
    end

    -- Transform spaces and dots into +
    name = string.gsub(string.gsub(name, "[%p%s%c]", "+"), "%++", "+")

    -- Build URL
    local url = "http://www.allocine.fr/recherche/?q=" .. name

    -- Please wait...
    local message_text = "Recherche <a href=\"" .. url .. "\">" .. string.gsub(name, "%+", " ") .. "</a> sur Allociné..."
    if not message then
        message = dlg:add_label(message_text, 1, 2, 4, 1)
    else
        message:set_text(message_text)
    end
    if list then dlg:del_widget(list) end
    if okay then dlg:del_widget(okay) end
    if html then dlg:del_widget(html) end
    list = nil
    okay = nil
    html = nil

    -- Show progress
    spin:animate()
    dlg:update()

    -- Open URL
    local s, msg = vlc.stream(url)
    if not s then
        vlc.msg.warn("[ALLOCINE.COM] " .. msg)
        spin:stop()
        return
    end
    vlc.keep_alive()

    -- Fetch HTML data (max 65 kb)
    local data = "", tmpdata
    repeat
       tmpdata = s:read(65535)
       vlc.keep_alive()
       if not tmpdata then break end
       data = data .. tmpdata
    until tmpdata == ""

    -- Clean data
    data = string.gsub(data, "<b>", "")
    data = string.gsub(data, "</b>", "")
    data = string.gsub(data, "%s+", " ")

    -- Data storage
    films = {}

    -- Find categories
    for category in string.gmatch(data, "<[hH]2>%s*([^<]+)%s*</[hH]2>") do
        local category = trim(category)

        -- Split substring corresponding to this table
        local _, first = string.find(data, "<[hH]2>%s*" .. category .. "%s*</[hH]2>")
        first, _ = string.find(data, "<table", first)
        local _, last = string.find(data, "</table>", first)

        -- Find movies and TV shows
        if category == "Films" or category == "Séries TV" then
            -- Read <table> tag as xml
{'fraction_non_alphanumeric': 0.1344111288654949, 'fraction_numerical': 0.00996334241940032, 'mean_word_length': 2.3448601068846275, 'pattern_counts': {'":': 0, '<': 55, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, 'xml': 4}, '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': '4497465', 'n_tokens_mistral': 3416, 'n_tokens_neox': 3277, 'n_words': 1095}