<commit_before>%%%-------------------------------------------------------------------
%%% File    : metainfo.erl
%%% Author  : <NAME> <<EMAIL>>
%%% License : See COPYING
%%% Description : Code for manipulating the metainfo file
%%%
%%% Created : 24 Jan 2007 by <NAME> <<EMAIL>>
%%%-------------------------------------------------------------------

-module(etorrent_metainfo).
-author("<NAME> <<EMAIL>>").
-vsn(1).

%% API
-export([get_piece_length/1, get_length/1, get_pieces/1, get_url/1,
	 get_infohash/1,
	 get_files/1, get_name/1, hexify/1]).


%%====================================================================
%% API
%%====================================================================

%%--------------------------------------------------------------------
%% Function: get_piece_length/1
%% Description: Search a torrent file, return the piece length
%%--------------------------------------------------------------------
get_piece_length(Torrent) ->
    {integer, Size} =
	etorrent_bcoding:search_dict({string, "piece length"},
				     get_info(Torrent)),
    Size.

%%--------------------------------------------------------------------
%% Function: get_pieces/1
%% Description: Search a torrent, return pieces as a list
%%--------------------------------------------------------------------
get_pieces(Torrent) ->
    {string, Ps} = etorrent_bcoding:search_dict({string, "pieces"},
						get_info(Torrent)),
    [list_to_binary(S) || S <- split_into_chunks(20, Ps)].

get_length(Torrent) ->
    case etorrent_bcoding:search_dict({string, "length"},
				      get_info(Torrent)) of
	{ok, {integer, L}} ->
	    L;
	false ->
	    %% Multifile torrent
	    sum_files(Torrent)
    end.

get_file_length(File) ->
    {integer, N} = find_target(File, "length"),
    N.

sum_files(Torrent) ->
    {list, Files} = find_target(get_info(Torrent), "files"),
    lists:sum([get_file_length(F) || F <- Files]).

%%--------------------------------------------------------------------
%% Function: get_files/1
%% Description: Get a file list from the torrent
%%--------------------------------------------------------------------
get_files(Torrent) ->
    {list, FilesEntries} = get_files_section(Torrent),
    [process_file_entry(Path) || Path <- FilesEntries].

%%--------------------------------------------------------------------
%% Function: get_name/1
%% Description: Get the name of a torrent. Returns either {ok, N} for
%%   for a valid name or {error, security_violation, N} for something<commit_msg>Sync bcoding-simplify branch with master.<commit_after>%%%-------------------------------------------------------------------
%%% File    : metainfo.erl
%%% Author  : <NAME> <<EMAIL>>
%%% License : See COPYING
%%% Description : Code for manipulating the metainfo file
%%%
%%% Created : 24 Jan 2007 by <NAME> <<EMAIL>>
%%%-------------------------------------------------------------------

-module(etorrent_metainfo).
-author("<NAME> <<EMAIL>>").
-vsn(1).

%% API
-export([get_piece_length/1, get_length/1, get_pieces/1, get_url/1,
	 get_infohash/1,
	 get_files/1, get_name/1, hexify/1]).


%%====================================================================
%% API
%%====================================================================

%%--------------------------------------------------------------------
%% Function: get_piece_length/1
%% Description: Search a torrent file, return the piece length
%%--------------------------------------------------------------------
get_piece_length(Torrent) ->
    {integer, Size} =
	etorrent_bcoding:search_dict({string, "piece length"},
				     get_info(Torrent)),
    Size.

%%--------------------------------------------------------------------
%% Function: get_pieces/1
%% Description: Search a torrent, return pieces as a list
%%--------------------------------------------------------------------
get_pieces(Torrent) ->
    {string, Ps} = etorrent_bcoding:search_dict({string, "pieces"},
						get_info(Torrent)),
    [list_to_binary(S) || S <- split_into_chunks(20, Ps)].

get_length(Torrent) ->
    case etorrent_bcoding:search_dict({string, "length"},
				      get_info(Torrent)) of
	{integer, L} ->
	    L;
	false ->
	    %% Multifile torrent
	    sum_files(Torrent)
    end.

get_file_length(File) ->
    {integer, N} = etorrent_bcoding:search_dict({string, "length"},
						File),
    N.

sum_files(Torrent) ->
    {list, Files} = etorrent_bcoding:search_dict({string, "files"},
					get_info(Torrent)),
    lists:sum([get_file_length(F) || F <- Files]).

%%--------------------------------------------------------------------
%% Function: get_files/1
%% Description: Get a file list from the torrent
%%--------------------------------------------------------------------
get_files(Torrent) ->
    {list, FilesEntries} = get_files_section(Torrent),
    [process_file_entry(Path) || Path <- FilesEntries].

%%--------------------------------------------------------------------
%% Function: get_name/1
%% Description: Get the name of a torrent. Returns either {ok, N} for
%%   for a valid name or {error, security_violation, N} for something
{'fraction_non_alphanumeric': 0.4084066471163245, 'fraction_numerical': 0.008211143695014663, 'mean_word_length': 1.6411977284460506, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '4769502', 'n_tokens_mistral': 1426, 'n_tokens_neox': 1313, 'n_words': 298}