<commit_before>%%%-----------------------------------------------------------------------------
%%% @author <NAME> <<EMAIL>>
%%% @doc
%%% VFFOV API
%%% @end
%%% Created : 5 Mar 2013 by tgrk <<EMAIL>>
%%%-----------------------------------------------------------------------------
-module(vffov).
-include("vffov.hrl").

%% API
-export([download/0,
         download/1,
         download_pocket/1,

         status/0,
         queue/0,
         set_download_mode/1,

         start/0,
         stop/0
        ]).

%%%=============================================================================
%%% API
%%%=============================================================================
-spec download() -> any().
download() ->
    download(vffov_utils:priv_dir(vffov) ++ "playlist.txt").

-spec download(url()) -> any().
download(L) when is_list(L) ->
    case filelib:is_regular(vffov_utils:get_downloader()) of
        true  ->
            case filelib:is_regular(L) of
                true  -> download_1(L);
                false -> process_url_list(L)
            end;
        false ->
            vffov_utils:verbose(error, "Downloader not found! Please check "
                                "configuration.", [])
    end.

-spec download_pocket([{atom(), any()}]) -> any().
download_pocket(Opts) ->
    case vffov_getpocket:auth() of
        {ok, {request_url, Url}} ->
            vffov_utils:verbose(info, "Open following ~p in your browser and"
                                " run again.~n", [Url]);
        ok ->
            case vffov_getpocket:list(Opts) of
                empty ->
                    vffov_utils:verbose(info, "No matching items.~n", []);
                {error, Reason} ->
                    vffov_utils:verbose(error,
                                        "Unable to get items from getpocket "
                                        "service! Error ~p", [Reason]);
                List  -> handle_download(List)
            end;
        error ->
            vffov_utils:verbose(error,
                                "Unable to reqeust authentification code! "
                                "Check you consumer key!", [])
    end.

-spec status() -> [any()].
status() ->
    lists:filtermap(
      fun ({_Id, Pid, worker, [vffov_parallel_worker]}) ->
              {ok, Url} = gen_server:call(Pid, current_url),
              {true, {paralel_worker, Url}};
          ({_Id, Pid, worker, [vffov_queued_worker]}) ->
              {ok, Url} = gen_server:call(Pid, current_url),
              {true, {queued_worker, Url}};
          (_) ->
              false
      end, supervisor:which_children(vffov_sup)).

%%FIXME: why there is more than one queued_worker each with its own queue?!
-spec queue() -> [any()].
queue() ->
    lists:filtermap(
      fun ({_Id, Pid, worker, [vffov_queued_worker]}) ->
              {ok, Queue} = gen_server:call(Pid, current_queue),
              {true, Queue};
          (_) ->
              false
      end, supervisor:which_children(vffov_sup)).

-spec set_download_mode(paralel | queued) -> any().
set_download_mode(parallel) ->
    application:set_env(vffov, download_parallel, true);
set_download_mode(queued) ->
    application:set_env(vffov, download_parallel, false);
set_download_mode(_Other) ->
    vffov_utils:verbose(info, "Allowed modes: parallel or queued", []).

-spec start() -> ok.
start() ->
    [application:start(A) || A <- deps()],

    application:load(vffov),
    load_plugins(),
    application:start(vffov),
    ok.

-spec stop() -> ok.
stop() ->
    [application:stop(A) || A <- deps() ++ [vffov]],
    ok.

%%%=============================================================================
%%% Internal functionality
%%%=============================================================================
load_plugins() ->
    Plugins = [getpocket],
    lists:foreach(
      fun (Plugin) ->
              PluginL = atom_to_list(Plugin),<commit_msg>Fix call for getting queue<commit_after>%%%-----------------------------------------------------------------------------
%%% @author <NAME> <<EMAIL>>
%%% @doc
%%% VFFOV API
%%% @end
%%% Created : 5 Mar 2013 by tgrk <<EMAIL>>
%%%-----------------------------------------------------------------------------
-module(vffov).
-include("vffov.hrl").

%% API
-export([download/0,
         download/1,
         download_pocket/1,

         status/0,
         queue/0,
         set_download_mode/1,

         start/0,
         stop/0
        ]).

%%%=============================================================================
%%% API
%%%=============================================================================
-spec download() -> any().
download() ->
    download(vffov_utils:priv_dir(vffov) ++ "playlist.txt").

-spec download(url()) -> any().
download(L) when is_list(L) ->
    case filelib:is_regular(vffov_utils:get_downloader()) of
        true  ->
            case filelib:is_regular(L) of
                true  -> download_1(L);
                false -> process_url_list(L)
            end;
        false ->
            vffov_utils:verbose(error, "Downloader not found! Please check "
                                "configuration.", [])
    end.

-spec download_pocket([{atom(), any()}]) -> any().
download_pocket(Opts) ->
    case vffov_getpocket:auth() of
        {ok, {request_url, Url}} ->
            vffov_utils:verbose(info, "Open following ~p in your browser and"
                                " run again.~n", [Url]);
        ok ->
            case vffov_getpocket:list(Opts) of
                empty ->
                    vffov_utils:verbose(info, "No matching items.~n", []);
                {error, Reason} ->
                    vffov_utils:verbose(error,
                                        "Unable to get items from getpocket "
                                        "service! Error ~p", [Reason]);
                List  -> handle_download(List)
            end;
        error ->
            vffov_utils:verbose(error,
                                "Unable to reqeust authentification code! "
                                "Check you consumer key!", [])
    end.

-spec status() -> [any()].
status() ->
    lists:filtermap(
      fun ({_Id, Pid, worker, [vffov_parallel_worker]}) ->
              {ok, Url} = gen_server:call(Pid, current_url),
              {true, {paralel_worker, Url}};
          ({_Id, Pid, worker, [vffov_queued_worker]}) ->
              {ok, Url} = gen_server:call(Pid, current_url),
              {true, {queued_worker, Url}};
          (_) ->
              false
      end, supervisor:which_children(vffov_sup)).

-spec queue() -> queue:queue().
queue() ->
    Filtered = lists:filtermap(
                 fun ({_Id, Pid, worker, [vffov_queued_worker]}) ->
                         {ok, Queue} = gen_server:call(Pid, current_queue),
                         {true, Queue};
                     (_) ->
                         false
                 end, supervisor:which_children(vffov_sup)),
    case Filtered =:= [] of
        true  -> queue:new();
        false -> hd(Filtered)
    end.

-spec set_download_mode(paralel | queued) -> any().
set_download_mode(parallel) ->
    application:set_env(vffov, download_parallel, true);
set_download_mode(queued) ->
    application:set_env(vffov, download_parallel, false);
set_download_mode(_Other) ->
    vffov_utils:verbose(info, "Allowed modes: parallel or queued", []).

-spec start() -> ok.
start() ->
    [application:start(A) || A <- deps()],

    application:load(vffov),
    load_plugins(),
    application:start(vffov),
    ok.

-spec stop() -> ok.
stop() ->
    [application:stop(A) || A <- deps() ++ [vffov]],
    ok.

%%%=============================================================================
%%% Internal functionality
%%%=============================================================================
load_plugins() ->
    Plugins = [getpocket],
    lists:foreach(
      fun (Plugin) ->
              PluginL = atom_to_list(Plugin),
{'fraction_non_alphanumeric': 0.24953095684803, 'fraction_numerical': 0.0035021888680425266, 'mean_word_length': 1.8835196538045438, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 85, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 48, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3983956', 'n_tokens_mistral': 2336, 'n_tokens_neox': 2244, 'n_words': 461}